nl.knowledgeplaza.util
Class TypeToken
java.lang.Object
nl.knowledgeplaza.util.TypeToken
public class TypeToken
- extends java.lang.Object
http://www.jquantlib.org/index.php/Using_TypeTokens_to_retrieve_generic_parameters
TypeNode keeps a Class information and a list of children classes.
A typical usage consists on obtain a TypeToken root which can be used to traverse a hierarchy of generic parameters. Each TypeToken holds a Class information and contains a list of children nodes. A typical usage is in the context of retrieving actual generic parameters. The example below shows how a generic class can verify at instantiation time which actual generic parameters where passed by the caller.
public class TimeSeries {
public TimeSeries() {
this.klass = new TypeTokenTree(this.getClass()).getRoot().get(0).getElement();
if (Double.class.isAssignableFrom(klass)) {
this.delegate = new SeriesDouble();
} else if (IntervalPrice.class.isAssignableFrom(klass)) {
this.delegate = new SeriesIntervalPrice();
} else {
throw new UnsupportedOperationException("only Double and IntervalPrice are supported");
}
}
}
Below you can see how the caller code looks like:
TimeSeries ts = new TimeSeries() { }
Notice that ts is an anonymous class, denoted by { }.
- Author:
- Richard Gomes
- See Also:
- Using TypeTokens to retrieve generic parameters,
TypeNodeTree
|
Constructor Summary |
TypeToken(java.lang.Class<?> klass)
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
TypeToken
public TypeToken(java.lang.Class<?> klass)
getElement
public java.lang.Class<?> getElement()
- Returns:
- the contents of this TypeNode
get
public TypeToken get(int index)
children
public java.lang.Iterable<TypeToken> children()
Copyright © 2010 KnowledgePlaza. All Rights Reserved.