net.sf.jabb.util.col
Class PutIfAbsentMap<K,V>

java.lang.Object
  extended by net.sf.jabb.util.col.PutIfAbsentMap<K,V>
Type Parameters:
K - Type of the key of the Map entries
Map的key的类型
V - Type of the value of the Map entries
Map的value的类型
All Implemented Interfaces:
Map<K,V>, NavigableMap<K,V>, SortedMap<K,V>

public class PutIfAbsentMap<K,V>
extends Object
implements Map<K,V>, SortedMap<K,V>, NavigableMap<K,V>

Encapsulates Map so that a new entry is put in to the Map whenever there is a get for a non-existing entry.
把Map进一步封装,使得每次get的时候,如果没有,就自动put。

Please note that only these methods are synchronized: get/put/putAll/remove/clear.

注意只有这几个方法是同步的:get/put/putAll/remove/clear。

Although PutIfAbsentMap implemented SortedMap and NavigableMap, if the encapsulated Map does not support those interfaces, then if any method of those interfaces was called, Exception will be thrown.

虽然PutIfAbsentMap实现了SortedMap与NavigableMap接口, 但是如果被封装的Map本身不支持这些接口, 那么当运行时调用这些接口所特有的方法的时候,会抛出Exception。

Author:
Zhengmao HU (James)

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  Map<K,V> map
           
protected  Object structureLock
           
protected  Class<?> valueClass
           
protected  Constructor<?> valueConstructor
           
protected  Object valueParameter
           
 
Constructor Summary
PutIfAbsentMap(Class<? extends Map> mapClazz, Class<? extends V> valueClazz)
          Constructs an instance with specified Map Class and value Class.
给定Map的类以及value的类,封装出一个“每次get的时候,如果没有,就自动put”的实例。
PutIfAbsentMap(Class<? extends Map> mapClazz, Class<? extends V> valueClazz, Object... valueParams)
          Constructs an instance with specified Map Class, value Class and the constructor parameter of the value Class.
给定Map的类、value的类以及value类构造方法的参数,封装出一个“每次get的时候,如果没有,就自动put”的实例。
PutIfAbsentMap(Class<? extends Map> mapClazz, Class<? extends V> valueClazz, Object valueParam)
          Constructs an instance with specified Map Class, value Class and the constructor parameter of the value Class.
给定Map的类、value的类以及value类构造方法的参数,封装出一个“每次get的时候,如果没有,就自动put”的实例。
PutIfAbsentMap(Map<K,V> originalMap, Class<? extends V> valueClazz)
          Constructs an instance with specified Map instance and value Class.
给定Map实例以及value的类,把一个普通的Map实例封装成“每次get的时候,如果没有,就自动put”。
 
Method Summary
 Map.Entry<K,V> ceilingEntry(K key)
           
 K ceilingKey(K key)
           
 void clear()
           
 Comparator<? super K> comparator()
           
 boolean containsKey(Object obj)
           
 boolean containsValue(Object obj)
           
 NavigableSet<K> descendingKeySet()
           
 NavigableMap<K,V> descendingMap()
           
 Set<Map.Entry<K,V>> entrySet()
           
 Map.Entry<K,V> firstEntry()
           
 K firstKey()
           
 Map.Entry<K,V> floorEntry(K key)
           
 K floorKey(K key)
           
 V get(Object key)
          Get the value object corresponding to the key object specified, if such entry does not exist in the Map, then create one and put into the Map and return the value object in the newly created entry.
取得key所对应的value,如果目前在Map里没有,则在Map里新建一个并返回新建 的这个value对象。
 Map<K,V> getMap()
          Get the encapsulated Map instance.
获得最初被封装的那个Map。
 SortedMap<K,V> headMap(K key)
           
 NavigableMap<K,V> headMap(K arg0, boolean arg1)
           
 Map.Entry<K,V> higherEntry(K arg0)
           
 K higherKey(K arg0)
           
 boolean isEmpty()
           
 Set<K> keySet()
           
 Map.Entry<K,V> lastEntry()
           
 K lastKey()
           
 Map.Entry<K,V> lowerEntry(K arg0)
           
 K lowerKey(K arg0)
           
 NavigableSet<K> navigableKeySet()
           
 Map.Entry<K,V> pollFirstEntry()
           
 Map.Entry<K,V> pollLastEntry()
           
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> anotherMap)
           
 V remove(Object obj)
           
 int size()
           
 NavigableMap<K,V> subMap(K arg0, boolean arg1, K arg2, boolean arg3)
           
 SortedMap<K,V> subMap(K from, K to)
           
 SortedMap<K,V> tailMap(K key)
           
 NavigableMap<K,V> tailMap(K arg0, boolean arg1)
           
 Collection<V> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

map

protected Map<K,V> map

valueClass

protected Class<?> valueClass

valueConstructor

protected Constructor<?> valueConstructor

valueParameter

protected Object valueParameter

structureLock

protected Object structureLock
Constructor Detail

PutIfAbsentMap

public PutIfAbsentMap(Map<K,V> originalMap,
                      Class<? extends V> valueClazz)
Constructs an instance with specified Map instance and value Class.
给定Map实例以及value的类,把一个普通的Map实例封装成“每次get的时候,如果没有,就自动put”。

Parameters:
originalMap - The Map instance that will be encapsulated.
被封装进来的Map实例。
valueClazz - Class of the value of the Map entry.
Map的value的类。

PutIfAbsentMap

public PutIfAbsentMap(Class<? extends Map> mapClazz,
                      Class<? extends V> valueClazz)
Constructs an instance with specified Map Class and value Class.
给定Map的类以及value的类,封装出一个“每次get的时候,如果没有,就自动put”的实例。

Parameters:
mapClazz - The Map Class that its instance will be created and encapsulated.
被封装进来的Map的类
valueClazz - Class of the value of the Map entry.
Map的value的类。

PutIfAbsentMap

public PutIfAbsentMap(Class<? extends Map> mapClazz,
                      Class<? extends V> valueClazz,
                      Object valueParam)
Constructs an instance with specified Map Class, value Class and the constructor parameter of the value Class.
给定Map的类、value的类以及value类构造方法的参数,封装出一个“每次get的时候,如果没有,就自动put”的实例。

Parameters:
mapClazz - The Map Class that its instance will be created and encapsulated.
被封装进来的Map的类
valueClazz - Class of the value of the Map entry.
Map的value的类。
valueParam - Constructor parameter for the value Class.
value的类的构造方法所需要的参数。

PutIfAbsentMap

public PutIfAbsentMap(Class<? extends Map> mapClazz,
                      Class<? extends V> valueClazz,
                      Object... valueParams)
Constructs an instance with specified Map Class, value Class and the constructor parameter of the value Class.
给定Map的类、value的类以及value类构造方法的参数,封装出一个“每次get的时候,如果没有,就自动put”的实例。

Parameters:
mapClazz - The Map Class that its instance will be created and encapsulated.
被封装进来的Map的类
valueClazz - Class of the value of the Map entry.
Map的value的类。
valueParams - Constructor parameter for the value Class.
value的类的构造方法所需要的参数。
Method Detail

getMap

public Map<K,V> getMap()
Get the encapsulated Map instance.
获得最初被封装的那个Map。

Returns:
The map instance that is encapsulated inside.

get

public V get(Object key)
Get the value object corresponding to the key object specified, if such entry does not exist in the Map, then create one and put into the Map and return the value object in the newly created entry.
取得key所对应的value,如果目前在Map里没有,则在Map里新建一个并返回新建 的这个value对象。

Specified by:
get in interface Map<K,V>
Parameters:
key - The key object that will be used to look for the value object.

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>

putAll

public void putAll(Map<? extends K,? extends V> anotherMap)
Specified by:
putAll in interface Map<K,V>

remove

public V remove(Object obj)
Specified by:
remove in interface Map<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>

containsKey

public boolean containsKey(Object obj)
Specified by:
containsKey in interface Map<K,V>

containsValue

public boolean containsValue(Object obj)
Specified by:
containsValue in interface Map<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>
Specified by:
entrySet in interface SortedMap<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>
Specified by:
keySet in interface SortedMap<K,V>

size

public int size()
Specified by:
size in interface Map<K,V>

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>
Specified by:
values in interface SortedMap<K,V>

comparator

public Comparator<? super K> comparator()
Specified by:
comparator in interface SortedMap<K,V>

firstKey

public K firstKey()
Specified by:
firstKey in interface SortedMap<K,V>

headMap

public SortedMap<K,V> headMap(K key)
Specified by:
headMap in interface NavigableMap<K,V>
Specified by:
headMap in interface SortedMap<K,V>

lastKey

public K lastKey()
Specified by:
lastKey in interface SortedMap<K,V>

subMap

public SortedMap<K,V> subMap(K from,
                             K to)
Specified by:
subMap in interface NavigableMap<K,V>
Specified by:
subMap in interface SortedMap<K,V>

tailMap

public SortedMap<K,V> tailMap(K key)
Specified by:
tailMap in interface NavigableMap<K,V>
Specified by:
tailMap in interface SortedMap<K,V>

ceilingEntry

public Map.Entry<K,V> ceilingEntry(K key)
Specified by:
ceilingEntry in interface NavigableMap<K,V>

ceilingKey

public K ceilingKey(K key)
Specified by:
ceilingKey in interface NavigableMap<K,V>

descendingKeySet

public NavigableSet<K> descendingKeySet()
Specified by:
descendingKeySet in interface NavigableMap<K,V>

descendingMap

public NavigableMap<K,V> descendingMap()
Specified by:
descendingMap in interface NavigableMap<K,V>

firstEntry

public Map.Entry<K,V> firstEntry()
Specified by:
firstEntry in interface NavigableMap<K,V>

floorEntry

public Map.Entry<K,V> floorEntry(K key)
Specified by:
floorEntry in interface NavigableMap<K,V>

floorKey

public K floorKey(K key)
Specified by:
floorKey in interface NavigableMap<K,V>

headMap

public NavigableMap<K,V> headMap(K arg0,
                                 boolean arg1)
Specified by:
headMap in interface NavigableMap<K,V>

higherEntry

public Map.Entry<K,V> higherEntry(K arg0)
Specified by:
higherEntry in interface NavigableMap<K,V>

higherKey

public K higherKey(K arg0)
Specified by:
higherKey in interface NavigableMap<K,V>

lastEntry

public Map.Entry<K,V> lastEntry()
Specified by:
lastEntry in interface NavigableMap<K,V>

lowerEntry

public Map.Entry<K,V> lowerEntry(K arg0)
Specified by:
lowerEntry in interface NavigableMap<K,V>

lowerKey

public K lowerKey(K arg0)
Specified by:
lowerKey in interface NavigableMap<K,V>

navigableKeySet

public NavigableSet<K> navigableKeySet()
Specified by:
navigableKeySet in interface NavigableMap<K,V>

pollFirstEntry

public Map.Entry<K,V> pollFirstEntry()
Specified by:
pollFirstEntry in interface NavigableMap<K,V>

pollLastEntry

public Map.Entry<K,V> pollLastEntry()
Specified by:
pollLastEntry in interface NavigableMap<K,V>

subMap

public NavigableMap<K,V> subMap(K arg0,
                                boolean arg1,
                                K arg2,
                                boolean arg3)
Specified by:
subMap in interface NavigableMap<K,V>

tailMap

public NavigableMap<K,V> tailMap(K arg0,
                                 boolean arg1)
Specified by:
tailMap in interface NavigableMap<K,V>


Copyright © 2012. All Rights Reserved.