public abstract class Indexer<K,V> extends Object
IndexedSet
and IndexedMap
.
The Indexer defines 3 equivalent ways to identify elements:
The Indexer is not restricted to use explicit key concept for identification. Identity of an element may be defined by a number of attributes, specified in a value itself, in a template object, in a formal key object, or encoded in a number key. The Indexer may use all these ways interchangeable to distinguish and identify elements.
Being a strategy, the Indexer is required to be stateless, concurrent and thread-safe.
The Indexer is an abstract class with a sole abstract method that shall be implemented
in order to use identification using explicit object key - no other methods are required to be
overridden in such simple cases. There is another abstract class - NumberKeyIndexer
-
which is similarly designed with sole abstract method to simplify identification using explicit
number key. The third similar class - IdentityIndexer
- is a similar extension of basic
Indexer for cases when explicit object keys must be compared by reference rather than
using their equals
method.
The Indexer itself is not serializable. However, concrete subclasses shall be serializable in order to support serialization of indexed set and map.
Modifier and Type | Field and Description |
---|---|
static Indexer |
DEFAULT
Default strategy that treats values as their own keys (key == value) and delegates to
Object.hashCode() and Object.equals(Object)
methods as appropriate. |
Modifier | Constructor and Description |
---|---|
protected |
Indexer()
Sole constructor; for invocation by subclass constructors, typically implicit.
|
Modifier and Type | Method and Description |
---|---|
long |
getNumberKey(V value)
Returns number key for specified value to be used for hashing and identification;
called when explicit number key is needed or when other methods delegate operations as specified.
|
abstract K |
getObjectKey(V value)
Returns object key for specified value to be used for hashing and identification;
called when explicit object key is needed or when other methods delegate operations as specified.
|
int |
hashCodeByKey(K key)
Returns hash code for specified object key; called when performing operations using object keys.
|
int |
hashCodeByKey(long key)
Returns hash code for specified number key; called when performing operations using number keys.
|
int |
hashCodeByValue(V value)
Returns hash code for specified value; called when performing value-based operations, including rehash.
|
boolean |
matchesByKey(K key,
V value)
Determines if specified object key matches specified value; called when performing operations using object keys.
|
boolean |
matchesByKey(long key,
V value)
Determines if specified number key matches specified value; called when performing operations using number keys.
|
boolean |
matchesByValue(V new_value,
V old_value)
Determines if specified new value matches specified old value; called when performing value-based operations.
|
public static final Indexer DEFAULT
Object.hashCode()
and Object.equals(Object)
methods as appropriate. This strategy does not support primitive keys.
This strategy basically turns IndexedSet
into plain hash set of objects and IndexedMap
into a self-reference mapping.
protected Indexer()
This implementation does nothing.
public int hashCodeByValue(V value)
This implementation delegates to
expression.hashCodeByKey
(getObjectKey
(value))
public boolean matchesByValue(V new_value, V old_value)
This implementation delegates to
expression.matchesByKey
(getObjectKey
(new_value), old_value)
public abstract K getObjectKey(V value)
UnsupportedOperationException
- if this strategy does not support object keys.public int hashCodeByKey(K key)
This implementation delegates to (key == null ? 0 : key.
expression.hashCode
())
UnsupportedOperationException
- if this strategy does not support object keys.public boolean matchesByKey(K key, V value)
This implementation delegates to (key == null ?
expression.getObjectKey
(value) == null : key.equals
(getObjectKey
(value)))
UnsupportedOperationException
- if this strategy does not support object keys.public long getNumberKey(V value)
This implementation throws UnsupportedOperationException
.
UnsupportedOperationException
- if this strategy does not support number keys.public int hashCodeByKey(long key)
This implementation throws UnsupportedOperationException
.
UnsupportedOperationException
- if this strategy does not support number keys.public boolean matchesByKey(long key, V value)
This implementation throws UnsupportedOperationException
.
UnsupportedOperationException
- if this strategy does not support number keys.Copyright © 2014 Devexperts. All Rights Reserved.