|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
ObjectConverterRegistry
@ThreadSafe public class ConverterRegistry
A collection of Object Converters. A converter from the given
source type to the given target type can be obtained by a call to
converter(Class, Class). If no converter exists for the given source and target
types, then this registry searches for a suitable converter accepting a parent class of the
given source type, or returning a sub-class of the given target type.
New instances of ConverterRegistry are initially empty. Custom converters must be
explicitly registered. However a system-wide registry initialized
with default converters is provided by the system() method.
Note about conversions from interfaces
ConverterRegistry is primarily designed for handling converters from classes to
other classes. Handling of interfaces are not prohibited (and actually sometime supported),
but their behavior may be more ambiguous than in the case of classes because of
multi-inheritance in interface hierarchy.
| utility/geotk-utility (download) | View source code for this class |
| Constructor Summary | |
|---|---|
ConverterRegistry()
Creates an initially empty set of object converters. |
|
| Method Summary | ||
|---|---|---|
|
converter(Class<S> source,
Class<T> target)
Returns a converter for the specified source and target classes. |
|
|
findCommonTarget(Class<T> base,
Class<?>... sources)
Returns a target class which is both assignable to the given base and convertible from all the given sources. |
|
void |
register(ObjectConverter<?,?> converter)
Registers a new converter. |
|
static ConverterRegistry |
system()
Returns the default system-wide instance. |
|
String |
toString()
Returns a string representation of registered converters. |
|
| Methods inherited from class Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public ConverterRegistry()
| Method Detail |
|---|
public static ConverterRegistry system()
Date and Long. Those conversions are defined for
the lifetime of the JVM.
If a temporary set of converters is desired, a new instance of ConverterRegistry
should be created explicitly instead.
Adding system-wide converters
Applications can add system-wide custom providers either by explicit call to the
register(ObjectConverter) method on the system converter, or by listing
the fully qualified classnames of their ObjectConverter instances in the
following file (see ServiceLoader for more info about services loading):
META-INF/services/org.geotoolkit.util.converter.ObjectConverter
public void register(ObjectConverter<?,?> converter)
Angle class is defined,
the static initializer of that class could register a converter from Angle to
Double.
This method registers the converter for its target class, some parents of the target class (see below) and every interfaces except
Cloneable which are implemented by the target class and not by the source class.
For example a converter producing Double can be used for clients that just ask
for a Number.
Which super-classes of the target class are registered
Consider a converter from class S to class T where the two classes
are related in a hierarchy as below:
Invoking this method will register the given converter for all the following cases:C1 └───C2 ├───C3 │ └───S └───C4 └───T
S → TS → C4
No S → C2 or S → C1 converter will be registered,
because an identity converter would be sufficient for those cases.
Which sub-classes of the source class are registered
Sub-classes of the source class will be registered on a case-by-case basis when the
converter(Class, Class) is invoked, because we can not know the set of all
sub-classes in advance (and would not necessarily want to register all of them anyway).
converter - The converter to register.
public <S,T> ObjectConverter<S,T> converter(Class<S> source,
Class<T> target)
throws NonconvertibleObjectException
S - The source class.T - The target class.source - The source class.target - The target class, or Object.class for any.
NonconvertibleObjectException - if no converter is found.
public <T> Class<? extends T> findCommonTarget(Class<T> base,
Class<?>... sources)
Comparable interface, in order to compare objects
that are normally not comparable each other.
Example 1: comparing File with URL
File implements the Comparable interface, while URL
does not. Consequently the code below will return unconditionally File no matter the
order of sources arguments:
Class<? extends Comparable> target = registry.findCommonTarget(Comparable.class, File.class, URL.class);
Example 2: comparing Date with Long
Both Long and Date implement Comparable, and both types
are convertible to the other type. There is no obvious rule for selecting a type instead
than the other. In order to keep this method determinist, the code below will prefer the
first sources argument assignable to the common target: Long.
Class<? extends Comparable> target = registry.findCommonTarget(Comparable.class, Long.class, Date.class);
T - The type represented by the base argument.base - The base type of the desired target.sources - The source for which a common target is desired.
base and convertible from all sources,
or null if no suitable target has been found.public String toString()
toString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||