/** * Get a key which can be used to retrieve an Optional value from a tuple in a type-safe way, without having to do an index lookup. * @param name The name of the slot to get a key for. * @param valueType The class of the Optional value to retrieve with the key. * @param <T> The type of the Optinoal value to retrieve with the key. * @return The created key. */ public <T> TupleKey<Optional<T>> getOptionalKey(String name, Class<T> valueType) { return getKey(name, Types.optionalOf(valueType)); }
/** * Get a key which can be used to retrieve a list of value from a tuple in a type-safe way, without having to do an index lookup. * @param name The name of the slot to get a key for. * @param elementType The element class of the values to retrieve with the key. * @param <T> The element type of the values to retrieve with the key. * @return The created key. */ public <T> TupleKey<List<T>> getListKey(String name, Class<T> elementType) { return getKey(name, Types.listOf(elementType)); }
/** * Get a key which can be used to retrieve a map of values from a tuple in a type-safe way, without having to do an index lookup. * @param name The name of the slot to get a key for. * @param keyType The key class of the map to retrieve with the key. * @param valueType The value class of the map to retrieve with the key. * @param <K> The key type of the value to retrieve with the key. * @param <V> The value type of the value to retrieve with the key. * @return The created key. */ public <K, V> TupleKey<Map<K, V>> getMapKey(String name, Class<K> keyType, Class<V> valueType) { return getKey(name, Types.mapOf(keyType, valueType)); }
/** * Make a tuple of the supplied values, first validating that they conform to this schema. * @param values The values to put in the Tuple. * @return The created Tuple. */ public Tuple makeWith(Object...values) { return make(values); }
/** * Create a new {@link Optional} TupleSlot, with the supplied name and value type. * @param name The name of the TupleSlot. * @param valueType The value type of the TupleSlot. * @return The created TupleSlot. */ public static <T> TupleSlot ofOptional(String name, Class<T> valueType) { if (valueType == null) throw new IllegalArgumentException("valueType must not be null"); return of(name, Types.optionalOf(valueType)); }
/** * Create a new {@link List} TupleSlot, with the supplied name and element type. * @param name The name of the TupleSlot. * @param elementType The element type of the TupleSlot. * @return The created TupleSlot. */ public static <T> TupleSlot ofList(String name, Class<T> elementType) { if (elementType == null) throw new IllegalArgumentException("elementType must not be null"); return of(name, Types.listOf(elementType)); }
/** * Create a new {@link Map} TupleSlot, with the supplied name and key and value types. * @param name The name of the TupleSlot. * @param keyType The key type of the TupleSlot. * @param valueType The value type of the TupleSlot. * @return The created TupleSlot. */ public static <K, V> TupleSlot ofMap(String name, Class<K> keyType, Class<V> valueType) { if (keyType == null) throw new IllegalArgumentException("keyType must not be null"); if (valueType == null) throw new IllegalArgumentException("valueType must not be null"); return of(name, Types.mapOf(keyType, valueType)); }
@SuppressWarnings("unchecked") private IntFunction<TupleKeyValue> getValueFrom(Object[] args) { return i -> tupleKeys[i].of(args[i + 2]); } }
@Override public String toString() { return tuple.toString(); } }
/** * Obtain the contents of the tuple as a map. * @return The contents of the tuple as a map. */ public Map<String, Object> toMap() { return schema.toMap(values); }
@Override public String toString() { return getTypeName(); } }
@Override public String toString() { return schema.format(values); } }
@Override public boolean equals(Object o) { return this == o || (o instanceof TupleKey && ((TupleKey<?>) o).schema.equals(schema) && ((TupleKey<?>) o).index == index); }
/** * Serialise the tuple using the supplied serialiser. * @param serialiser The serialiser to use to serialise each value in the tuple. * @param <V> The type to serialise each value to, e.g. String. * @return A map of serialised values. */ public <V> Map<String, V> serialise(Function<Object, V> serialiser) { return schema.serialise(serialiser, values); }
@Override public int hashCode() { return tuple.hashCode(); }
boolean acceptsType(Type type) { return Types.isAssignableFrom(this.type, type); }
static <T> Type listOf(Class<T> elementType) { return new ListType(elementType); }
boolean belongsToSchema(TupleSchema schema) { return tupleKey.belongsToSchema(schema); }
void build(Object[] values) { tupleKey.set(values, value); }
@SuppressWarnings("unchecked") private IntFunction<TupleKeyValue> getValueFrom(Object[] args) { return i -> tupleKeys[i].of(args[i + 2]); }