I would like to propose an alternative solution to the problem that IMO fits well with current [collections] direction.
Consider a new Map implementation that acts as a direct replacement for HashMap, call it HashMapA (A for apache ;-). This class contains basically the same code as HashMap, although it must be written without using cut and paste (ie from scratch - Sun licence issues).
HashMapA differs from HashMap in that the hashing algorithm and equals comparison methods are dedicated protected methods. It then becomes easy for a subclass to be written that compares keys using case insensitivity (amongst other things).
You could also consider having a subclass which delegates these methods to another object (a Hasher?) to make it possible to override the logic without subclassing.
public interface Hasher {
/** * Get the hash code for a key. */ public int keyHashCode(Object key);
/** * Tests if a pair of keys are equal. */ public boolean keysEqual(Object key1, Object key2);
}
public class DefaultHasher {
public int keyHashCode(Object key) {
return key.hashCode();
} public boolean keysEqual(Object key1, Object key2) {
return key1.equals(key2);
}}
public class ComposableHashMapA extends HashMapA {
private Hasher hasher = new DefaultHasher();
...
protected int keyHashCode(Object key) {
return hasher.keyHashCode(key);
} protected boolean keysEqual(Object key1, Object key2) {
return hasher.keysEqual(key1, key2);
}...
}
Rich -- Rich Dougherty http://www.rd.gen.nz/
pgp00000.pgp
Description: PGP signature
