예제 #1
0
  @SuppressWarnings("unchecked")
  public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass) {
    DataType type = metadata.getType(i);
    if (type.getName() != DataType.Name.MAP)
      throw new InvalidTypeException(
          String.format("Column %s is not of map type", metadata.getName(i)));

    Class<?> expectedKeysClass = type.getTypeArguments().get(0).getName().javaType;
    Class<?> expectedValuesClass = type.getTypeArguments().get(1).getName().javaType;
    if (!keysClass.isAssignableFrom(expectedKeysClass)
        || !valuesClass.isAssignableFrom(expectedValuesClass))
      throw new InvalidTypeException(
          String.format(
              "Column %s is a map of %s->%s (CQL type %s), cannot be retrieve as a map of %s->%s",
              metadata.getName(i),
              expectedKeysClass,
              expectedValuesClass,
              type,
              keysClass,
              valuesClass));

    ByteBuffer value = data.get(i);
    if (value == null) return Collections.<K, V>emptyMap();

    return Collections.unmodifiableMap(Codec.<Map<K, V>>getCodec(type).compose(value));
  }
예제 #2
0
  @SuppressWarnings("unchecked")
  public <T> Set<T> getSet(int i, Class<T> elementsClass) {
    DataType type = metadata.getType(i);
    if (type.getName() != DataType.Name.SET)
      throw new InvalidTypeException(
          String.format("Column %s is not of set type", metadata.getName(i)));

    Class<?> expectedClass = type.getTypeArguments().get(0).getName().javaType;
    if (!elementsClass.isAssignableFrom(expectedClass))
      throw new InvalidTypeException(
          String.format(
              "Column %s is a set of %s (CQL type %s), cannot be retrieve as a set of %s",
              metadata.getName(i), expectedClass, type, elementsClass));

    ByteBuffer value = data.get(i);
    if (value == null) return Collections.<T>emptySet();

    return Collections.unmodifiableSet(Codec.<Set<T>>getCodec(type).compose(value));
  }
예제 #3
0
  @SuppressWarnings("unchecked")
  public <T> List<T> getList(int i, Class<T> elementsClass) {
    DataType type = metadata.getType(i);
    if (type.getName() != DataType.Name.LIST)
      throw new InvalidTypeException(
          String.format("Column %s is not of list type", metadata.getName(i)));

    Class<?> expectedClass = type.getTypeArguments().get(0).getName().javaType;
    if (!elementsClass.isAssignableFrom(expectedClass))
      throw new InvalidTypeException(
          String.format(
              "Column %s is a list of %s (CQL type %s), cannot be retrieve as a list of %s",
              metadata.getName(i), expectedClass, type, elementsClass));

    ByteBuffer value = data.get(i);
    if (value == null) return Collections.<T>emptyList();

    // TODO: we could avoid the getCodec call if we kept a reference to the original message.
    return Collections.unmodifiableList(Codec.<List<T>>getCodec(type).compose(value));
  }