com.devexperts.util
Class Indexer<K,V>

java.lang.Object
  extended by com.devexperts.util.Indexer<K,V>
Direct Known Subclasses:
IdentityIndexer, NumberKeyIndexer

public abstract class Indexer<K,V>
extends Object

A strategy that distinguishes and identifies elements in an 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.


Field Summary
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.
 
Constructor Summary
protected Indexer()
          Sole constructor; for invocation by subclass constructors, typically implicit.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT

public static final 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. 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.

Constructor Detail

Indexer

protected Indexer()
Sole constructor; for invocation by subclass constructors, typically implicit.

This implementation does nothing.

Method Detail

hashCodeByValue

public int hashCodeByValue(V value)
Returns hash code for specified value; called when performing value-based operations, including rehash.

This implementation delegates to getObjectKey(value).hashCode() expression.


matchesByValue

public boolean matchesByValue(V new_value,
                              V old_value)
Determines if specified new value matches specified old value; called when performing value-based operations.

This implementation delegates to getObjectKey(new_value).equals(getObjectKey(old_value)) expression.


getObjectKey

public 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.

Throws:
UnsupportedOperationException - if this strategy does not support object keys.

hashCodeByKey

public int hashCodeByKey(K key)
Returns hash code for specified object key; called when performing operations using object keys.

This implementation delegates to key.hashCode() expression.

Throws:
UnsupportedOperationException - if this strategy does not support object keys.

matchesByKey

public boolean matchesByKey(K key,
                            V value)
Determines if specified object key matches specified value; called when performing operations using object keys.

This implementation delegates to key.equals(getObjectKey(value)) expression.

Throws:
UnsupportedOperationException - if this strategy does not support object keys.

getNumberKey

public 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.

This implementation throws UnsupportedOperationException.

Throws:
UnsupportedOperationException - if this strategy does not support number keys.

hashCodeByKey

public int hashCodeByKey(long key)
Returns hash code for specified number key; called when performing operations using number keys.

This implementation throws UnsupportedOperationException.

Throws:
UnsupportedOperationException - if this strategy does not support number keys.

matchesByKey

public boolean matchesByKey(long key,
                            V value)
Determines if specified number key matches specified value; called when performing operations using number keys.

This implementation throws UnsupportedOperationException.

Throws:
UnsupportedOperationException - if this strategy does not support number keys.


Copyright © 2013 Devexperts. All Rights Reserved.