@Test public void testConvert() throws Exception { System.out.println("convert"); String str; TimestampField instance; Timestamp referenceValue; Timestamp expectedValue; instance = new TimestampField(); referenceValue = new Timestamp(System.currentTimeMillis()); assertEquals(referenceValue, instance.convert(referenceValue)); expectedValue = new Timestamp(referenceValue.getTime()); referenceValue.setNanos(999999999); expectedValue.setNanos(0); assertEquals(expectedValue, instance.convert(referenceValue)); referenceValue.setNanos(123456789); expectedValue.setNanos(123457000); assertEquals(expectedValue, instance.convert(referenceValue)); assertEquals(expectedValue, instance.convert(referenceValue.toString())); expectedValue.setNanos(123000000); assertEquals(expectedValue, instance.convert(referenceValue.getTime())); instance = new TimestampField(9); referenceValue = new Timestamp(System.currentTimeMillis()); assertEquals(referenceValue, instance.convert(referenceValue)); expectedValue = new Timestamp(referenceValue.getTime()); referenceValue.setNanos(999999999); expectedValue.setNanos(999999999); assertEquals(expectedValue, instance.convert(referenceValue)); referenceValue.setNanos(123456789); expectedValue.setNanos(123456789); assertEquals(expectedValue, instance.convert(referenceValue)); assertEquals(expectedValue, instance.convert(referenceValue.toString())); expectedValue.setNanos(123000000); assertEquals(expectedValue, instance.convert(referenceValue.getTime())); instance = new TimestampField(0); referenceValue = new Timestamp(System.currentTimeMillis()); assertEquals(referenceValue, instance.convert(referenceValue)); expectedValue = new Timestamp(referenceValue.getTime()); referenceValue.setNanos(999999999); expectedValue.setNanos(0); assertEquals(expectedValue, instance.convert(referenceValue)); referenceValue.setNanos(123456789); expectedValue.setNanos(0); assertEquals(expectedValue, instance.convert(referenceValue)); assertEquals(expectedValue, instance.convert(referenceValue.toString())); expectedValue.setNanos(0); assertEquals(expectedValue, instance.convert(referenceValue.getTime())); try { instance.convert(null); fail("null value not checked"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_MISMATCH, ex.getErrorCode()); } try { instance.convert(Collections.EMPTY_LIST); fail("accepted invalid input data type"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_MISMATCH, ex.getErrorCode()); } }
@Override public final Timestamp convert(Object objValue) throws OperationException { Timestamp timestamp; try { if (objValue instanceof String) { timestamp = Timestamp.valueOf(objValue.toString()); timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_TIMESTAMP_FORMAT, "The value " + objValue.toString() + " could not parsed to a valid timestamp format. Supported format yyyy-[m]m-[d]d hh:mm:ss[.f...]"); } if (objValue instanceof Long) { timestamp = new Timestamp((Long) objValue); timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } if (objValue instanceof Timestamp) { timestamp = (Timestamp) objValue; timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Could not convert " + objValue + " to a valid time type"); }
TimestampField implements FieldType<Timestamp> { @Override public final Timestamp convert(Object objValue) throws OperationException { Timestamp timestamp; try { if (objValue instanceof String) { timestamp = Timestamp.valueOf(objValue.toString()); timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_TIMESTAMP_FORMAT, "The value " + objValue.toString() + " could not parsed to a valid timestamp format. Supported format yyyy-[m]m-[d]d hh:mm:ss[.f...]"); } if (objValue instanceof Long) { timestamp = new Timestamp((Long) objValue); timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } if (objValue instanceof Timestamp) { timestamp = (Timestamp) objValue; timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Could not convert " + objValue + " to a valid time type"); } }
TimestampField implements FieldType<Timestamp> { @Override public final Timestamp convert(Object objValue) throws OperationException { Timestamp timestamp; try { if (objValue instanceof String) { timestamp = Timestamp.valueOf(objValue.toString()); timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_TIMESTAMP_FORMAT, "The value " + objValue.toString() + " could not parsed to a valid timestamp format. Supported format yyyy-[m]m-[d]d hh:mm:ss[.f...]"); } if (objValue instanceof Long) { timestamp = new Timestamp((Long) objValue); timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } if (objValue instanceof Timestamp) { timestamp = (Timestamp) objValue; timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Could not convert " + objValue + " to a valid time type"); } TimestampField();  TimestampField(final Timestamp defaultValue);  TimestampField(final boolean withTimeZone);  TimestampField(final int precision);  TimestampField(final boolean withTimeZone, final Timestamp defaultValue);  TimestampField(final boolean withTimeZone, final int precision);  TimestampField(final boolean withTimeZone, final int precision, final Timestamp defaultValue); }
TimestampField implements FieldType<Timestamp> { @Override public final Timestamp convert(Object objValue) throws OperationException { Timestamp timestamp; try { if (objValue instanceof String) { timestamp = Timestamp.valueOf(objValue.toString()); timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_TIMESTAMP_FORMAT, "The value " + objValue.toString() + " could not parsed to a valid timestamp format. Supported format yyyy-[m]m-[d]d hh:mm:ss[.f...]"); } if (objValue instanceof Long) { timestamp = new Timestamp((Long) objValue); timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } if (objValue instanceof Timestamp) { timestamp = (Timestamp) objValue; timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Could not convert " + objValue + " to a valid time type"); } TimestampField();  TimestampField(final Timestamp defaultValue);  TimestampField(final boolean withTimeZone);  TimestampField(final int precision);  TimestampField(final boolean withTimeZone, final Timestamp defaultValue);  TimestampField(final boolean withTimeZone, final int precision);  TimestampField(final boolean withTimeZone, final int precision, final Timestamp defaultValue); @Override Types getType(); boolean isWithTimeZone(); int getPrecision(); @Override Timestamp getDefaultValue(); @Override boolean hasDefaultValue(); @Override final Timestamp convert(Object objValue); @Override JSONObject toJson(); }
TimestampField implements FieldType<Timestamp> { @Override public final Timestamp convert(Object objValue) throws OperationException { Timestamp timestamp; try { if (objValue instanceof String) { timestamp = Timestamp.valueOf(objValue.toString()); timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_TIMESTAMP_FORMAT, "The value " + objValue.toString() + " could not parsed to a valid timestamp format. Supported format yyyy-[m]m-[d]d hh:mm:ss[.f...]"); } if (objValue instanceof Long) { timestamp = new Timestamp((Long) objValue); timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } if (objValue instanceof Timestamp) { timestamp = (Timestamp) objValue; timestamp.setNanos(applyPrecisionRounding(timestamp.getNanos())); return timestamp; } throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Could not convert " + objValue + " to a valid time type"); } TimestampField();  TimestampField(final Timestamp defaultValue);  TimestampField(final boolean withTimeZone);  TimestampField(final int precision);  TimestampField(final boolean withTimeZone, final Timestamp defaultValue);  TimestampField(final boolean withTimeZone, final int precision);  TimestampField(final boolean withTimeZone, final int precision, final Timestamp defaultValue); @Override Types getType(); boolean isWithTimeZone(); int getPrecision(); @Override Timestamp getDefaultValue(); @Override boolean hasDefaultValue(); @Override final Timestamp convert(Object objValue); @Override JSONObject toJson();  }