/** * Returns a type modelling a {@link Set} whose elements are of type {@code elementType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */ public static ParameterizedType setOf(Type elementType) { return newParameterizedType(Set.class, elementType); }
/** * Returns a type modelling a {@link List} whose elements are of type {@code elementType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */ public static ParameterizedType listOf(Type elementType) { return newParameterizedType(List.class, elementType); }
/** * Returns a type modelling a {@link Provider} that provides elements of type {@code elementType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */ public static ParameterizedType providerOf(Type providedType) { return newParameterizedType(Provider.class, providedType); }
/** * Returns a type modelling a {@link javax.inject.Provider} that provides elements of type {@code * elementType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */ public static Type javaxProviderOf(Type type) { return Types.newParameterizedType(javax.inject.Provider.class, type); } }
/** * Returns a type modelling a {@link Collection} whose elements are of type {@code elementType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */ public static ParameterizedType collectionOf(Type elementType) { return newParameterizedType(Collection.class, elementType); }
/** * Returns a type modelling a {@link Map} whose keys are of type {@code keyType} and whose values * are of type {@code valueType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */ public static ParameterizedType mapOf(Type keyType, Type valueType) { return newParameterizedType(Map.class, keyType, valueType); }
@SuppressWarnings("unchecked") static <T> TypeLiteral<Optional<javax.inject.Provider<T>>> optionalOfJavaxProvider( TypeLiteral<T> type) { return (TypeLiteral<Optional<javax.inject.Provider<T>>>) TypeLiteral.get( Types.newParameterizedType( Optional.class, newParameterizedType(javax.inject.Provider.class, type.getType()))); }
@SuppressWarnings("unchecked") static <T> TypeLiteral<Optional<Provider<T>>> optionalOfProvider(TypeLiteral<T> type) { return (TypeLiteral<Optional<Provider<T>>>) TypeLiteral.get( Types.newParameterizedType( Optional.class, newParameterizedType(Provider.class, type.getType()))); }
@SuppressWarnings("unchecked") public static <T> void bind(Binder binder, String propertyBase, Class<T> classToProvide) { bind( binder, propertyBase, classToProvide, Key.get(classToProvide), (Key) Key.get(Types.newParameterizedType(Supplier.class, classToProvide)) ); }
@SuppressWarnings("unchecked") public static <T> void bind(Binder binder, String propertyBase, Class<T> classToProvide, Annotation annotation) { bind( binder, propertyBase, classToProvide, Key.get(classToProvide, annotation), (Key) Key.get(Types.newParameterizedType(Supplier.class, classToProvide), annotation) ); }
public static <T> void bind(Binder binder, String key, Class<T> clazz, T defaultVal) { binder.bind(Key.get(Types.newParameterizedType(Supplier.class, clazz))) .toProvider((Provider) of(key, clazz, defaultVal)) .in(LazySingleton.class); }
static <T> TypeLiteral<?> javaOptionalOfJavaxProvider(TypeLiteral<T> type) { checkState(JAVA_OPTIONAL_CLASS != null, "java.util.Optional not found"); return TypeLiteral.get( Types.newParameterizedType( JAVA_OPTIONAL_CLASS, newParameterizedType(javax.inject.Provider.class, type.getType()))); }
static <T> TypeLiteral<?> javaOptionalOfProvider(TypeLiteral<T> type) { checkState(JAVA_OPTIONAL_CLASS != null, "java.util.Optional not found"); return TypeLiteral.get( Types.newParameterizedType( JAVA_OPTIONAL_CLASS, newParameterizedType(Provider.class, type.getType()))); }
@SuppressWarnings("unchecked") public static <T> void bind( Binder binder, String propertyBase, Class<T> classToProvide, Class<? extends Annotation> annotation ) { bind( binder, propertyBase, classToProvide, Key.get(classToProvide, annotation), (Key) Key.get(Types.newParameterizedType(Supplier.class, classToProvide), annotation) ); }
@SuppressWarnings("unchecked") public static <T> void bindWithDefault( Binder binder, String propertyBase, Class<T> classToProvide, Class<? extends T> defaultClass ) { bind( binder, propertyBase, classToProvide, defaultClass, Key.get(classToProvide), (Key) Key.get(Types.newParameterizedType(Supplier.class, classToProvide)) ); }
@SuppressWarnings("unchecked") static <T> TypeLiteral<Optional<T>> optionalOf(TypeLiteral<T> type) { return (TypeLiteral<Optional<T>>) TypeLiteral.get(Types.newParameterizedType(Optional.class, type.getType())); }
static <T> TypeLiteral<?> javaOptionalOf(TypeLiteral<T> type) { checkState(JAVA_OPTIONAL_CLASS != null, "java.util.Optional not found"); return TypeLiteral.get(Types.newParameterizedType(JAVA_OPTIONAL_CLASS, type.getType())); }
@SuppressWarnings("unchecked") static <T> TypeLiteral<Collection<javax.inject.Provider<T>>> collectionOfJavaxProvidersOf( TypeLiteral<T> elementType) { Type providerType = Types.newParameterizedType(javax.inject.Provider.class, elementType.getType()); Type type = Types.collectionOf(providerType); return (TypeLiteral<Collection<javax.inject.Provider<T>>>) TypeLiteral.get(type); }
@SuppressWarnings("unchecked") public static <T> void bindInstance( Binder binder, Key<T> bindKey, T instance ) { binder.bind(bindKey).toInstance(instance); final ParameterizedType supType = Types.newParameterizedType(Supplier.class, bindKey.getTypeLiteral().getType()); final Key supplierKey; if (bindKey.getAnnotationType() != null) { supplierKey = Key.get(supType, bindKey.getAnnotationType()); } else if (bindKey.getAnnotation() != null) { supplierKey = Key.get(supType, bindKey.getAnnotation()); } else { supplierKey = Key.get(supType); } binder.bind(supplierKey).toInstance(Suppliers.ofInstance(instance)); }
@SuppressWarnings("unchecked") static <K, V> TypeLiteral<Map<K, javax.inject.Provider<V>>> mapOfJavaxProviderOf( TypeLiteral<K> keyType, TypeLiteral<V> valueType) { return (TypeLiteral<Map<K, javax.inject.Provider<V>>>) TypeLiteral.get( Types.mapOf( keyType.getType(), newParameterizedType(javax.inject.Provider.class, valueType.getType()))); }