com.twitter.common.inject
Class DefaultProvider<T>

java.lang.Object
  extended by com.twitter.common.inject.DefaultProvider<T>
Type Parameters:
T - the type of object this provides
All Implemented Interfaces:
javax.inject.Provider<T>

public class DefaultProvider<T>
extends Object
implements Provider<T>

Provider that has a default value which can be overridden. The intended use of this class is:

 Default installer:
   bind(DefaultProvider.makeDefaultKey(Runnable.class, "mykey").toInstance(defaultRunnable);
   DefaultProvider.bindOrElse(Runnable.class, "mykey", binder());

 Custom override:
     bind(DefaultProvider.makeCustomKey(Runnable.class, "mykey")).toInstance(myCustomRunnable);

 Injection:
     Inject Named("myKey") Runnable runnable;

 


Constructor Summary
DefaultProvider(Key<T> defaultProviderKey, Key<T> customProviderKey)
           
 
Method Summary
static
<T> void
bindOrElse(Class<T> type, String exposedKey, Binder binder)
          Convenience method for calls to bindOrElse(TypeLiteral, String, Binder), that are not binding a parameterized type.
static
<T> void
bindOrElse(Key<T> customBinding, Key<T> defaultBinding, Key<T> exposedBinding, Binder binder)
          Creates a DefaultProvider and installs a new module to binder, which will serve as an indirection layer for swapping the default binding with a custom one.
static
<T> void
bindOrElse(TypeLiteral<T> type, String exposedKey, Binder binder)
          Convenience function for creating and installing a DefaultProvider.
 T get()
          Provides a fully-constructed and injected instance of T.
static String makeCustomBindingKey(String rootKey)
           
static Named makeCustomBindingName(String rootKey)
           
static
<T> Key<T>
makeCustomKey(Class<T> type, String rootKey)
           
static
<T> Key<T>
makeCustomKey(TypeLiteral<T> type, String rootKey)
           
static String makeDefaultBindingKey(String rootKey)
           
static Named makeDefaultBindingName(String rootKey)
           
static
<T> Key<T>
makeDefaultKey(Class<T> type, String rootKey)
           
static
<T> Key<T>
makeDefaultKey(TypeLiteral<T> type, String rootKey)
           
 void setInjector(Injector injector)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultProvider

public DefaultProvider(Key<T> defaultProviderKey,
                       Key<T> customProviderKey)
Method Detail

setInjector

@Inject
public void setInjector(Injector injector)

get

public T get()
Description copied from interface: javax.inject.Provider
Provides a fully-constructed and injected instance of T.

Specified by:
get in interface javax.inject.Provider<T>

bindOrElse

public static <T> void bindOrElse(Key<T> customBinding,
                                  Key<T> defaultBinding,
                                  Key<T> exposedBinding,
                                  Binder binder)
Creates a DefaultProvider and installs a new module to binder, which will serve as an indirection layer for swapping the default binding with a custom one.

Type Parameters:
T - The type of binding to make.
Parameters:
customBinding - The custom binding key.
defaultBinding - The default binding key.
exposedBinding - The exposed binding key.
binder - The binder to install bindings to.

bindOrElse

public static <T> void bindOrElse(TypeLiteral<T> type,
                                  String exposedKey,
                                  Binder binder)
Convenience function for creating and installing a DefaultProvider. This will use internal suffixes to create names for the custom and default bindings. When bound this way, callers should use one of the functions such as makeDefaultBindingKey(String) to set default and custom bindings.

Type Parameters:
T - The type of binding to make.
Parameters:
type - The type of object to bind.
exposedKey - The exposed key.
binder - The binder to install to.

bindOrElse

public static <T> void bindOrElse(Class<T> type,
                                  String exposedKey,
                                  Binder binder)
Convenience method for calls to bindOrElse(TypeLiteral, String, Binder), that are not binding a parameterized type.

Type Parameters:
T - The type of binding to make.
Parameters:
type - The class of the object to bind.
exposedKey - The exposed key.
binder - The binder to install to.

makeDefaultBindingKey

public static String makeDefaultBindingKey(String rootKey)

makeDefaultBindingName

public static Named makeDefaultBindingName(String rootKey)

makeDefaultKey

public static <T> Key<T> makeDefaultKey(TypeLiteral<T> type,
                                        String rootKey)

makeDefaultKey

public static <T> Key<T> makeDefaultKey(Class<T> type,
                                        String rootKey)

makeCustomBindingKey

public static String makeCustomBindingKey(String rootKey)

makeCustomBindingName

public static Named makeCustomBindingName(String rootKey)

makeCustomKey

public static <T> Key<T> makeCustomKey(Class<T> type,
                                       String rootKey)

makeCustomKey

public static <T> Key<T> makeCustomKey(TypeLiteral<T> type,
                                       String rootKey)