-
Enhancement
-
Resolution: Unresolved
-
P5
-
None
-
1.3.1
-
x86
-
windows_2000
Name: rl43681 Date: 10/22/2003
A DESCRIPTION OF THE REQUEST :
// ///////////////////////////////////////////////////
public interface KeyModifier
{
public Object modifyKey(Object o);
}
//--------------------
public class IdentityModifier implements KeyModifier
{
public Object modifyKey(Object o)
{ return o;}
}
//--------------------
public class UpperCaseModifier implements KeyModifier
{
public Object modifyKey(Object o)
{ return o.toString().toUpperCase();}
}
//--------------------
public interface MapWModifiedKey extends Map
{
public KeyModifier getKeyModifier();
public KeyModifier setKeyModifier(KeyModifier mod);
}
JUSTIFICATION :
In order to solve issues/problems related to case sensitivity and other
map key related in a generic way this would be very useful.
---------- BEGIN SOURCE ----------
import java.util.*;
// ***************************************************
// ///////////////////////////////////////////////////
/**
*
* Utility class to make modified key and case-insensitive hashtables.
*/
public class HashtableModifiedKey extends Hashtable
{
KeyModifier keyModifier;
public static KeyModifier IdentityMod = null;
public static KeyModifier StringUpperCaseMod = null;
public static KeyModifier StringLowerCaseMod = null;
static
{
IdentityMod = new KeyModifier();
StringUpperCaseMod = new KeyModifier()
{
public Object modifyKey(Object o)
{return o.toString().toUpperCase();}
} ;
StringLowerCaseMod = new KeyModifier()
{
public Object modifyKey(Object o)
{return o.toString().toLowerCase();}
};
}
// ///////////////////////////////////////////////////
/**
*
* Class to modify key's.
* @param srcStr Source String
* @return Hashtable of key/Value pairs
*/
public static class KeyModifier
{
public Object modifyKey(Object o)
{return o;}
}
// -----------------------------------
public HashtableModifiedKey(KeyModifier mod)
{
keyModifier = (mod == null ? IdentityMod : mod);
}
// -----------------------------------
public HashtableModifiedKey(KeyModifier mod, Map map)
{
super(map);
keyModifier = (mod == null ? IdentityMod : mod);
}
// -----------------------------------
/** @see java.util.Hashtable#clone */
public Object clone()
{ return new HashtableModifiedKey(keyModifier, this);
}
// -----------------------------------
/** @see java.util.Hashtable#get */
public Object get(Object key)
{return super.get(keyModifier.modifyKey(key));}
// -----------------------------------
/** @see java.util.Hashtable#put */
public Object put(Object key, Object value)
{return super.put(keyModifier.modifyKey(key), value);}
// -----------------------------------
/** @see java.util.Hashtable#containsKey */
public boolean containsKey(Object key)
{return super.containsKey(keyModifier.modifyKey(key));}
// -----------------------------------
/**
* Static factory method for creating a Haschtable with a key modifier.
*
* @param mod Key modifier
* @return Hashtable of key/Value pairs
*/
public static Hashtable getHashtable(KeyModifier mod)
{
return new HashtableModifiedKey(mod);
}
} // Class
---------- END SOURCE ----------
(Incident Review ID: 217138)
======================================================================