From: <kon...@us...> - 2010-09-07 19:15:07
|
Revision: 1131 http://cishell.svn.sourceforge.net/cishell/?rev=1131&view=rev Author: kongchinhua Date: 2010-09-07 19:15:01 +0000 (Tue, 07 Sep 2010) Log Message: ----------- Support Frequency counting - It is important to use key with unmodified value with deep comp[are of equal function - Safe to use with generic objects: String, Integer, etc - Non-generic class need customized equal and hashcode method Added Paths: ----------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/FrequencyMap.java Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/FrequencyMap.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/FrequencyMap.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/FrequencyMap.java 2010-09-07 19:15:01 UTC (rev 1131) @@ -0,0 +1,107 @@ +package org.cishell.utilities; + +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Map; +import java.util.Set; + +/** + * FrequencyMap is a map of key with its emergence frequency. + * It supports two type of mapping structures: null key and + * non-null key; by providing boolean value to enable + * allowNullValue through constructor call. Add() method will + * add a new key if the key is not exist. Else it will increase + * the frequency by 1. sum() will return the count that + * represents sum of all the frequency values in the map + * @author kongch + * + * @param <E> + */ +public class FrequencyMap <E> { + private Map<E, Frequency> itemToFrequencyMap; + private int count; + + public FrequencyMap(boolean allowNullValue) { + count = 0; + if (allowNullValue) { + itemToFrequencyMap = new HashMap<E, Frequency>(); + } else { + itemToFrequencyMap = new Hashtable<E, Frequency>(); + } + } + + /** + * Add the given key to the map and increase the frequency by 1 + * @param key - key to be added + */ + public void add(E key) { + Frequency frequency; + + /* + * Get the Frequency object for the given key if key already exist. + * Else create a new Frequency object and add it to the map + */ + if (itemToFrequencyMap.containsKey(key)) { + frequency = itemToFrequencyMap.get(key); + } else { + frequency = new Frequency(); + try { + itemToFrequencyMap.put(key, frequency); + } catch (NullPointerException e) { + /* It is a non-null map. Throw a runtime exception */ + String message = "FrequencyMap.add(E key) was called with a null key." + + " If null support is desired, " + + " construct FrequencyMap with allowNullValue = true"; + throw new NullValueSupportException(message, e); + } + } + + /* increase frequency by 1 and also sum of all frequencies by 1 */ + count++; + frequency.increase(); + } + + /** + * Retrieve a set of keys stored in this map + */ + public Set<E> keySet() { + return itemToFrequencyMap.keySet(); + } + + /** + * Get the emergence frequency for the given key + * @param key - item to be lookup + * @return Return number of times the key exists + * if lookup success. Return zero if key not exist + */ + public int getFrequency(E key) { + if (itemToFrequencyMap.containsKey(key)) { + return itemToFrequencyMap.get(key).getValue(); + } else { + return 0; + } + } + + public int sum() { + return count; + } + + public boolean isEmpty() { + return itemToFrequencyMap.isEmpty(); + } + + private class Frequency { + private int value; + public Frequency() { + value = 0; + } + + public void increase() { + value++; + } + + public int getValue() { + return value; + } + } +} Property changes on: trunk/core/org.cishell.utilities/src/org/cishell/utilities/FrequencyMap.java ___________________________________________________________________ Added: svn:mime-type + text/plain This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <kon...@us...> - 2010-09-07 19:24:16
|
Revision: 1132 http://cishell.svn.sourceforge.net/cishell/?rev=1132&view=rev Author: kongchinhua Date: 2010-09-07 19:24:10 +0000 (Tue, 07 Sep 2010) Log Message: ----------- Fixed style guide warning Modified Paths: -------------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/FrequencyMap.java Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/FrequencyMap.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/FrequencyMap.java 2010-09-07 19:15:01 UTC (rev 1131) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/FrequencyMap.java 2010-09-07 19:24:10 UTC (rev 1132) @@ -31,7 +31,7 @@ } /** - * Add the given key to the map and increase the frequency by 1 + * Add the given key to the map and increase the frequency by 1. * @param key - key to be added */ public void add(E key) { @@ -62,14 +62,14 @@ } /** - * Retrieve a set of keys stored in this map + * Retrieve a set of keys stored in this map. */ public Set<E> keySet() { return itemToFrequencyMap.keySet(); } /** - * Get the emergence frequency for the given key + * Get the emergence frequency for the given key. * @param key - item to be lookup * @return Return number of times the key exists * if lookup success. Return zero if key not exist This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |