DataType columnType = statement.getVariables().getType(i);
switch (columnType.getName()) {
case LIST:
if (!(toSet instanceof List))
throw new InvalidTypeException(String.format("Invalid type for value %d, column is a list but %s provided", i, toSet.getClass()));
List<?> l = (List)toSet;
// If the list is empty, it will never fail validation, but otherwise we should check the list given if of the right type
if (!l.isEmpty()) {
// Ugly? Yes
Class<?> providedClass = l.get(0).getClass();
Class<?> expectedClass = columnType.getTypeArguments().get(0).asJavaClass();
if (!expectedClass.isAssignableFrom(providedClass))
throw new InvalidTypeException(String.format("Invalid type for value %d of CQL type %s, expecting list of %s but provided list of %s", i, columnType, expectedClass, providedClass));
}
break;
case SET:
if (!(toSet instanceof Set))
throw new InvalidTypeException(String.format("Invalid type for value %d, column is a set but %s provided", i, toSet.getClass()));
Set<?> s = (Set)toSet;
// If the list is empty, it will never fail validation, but otherwise we should check the list given if of the right type
if (!s.isEmpty()) {
// Ugly? Yes
Class<?> providedClass = s.iterator().next().getClass();
Class<?> expectedClass = columnType.getTypeArguments().get(0).getName().javaType;
if (!expectedClass.isAssignableFrom(providedClass))
throw new InvalidTypeException(String.format("Invalid type for value %d of CQL type %s, expecting set of %s but provided set of %s", i, columnType, expectedClass, providedClass));
}
break;
case MAP:
if (!(toSet instanceof Map))
throw new InvalidTypeException(String.format("Invalid type for value %d, column is a map but %s provided", i, toSet.getClass()));
Map<?, ?> m = (Map)toSet;
// If the list is empty, it will never fail validation, but otherwise we should check the list given if of the right type
if (!m.isEmpty()) {
// Ugly? Yes
Map.Entry entry = (Map.Entry)m.entrySet().iterator().next();
Class<?> providedKeysClass = entry.getKey().getClass();
Class<?> providedValuesClass = entry.getValue().getClass();
Class<?> expectedKeysClass = columnType.getTypeArguments().get(0).getName().javaType;
Class<?> expectedValuesClass = columnType.getTypeArguments().get(1).getName().javaType;
if (!expectedKeysClass.isAssignableFrom(providedKeysClass) || !expectedValuesClass.isAssignableFrom(providedValuesClass))
throw new InvalidTypeException(String.format("Invalid type for value %d of CQL type %s, expecting map of %s->%s but provided set of %s->%s", i, columnType, expectedKeysClass, expectedValuesClass, providedKeysClass, providedValuesClass));
}
break;
default:
Class<?> providedClass = toSet.getClass();
Class<?> expectedClass = columnType.getName().javaType;
if (!expectedClass.isAssignableFrom(providedClass))
throw new InvalidTypeException(String.format("Invalid type for value %d of CQL type %s, expecting %s but %s provided", i, columnType, expectedClass, providedClass));
break;
}
setValue(i, Codec.getCodec(columnType).decompose(toSet));
}
return this;