public static void toString(Object value, Text t) { if (value == null) { t.append("null"); } else { final Class<? extends Object> clazz = value.getClass(); final DataType type = getDataType(clazz); if (type == null) { throw new RuntimeException("Invalid type: " + clazz.getName()); } type.toMarkup(value, t); } }
/** * Type converts values to other classes. For example an Integer can be converted to a Long. * * @param <T> Data Type. * @param value The value to be converted. * @param to The class to be converted to. Must be one of the Java types corresponding to the * DataTypes. * @return The converted value. * @throws TypeMismatchException Thrown desired class is incompatible with the source class. * @throws OverflowException Thrown only on narrowing value conversions, e.g from a BigInteger to * an Integer. */ @Nullable public static <T> T convert(Object value, Class<?> to) throws TypeMismatchException, OverflowException { final Object result; if (value == null || value.getClass() == to) { result = value; } else { final Class<?> from = value.getClass(); try { if (from == Integer.class) { // Integer -> ... if (to == Long.class) { result = new Long(((Number) value).longValue()); } else if (to == BigInteger.class) { result = BigInteger.valueOf(((Number) value).intValue()); } else if (to == Float.class) { result = new Float(((Number) value).floatValue()); } else if (to == Double.class) { result = new Double(((Number) value).doubleValue()); } else if (to == BigDecimal.class) { // Use intValue() to avoid precision errors result = new BigDecimal(((Number) value).intValue()); } else { throw new TypeMismatchException(value.getClass(), to); } } else if (from == Long.class) { // Long -> ... if (to == Integer.class) { final long l = ((Long) value).longValue(); if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) { throw new OverflowException((Number) value, to); } result = new Integer((int) l); } else if (to == BigInteger.class) { result = BigInteger.valueOf(((Number) value).longValue()); } else if (to == Float.class) { result = new Float(((Number) value).floatValue()); } else if (to == Double.class) { result = new Double(((Number) value).doubleValue()); } else if (to == BigDecimal.class) { // Use longValue() to avoid precision errors result = new BigDecimal(((Number) value).longValue()); } else { throw new TypeMismatchException(value.getClass(), to); } } else if (from == BigInteger.class) { // BigInteger -> ... final BigInteger bi = (BigInteger) value; if (to == Integer.class) { result = new Integer(bi.intValueExact()); } else if (to == Long.class) { result = new Long(bi.longValueExact()); } else if (to == Float.class) { final float f1 = bi.floatValue(); if (f1 == Float.NEGATIVE_INFINITY || f1 == Float.POSITIVE_INFINITY) { throw new OverflowException(bi, to); } result = new Float(f1); } else if (to == Double.class) { final double d = bi.doubleValue(); if (d == Double.NEGATIVE_INFINITY || d == Double.POSITIVE_INFINITY) { throw new OverflowException(bi, to); } result = new Double(d); } else if (to == BigDecimal.class) { result = new BigDecimal(bi); } else { throw new TypeMismatchException(value.getClass(), to); } } else if (from == Float.class) { // Float -> ... final float fl = ((Float) value).floatValue(); if (to == Integer.class) { if (fl < Integer.MIN_VALUE || fl > Integer.MAX_VALUE | fl % 1 > 0) { throw new OverflowException((Number) value, to); } result = new Integer((int) fl); } else if (to == Long.class) { if (fl < Long.MIN_VALUE || fl > Long.MAX_VALUE | fl % 1 > 0) { throw new OverflowException((Number) value, to); } result = new Long((long) fl); } else if (to == BigInteger.class) { if (fl % 1 > 0) { throw new OverflowException((Number) value, to); } final BigDecimal bd = BigDecimal.valueOf(fl); result = bd.toBigInteger(); } else if (to == Double.class) { result = new Double(((Number) value).doubleValue()); } else if (to == BigDecimal.class) { result = BigDecimal.valueOf(fl); } else { throw new TypeMismatchException(value.getClass(), to); } } else if (from == Double.class) { // Double -> ... final double d = ((Double) value).doubleValue(); if (to == Integer.class) { if (d < Integer.MIN_VALUE || d > Integer.MAX_VALUE || d % 1 > 0) { throw new OverflowException((Number) value, to); } result = new Integer((int) d); } else if (to == Long.class) { // OK if (d < Long.MIN_VALUE || d > Long.MAX_VALUE || d % 1 > 0) { throw new OverflowException((Number) value, to); } result = new Long((int) d); } else if (to == BigInteger.class) { // OK if (d % 1 > 0) { throw new OverflowException((Number) value, to); } final BigDecimal bd = BigDecimal.valueOf(d); result = bd.toBigInteger(); } else if (to == Float.class) { // OK if (d < -Float.MAX_VALUE || d > Float.MAX_VALUE) { throw new OverflowException((Number) value, to); } result = new Float((float) d); } else if (to == BigDecimal.class) { // OK result = BigDecimal.valueOf(d); } else { throw new TypeMismatchException(value.getClass(), to); } } else if (from == BigDecimal.class) { // BigDecimal -> ... final BigDecimal bd = (BigDecimal) value; if (to == Integer.class) { // OK result = new Integer(bd.intValueExact()); } else if (to == Long.class) { // OK result = new Long(bd.longValueExact()); } else if (to == BigInteger.class) { // OK // BigDecimal modulus final BigDecimal remainder = bd.remainder(BigDecimal.ONE); if (!remainder.equals(BigDecimal.ZERO)) { throw new OverflowException(bd, to); } result = bd.toBigInteger(); } else if (to == Float.class) { // OK if (bd.compareTo(BigDecimal_MIN_FLOAT) < 0 || bd.compareTo(BigDecimal_MAX_FLOAT) > 0) { throw new OverflowException(bd, to); } result = new Float(bd.floatValue()); } else if (to == Double.class) { // OK if (bd.compareTo(BigDecimal_MIN_DOUBLE) < 0 || bd.compareTo(BigDecimal_MAX_DOUBLE) > 0) { throw new OverflowException(bd, to); } result = new Double(bd.doubleValue()); } else { throw new TypeMismatchException(value.getClass(), to); } } else { throw new UnexpectedException("convert: " + from.getName()); } } catch (final ArithmeticException e) { // Thrown by intValueExact() etc. throw new OverflowException((Number) value, to); } } @SuppressWarnings("unchecked") final T t = (T) result; return t; }