private static <T> T coerceObject(String value) {
        T result;
        //try boolean first
        if ("true".equals(value) || "false".equals(value)) {
            result = (T) (Boolean) Boolean.parseBoolean(value);
        }
        //then float numbers
        else if (floatPattern.matcher(value).matches()) {
            //handle overflow by converting to BigDecimal
            BigDecimal bd = new BigDecimal(value);
            Double d = bd.doubleValue();
            result = (T) ((d == Double.NEGATIVE_INFINITY || d == Double.POSITIVE_INFINITY) ? bd : d);
        }
        //then int numbers
        else if (intPattern.matcher(value).matches()) {
            //handle overflow by converting to BigInteger
            BigInteger bd = new BigInteger(value);
            result = (T) (bd.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) == 1 ? bd : bd.longValue());
        }
        //else just pass the String value to the Object parameter
        else {
            result = (T) value;
        }
        return result;
    }
private static < T > T coerceObject ( String value ) { T result ; if ( "true" . equals ( value ) || "false" . equals ( value ) ) { result = ( T ) ( Boolean ) Boolean . parseBoolean ( value ) ; } else if ( floatPattern . matcher ( value ) . matches ( ) ) { BigDecimal bd = new BigDecimal ( value ) ; Double d = bd . doubleValue ( ) ; result = ( T ) ( ( d == Double . NEGATIVE_INFINITY || d == Double . POSITIVE_INFINITY ) ? bd : d ) ; } else if ( intPattern . matcher ( value ) . matches ( ) ) { BigInteger bd = new BigInteger ( value ) ; result = ( T ) ( bd . compareTo ( BigInteger . valueOf ( Long . MAX_VALUE ) ) == 1 ? bd : bd . longValue ( ) ) ; } else { result = ( T ) value ; } return result ; }
Rules for object coercion are probably most important for the ChorusContext
Here when we set the value of a variable, these rules are used to determine how the
String value supplied is represented - since float pattern comes first
I set the variable x with value 1.2 will become a float within the ChorusContext
- this will give some extra utility if we add more powerful comparison methods to ChorusContext