1 /*
2 * Copyright 2007 Kasper B. Graversen
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.supercsv.util;
17
18 import java.util.HashMap;
19 import java.util.Set;
20
21 /**
22 * A two-dimensional hashmap, is a HashMap that enables you to refer to values via two keys rather than one. The
23 * underlying implementation is simply a HashMap containing HashMap, each of which maps to values.
24 *
25 * @param <K1>
26 * the first key type
27 * @param <K2>
28 * the second key type
29 * @param <V>
30 * the value type
31 * @see java.util.HashMap
32 * @author Kasper B. Graversen
33 * @since 2.0.0 (migrated from Spiffy 0.5)
34 */
35 public class TwoDHashMap<K1, K2, V> {
36
37 private final HashMap<K1, HashMap<K2, V>> map;
38
39 /**
40 * Constructs a new <tt>TwoDHashMap</tt>.
41 */
42 public TwoDHashMap() {
43 map = new HashMap<K1, HashMap<K2, V>>();
44 }
45
46 /**
47 * Constructs a new <tt>TwoDHashMap</tt> using the supplied map.
48 *
49 * @param map
50 * the map
51 * @throws NullPointerException
52 * if map is null
53 */
54 public TwoDHashMap(final HashMap<K1, HashMap<K2, V>> map) {
55 if( map == null ) {
56 throw new NullPointerException("map should not be null");
57 }
58 this.map = map;
59 }
60
61 /**
62 * Existence check of a value (or <tt>null</tt>) mapped to the keys.
63 *
64 * @param firstKey
65 * first key
66 * @param secondKey
67 * second key
68 * @return true when an element (or <tt>null</tt>) has been stored with the keys
69 */
70 public boolean containsKey(final K1 firstKey, final K2 secondKey) {
71 // existence check on inner map
72 final HashMap<K2, V> innerMap = map.get(firstKey);
73 if( innerMap == null ) {
74 return false;
75 }
76 return innerMap.containsKey(secondKey);
77 }
78
79 /**
80 * Fetch a value from the Hashmap .
81 *
82 * @param firstKey
83 * first key
84 * @param secondKey
85 * second key
86 * @return the element or null.
87 */
88 public V get(final K1 firstKey, final K2 secondKey) {
89 // existence check on inner map
90 final HashMap<K2, V> innerMap = map.get(firstKey);
91 if( innerMap == null ) {
92 return null;
93 }
94 return innerMap.get(secondKey);
95 }
96
97 /**
98 * Insert a value
99 *
100 * @param firstKey
101 * first key
102 * @param secondKey
103 * second key
104 * @param value
105 * the value to be inserted. <tt>null</tt> may be inserted as well.
106 * @return null or the value the insert is replacing.
107 */
108 public Object set(final K1 firstKey, final K2 secondKey, final V value) {
109 // existence check on inner map
110 HashMap<K2, V> innerMap = map.get(firstKey);
111
112 if( innerMap == null ) {
113 // no inner map, create it
114 innerMap = new HashMap<K2, V>();
115 map.put(firstKey, innerMap);
116 }
117
118 return innerMap.put(secondKey, value);
119 }
120
121 /**
122 * Returns the number of key-value mappings in this map for the first key.
123 *
124 * @return Returns the number of key-value mappings in this map for the first key.
125 */
126 public int size() {
127 return map.size();
128 }
129
130 /**
131 * Returns the number of key-value mappings in this map for the second key.
132 *
133 * @param firstKey
134 * the first key
135 * @return Returns the number of key-value mappings in this map for the second key.
136 */
137 public int size(final K1 firstKey) {
138 // existence check on inner map
139 final HashMap<K2, V> innerMap = map.get(firstKey);
140 if( innerMap == null ) {
141 return 0;
142 }
143 return innerMap.size();
144 }
145
146 /**
147 * Returns a set of the keys of the outermost map.
148 *
149 * @return the key set for the outermost map
150 */
151 public Set<K1> keySet() {
152 return map.keySet();
153 }
154
155 }