Module org.tquadrat.foundation.util
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"
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final boolean
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.The cache for the keys.private final Comparator
<K> The comparator that determines the sort order for the keys.private final Comparators.KeyProvider
<T, K> The method that generates the key for the given element. -
Constructor Summary
ConstructorsConstructorDescriptionKeyBasedComparator
(Comparators.KeyProvider<T, K> keyProvider, Comparator<K> keyComparator, boolean cacheKeys) Creates a newKeyBasedComparator
instance. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Field Details
-
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
The cache for the keys. -
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 newKeyBasedComparator
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
- Specified by:
compare
in interfaceComparator<T>
-
retrieveKey
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.
-