org.omnaest.utils.structure.map.decorator
Class CaseinsensitiveMapDecorator<V>

java.lang.Object
  extended by org.omnaest.utils.structure.map.decorator.MapDecorator<String,V>
      extended by org.omnaest.utils.structure.map.decorator.CaseinsensitiveMapDecorator<V>
All Implemented Interfaces:
Serializable, Map<String,V>

public class CaseinsensitiveMapDecorator<V>
extends MapDecorator<String,V>

Decorator for a Map with String based keys which will provide a caseinsensitive get(Object) method.

The CaseinsensitiveMapDecorator will first try to resolve the unmodified key from the underlying Map, if it fails it tries the lowercased, uppercased and capitalized key. If this fails, too, it will iterate over the keyset of the underlying Map and compares each key to the given one using caseinsensitive comparision.

This behavior result in at least retaining the performance of the underlying Map if one of the special cases does match, otherwise the performance is reduced due to the iteration over the whole keyset for each get(Object) request.

If the simple constructor is used or the useCacheForKeys flag is set to true, a WeakHashMap is used to cache known key to key mappings, but only these which make a full iteration over the keys of the underlying Map necessary. The internal translation Map is using a ReentrantLock for synchronization. This makes the CaseinsensitiveMapDecorator thread safe for underlying Maps which are thread safe.

Author:
Omnaest
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  boolean fastHit
           
protected  Map<String,String> sourceKeyToTargetKeyTranslationMap
           
 
Fields inherited from class org.omnaest.utils.structure.map.decorator.MapDecorator
map
 
Constructor Summary
CaseinsensitiveMapDecorator(Map<String,V> map)
           
CaseinsensitiveMapDecorator(Map<String,V> map, boolean useCacheForKeys)
           
 
Method Summary
 void addKeyToKeyTranslationToCache(String sourceKey, String targetKey)
           
 V get(Object object)
           
protected  boolean isFastHit()
           
 String tryTranslateKeyByCache(String sourceKey)
           
 
Methods inherited from class org.omnaest.utils.structure.map.decorator.MapDecorator
clear, containsKey, containsValue, entrySet, equals, getMap, hashCode, isEmpty, keySet, put, putAll, remove, setMap, size, toString, values
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

sourceKeyToTargetKeyTranslationMap

protected final Map<String,String> sourceKeyToTargetKeyTranslationMap

fastHit

protected boolean fastHit
Constructor Detail

CaseinsensitiveMapDecorator

public CaseinsensitiveMapDecorator(Map<String,V> map,
                                   boolean useCacheForKeys)
Parameters:
map -
useCacheForKeys -
See Also:
CaseinsensitiveMapDecorator

CaseinsensitiveMapDecorator

public CaseinsensitiveMapDecorator(Map<String,V> map)
Parameters:
map -
See Also:
CaseinsensitiveMapDecorator
Method Detail

tryTranslateKeyByCache

public String tryTranslateKeyByCache(String sourceKey)
Parameters:
sourceKey -
Returns:

addKeyToKeyTranslationToCache

public void addKeyToKeyTranslationToCache(String sourceKey,
                                          String targetKey)
Parameters:
sourceKey -
targetKey -

get

public V get(Object object)
Specified by:
get in interface Map<String,V>
Overrides:
get in class MapDecorator<String,V>
See Also:
Map.get(java.lang.Object)

isFastHit

protected boolean isFastHit()
Returns:
the fastHit


Copyright © 2013. All Rights Reserved.