Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8686a7f59f7b20f4a2ff1a26eaf9bbf80fcaf9f6 (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
/*******************************************************************************
 * 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.Map.Entry;
import java.util.Set;

/**
 * @author Ryan D. Brooks
 */
public class CountingMap<K> {
   private final HashMap<K, MutableInteger> countingMap;

   public CountingMap(int initialCapacity) {
      countingMap = new HashMap<K, MutableInteger>(initialCapacity);
   }

   public CountingMap() {
      countingMap = new HashMap<K, MutableInteger>();
   }

   public int get(K key) {
      MutableInteger count = countingMap.get(key);
      if (count == null) {
         return 0;
      }
      return count.getValue();
   }

   public boolean contains(K key) {
      return countingMap.containsKey(key);
   }

   public void put(K key) {
      MutableInteger count = countingMap.get(key);
      if (count == null) {
         countingMap.put(key, new MutableInteger(1));
      } else {
         count.getValueAndInc();
      }
   }

   public void put(K key, int byAmt) {
      MutableInteger count = countingMap.get(key);
      if (count == null) {
         countingMap.put(key, new MutableInteger(byAmt));
      } else {
         count.getValueAndInc(byAmt);
      }
   }

   public void put(Collection<K> keys) {
      for (K key : keys) {
         put(key);
      }
   }

   public Set<Entry<K, MutableInteger>> getCounts() {
      return countingMap.entrySet();
   }

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

Back to the top