Skip to main content
summaryrefslogtreecommitdiffstats
blob: 25041c7a324421cda15545c62cce8d5c09b50366 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.type;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Donald G. Dunne
 */
public class DoubleKeyCountingMap<KeyOne, KeyTwo> {

   HashMap<KeyOne, HashMap<KeyTwo, MutableInteger>> k1ToHashMap;

   public DoubleKeyCountingMap() {
      k1ToHashMap = new HashMap<KeyOne, HashMap<KeyTwo, MutableInteger>>();
   }

   public DoubleKeyCountingMap(int initialCapacity) {
      k1ToHashMap = new HashMap<KeyOne, HashMap<KeyTwo, MutableInteger>>(initialCapacity);
   }

   public Map<KeyOne, KeyTwo> keySet() {
      Map<KeyOne, KeyTwo> keySet = new HashMap<KeyOne, KeyTwo>();
      for (KeyOne one : k1ToHashMap.keySet()) {
         for (KeyTwo two : k1ToHashMap.get(one).keySet()) {
            keySet.put(one, two);
         }
      }
      return keySet;

   }

   public Collection<MutableInteger> get(KeyOne k1) {
      HashMap<KeyTwo, MutableInteger> o = k1ToHashMap.get(k1);
      if (o == null) {
         return null;
      }
      return o.values();
   }

   public MutableInteger get(KeyOne k1, KeyTwo k2) {
      HashMap<KeyTwo, MutableInteger> o = k1ToHashMap.get(k1);
      if (o != null) {
         return o.get(k2);
      }
      return null;
   }

   public MutableInteger put(KeyOne k1, KeyTwo k2, int v) {
      MutableInteger returnV = null;
      HashMap<KeyTwo, MutableInteger> o = k1ToHashMap.get(k1);
      if (o != null) {
         returnV = o.put(k2, new MutableInteger(v));
      } else {
         o = new HashMap<KeyTwo, MutableInteger>(20);
         returnV = o.put(k2, new MutableInteger(v));
         k1ToHashMap.put(k1, o);
      }
      return returnV;
   }

   public MutableInteger add(KeyOne k1, KeyTwo k2, int v) {
      MutableInteger returnV = null;
      HashMap<KeyTwo, MutableInteger> o = k1ToHashMap.get(k1);
      if (o != null) {
         returnV = o.get(k2);
         if (returnV != null) {
            returnV.getValueAndInc(v);
            o.put(k2, returnV);
         } else {
            o.put(k2, new MutableInteger(v));
         }
      } else {
         o = new HashMap<KeyTwo, MutableInteger>(20);
         returnV = o.put(k2, new MutableInteger(v));
         k1ToHashMap.put(k1, o);
      }
      return returnV;
   }

   public MutableInteger remove(KeyOne k1, KeyTwo k2) {
      MutableInteger value = null;
      HashMap<KeyTwo, MutableInteger> o = k1ToHashMap.get(k1);
      if (o != null) {
         value = o.remove(k2);
         if (o.size() == 0) {
            k1ToHashMap.remove(k1);
         }
      }
      return value;
   }

   @Override
   public String toString() {
      return k1ToHashMap.toString();
   }

   /**
    * The collection provided by this method is not backed by this DoubleKeyHashMap, and thusly any modifications to
    * Collection will not modify the map, and future modifications to the map will not modify the Collection.
    * 
    * @return Return value collection
    */
   public Collection<MutableInteger> allValues() {
      Collection<MutableInteger> values = new HashSet<MutableInteger>();
      for (HashMap<KeyTwo, MutableInteger> map : k1ToHashMap.values()) {
         values.addAll(map.values());
      }
      return values;
   }

   public Collection<MutableInteger> allValues(KeyOne key) {
      HashMap<KeyTwo, MutableInteger> map = k1ToHashMap.get(key);
      if (map != null) {
         return new HashSet<MutableInteger>(map.values());
      }
      return new HashSet<MutableInteger>();
   }

   public Map<KeyTwo, MutableInteger> getSubHash(KeyOne k1) {
      return k1ToHashMap.get(k1);
   }

   public boolean containsKey(KeyOne k1, KeyTwo k2) {
      return (k1ToHashMap.containsKey(k1) && k1ToHashMap.get(k1).containsKey(k2));
   }

   public void clear() {
      k1ToHashMap.clear();
   }

   public boolean isEmpty() {
      return k1ToHashMap.size() == 0;
   }

   public Set<KeyOne> getKeySetOne() {
      return k1ToHashMap.keySet();
   }

   public Collection<? extends Map<KeyTwo, MutableInteger>> getInnerMaps() {
      return k1ToHashMap.values();
   }

   /**
    * Test for DoubleKeyCountingMap
    * 
    * @param args
    */
   public static void main(String[] args) {
      DoubleKeyCountingMap<String, String> map = new DoubleKeyCountingMap<String, String>(23);
      map.put("aaa", "now", 4);
      System.out.println("Value should be 4 and is -> " + map.get("aaa", "now"));
      for (int x = 1; x < 3; x++) {
         map.add("aaa", "now", x);
      }
      map.add("bbb", "now", 4);
      map.add("bbb", "now", 1);
      map.add("aaa", "the", 3);
      System.out.println("Value aaa,now should be 7 and is -> " + map.get("aaa", "now"));
      System.out.println("Value bbb,now should be 5 and is -> " + map.get("bbb", "now"));
      System.out.println("Value aaa,the should be 3 and is -> " + map.get("aaa", "the"));

      for (String key1 : map.getKeySetOne()) {
         Map<String, MutableInteger> resolutionToCountMap = map.getSubHash(key1);
         for (String key2 : resolutionToCountMap.keySet()) {
            System.out.println(key1 + "," + key2 + "," + resolutionToCountMap.get(key2).getValue());
         }
      }
   }
}

Back to the top