use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class CachingCodecRegistry method codecFor.
@NonNull
@Override
public <JavaTypeT> TypeCodec<JavaTypeT> codecFor(@NonNull JavaTypeT value) {
Preconditions.checkNotNull(value);
LOG.trace("[{}] Looking up codec for object {}", logPrefix, value);
for (TypeCodec<?> primitiveCodec : primitiveCodecs) {
if (primitiveCodec.accepts(value)) {
LOG.trace("[{}] Found matching primitive codec {}", logPrefix, primitiveCodec);
return uncheckedCast(primitiveCodec);
}
}
for (TypeCodec<?> userCodec : userCodecs) {
if (userCodec.accepts(value)) {
LOG.trace("[{}] Found matching user codec {}", logPrefix, userCodec);
return uncheckedCast(userCodec);
}
}
DataType cqlType = inferCqlTypeFromValue(value);
GenericType<?> javaType = inspectType(value, cqlType);
LOG.trace("[{}] Continuing based on inferred CQL type {} and Java type {}", logPrefix, cqlType, javaType);
return uncheckedCast(getCachedCodec(cqlType, javaType, true));
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class CachingCodecRegistry method inferJavaTypeFromCqlType.
@NonNull
protected GenericType<?> inferJavaTypeFromCqlType(@NonNull DataType cqlType) {
if (cqlType instanceof ListType) {
DataType elementType = ((ListType) cqlType).getElementType();
return GenericType.listOf(inferJavaTypeFromCqlType(elementType));
} else if (cqlType instanceof SetType) {
DataType elementType = ((SetType) cqlType).getElementType();
return GenericType.setOf(inferJavaTypeFromCqlType(elementType));
} else if (cqlType instanceof MapType) {
DataType keyType = ((MapType) cqlType).getKeyType();
DataType valueType = ((MapType) cqlType).getValueType();
return GenericType.mapOf(inferJavaTypeFromCqlType(keyType), inferJavaTypeFromCqlType(valueType));
}
switch(cqlType.getProtocolCode()) {
case ProtocolConstants.DataType.CUSTOM:
case ProtocolConstants.DataType.BLOB:
return GenericType.BYTE_BUFFER;
case ProtocolConstants.DataType.ASCII:
case ProtocolConstants.DataType.VARCHAR:
return GenericType.STRING;
case ProtocolConstants.DataType.BIGINT:
case ProtocolConstants.DataType.COUNTER:
return GenericType.LONG;
case ProtocolConstants.DataType.BOOLEAN:
return GenericType.BOOLEAN;
case ProtocolConstants.DataType.DECIMAL:
return GenericType.BIG_DECIMAL;
case ProtocolConstants.DataType.DOUBLE:
return GenericType.DOUBLE;
case ProtocolConstants.DataType.FLOAT:
return GenericType.FLOAT;
case ProtocolConstants.DataType.INT:
return GenericType.INTEGER;
case ProtocolConstants.DataType.TIMESTAMP:
return GenericType.INSTANT;
case ProtocolConstants.DataType.UUID:
case ProtocolConstants.DataType.TIMEUUID:
return GenericType.UUID;
case ProtocolConstants.DataType.VARINT:
return GenericType.BIG_INTEGER;
case ProtocolConstants.DataType.INET:
return GenericType.INET_ADDRESS;
case ProtocolConstants.DataType.DATE:
return GenericType.LOCAL_DATE;
case ProtocolConstants.DataType.TIME:
return GenericType.LOCAL_TIME;
case ProtocolConstants.DataType.SMALLINT:
return GenericType.SHORT;
case ProtocolConstants.DataType.TINYINT:
return GenericType.BYTE;
case ProtocolConstants.DataType.DURATION:
return GenericType.CQL_DURATION;
case ProtocolConstants.DataType.UDT:
return GenericType.UDT_VALUE;
case ProtocolConstants.DataType.TUPLE:
return GenericType.TUPLE_VALUE;
default:
throw new CodecNotFoundException(cqlType, null);
}
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class UdtCodec method format.
@NonNull
@Override
public String format(@Nullable UdtValue value) {
if (value == null) {
return "NULL";
}
CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
StringBuilder sb = new StringBuilder("{");
int size = cqlType.getFieldTypes().size();
boolean first = true;
for (int i = 0; i < size; i++) {
if (first) {
first = false;
} else {
sb.append(",");
}
CqlIdentifier elementName = cqlType.getFieldNames().get(i);
sb.append(elementName.asCql(true));
sb.append(":");
DataType elementType = cqlType.getFieldTypes().get(i);
TypeCodec<Object> codec = registry.codecFor(elementType);
sb.append(codec.format(value.get(i, codec)));
}
sb.append("}");
return sb.toString();
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class CachingCodecRegistryTest method should_find_primitive_codecs_for_types.
@Test
@UseDataProvider(value = "primitiveCodecs", location = CachingCodecRegistryTestDataProviders.class)
public void should_find_primitive_codecs_for_types(TypeCodec<?> codec) {
TestCachingCodecRegistry registry = new TestCachingCodecRegistry(mockCache);
DataType cqlType = codec.getCqlType();
GenericType<?> javaType = codec.getJavaType();
assertThat(registry.codecFor(cqlType, javaType)).isSameAs(codec);
assertThat(registry.codecFor(cqlType)).isSameAs(codec);
assertThat(javaType.__getToken().getType()).isInstanceOf(Class.class);
Class<?> javaClass = (Class<?>) javaType.__getToken().getType();
assertThat(registry.codecFor(cqlType, javaClass)).isSameAs(codec);
// Primitive mappings never hit the cache
verifyZeroInteractions(mockCache);
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class DefaultTupleValue method hashCode.
@Override
public int hashCode() {
int result = type.hashCode();
for (int i = 0; i < values.length; i++) {
DataType innerThisType = type.getComponentTypes().get(i);
Object thisValue = this.codecRegistry().codecFor(innerThisType).decode(this.values[i], this.protocolVersion());
if (thisValue != null) {
result = 31 * result + thisValue.hashCode();
}
}
return result;
}
Aggregations