SQLRecordPredicate extends Predicate { public static Object cast(Object value, int type) { if (value == null) { return null; } try { switch (type) { case ColumnTypes.BOOLEAN: case ColumnTypes.NOTNULL_BOOLEAN: { if (value instanceof Boolean) { return ((Boolean) value); } if (value instanceof Number) { int asInt = ((Number) value).intValue(); switch (asInt) { case 1: return Boolean.TRUE; case 0: return Boolean.FALSE; default: throw new IllegalArgumentException("Numeric value " + value + " cannot be converted to a Boolean"); } } String s = value.toString(); switch (s) { case "true": case "1": return Boolean.TRUE; case "0": case "false": return Boolean.FALSE; default: throw new IllegalArgumentException("Value " + value + " cannot be converted to a Boolean"); } } case ColumnTypes.STRING: case ColumnTypes.NOTNULL_STRING: if (value instanceof RawString || value instanceof String) { return value; } if (value instanceof Number || value instanceof Boolean) { return value.toString(); } if (value instanceof Timestamp) { return value.toString(); } throw new IllegalArgumentException("Value " + value + " (" + value.getClass() + ") cannot be converted to a STRING"); case ColumnTypes.INTEGER: case ColumnTypes.NOTNULL_INTEGER: { if (value instanceof Boolean) { return ((Boolean) value) ? 1 : 0; } if (value instanceof Number) { return ((Number) value).intValue(); } if (value instanceof RawString) { return Integer.parseInt(((RawString) value).toString()); } if (value instanceof String) { return Integer.parseInt(((String) value)); } throw new IllegalArgumentException("type " + value.getClass() + " (value " + value + ") cannot be cast to INTEGER"); } case ColumnTypes.LONG: case ColumnTypes.NOTNULL_LONG: { if (value instanceof Boolean) { return ((Boolean) value) ? 1L : 0L; } if (value instanceof Number) { return ((Number) value).longValue(); } if (value instanceof RawString) { return Long.parseLong(((RawString) value).toString()); } if (value instanceof String) { return Long.parseLong(((String) value)); } throw new IllegalArgumentException("type " + value.getClass() + " (value " + value + ") cannot be cast to LONG"); } case ColumnTypes.DOUBLE: case ColumnTypes.NOTNULL_DOUBLE: { if (value instanceof Number) { return ((Number) value).doubleValue(); } if (value instanceof RawString) { return Double.parseDouble(((RawString) value).toString()); } if (value instanceof String) { return Double.parseDouble(((String) value)); } if (value instanceof Boolean) { return ((Boolean) value) ? 1d : 0d; } throw new IllegalArgumentException("type " + value.getClass() + " (value " + value + ") cannot be cast to DOUBLE"); } case ColumnTypes.TIMESTAMP: case ColumnTypes.NOTNULL_TIMESTAMP: { if (value instanceof java.sql.Timestamp) { return (java.sql.Timestamp) value; } if (value instanceof Number) { return new java.sql.Timestamp(((Number) value).longValue()); } if (value instanceof RawString) { return java.sql.Timestamp.valueOf(((RawString) value).toString()); } if (value instanceof String) { return java.sql.Timestamp.valueOf(((String) value)); } throw new IllegalArgumentException("type " + value.getClass() + " (value " + value + ") cannot be cast to TIMESTAMP"); } default: return value; } } catch (ClassCastException err) { throw new IllegalArgumentException("Unexpected error on cast of value " + value + " (" + value.getClass() + "): " + err, err); } } SQLRecordPredicate(Table table, String tableAlias, CompiledSQLExpression where); @Override PrimaryKeyMatchOutcome matchesRawPrimaryKey(Bytes key, StatementEvaluationContext context); @Override boolean evaluate(Record record, StatementEvaluationContext context); @Override void validateContext(StatementEvaluationContext context); @Override String toString(); CompiledSQLExpression getPrimaryKeyFilter(); void setPrimaryKeyFilter(CompiledSQLExpression primaryKeyFilter); CompiledSQLExpression getWhere(); static Object cast(Object value, int type); @Override T unwrap(Class<T> clazz); @Override int estimateObjectSizeForCache();  }
@Test public void testCast() throws Exception { testCast(null, null, ColumnTypes.INTEGER); testCast(1, 1L, ColumnTypes.INTEGER); testCast(1, 1d, ColumnTypes.INTEGER); testCast(1, true, ColumnTypes.INTEGER); testCast(0, false, ColumnTypes.INTEGER); testCast(-1, "-1", ColumnTypes.INTEGER); testCast(-1, RawString.of("-1"), ColumnTypes.INTEGER); testCast(1L, 1L, ColumnTypes.LONG); testCast(1L, 1d, ColumnTypes.LONG); testCast(1L, true, ColumnTypes.LONG); testCast(0L, false, ColumnTypes.LONG); testCast(-1L, "-1", ColumnTypes.LONG); testCast(-1L, RawString.of("-1"), ColumnTypes.LONG); testCast(1d, 1L, ColumnTypes.DOUBLE); testCast(1d, 1d, ColumnTypes.DOUBLE); testCast(1d, true, ColumnTypes.DOUBLE); testCast(0d, false, ColumnTypes.DOUBLE); testCast(-1d, "-1", ColumnTypes.DOUBLE); testCast(-1d, RawString.of("-1"), ColumnTypes.DOUBLE); testCast(true, 1L, ColumnTypes.BOOLEAN); testCast(true, 1d, ColumnTypes.BOOLEAN); testCast(true, true, ColumnTypes.BOOLEAN); testCast(false, false, ColumnTypes.BOOLEAN); testCast(true, "1", ColumnTypes.BOOLEAN); testCast(false, "0", ColumnTypes.BOOLEAN); testCast("1", 1L, ColumnTypes.STRING); testCast("1.0", 1d, ColumnTypes.STRING); testCast("true", true, ColumnTypes.STRING); testCast("false", false, ColumnTypes.STRING); testCast("-1", "-1", ColumnTypes.STRING); testCast(RawString.of("-1"), RawString.of("-1"), ColumnTypes.STRING); long now = System.currentTimeMillis(); testCast(new java.sql.Timestamp(1), 1L, ColumnTypes.TIMESTAMP); testCast(new java.sql.Timestamp(now), new java.sql.Timestamp(now), ColumnTypes.TIMESTAMP); testCast(new java.sql.Timestamp(now), RawString.of(new java.sql.Timestamp(now).toString()), ColumnTypes.TIMESTAMP); testCast(new java.sql.Timestamp(now), new java.sql.Timestamp(now).toString(), ColumnTypes.TIMESTAMP); }