001    /*--------------------------------------------------------------------------+
002    $Id: CounterSet.java 26268 2010-02-18 10:44:30Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.collections;
019    
020    import java.util.LinkedHashMap;
021    import java.util.Map;
022    
023    /**
024     * This class manages a set of counters (i.e. is a mapping from some key objects
025     * to integers). As the implementation is based on hash maps, key objects must
026     * provide suitable hash keys.
027     * 
028     * 
029     * @author Florian Deissenboeck
030     * @author $Author: juergens $
031     * @version $Rev: 26268 $
032     * @levd.rating GREEN Hash: FE6470D850FAD6E9A77E4059CD0692C5
033     */
034    public class CounterSet<E> {
035    
036            /** The underlying map. */
037            protected final Map<E, Integer> map = new LinkedHashMap<E, Integer>();
038    
039            /** Stores total value. */
040            protected int total = 0;
041    
042            /**
043             * Add the given increment to an element. If the element was not present
044             * before, it is interpreted as if it was present with value 0.
045             * 
046             * @param key
047             *            the key of the counter to increment.
048             * @param increment
049             *            the increment.
050             */
051            public void inc(E key, int increment) {
052                    Integer value = map.get(key);
053                    int newValue;
054                    if (value == null) {
055                            newValue = increment;
056                    } else {
057                            newValue = value + increment;
058                    }
059                    map.put(key, newValue);
060    
061                    // update total sum
062                    total += increment;
063            }
064    
065            /**
066             * Same as <code>inc(key, 1)</code>.
067             * 
068             * @see #inc(Object, int)
069             */
070            public void inc(E key) {
071                    inc(key, 1);
072            }
073    
074            /**
075             * Checks if an element is stored in the array.
076             */
077            public boolean contains(E key) {
078                    return map.containsKey(key);
079            }
080    
081            /**
082             * Get the value for an element. If the the element is not stored in the
083             * counter <code>0</code> is returned.
084             */
085            public int getValue(E key) {
086                    Integer value = map.get(key);
087                    if (value == null) {
088                            return 0;
089                    }
090                    return value;
091            }
092    
093            /**
094             * Returns the set of all elements used a keys for counters.
095             */
096            public UnmodifiableSet<E> getKeys() {
097                    return CollectionUtils.asUnmodifiable(map.keySet());
098            }
099    
100            /**
101             * Get total sum of all elements.
102             */
103            public int getTotal() {
104                    return total;
105            }
106    
107    }