1 package jp.sourceforge.stigmata.birthmarks;
7 import java.util.Arrays;
8 import java.util.Comparator;
9 import java.util.HashMap;
10 import java.util.Iterator;
13 import jp.sourceforge.stigmata.BirthmarkElement;
14 import jp.sourceforge.stigmata.utils.ArrayIterator;
18 * @author Haruaki Tamada
21 public class FrequencyBirthmark extends AbstractBirthmark{
22 private static final long serialVersionUID = 1905526895627693908L;
24 private Map<String, FrequencyBirthmarkElement> counts = new HashMap<String, FrequencyBirthmarkElement>();
27 public FrequencyBirthmark(String type){
32 public int getElementCount(){
37 public synchronized BirthmarkElement[] getElements(){
38 FrequencyBirthmarkElement[] elements = new FrequencyBirthmarkElement[counts.size()];
40 for(Map.Entry<String, FrequencyBirthmarkElement> entry: counts.entrySet()){
41 elements[index] = entry.getValue();
44 Arrays.sort(elements, new Comparator<FrequencyBirthmarkElement>(){
46 public int compare(FrequencyBirthmarkElement o1, FrequencyBirthmarkElement o2){
47 return o1.getValueName().compareTo(o2.getValueName());
55 public Iterator<BirthmarkElement> iterator(){
56 return new ArrayIterator<BirthmarkElement>(getElements());
60 public void addElement(BirthmarkElement element){
62 if(element instanceof FrequencyBirthmarkElement){
63 FrequencyBirthmarkElement e = (FrequencyBirthmarkElement)element;
64 value = e.getValueName();
67 value = (String)element.getValue();
69 FrequencyBirthmarkElement foundElement = counts.get(value);
70 if(foundElement != null){
71 foundElement.incrementValueCount();
74 foundElement = new FrequencyBirthmarkElement(value);
76 counts.put(value, foundElement);
80 public String getType(){