DataTypeUtils { public static Number convertTo(double value, DataType dataType) { switch (dataType) { case BYTE: return (byte) value; case SHORT: return (short) value; case INT: return (int) value; case LONG: return (long) value; case FLOAT: return (float) value; case DOUBLE: return value; default: throw new IllegalArgumentException("Can not convert data type:" + dataType.name()); } }  static boolean isValidRasterDataType(final DataType dataType); static int getRasterDataType(Variable variable); static int getRasterDataType(final DataType dataType, boolean unsigned); static int getEquivalentProductDataType(DataType dataType, boolean unsigned, boolean rasterDataOnly); static DataType getNetcdfDataType(int dataType); static Number convertTo(double value, DataType dataType); static ProductData createProductData(Attribute attribute);  }
@Test public void testConvertToBYTE() { Number convertedNumber = DataTypeUtils.convertTo(12.3, DataType.BYTE); assertEquals((byte) 12, convertedNumber); assertEquals(DataType.BYTE, DataType.getType(convertedNumber.getClass())); convertedNumber = DataTypeUtils.convertTo(-123, DataType.BYTE); assertEquals((byte) -123, convertedNumber); assertEquals(DataType.BYTE, DataType.getType(convertedNumber.getClass())); }
@Test public void testConvertToSHORT() { Number convertedNumber = DataTypeUtils.convertTo(12.3, DataType.SHORT); assertEquals(DataType.SHORT, DataType.getType(convertedNumber.getClass())); assertEquals((short) 12, convertedNumber); convertedNumber = DataTypeUtils.convertTo(-123, DataType.SHORT); assertEquals(DataType.SHORT, DataType.getType(convertedNumber.getClass())); assertEquals((short) -123, convertedNumber); }
@Test public void testConvertToINT() { Number convertedNumber = DataTypeUtils.convertTo(12.3, DataType.INT); assertEquals(DataType.INT, DataType.getType(convertedNumber.getClass())); assertEquals(12, convertedNumber); convertedNumber = DataTypeUtils.convertTo(-123, DataType.INT); assertEquals(DataType.INT, DataType.getType(convertedNumber.getClass())); assertEquals(-123, convertedNumber); }
@Test public void testConvertToLONG() { Number convertedNumber = DataTypeUtils.convertTo(12.3, DataType.LONG); assertEquals(DataType.LONG, DataType.getType(convertedNumber.getClass())); assertEquals(12L, convertedNumber); convertedNumber = DataTypeUtils.convertTo(-123, DataType.LONG); assertEquals(DataType.LONG, DataType.getType(convertedNumber.getClass())); assertEquals(-123L, convertedNumber); }
@Test public void testConvertToFLOAT() { Number convertedNumber = DataTypeUtils.convertTo(12.3, DataType.FLOAT); assertEquals(DataType.FLOAT, DataType.getType(convertedNumber.getClass())); assertEquals(12.3f, convertedNumber); convertedNumber = DataTypeUtils.convertTo(-123, DataType.FLOAT); assertEquals(DataType.FLOAT, DataType.getType(convertedNumber.getClass())); assertEquals(-123f, convertedNumber); }
@Test public void testConvertToDOUBLE() { Number convertedNumber = DataTypeUtils.convertTo(12.3, DataType.DOUBLE); assertEquals(DataType.DOUBLE, DataType.getType(convertedNumber.getClass())); assertEquals(12.3, convertedNumber); convertedNumber = DataTypeUtils.convertTo(-123, DataType.DOUBLE); assertEquals(DataType.DOUBLE, DataType.getType(convertedNumber.getClass())); assertEquals(-123.0, convertedNumber); }
@Test(expected = IllegalArgumentException.class) public void testConvertToWithIllegalArgument() { DataTypeUtils.convertTo(12.3, DataType.STRING); }