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

/**
 * @author Donald G. Dunne
 */
public class AddingDoubleMap<K> {
   private final HashMap<K, MutableDouble> addingMap;

   public AddingDoubleMap(int initialCapacity) {
      addingMap = new HashMap<K, MutableDouble>(initialCapacity);
   }

   public AddingDoubleMap() {
      addingMap = new HashMap<K, MutableDouble>();
   }

   public double get(K key) {
      for (Entry<K, MutableDouble> entry : getCounts()) {
         if (entry.getKey().equals(key)) return entry.getValue().getValue();
      }
      return 0;
   }

   public boolean contains(K key) {
      for (Entry<K, MutableDouble> entry : getCounts()) {
         if (entry.getKey().equals(key)) return true;
      }
      return false;
   }

   public void put(K key, double byAmmount) {
      MutableDouble count = addingMap.get(key);
      if (count == null) {
         addingMap.put(key, new MutableDouble(byAmmount));
      } else {
         count.getValueAndInc(byAmmount);
      }
   }

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

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

Back to the top