java.lang.Object
org.apache.sis.internal.converter.ClassPair<S,T>
org.apache.sis.internal.converter.SystemConverter<S,T>
org.apache.sis.internal.converter.NumberConverter<S,T>
- Type Parameters:
S- the source number type.T- the target number type.
- All Implemented Interfaces:
Serializable,Function<S,,T> ObjectConverter<S,T>
Handles conversions from
Number to other kind of numbers.
This class supports only the type supported by Numbers.
Performance note
We provide a single class for all supported kinds ofNumber and delegate the actual
work to the Numbers static methods. This is not a very efficient way to do the work.
For example, it may be more efficient to provide specialized subclasses for each target class,
so we don't have to execute the switch inside the Numbers class every time a
value is converted. However, performance is not the primary concern here, since those converters
will typically be used by code doing more costly work (e.g. the sis-metadata module
providing Map views using Java reflection). So we rather try to be more compact.
If nevertheless performance appears to be a problem, consider reverting to revision
d73a10558dda4b41723d4f5652a792ae9c24f69e (subversion: 1455255) of this class,
which was using one subclass per target type as described above.
Immutability and thread safety
This class and all inner classes are immutable, and thus inherently thread-safe.- Since:
- 0.3
- Version:
- 0.3
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final classNumberConverter.Comparable<S extends Number>Converter from numbers to comparables. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ObjectConverter<T,S> The inverse converter, created when first needed.private static final longFor cross-version compatibility.Fields inherited from class org.apache.sis.internal.converter.ClassPair
sourceClass, targetClass -
Constructor Summary
ConstructorsConstructorDescriptionNumberConverter(Class<S> sourceClass, Class<T> targetClass) Creates a new converter for the given source and target classes. -
Method Summary
Modifier and TypeMethodDescriptionConverts the given number to the target type if that type is different.inverse()Returns the inverse converter, creating it when first needed.Declares this converter as a injective or surjective function, depending on whether conversions loose information or not.Methods inherited from class org.apache.sis.internal.converter.SystemConverter
bijective, equals, formatErrorMessage, getSourceClass, getTargetClass, readResolve, uniqueMethods inherited from class org.apache.sis.internal.converter.ClassPair
cast, hashCode, parentSource, toString
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDFor cross-version compatibility.- See Also:
-
inverse
The inverse converter, created when first needed.
-
-
Constructor Details
-
NumberConverter
Creates a new converter for the given source and target classes. This constructor does not verify the validity of parameter values. It is caller's responsibility to ensure that the given class are supported by theNumbersmethods.
-
-
Method Details
-
inverse
Returns the inverse converter, creating it when first needed. This method delegates toSystemRegistry.INSTANCEand caches the result. We do not provide predefined constant for the various converter because there is too many possibly combinations.- Specified by:
inversein interfaceObjectConverter<S extends Number,T extends Number> - Overrides:
inversein classSystemConverter<S extends Number,T extends Number> - Returns:
- a converter for converting instances of T back to instances of S.
- Throws:
UnsupportedOperationException- if this converter is not invertible.- See Also:
-
properties
Declares this converter as a injective or surjective function, depending on whether conversions loose information or not.- Returns:
- the manners in which source values are mapped to target values. May be an empty set, but never null.
-
apply
Converts the given number to the target type if that type is different. This implementation is inefficient, but avoid us the need to create one subclass for each number type. See class javadoc for more details.- Parameters:
source- the object to convert, ornull.- Returns:
- the converted object, or
null.
-