class EvaluationUtil implements Serializable {

    public static Object castTo(Object x, TypeInformation t) {
        if (x == null) {
            return null;
        } else if (t.equals(Types.BOOLEAN)) {
            if (x instanceof Boolean) {
                return x;
            }
            return Boolean.valueOf(x.toString());
        } else if (t.equals(Types.BYTE)) {
            if (x instanceof Number) {
                return ((Number)x).byteValue();
            }
            return Byte.valueOf(x.toString());
        } else if (t.equals(Types.SHORT)) {
            if (x instanceof Number) {
                return ((Number)x).shortValue();
            }
            return Short.valueOf(x.toString());
        } else if (t.equals(Types.INT)) {
            if (x instanceof Number) {
                return ((Number)x).intValue();
            }
            return Integer.valueOf(x.toString());
        } else if (t.equals(Types.LONG)) {
            if (x instanceof Number) {
                return ((Number)x).longValue();
            }
            return Long.valueOf(x.toString());
        } else if (t.equals(Types.FLOAT)) {
            if (x instanceof Number) {
                return ((Number)x).floatValue();
            }
            return Float.valueOf(x.toString());
        } else if (t.equals(Types.DOUBLE)) {
            if (x instanceof Number) {
                return ((Number)x).doubleValue();
            }
            return Double.valueOf(x.toString());
        } else if (t.equals(Types.STRING)) {
            if (x instanceof String) {
                return x;
            }
            return x.toString();
        } else {
            throw new RuntimeException("unsupported type: " + t.getClass().getName());
        }
    }

}