Coverage Report - org.apache.commons.math.stat.descriptive.DescriptiveStatisticsImpl

Classes in this File Line Coverage Branch Coverage Complexity
DescriptiveStatisticsImpl
93% 
100% 
1.7

 1  
 /*
 2  
  * Copyright 2003-2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 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  
 /**
 23  
  * Default implementation of
 24  
  * {@link org.apache.commons.math.stat.descriptive.DescriptiveStatistics}.
 25  
  *
 26  
  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
 27  
  */
 28  
 public class DescriptiveStatisticsImpl extends DescriptiveStatistics implements Serializable {
 29  
 
 30  
     /** Serializable version identifier */
 31  
     static final long serialVersionUID = -1868088725461221010L;
 32  
     
 33  
     /** hold the window size **/
 34  
     protected int windowSize;
 35  
     
 36  
     /** 
 37  
      *  Stored data values
 38  
      */
 39  
     protected ResizableDoubleArray eDA;
 40  
 
 41  
     /**
 42  
      * Construct a DescriptiveStatisticsImpl with infinite window
 43  
      */
 44  
     public DescriptiveStatisticsImpl() {
 45  132
         this(INFINITE_WINDOW);
 46  132
     }
 47  
     
 48  
     /**
 49  
      * Construct a DescriptiveStatisticsImpl with finite window
 50  
      * @param window the finite window size.
 51  
      */
 52  
     public DescriptiveStatisticsImpl(int window) {
 53  132
         super();
 54  132
         eDA = new ResizableDoubleArray();
 55  132
         setWindowSize(window);
 56  132
     }
 57  
 
 58  
     /**
 59  
      * Access the window size.
 60  
      * @return the current window size.
 61  
      */
 62  
     public int getWindowSize() {
 63  2
         return windowSize;
 64  
     }
 65  
     
 66  
     /**
 67  
      * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getValues()
 68  
      */
 69  
     public double[] getValues() {
 70  
 
 71  98
         double[] copiedArray = new double[eDA.getNumElements()];
 72  98
         System.arraycopy(
 73  
             eDA.getElements(),
 74  
             0,
 75  
             copiedArray,
 76  
             0,
 77  
             eDA.getNumElements());
 78  98
         return copiedArray;
 79  
     }
 80  
     
 81  
     /**
 82  
      * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getElement(int)
 83  
      */
 84  
     public double getElement(int index) {
 85  0
         return eDA.getElement(index);
 86  
     }
 87  
 
 88  
     /**
 89  
      * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getN()
 90  
      */
 91  
     public long getN() {
 92  1232
         return eDA.getNumElements();
 93  
     }
 94  
 
 95  
     /**
 96  
      * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#addValue(double)
 97  
      */
 98  
     public void addValue(double v) {
 99  12202
         if (windowSize != INFINITE_WINDOW) {
 100  582
             if (getN() == windowSize) {
 101  30
                 eDA.addElementRolling(v);
 102  552
             } else if (getN() < windowSize) {
 103  552
                 eDA.addElement(v);
 104  
             }
 105  
         } else {
 106  11620
             eDA.addElement(v);
 107  
         }
 108  12202
     }
 109  
 
 110  
     /**
 111  
      * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#clear()
 112  
      */
 113  
     public void clear() {
 114  36
         eDA.clear();
 115  36
     }
 116  
 
 117  
     /**
 118  
      * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#setWindowSize(int)
 119  
      */
 120  
     public void setWindowSize(int windowSize) {
 121  194
         if (windowSize < 1) {
 122  134
             if (windowSize != INFINITE_WINDOW) {
 123  0
                 throw new IllegalArgumentException("window size must be positive.");
 124  
             }
 125  
         }
 126  
         
 127  194
         this.windowSize = windowSize;
 128  
 
 129  
         // We need to check to see if we need to discard elements
 130  
         // from the front of the array.  If the windowSize is less than 
 131  
         // the current number of elements.
 132  194
         if (windowSize != INFINITE_WINDOW && windowSize < eDA.getNumElements()) {
 133  2
             eDA.discardFrontElements(eDA.getNumElements() - windowSize);
 134  
         }
 135  194
     }
 136  
 
 137  
     /**
 138  
      * Apply the given statistic to this univariate collection.
 139  
      * @param stat the statistic to apply
 140  
      * @return the computed value of the statistic.
 141  
      */
 142  
     public double apply(UnivariateStatistic stat) {
 143  618
         return stat.evaluate(eDA.getValues(), eDA.start(), eDA.getNumElements());
 144  
     }
 145  
 }