1 16 package org.apache.commons.math.stat.descriptive; 17 18 import java.io.Serializable ; 19 20 import org.apache.commons.math.util.ResizableDoubleArray; 21 22 28 public class DescriptiveStatisticsImpl extends DescriptiveStatistics implements Serializable { 29 30 31 static final long serialVersionUID = -1868088725461221010L; 32 33 34 protected int windowSize; 35 36 39 protected ResizableDoubleArray eDA; 40 41 44 public DescriptiveStatisticsImpl() { 45 this(INFINITE_WINDOW); 46 } 47 48 52 public DescriptiveStatisticsImpl(int window) { 53 super(); 54 eDA = new ResizableDoubleArray(); 55 setWindowSize(window); 56 } 57 58 62 public int getWindowSize() { 63 return windowSize; 64 } 65 66 69 public double[] getValues() { 70 71 double[] copiedArray = new double[eDA.getNumElements()]; 72 System.arraycopy( 73 eDA.getElements(), 74 0, 75 copiedArray, 76 0, 77 eDA.getNumElements()); 78 return copiedArray; 79 } 80 81 84 public double getElement(int index) { 85 return eDA.getElement(index); 86 } 87 88 91 public long getN() { 92 return eDA.getNumElements(); 93 } 94 95 98 public void addValue(double v) { 99 if (windowSize != INFINITE_WINDOW) { 100 if (getN() == windowSize) { 101 eDA.addElementRolling(v); 102 } else if (getN() < windowSize) { 103 eDA.addElement(v); 104 } 105 } else { 106 eDA.addElement(v); 107 } 108 } 109 110 113 public void clear() { 114 eDA.clear(); 115 } 116 117 120 public void setWindowSize(int windowSize) { 121 if (windowSize < 1) { 122 if (windowSize != INFINITE_WINDOW) { 123 throw new IllegalArgumentException ("window size must be positive."); 124 } 125 } 126 127 this.windowSize = windowSize; 128 129 if (windowSize != INFINITE_WINDOW && windowSize < eDA.getNumElements()) { 133 eDA.discardFrontElements(eDA.getNumElements() - windowSize); 134 } 135 } 136 137 142 public double apply(UnivariateStatistic stat) { 143 return stat.evaluate(eDA.getValues(), eDA.start(), eDA.getNumElements()); 144 } 145 } 146 | Popular Tags |