View Javadoc

1   package cz.cuni.amis.utils.maps;
2   
3   import java.util.Collections;
4   import java.util.HashMap;
5   import java.util.Map;
6   import java.util.WeakHashMap;
7   
8   /**
9    * A 4-level hashMap, the PrimaryKeys are weakly referenced.
10   * Get methods of all levels assure that the corresponding maps are created.
11   * @author srlok
12   *
13   * @param <PRIMARY_KEY>
14   * @param <SECONDARY_KEY>
15   * @param <TERTIARY_KEY>
16   * @param <QUATERNARY_KEY>
17   * @param <ITEM>
18   */
19  public class WeakHashQuadMap<PRIMARY_KEY,SECONDARY_KEY,TERTIARY_KEY,QUATERNARY_KEY,ITEM>
20  extends WeakHashMap<PRIMARY_KEY, Map<SECONDARY_KEY,Map<TERTIARY_KEY,Map<QUATERNARY_KEY,ITEM>>>>{
21  	
22  	private int secondaryCapacity;
23  	private int tertiaryCapacity;
24  	private int quaternaryCapacity;
25  	
26  	public WeakHashQuadMap()
27  	{
28  		this.secondaryCapacity = 16;
29  		this.tertiaryCapacity = 16;
30  		this.quaternaryCapacity = 16;
31  	}
32  	
33  	public WeakHashQuadMap(int primaryCapacity, int secondaryCapacity, int tertiaryCapacity, int quaternaryCapacity)
34  	{
35  		super(primaryCapacity);
36  		this.secondaryCapacity = secondaryCapacity;
37  		this.tertiaryCapacity = tertiaryCapacity;
38  		this.quaternaryCapacity = quaternaryCapacity;
39  	}
40  	
41  	@SuppressWarnings("unchecked")
42  	@Override
43  	/**
44  	 * If the primary_key map does not exist, an empty map is created, inserted and returned.
45  	 */
46  	public Map<SECONDARY_KEY,Map<TERTIARY_KEY, Map<QUATERNARY_KEY,ITEM>>> get(Object key)
47  	{
48  		Map<SECONDARY_KEY,Map<TERTIARY_KEY,Map<QUATERNARY_KEY,ITEM>>> result = super.get(key);
49  		if ( result != null)
50  		{
51  			return result;
52  		}
53  		result = Collections.synchronizedMap( new HashTriMap<SECONDARY_KEY,TERTIARY_KEY,QUATERNARY_KEY,ITEM>(secondaryCapacity,tertiaryCapacity,quaternaryCapacity) );
54  		super.put((PRIMARY_KEY) key, result);
55  		return result;
56  	}
57  	
58  	/**
59  	 * If the requested secondLevel map does not exist an empty one is created, inserted
60  	 * according to the primaryKey and returned.
61  	 * @param primaryKey
62  	 * @param secondaryKey
63  	 * @return
64  	 */
65  	public Map<TERTIARY_KEY,Map<QUATERNARY_KEY,ITEM>> get(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey)
66  	{
67  		Map<TERTIARY_KEY,Map<QUATERNARY_KEY,ITEM>> result = get(primaryKey).get(secondaryKey);
68  		if ( result != null)
69  		{
70  			return result;
71  		}
72  		result = Collections.synchronizedMap( new HashMapMap<TERTIARY_KEY, QUATERNARY_KEY,ITEM>(tertiaryCapacity, quaternaryCapacity ) );
73  		get(primaryKey).put(secondaryKey, result);
74  		return result;
75  	}
76  
77  	/**
78  	 * If the requested level 3 map does not exist
79  	 * an empty one is created, inserted and returned.
80  	 * @param primaryKey
81  	 * @param secondaryKey
82  	 * @param tertiaryKey
83  	 * @return
84  	 */
85  	public Map<QUATERNARY_KEY,ITEM> get(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey, TERTIARY_KEY tertiaryKey)
86  	{
87  		Map<QUATERNARY_KEY,ITEM> result = get(primaryKey,secondaryKey).get(tertiaryKey);
88  		if ( result != null)
89  		{
90  			return result;
91  		}
92  		result = Collections.synchronizedMap( new HashMap<QUATERNARY_KEY,ITEM>(quaternaryCapacity));
93  		get(primaryKey,secondaryKey).put(tertiaryKey, result);
94  		return result;
95  	}
96  	
97  	/**
98  	 * Returns null if the mapping is not present.
99  	 * @param primaryKey
100 	 * @param secondaryKey
101 	 * @param tertiaryKey
102 	 * @param quaternaryKey
103 	 * @return
104 	 */
105 	public ITEM get(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey, TERTIARY_KEY tertiaryKey, QUATERNARY_KEY quaternaryKey)
106 	{
107 		return get(primaryKey,secondaryKey,tertiaryKey).get(quaternaryKey);
108 	}
109 	
110 	/**
111 	 * Returns the inserted item.
112 	 * @param primaryKey
113 	 * @param secondaryKey
114 	 * @param tertiaryKey
115 	 * @param quaternaryKey
116 	 * @param item
117 	 * @return
118 	 */
119 	public ITEM put(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey, TERTIARY_KEY tertiaryKey, QUATERNARY_KEY quaternaryKey, ITEM item)
120 	{
121 		return get(primaryKey,secondaryKey,tertiaryKey).put(quaternaryKey, item);
122 	}
123 	
124 	/**
125 	 * 
126 	 */
127 	@Override
128 	public Map<SECONDARY_KEY,Map<TERTIARY_KEY, Map<QUATERNARY_KEY,ITEM>>> remove(Object key )
129 	{
130 		Map<SECONDARY_KEY,Map<TERTIARY_KEY, Map<QUATERNARY_KEY,ITEM>>> result = super.remove(key);
131 		if ( result != null)
132 		{
133 			return result;
134 		}
135 		return Collections.synchronizedMap( new HashTriMap<SECONDARY_KEY, TERTIARY_KEY, QUATERNARY_KEY,ITEM>(secondaryCapacity,tertiaryCapacity,quaternaryCapacity));
136 	}
137 	
138 	public Map<TERTIARY_KEY,Map<QUATERNARY_KEY,ITEM>> remove(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey)
139 	{
140 		 Map<TERTIARY_KEY,Map<QUATERNARY_KEY,ITEM>> result = get(primaryKey).remove(secondaryKey);
141 		 if (result != null)
142 		 {
143 			return result; 
144 		 }
145 		 return Collections.synchronizedMap(new  HashMapMap<TERTIARY_KEY, QUATERNARY_KEY, ITEM>(tertiaryCapacity,quaternaryCapacity));
146 	}
147 	
148 	public Map<QUATERNARY_KEY,ITEM> remove(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey, TERTIARY_KEY tertiaryKey)
149 	{
150 		Map<QUATERNARY_KEY,ITEM> result = get(primaryKey,secondaryKey).remove(tertiaryKey);
151 		if (result != null)
152 		{
153 			return result;
154 		}
155 		return Collections.synchronizedMap( new HashMap<QUATERNARY_KEY,ITEM>(quaternaryCapacity));
156 	}
157 	
158 	public ITEM remove(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey, TERTIARY_KEY tertiaryKey, QUATERNARY_KEY quaternaryKey)
159 	{
160 		return get(primaryKey,secondaryKey,tertiaryKey).remove(quaternaryKey);
161 		
162 	}
163 }