Class SystemConverter<S,T>
java.lang.Object
org.apache.sis.internal.converter.ClassPair<S,T>
org.apache.sis.internal.converter.SystemConverter<S,T>
- Type Parameters:
S
- the base type of source objects.T
- the base type of converted objects.
- All Implemented Interfaces:
Serializable
,Function<S,
,T> ObjectConverter<S,
T>
- Direct Known Subclasses:
AngleConverter
,AngleConverter.Inverse
,ArrayConverter
,CharSequenceConverter
,CollectionConverter
,DateConverter
,DateConverter.Inverse
,FallbackConverter
,FractionConverter
,FractionConverter.FromInteger
,IdentityConverter
,NumberConverter
,NumberConverter.Comparable
,ObjectToString
,PathConverter
,StringConverter
Base class of all converters defined in the
org.apache.sis.internal
package.
Those converters are returned by system-wide ConverterRegistry
, and cached for reuse.
Immutability and thread safety
This base class is immutable, and thus inherently thread-safe. Subclasses should be immutable and thread-safe too if they are intended to be cached inConverterRegistry
.- Since:
- 0.3
- Version:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final long
For cross-version compatibility.Fields inherited from class org.apache.sis.internal.converter.ClassPair
sourceClass, targetClass
-
Constructor Summary
ConstructorsConstructorDescriptionSystemConverter
(Class<S> sourceClass, Class<T> targetClass) Creates a new converter for the given source and target classes. -
Method Summary
Modifier and TypeMethodDescription(package private) static Set<FunctionProperty>
Convenience method forObjectConverter.properties()
implementation of bijective converters between comparable objects.final boolean
Performs the comparisons documented inClassPair.equals(Object)
with an additional check: if both objects to compare areSystemConverter
, then also requires the two objects to be of the same class.(package private) final String
formatErrorMessage
(S value) Formats an error message for a value that cannot be converted.Returns the source class given at construction time.Returns the target class given at construction time.inverse()
Default to non-invertible conversion.protected final Object
Returns the singleton instance on deserialization, if any.unique()
Returns an unique instance of this converter if one exists.Methods inherited from class org.apache.sis.internal.converter.ClassPair
cast, hashCode, parentSource, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.sis.util.ObjectConverter
apply, properties
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDFor cross-version compatibility.- See Also:
-
-
Constructor Details
-
SystemConverter
Creates a new converter for the given source and target classes.- Parameters:
sourceClass
- the source class.targetClass
- the target class.
-
-
Method Details
-
getSourceClass
Returns the source class given at construction time.- Specified by:
getSourceClass
in interfaceObjectConverter<S,
T> - Returns:
- the type of objects to convert.
-
getTargetClass
Returns the target class given at construction time.- Specified by:
getTargetClass
in interfaceObjectConverter<S,
T> - Returns:
- the type of converted objects.
-
bijective
Convenience method forObjectConverter.properties()
implementation of bijective converters between comparable objects. The converter is presumed invertible and to preserve order. -
inverse
Default to non-invertible conversion. Must be overridden by subclasses that support inversions.- Specified by:
inverse
in interfaceObjectConverter<S,
T> - Returns:
- a converter for converting instances of T back to instances of S.
- Throws:
UnsupportedOperationException
- if this converter is not invertible.- See Also:
-
equals
Performs the comparisons documented inClassPair.equals(Object)
with an additional check: if both objects to compare areSystemConverter
, then also requires the two objects to be of the same class. We do that in order to differentiate the "ordinary" converters from theFallbackConverter
.Implementation note
This is admittedly a little bit convolved. A cleaner approach would have been to not allow theConverterRegister
hash map to contain anything else thanClassPair
keys, but the current strategy of using the same instance for keys and values reduces a little bit the number of objects to create in the JVM. Another cleaner approach would have been to compareObjectConverter
s in a separated method, but users invokingequals
on our system converters could be surprised.Our
equals(Object)
definition have the following implications regarding the way to use theConverterRegistry.converters
map:- When searching for a converter of the same class than the key (as in the
ConverterRegistry.findEquals(SystemConverter)
method), then there is no restriction on the key that can be given to theMap.get(K)
method. TheMap
is "normal". - When searching for a converter for a pair of source and target classes
(as in
ConverterRegistry.find(Class, Class)
), the key shall be an instance ofClassPair
instance (not a subclass).
- When searching for a converter of the same class than the key (as in the
-
unique
Returns an unique instance of this converter if one exists. If a converter already exists for the same source an target classes, then this converter is returned. Otherwise this converter is returned without being cached.- Returns:
- the unique instance, or
this
if no unique instance can be found. - See Also:
-
readResolve
Returns the singleton instance on deserialization, if any. If no instance already exist in the virtual machine, we do not cache the instance (for now) for security reasons.- Returns:
- the object to use after deserialization.
- Throws:
ObjectStreamException
- if the serialized object defines an unknown data type.
-
formatErrorMessage
Formats an error message for a value that cannot be converted.- Parameters:
value
- the value that cannot be converted.- Returns:
- the error message.
-