Class NumberConverter<S extends Number,T extends Number>

Type Parameters:
S - the source number type.
T - the target number type.
All Implemented Interfaces:
Serializable, Function<S,T>, ObjectConverter<S,T>

final class NumberConverter<S extends Number,T extends Number> extends SystemConverter<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 of Number 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
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For cross-version compatibility.
      See Also:
    • inverse

      private transient volatile ObjectConverter<T extends Number,S extends Number> inverse
      The inverse converter, created when first needed.
  • Constructor Details

    • NumberConverter

      NumberConverter(Class<S> sourceClass, Class<T> targetClass)
      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 the Numbers methods.
  • Method Details

    • inverse

      Returns the inverse converter, creating it when first needed. This method delegates to SystemRegistry.INSTANCE and caches the result. We do not provide predefined constant for the various converter because there is too many possibly combinations.
      Specified by:
      inverse in interface ObjectConverter<S extends Number,T extends Number>
      Overrides:
      inverse in class SystemConverter<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

      public Set<FunctionProperty> 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

      public T apply(S source)
      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, or null.
      Returns:
      the converted object, or null.