extended documentation for TypeLiteral/Typed

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1531968 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2013-10-14 16:28:50 +00:00
parent f9b486a171
commit 294c292487
2 changed files with 44 additions and 5 deletions

View File

@ -22,13 +22,51 @@ import java.lang.reflect.TypeVariable;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
/** /**
* Type literal comparable to {@code javax.enterprise.util.TypeLiteral}, * <p>Type literal comparable to {@code javax.enterprise.util.TypeLiteral},
* made generally available outside the JEE context. Allows the passing around of * made generally available outside the JEE context. Allows the passing around of
* a "token" that represents a type in a typesafe manner, as opposed to * a "token" that represents a type in a typesafe manner, as opposed to
* passing the (non-parameterized) {@link Type} object itself. * passing the (non-parameterized) {@link Type} object itself. Consider:</p>
* Additionally {@link TypeLiteral} implements the {@link Typed} interface which * <p>
* is a generalization of this concept. It is suggested that APIs be defined in * You might see such a typesafe API as:
* terms of the interface, which others might implemented in custom classes. * <pre>
* class Typesafe {
* &lt;T&gt; T obtain(Class&lt;T&gt; type, ...);
* }
* </pre>
* Consumed in the manner of:
* <pre>
* Foo foo = typesafe.obtain(Foo.class, ...);
* </pre>
* Yet, you run into problems when you want to do this with a parameterized type:
* <pre>
* List&lt;String&gt; listOfString = typesafe.obtain(List.class, ...); // could only give us a raw List
* </pre>
* {@code java.lang.reflect.Type} might provide some value:
* <pre>
* Type listOfStringType = ...; // firstly, how to obtain this? Doable, but not straightforward.
* List&lt;String&gt; listOfString = (List&lt;String&gt;) typesafe.obtain(listOfStringType, ...); // nongeneric Type would necessitate a cast
* </pre>
* The "type literal" concept was introduced to provide an alternative, i.e.:
* <pre>
* class Typesafe {
* &lt;T&gt; T obtain(TypeLiteral&lt;T&gt; type, ...);
* }
* </pre>
* Consuming code looks like:
* <pre>
* List&lt;String&gt; listOfString = typesafe.obtain(new TypeLiteral&lt;List&lt;String&gt;&gt;() {}, ...);
* </pre>
* This has the effect of "jumping up" a level to tie a {@code java.lang.reflect.Type}
* to a type variable while simultaneously making it short work to obtain a
* {@code Type} instance for any given type, inline.
* </p>
* <p>Additionally {@link TypeLiteral} implements the {@link Typed} interface which
* is a generalization of this concept, and which may be implemented in custom classes.
* It is suggested that APIs be defined in terms of the interface, in the following manner:
* <pre>
* &lt;T&gt; T obtain(Typed&lt;T&gt; typed, ...);
* </pre>
* </p>
*/ */
public abstract class TypeLiteral<T> implements Typed<T> { public abstract class TypeLiteral<T> implements Typed<T> {

View File

@ -20,6 +20,7 @@ import java.lang.reflect.Type;
/** /**
* Generalization of "has a type." * Generalization of "has a type."
* @see TypeLiteral
*/ */
public interface Typed<T> { public interface Typed<T> {