@Test public void testConvert() throws Exception { System.out.println("convert"); NumberField instance; instance = new NumberField(Types.NUMERIC); assertEquals(new BigDecimal("7456123.89"), instance.convert("7456123.89")); instance = new NumberField(Types.NUMERIC, 38, 1); assertEquals(new BigDecimal("7456123.9"), instance.convert("7456123.89")); instance = new NumberField(1, Types.NUMERIC); assertEquals(new BigDecimal("7456123.9"), instance.convert("7456123.89")); instance = new NumberField(Types.NUMERIC, 9); assertEquals(new BigDecimal("7456124"), instance.convert("7456123.89")); instance = new NumberField(Types.NUMERIC, 9, 2); assertEquals(new BigDecimal("7456123.89"), instance.convert("7456123.89")); instance = new NumberField(Types.NUMERIC, 9, 1); assertEquals(new BigDecimal("7456123.9"), instance.convert("7456123.89")); instance = new NumberField(Types.NUMERIC, 7, -2); assertEquals(new BigDecimal("7456100").setScale(-2), instance.convert("7456123.89")); try { instance = new NumberField(Types.NUMERIC, 6); instance.convert("7456123.89"); fail("Accepted number with precision surpassing the specified precision"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, ex.getErrorCode()); } instance = new NumberField(Types.DEC); assertEquals(new BigDecimal("7456123.89"), instance.convert("7456123.89")); instance = new NumberField(Types.DEC, 38, 1); assertEquals(new BigDecimal("7456123.9"), instance.convert("7456123.89")); instance = new NumberField(1, Types.DEC); assertEquals(new BigDecimal("7456123.9"), instance.convert("7456123.89")); instance = new NumberField(Types.DEC, 9); assertEquals(new BigDecimal("7456124"), instance.convert("7456123.89")); instance = new NumberField(Types.DEC, 9, 2); assertEquals(new BigDecimal("7456123.89"), instance.convert("7456123.89")); instance = new NumberField(Types.DEC, 9, 1); assertEquals(new BigDecimal("7456123.9"), instance.convert("7456123.89")); instance = new NumberField(Types.DEC, 7, -2); assertEquals(new BigDecimal("7456100").setScale(-2), instance.convert("7456123.89")); try { instance = new NumberField(Types.DEC, 6); instance.convert("7456123.89"); fail("Accepted number with precision surpassing the specified precision"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, ex.getErrorCode()); } instance = new NumberField(Types.DECIMAL); assertEquals(new BigDecimal("7456123.89"), instance.convert("7456123.89")); instance = new NumberField(Types.DECIMAL, 38, 1); assertEquals(new BigDecimal("7456123.9"), instance.convert("7456123.89")); instance = new NumberField(1, Types.DECIMAL); assertEquals(new BigDecimal("7456123.9"), instance.convert("7456123.89")); instance = new NumberField(Types.DECIMAL, 9); assertEquals(new BigDecimal("7456124"), instance.convert("7456123.89")); instance = new NumberField(Types.DECIMAL, 9, 2); assertEquals(new BigDecimal("7456123.89"), instance.convert("7456123.89")); instance = new NumberField(Types.DECIMAL, 9, 1); assertEquals(new BigDecimal("7456123.9"), instance.convert("7456123.89")); instance = new NumberField(Types.DECIMAL, 7, -2); assertEquals(new BigDecimal("7456100").setScale(-2), instance.convert("7456123.89")); try { instance = new NumberField(Types.DECIMAL, 6); instance.convert("7456123.89"); fail("Accepted number with precision surpassing the specified precision"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, ex.getErrorCode()); } instance = new NumberField(Types.FLOAT); assertEquals(new BigDecimal("7456123.89"), instance.convert("7456123.89")); instance = new NumberField(Types.FLOAT, 9); assertEquals(new BigDecimal("7456123.89"), instance.convert("7456123.89")); try { instance = new NumberField(Types.FLOAT, 6); instance.convert("7456123.89"); fail("Accepted number with precision surpassing the specified precision"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, ex.getErrorCode()); } instance = new NumberField(Types.INTEGER); assertEquals(1000, instance.convert(1000)); assertEquals(Integer.MAX_VALUE, instance.convert(Integer.MAX_VALUE)); assertEquals(1000, instance.convert("1000")); try { instance.convert(1000.1); } catch (OperationException ex) { fail("Failed to approximate float to integer"); } instance = new NumberField(Types.LONG); assertEquals(1000L, instance.convert(1000L)); assertEquals(Long.MAX_VALUE, instance.convert(Long.MAX_VALUE)); assertEquals(1000L, instance.convert("1000")); try { instance.convert(1000.1); } catch (OperationException ex) { fail("Failed to approximate float point value to long value"); } instance = new NumberField(Types.SMALLINT); assertEquals((short) 1000, instance.convert(1000)); assertEquals(Short.MAX_VALUE, instance.convert(Short.MAX_VALUE)); assertEquals((short) 1000, instance.convert("1000")); try { instance.convert(1000.1); } catch (OperationException ex) { fail("Failed to approximate float point value to smallint value"); } instance = new NumberField(Types.BIGINT); assertEquals((long) 1000, instance.convert(1000)); assertEquals(Long.MAX_VALUE, instance.convert(Long.MAX_VALUE)); assertEquals((long) 1000, instance.convert("1000")); try { instance.convert(1000.1); } catch (OperationException ex) { fail("Failed to approximate float point value to bigint value"); } instance = new NumberField(Types.DOUBLE_PRECISION); assertEquals(Double.MAX_VALUE, instance.convert(Double.MAX_VALUE)); try { instance.convert("1000ld"); fail("Invalid conversion of non-number to double succeeded instead of reporting an error"); } catch (OperationException ex) { assertEquals(ErrorCode.INVALID_NUMBER_FORMAT, ex.getErrorCode()); } instance = new NumberField(Types.DOUBLE); assertEquals(Double.MAX_VALUE, instance.convert(Double.MAX_VALUE)); try { instance.convert("1000ld"); fail("Invalid conversion of non-number to double succeeded instead of reporting an error"); } catch (OperationException ex) { assertEquals(ErrorCode.INVALID_NUMBER_FORMAT, ex.getErrorCode()); } }
@Override public Number convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } switch (type) { case SMALLINT: try { return Double.valueOf(objValue.toString()).shortValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid SMALLINT"); } case INTEGER: case INT: try { return Double.valueOf(objValue.toString()).intValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid INT/INTEGER"); } case LONG: case BIGINT: try { return Double.valueOf(objValue.toString()).longValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid BIGINT"); } case DOUBLE: case DOUBLE_PRECISION: try { return Double.valueOf(objValue.toString()); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid DOUBLE/DOUBLE_PRECISION"); } case REAL: case FLOAT: try { return new PreciseNumber(new BigDecimal(objValue.toString()), precision).get(); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid FLOAT / REAL"); } case NUMERIC: case DECIMAL: case DEC: try { if (scale == null) { return new PreciseNumber(new BigDecimal(objValue.toString()), precision).get(); } return new PreciseNumber(new BigDecimal(objValue.toString()), precision, scale).get(); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid NUMERIC/DECIMAL/DEC"); } default: throw new OperationException(ErrorCode.DATATYPE_ERROR, "The type " + type.getType() + " could not be identified as any valid numeric type"); } }
NumberField implements FieldType<Number> { @Override public Number convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } switch (type) { case SMALLINT: try { return Double.valueOf(objValue.toString()).shortValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid SMALLINT"); } case INTEGER: case INT: try { return Double.valueOf(objValue.toString()).intValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid INT/INTEGER"); } case LONG: case BIGINT: try { return Double.valueOf(objValue.toString()).longValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid BIGINT"); } case DOUBLE: case DOUBLE_PRECISION: try { return Double.valueOf(objValue.toString()); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid DOUBLE/DOUBLE_PRECISION"); } case REAL: case FLOAT: try { return new PreciseNumber(new BigDecimal(objValue.toString()), precision).get(); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid FLOAT / REAL"); } case NUMERIC: case DECIMAL: case DEC: try { if (scale == null) { return new PreciseNumber(new BigDecimal(objValue.toString()), precision).get(); } return new PreciseNumber(new BigDecimal(objValue.toString()), precision, scale).get(); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid NUMERIC/DECIMAL/DEC"); } default: throw new OperationException(ErrorCode.DATATYPE_ERROR, "The type " + type.getType() + " could not be identified as any valid numeric type"); } } }
NumberField implements FieldType<Number> { @Override public Number convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } switch (type) { case SMALLINT: try { return Double.valueOf(objValue.toString()).shortValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid SMALLINT"); } case INTEGER: case INT: try { return Double.valueOf(objValue.toString()).intValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid INT/INTEGER"); } case LONG: case BIGINT: try { return Double.valueOf(objValue.toString()).longValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid BIGINT"); } case DOUBLE: case DOUBLE_PRECISION: try { return Double.valueOf(objValue.toString()); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid DOUBLE/DOUBLE_PRECISION"); } case REAL: case FLOAT: try { return new PreciseNumber(new BigDecimal(objValue.toString()), precision).get(); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid FLOAT / REAL"); } case NUMERIC: case DECIMAL: case DEC: try { if (scale == null) { return new PreciseNumber(new BigDecimal(objValue.toString()), precision).get(); } return new PreciseNumber(new BigDecimal(objValue.toString()), precision, scale).get(); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid NUMERIC/DECIMAL/DEC"); } default: throw new OperationException(ErrorCode.DATATYPE_ERROR, "The type " + type.getType() + " could not be identified as any valid numeric type"); } } NumberField(final Types type);  NumberField(final Types type, final Number defaultValue);  NumberField(final Types type, final int precision);  NumberField(final int scale, final Types type);  NumberField(final Types type, final int precision, final Number defaultValue);  NumberField(final int scale, final Types type, final Number defaultValue);  NumberField(final Types type, final int precision, final int scale);  NumberField(final Types type, final int precision, final int scale, final Number defaultValue); }
NumberField implements FieldType<Number> { @Override public Number convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } switch (type) { case SMALLINT: try { return Double.valueOf(objValue.toString()).shortValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid SMALLINT"); } case INTEGER: case INT: try { return Double.valueOf(objValue.toString()).intValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid INT/INTEGER"); } case LONG: case BIGINT: try { return Double.valueOf(objValue.toString()).longValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid BIGINT"); } case DOUBLE: case DOUBLE_PRECISION: try { return Double.valueOf(objValue.toString()); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid DOUBLE/DOUBLE_PRECISION"); } case REAL: case FLOAT: try { return new PreciseNumber(new BigDecimal(objValue.toString()), precision).get(); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid FLOAT / REAL"); } case NUMERIC: case DECIMAL: case DEC: try { if (scale == null) { return new PreciseNumber(new BigDecimal(objValue.toString()), precision).get(); } return new PreciseNumber(new BigDecimal(objValue.toString()), precision, scale).get(); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid NUMERIC/DECIMAL/DEC"); } default: throw new OperationException(ErrorCode.DATATYPE_ERROR, "The type " + type.getType() + " could not be identified as any valid numeric type"); } } NumberField(final Types type);  NumberField(final Types type, final Number defaultValue);  NumberField(final Types type, final int precision);  NumberField(final int scale, final Types type);  NumberField(final Types type, final int precision, final Number defaultValue);  NumberField(final int scale, final Types type, final Number defaultValue);  NumberField(final Types type, final int precision, final int scale);  NumberField(final Types type, final int precision, final int scale, final Number defaultValue); @Override Types getType(); int getPrecision(); Integer getScale(); @Override Number getDefaultValue(); @Override boolean hasDefaultValue(); @Override Number convert(Object objValue); @Override JSONObject toJson(); }
NumberField implements FieldType<Number> { @Override public Number convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } switch (type) { case SMALLINT: try { return Double.valueOf(objValue.toString()).shortValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid SMALLINT"); } case INTEGER: case INT: try { return Double.valueOf(objValue.toString()).intValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid INT/INTEGER"); } case LONG: case BIGINT: try { return Double.valueOf(objValue.toString()).longValue(); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid BIGINT"); } case DOUBLE: case DOUBLE_PRECISION: try { return Double.valueOf(objValue.toString()); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid DOUBLE/DOUBLE_PRECISION"); } case REAL: case FLOAT: try { return new PreciseNumber(new BigDecimal(objValue.toString()), precision).get(); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid FLOAT / REAL"); } case NUMERIC: case DECIMAL: case DEC: try { if (scale == null) { return new PreciseNumber(new BigDecimal(objValue.toString()), precision).get(); } return new PreciseNumber(new BigDecimal(objValue.toString()), precision, scale).get(); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.INVALID_NUMBER_FORMAT, objValue.toString() + " could not be converted to a valid NUMERIC/DECIMAL/DEC"); } default: throw new OperationException(ErrorCode.DATATYPE_ERROR, "The type " + type.getType() + " could not be identified as any valid numeric type"); } } NumberField(final Types type);  NumberField(final Types type, final Number defaultValue);  NumberField(final Types type, final int precision);  NumberField(final int scale, final Types type);  NumberField(final Types type, final int precision, final Number defaultValue);  NumberField(final int scale, final Types type, final Number defaultValue);  NumberField(final Types type, final int precision, final int scale);  NumberField(final Types type, final int precision, final int scale, final Number defaultValue); @Override Types getType(); int getPrecision(); Integer getScale(); @Override Number getDefaultValue(); @Override boolean hasDefaultValue(); @Override Number convert(Object objValue); @Override JSONObject toJson();  }