Class ClassPair<S,T>

java.lang.Object
org.apache.sis.internal.converter.ClassPair<S,T>
Type Parameters:
S - the base type of source objects.
T - the base type of converted objects.
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
SystemConverter

class ClassPair<S,T> extends Object implements Serializable
Holds explicit sourceClass and targetClass values. Used as key in a hash map of converters. Also used as the base class for converters defined in this package.

The only direct subtype allowed is SystemConverter. No other direct subtype shall exist. See equals(Object) for an explanation.

Immutability and thread safety

This base class is immutable and thus inherently thread-safe. ClassPair immutability is necessary for ConverterRegistry. Subclasses should also be immutable, but this requirement is not as strong as for ClassPair (because subclasses are not used as keys in hash map).
Since:
0.3
Version:
0.3
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final long
    For cross-version compatibility.
    (package private) final Class<S>
    The source class.
    (package private) final Class<T>
    The target class.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ClassPair(Class<S> sourceClass, Class<T> targetClass)
    Creates an entry for the given source and target classes.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) final ObjectConverter<? super S,? extends T>
    cast(ObjectConverter<?,?> converter)
    Casts the given converter to the source and target classes of this ClassPair.
    boolean
    equals(Object other)
    Compares the given object with this ClassPair for equality.
    final int
    Returns a hash code value for this ClassPair.
    (package private) final ClassPair<? super S,T>
    Returns a key for the parent source, or null if none.
    Returns a string representation for this entry.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • serialVersionUID

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

      final Class<S> sourceClass
      The source class.
    • targetClass

      final Class<T> targetClass
      The target class.
  • Constructor Details

    • ClassPair

      ClassPair(Class<S> sourceClass, Class<T> targetClass)
      Creates an entry for the given source and target classes.
      Parameters:
      sourceClass - the source class.
      targetClass - the target class.
  • Method Details

    • parentSource

      final ClassPair<? super S,T> parentSource()
      Returns a key for the parent source, or null if none. This method applies the following rules:
      • If sourceClass is a class and have a parent class, then returns a new ClassPair having that parent class as the source.
      • Otherwise if sourceClass is an interface extending at least one interface, then returns a new ClassPair having the first parent interface as the source. we select the first interface on the assumption that it is the mean one.
      • Otherwise (i.e. if there is no parent class or interface), returns null.
      The target class is left unchanged.
      Returns:
      a key for the parent source, or null.
    • cast

      final ObjectConverter<? super S,? extends T> cast(ObjectConverter<?,?> converter)
      Casts the given converter to the source and target classes of this ClassPair. This method is not public because the checks are performed using assertions only. If this method was to goes public, the assertions would need to be replaced by unconditional checks.

      This method is used by ConverterRegistry after fetching a value from a hash map using this ClassPair as a key. In this context, the cast should never fail (assuming that the converters do not change their source and target classes).

    • equals

      public boolean equals(Object other)
      Compares the given object with this ClassPair for equality. Two ClassPair instances are considered equal if they have the same source and target classes, ignoring all other properties eventually defined in subclasses.

      This method is designed for use by ConverterRegistry as HashMap keys. Its primary purpose is not to determine if two objects are converters doing the same conversions. However, the SystemConverter subclass overrides this method with an additional safety check.

      Overrides:
      equals in class Object
      Parameters:
      other - the object to compare with this ClassPair.
      Returns:
      true if the given object is a ClassPair having the same source and target classes.
    • hashCode

      public final int hashCode()
      Returns a hash code value for this ClassPair. See equals(Object) javadoc for information on the scope of this method.
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns a string representation for this entry. Used for formatting error messages.
      Overrides:
      toString in class Object