Class KeyBasedComparator<T,K>

java.lang.Object
org.tquadrat.foundation.util.internal.KeyBasedComparator<T,K>
Type Parameters:
T - The type to order.
K - The key type that is used to determine the order; this may be the same as the type itself.
All Implemented Interfaces:
Comparator<T>

@ClassVersion(sourceVersion="$Id: KeyBasedComparator.java 820 2020-12-29 20:34:22Z tquadrat $") @API(status=INTERNAL, since="0.0.5") public class KeyBasedComparator<T,K> extends Object implements Comparator<T>
Sometimes a special sort key is used to order elements, and instead of hiding the generation of that key inside the method compare() of an implementation of Comparator, this implementation expects an instance of Comparators.KeyProvider as an argument to its constructor.
Author:
Thomas Thrien (thomas.thrien@tquadrat.org)
Version:
$Id: KeyBasedComparator.java 820 2020-12-29 20:34:22Z tquadrat $
Since:
0.0.5
UML Diagram
UML Diagram for "org.tquadrat.foundation.util.internal.KeyBasedComparator"

UML Diagram for "org.tquadrat.foundation.util.internal.KeyBasedComparator"

UML Diagram for "org.tquadrat.foundation.util.internal.KeyBasedComparator"
  • Field Details

    • m_CacheKeys

      private final boolean m_CacheKeys
      A flag that determines whether the keys are to be cached internally; true means they are kept, false means that the keys are generated newly for each comparison.
    • m_KeyCache

      private final Map<T,K> m_KeyCache
      The cache for the keys.
    • m_KeyComparator

      private final Comparator<K> m_KeyComparator
      The comparator that determines the sort order for the keys.
    • m_KeyProvider

      The method that generates the key for the given element.
  • Constructor Details

    • KeyBasedComparator

      public KeyBasedComparator(Comparators.KeyProvider<T,K> keyProvider, Comparator<K> keyComparator, boolean cacheKeys)
      Creates a new KeyBasedComparator instance.
      Parameters:
      keyProvider - The method that generates the key for a given element.
      keyComparator - The comparator that determines the sort order for the keys.
      cacheKeys - A flag that determines whether the keys are to be cached internally; true means they are kept, false means that the keys are generated newly for each comparison.
  • Method Details

    • compare

      public final int compare(T o1, T o2)
      Specified by:
      compare in interface Comparator<T>
    • retrieveKey

      private final K retrieveKey(T o)
      Provides the key for the given element, either from the cache or freshly generated.
      Parameters:
      o - The element.
      Returns:
      The sort key for the element.