Class SafeComparator<T>

java.lang.Object
com.tangosol.util.Base
com.tangosol.util.comparator.SafeComparator<T>
All Implemented Interfaces:
ExternalizableLite, PortableObject, EntryAwareComparator<T>, QueryMapComparator<T>, Remote.Comparator<T>, Serializable, Comparator<T>
Direct Known Subclasses:
EntryComparator, InverseComparator

public class SafeComparator<T> extends Base implements Remote.Comparator<T>, QueryMapComparator<T>, EntryAwareComparator<T>, Serializable, ExternalizableLite, PortableObject
Null-safe delegating comparator. Null values are evaluated as "less then" any non-null value. If the wrapped comparator is not specified then all non-null values must implement the Comparable interface.

Use SafeComparator.INSTANCE to obtain an instance of non-delegating SafeComparator.

Author:
gg 2002.12.10
See Also:
  • Field Details

    • INSTANCE

      public static final SafeComparator INSTANCE
      The trivial SafeComparator.
    • m_comparator

      protected Comparator<? super T> m_comparator
      The wrapped Comparator. Could be null.
    • m_fNullFirst

      protected boolean m_fNullFirst
      Flag specifying if nulls should be sorted as less (first) or greater (last) than all non-null elements. Default is "less than" (first).
  • Constructor Details

    • SafeComparator

      public SafeComparator()
      Default constructor (for ExternalizableLite and PortableObject).
    • SafeComparator

      public SafeComparator(Comparator<? super T> comparator)
      Construct a SafeComparator delegating to the specified (wrapped) comparator.
      Parameters:
      comparator - Comparator object to delegate comparison of non-null values (optional)
    • SafeComparator

      public SafeComparator(Comparator<? super T> comparator, boolean fNullFirst)
      Construct a SafeComparator delegating to the specified (wrapped) comparator.
      Parameters:
      comparator - Comparator object to delegate comparison of non-null values (optional)
      fNullFirst - flag specifying if null values should be treated as "less than" (true) or "greater than" (false) any non-null value
  • Method Details

    • ensureSafe

      public static <V> Comparator<? super V> ensureSafe(Comparator<? super V> comparator)
      Ensure that the specified comparator is safe, by wrapping it if it isn't.
      Type Parameters:
      V - the type of objects to compare
      Parameters:
      comparator - the comparator to wrap, if necessary
      Returns:
      a SafeComparator for the specified comparator
    • compare

      public int compare(T o1, T o2)
      Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Null values are evaluated as "less then" any non-null value. If the wrapped comparator is not specified, all non-null values must implement the Comparable interface.
      Specified by:
      compare in interface Comparator<T>
      Parameters:
      o1 - the first object to be compared
      o2 - the second object to be compared
      Returns:
      a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second
      Throws:
      ClassCastException - if the arguments' types prevent them from being compared by this Comparator.
    • compareEntries

      public int compareEntries(QueryMap.Entry entry1, QueryMap.Entry entry2)
      Compare two entries using the underlying comparator. If the wrapped comparator does not implement the QueryMapComparator interface, revert to the entry values comparison.
      Specified by:
      compareEntries in interface QueryMapComparator<T>
      Parameters:
      entry1 - the first entry to compare values from; read-only
      entry2 - the second entry to compare values from; read-only
      Returns:
      a negative integer, zero, or a positive integer as the first entry denotes a value that is is less than, equal to, or greater than the value denoted by the second entry
    • isKeyComparator

      public boolean isKeyComparator()
      Specifies whether this comparator expects to compare keys or values.
      Specified by:
      isKeyComparator in interface EntryAwareComparator<T>
      Returns:
      true if Entry keys are expected; false otherwise
    • isKeyComparator

      public static boolean isKeyComparator(Comparator comparator)
      Check whether the specified comparator expects to compare keys or values.
      Parameters:
      comparator - a Comparator to check
      Returns:
      true if the comparator expects keys; false otherwise
    • getComparator

      public Comparator<? super T> getComparator()
      Obtain the wrapped Comparator.
      Returns:
      the wrapped Comparator
    • isNullFirst

      public boolean isNullFirst()
      Obtain the m_fNullFirst flag.
      Returns:
      the m_fNullFirst flag
    • compareSafe

      public static <T> int compareSafe(Comparator comparator, Object o1, Object o2)
      Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Null values are evaluated as "less then" any non-null value. Non-null values must implement the Comparable interface.
      Parameters:
      comparator - a comparator to use for the comparison (optional)
      o1 - the first object to be compared
      o2 - the second object to be compared
      Returns:
      a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second
      Throws:
      ClassCastException - if the arguments are not Comparable
    • compareSafe

      public static int compareSafe(Comparator comparator, Object o1, Object o2, boolean fNullFirst)
      Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Null values are evaluated based on the value of a fNullFirst flag as either "less than" or "greater than" any non-null value. Non-null values must implement the Comparable interface.
      Parameters:
      comparator - a comparator to use for the comparison (optional)
      o1 - the first object to be compared
      o2 - the second object to be compared
      fNullFirst - flag specifying if null values should be treated as "less than" (true) or "greater than" (false) any non-null value
      Returns:
      a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second
      Throws:
      ClassCastException - if the arguments are not Comparable
    • toString

      public String toString()
      Return a human-readable description for this Comparator.
      Overrides:
      toString in class Object
      Returns:
      a String description of the Comparator
    • equals

      public boolean equals(Object o)
      Determine if two comparators are equal.
      Specified by:
      equals in interface Comparator<T>
      Overrides:
      equals in class Object
      Parameters:
      o - the other comparator
      Returns:
      true if the passed object is equal to this
    • hashCode

      public int hashCode()
      Return the hash code for this comparator.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this comparator
    • readExternal

      public void readExternal(DataInput in) throws IOException
      Restore the contents of this object by loading the object's state from the passed DataInput object.
      Specified by:
      readExternal in interface ExternalizableLite
      Parameters:
      in - the DataInput stream to read data from in order to restore the state of this object
      Throws:
      IOException - if an I/O exception occurs
    • writeExternal

      public void writeExternal(DataOutput out) throws IOException
      Save the contents of this object by storing the object's state into the passed DataOutput object.
      Specified by:
      writeExternal in interface ExternalizableLite
      Parameters:
      out - the DataOutput stream to write the state of this object to
      Throws:
      IOException - if an I/O exception occurs
    • readExternal

      public void readExternal(PofReader in) throws IOException
      Restore the contents of a user type instance by reading its state using the specified PofReader object.
      Specified by:
      readExternal in interface PortableObject
      Parameters:
      in - the PofReader from which to read the object's state
      Throws:
      IOException - if an I/O error occurs
    • writeExternal

      public void writeExternal(PofWriter out) throws IOException
      Save the contents of a POF user type instance by writing its state using the specified PofWriter object.
      Specified by:
      writeExternal in interface PortableObject
      Parameters:
      out - the PofWriter to which to write the object's state
      Throws:
      IOException - if an I/O error occurs
    • INSTANCE

      public static <T> SafeComparator<T> INSTANCE()
      Return the trivial SafeComparator.