boolean acceptsType(Type type) { return Types.isAssignableFrom(this.type, type); }
/** * 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)); }
/** * 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)); }
/** * 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)); }
/** * 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)); }
/** * 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)); }
boolean acceptsClass(Class<?> klass) { return Types.isAssignableFrom(type, klass); }
private boolean acceptsOptional(Object presentValue) { return Types.isAssignableFrom(type, Optional.class) && Types.isAssignableFrom( ((ParameterizedType) type).getActualTypeArguments()[0], presentValue.getClass()); }
private static boolean isAssignableFrom(Type targetType, Class<?> valueType) { return (targetType instanceof Class && isAssignableFrom(Class.class.cast(targetType), valueType) || targetType instanceof ParameterizedType && isAssignableFrom( Class.class.cast(ParameterizedType.class.cast(targetType).getRawType()), valueType)); }
static boolean isAssignableFrom(Type targetType, Type valueType) { return targetType.equals(valueType) || (valueType instanceof Class && isAssignableFrom(targetType, Class.class.cast(valueType))); }