Skip to content

Instantly share code, notes, and snippets.

@leksak
Created November 26, 2015 13:08
Show Gist options
  • Save leksak/c0ad06fff03872a14531 to your computer and use it in GitHub Desktop.
Save leksak/c0ad06fff03872a14531 to your computer and use it in GitHub Desktop.
Found in my old source-code
/**
* Provides the necessary interface for a typesafe heterogeneous
* container, where the key and the return type is parametrized.
*
* A parametrized key is used to insert and retrieve values. By
* utilising the generic type system the type of the value returned is
* guaranteed to be the same as its key.
*
* The parametrized return value is to allow for a facsimile to a
* builder pattern, leading to brevity of expression for adding
* different types of objects to the container
*
* @param <R> The type which acts as the container, hence if you
* have a class, Foo, which implements this interface
* then you'd write:
*
* {@code
* public class Foo implements HeterogeneousContainer<Foo, ...>
* }
*
* as such, whenever you call the
* {@link HeterogeneousContainer#add}
* method the return value will be <code>Foo</code> which
* affords you to write successive calls to
* {@link HeterogeneousContainer#add}
* during object instantiation, i.e.
*
* {@code
* Foo bar = new Foo().add(Some.class, classInstance)
* .add(Other.Class, otherInstance);
* }
* @param <E> the super-class which is maintained. This is so that you
* may, for legibility, specify that all the contained values
* are such that they either extend a certain class or that
* they themselves are interfaces which in turn extend
* a common interface. Hence,
*
* {@code
* public class Foo implements
* HeterogeneousContainer<Foo, Bar>
* }
*
* will mean that all values contained within Foo are such
* that they extend Bar.
*/
public interface HeterogeneousContainer<R, E> {
/**
* Gets a value of type <code>T</code> given a reference to
* its class-type. Will return any object corresponding to
* the specified class type should there exist such a one,
* otherwise null.
*
* @param type The type of the object you which to retrieve,
* passed as a type token, i.e. ClassName.class.
* @param <T> The type of the instance retrieved.
* @return an instance of the specified type, or null.
*/
public <T extends E> T get(Class<T> type);
/**
* Store an object instance of the specified type. If type is null
* an exception is thrown.
*
* @param type the type of object to be stored, passed as a
* type token, i.e. ClassName.class.
* @param instance an instance of the supplied type.
* @param <T> the type variable.
* @return the object, emulates a builder pattern. For succinctness.
*/
public <T extends E> R add(Class<T> type, T instance);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment