use of com.datastax.oss.driver.api.core.type.CustomType in project java-driver by datastax.
the class CachingCodecRegistry method createCodec.
// Try to create a codec when we haven't found it in the cache.
// Variant where the Java type is unknown.
@NonNull
protected TypeCodec<?> createCodec(@NonNull DataType cqlType) {
if (cqlType instanceof ListType) {
DataType elementType = ((ListType) cqlType).getElementType();
TypeCodec<Object> elementCodec = codecFor(elementType);
return TypeCodecs.listOf(elementCodec);
} else if (cqlType instanceof SetType) {
DataType elementType = ((SetType) cqlType).getElementType();
TypeCodec<Object> elementCodec = codecFor(elementType);
return TypeCodecs.setOf(elementCodec);
} else if (cqlType instanceof MapType) {
DataType keyType = ((MapType) cqlType).getKeyType();
DataType valueType = ((MapType) cqlType).getValueType();
TypeCodec<Object> keyCodec = codecFor(keyType);
TypeCodec<Object> valueCodec = codecFor(valueType);
return TypeCodecs.mapOf(keyCodec, valueCodec);
} else if (cqlType instanceof TupleType) {
return TypeCodecs.tupleOf((TupleType) cqlType);
} else if (cqlType instanceof UserDefinedType) {
return TypeCodecs.udtOf((UserDefinedType) cqlType);
} else if (cqlType instanceof CustomType) {
return TypeCodecs.custom(cqlType);
}
throw new CodecNotFoundException(cqlType, null);
}
use of com.datastax.oss.driver.api.core.type.CustomType in project java-driver by datastax.
the class ComplexTypeSerializerUtil method toProtocolSpec.
private static RawType toProtocolSpec(DataType dataType) {
int id = dataType.getProtocolCode();
RawType type = RawType.PRIMITIVES.get(id);
if (type != null) {
return type;
}
switch(id) {
case ProtocolConstants.DataType.CUSTOM:
CustomType customType = ((CustomType) dataType);
type = new RawType.RawCustom(customType.getClassName());
break;
case ProtocolConstants.DataType.LIST:
ListType listType = ((ListType) dataType);
type = new RawType.RawList(toProtocolSpec(listType.getElementType()));
break;
case ProtocolConstants.DataType.SET:
SetType setType = ((SetType) dataType);
type = new RawType.RawSet(toProtocolSpec(setType.getElementType()));
break;
case ProtocolConstants.DataType.MAP:
MapType mapType = ((MapType) dataType);
type = new RawType.RawMap(toProtocolSpec(mapType.getKeyType()), toProtocolSpec(mapType.getValueType()));
break;
case ProtocolConstants.DataType.TUPLE:
TupleType tupleType = ((TupleType) dataType);
ImmutableList.Builder<RawType> subTypesList = ImmutableList.builderWithExpectedSize(tupleType.getComponentTypes().size());
for (int i = 0; i < tupleType.getComponentTypes().size(); i++) {
subTypesList.add(toProtocolSpec(tupleType.getComponentTypes().get(i)));
}
type = new RawType.RawTuple(subTypesList.build());
break;
case ProtocolConstants.DataType.UDT:
UserDefinedType userDefinedType = ((UserDefinedType) dataType);
ImmutableMap.Builder<String, RawType> subTypesMap = ImmutableMap.builderWithExpectedSize(userDefinedType.getFieldNames().size());
for (int i = 0; i < userDefinedType.getFieldTypes().size(); i++) {
subTypesMap.put(userDefinedType.getFieldNames().get(i).asInternal(), toProtocolSpec(userDefinedType.getFieldTypes().get(i)));
}
type = new RawType.RawUdt(Objects.requireNonNull(userDefinedType.getKeyspace()).asInternal(), userDefinedType.getName().asInternal(), subTypesMap.build());
break;
default:
throw new IllegalArgumentException("Unsupported type: " + dataType.asCql(true, true));
}
return type;
}
use of com.datastax.oss.driver.api.core.type.CustomType in project java-driver by datastax.
the class CachingCodecRegistry method createCodec.
// Try to create a codec when we haven't found it in the cache
@NonNull
protected TypeCodec<?> createCodec(@Nullable DataType cqlType, @Nullable GenericType<?> javaType, boolean isJavaCovariant) {
LOG.trace("[{}] Cache miss, creating codec", logPrefix);
// Either type can be null, but not both.
if (javaType == null) {
assert cqlType != null;
return createCodec(cqlType);
} else if (cqlType == null) {
return createCodec(javaType, isJavaCovariant);
} else {
// Both non-null
TypeToken<?> token = javaType.__getToken();
if (cqlType instanceof ListType && List.class.isAssignableFrom(token.getRawType())) {
DataType elementCqlType = ((ListType) cqlType).getElementType();
TypeCodec<Object> elementCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
} else {
elementCodec = codecFor(elementCqlType);
}
return TypeCodecs.listOf(elementCodec);
} else if (cqlType instanceof SetType && Set.class.isAssignableFrom(token.getRawType())) {
DataType elementCqlType = ((SetType) cqlType).getElementType();
TypeCodec<Object> elementCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
} else {
elementCodec = codecFor(elementCqlType);
}
return TypeCodecs.setOf(elementCodec);
} else if (cqlType instanceof MapType && Map.class.isAssignableFrom(token.getRawType())) {
DataType keyCqlType = ((MapType) cqlType).getKeyType();
DataType valueCqlType = ((MapType) cqlType).getValueType();
TypeCodec<Object> keyCodec;
TypeCodec<Object> valueCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> keyJavaType = GenericType.of(typeArguments[0]);
GenericType<?> valueJavaType = GenericType.of(typeArguments[1]);
keyCodec = uncheckedCast(codecFor(keyCqlType, keyJavaType, isJavaCovariant));
valueCodec = uncheckedCast(codecFor(valueCqlType, valueJavaType, isJavaCovariant));
} else {
keyCodec = codecFor(keyCqlType);
valueCodec = codecFor(valueCqlType);
}
return TypeCodecs.mapOf(keyCodec, valueCodec);
} else if (cqlType instanceof TupleType && TupleValue.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.tupleOf((TupleType) cqlType);
} else if (cqlType instanceof UserDefinedType && UdtValue.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.udtOf((UserDefinedType) cqlType);
} else if (cqlType instanceof CustomType && ByteBuffer.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.custom(cqlType);
}
throw new CodecNotFoundException(cqlType, javaType);
}
}
Aggregations