ObjectCounter.java :  » Game » Arianne-RPG-0.83 » games » stendhal » server » util » Java Open Source

Java Open Source » Game » Arianne RPG 0.83 
Arianne RPG 0.83 » games » stendhal » server » util » ObjectCounter.java
package games.stendhal.server.util;

import java.util.HashMap;
import java.util.Map;

/**
 * Counts objects.
 * 
 * @author hendrik
 * @param <K>
 *            Type of objects to count
 */
public class ObjectCounter<K> {
  private final Map<K, Integer> counter = new HashMap<K, Integer>();

  /**
   * Clears the counter.
   */
  public void clear() {
    counter.clear();
  }

  /**
   * Gets the counter map.
   * 
   * @return Map
   */
  public Map<K, Integer> getMap() {
    return counter;
  }

  /**
   * Adds one to the appropriate entry.
   * 
   * @param o
   *            object
   */
  public void add(final K o) {
    Integer in = counter.get(o);
    if (in == null) {
      in = Integer.valueOf(1);
    } else {
      in += Integer.valueOf(1);
    }
    counter.put(o, in);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.