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:
parent
f9b486a171
commit
294c292487
|
@ -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 {
|
||||||
|
* <T> T obtain(Class<T> 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<String> 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<String> listOfString = (List<String>) 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 {
|
||||||
|
* <T> T obtain(TypeLiteral<T> type, ...);
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
* Consuming code looks like:
|
||||||
|
* <pre>
|
||||||
|
* List<String> listOfString = typesafe.obtain(new TypeLiteral<List<String>>() {}, ...);
|
||||||
|
* </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>
|
||||||
|
* <T> T obtain(Typed<T> typed, ...);
|
||||||
|
* </pre>
|
||||||
|
* </p>
|
||||||
*/
|
*/
|
||||||
public abstract class TypeLiteral<T> implements Typed<T> {
|
public abstract class TypeLiteral<T> implements Typed<T> {
|
||||||
|
|
||||||
|
|
|
@ -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> {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue