001    package org.expasy.jpl.commons.collection.stat;
002    
003    
004    import java.util.ArrayList;
005    import java.util.Collections;
006    import java.util.List;
007    import java.util.SortedMap;
008    import java.util.TreeMap;
009    import org.expasy.jpl.commons.base.CounterImpl;
010    
011    
012    /**
013     * A {@code StatisticalSerieDiscreteVar} contains only integer values.
014     * 
015     * @author nikitin
016     * 
017     * @version 1.0
018     */
019    public final class StatisticalSerieDiscreteDataSet implements
020        StatisticalSerie<Integer> {
021            
022            private SortedMap<Integer, CounterImpl> frequencies;
023            private List<Integer> values;
024            
025            private StatisticalSerieDiscreteDataSet() {
026                    frequencies = new TreeMap<Integer, CounterImpl>();
027                    values = new ArrayList<Integer>();
028            }
029            
030            public static StatisticalSerieDiscreteDataSet newInstance() {
031                    return new StatisticalSerieDiscreteDataSet();
032            }
033            
034            public int getSampleSize() {
035                    return values.size();
036            }
037            
038            public List<Integer> getSampleValues() {
039                    return getValues();
040            }
041            
042            public void loadSerie(List<? extends Number> values) {
043                    for (Number value : values) {
044                            loadDataInSerie(value);
045                    }
046            }
047            
048            public void loadDataInSerie(Number value) {
049                    if (!frequencies.containsKey(value)) {
050                            frequencies.put(value.intValue(), CounterImpl.valueOf(1));
051                    } else {
052                            frequencies.get(value).increment();
053                    }
054                    values.add(value.intValue());
055            }
056            
057            public final int getFrequency(Integer value) {
058                    if (!frequencies.containsKey(value)) {
059                            return 0;
060                    }
061                    return frequencies.get(value).getCount();
062            }
063            
064            public final double getRelativeFrequency(Integer value) {
065                    return getFrequency(value) / (double) getSampleSize();
066            }
067            
068            public List<Integer> getValues() {
069                    Collections.sort(values);
070                    return values;
071            }
072            
073            public final Integer getXAt(int index) {
074                    return values.get(index);
075            }
076            
077            public String toString() {
078                    StringBuilder sb = new StringBuilder("[");
079                    
080                    if (frequencies.size() > 0) {
081                            for (int next : frequencies.keySet()) {
082                                    sb.append(next);
083                                    sb.append(": ");
084                                    sb.append(frequencies.get(next).getCount());
085                                    sb.append(", ");
086                            }
087                            sb.delete(sb.length() - 2, sb.length());
088                    }
089                    
090                    sb.append("]");
091                    
092                    return sb.toString();
093            }
094    }