Coverage Report - org.apache.commons.math.stat.descriptive.summary.Product

Classes in this File Line Coverage Branch Coverage Complexity
Product
100% 
100% 
1.5

 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.summary;
 17  
 
 18  
 import java.io.Serializable;
 19  
 
 20  
 import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
 21  
 
 22  
 /**
 23  
  * Returns the product of the available values.
 24  
  * <p>
 25  
  * If there are no values in the dataset, or any of the values are 
 26  
  * <code>NaN</code>, then <code>NaN</code> is returned.  
 27  
 * <p>
 28  
  * <strong>Note that this implementation is not synchronized.</strong> If 
 29  
  * multiple threads access an instance of this class concurrently, and at least
 30  
  * one of the threads invokes the <code>increment()</code> or 
 31  
  * <code>clear()</code> method, it must be synchronized externally.
 32  
  * 
 33  
  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
 34  
  */
 35  
 public class Product extends AbstractStorelessUnivariateStatistic implements Serializable {
 36  
 
 37  
     /** Serializable version identifier */
 38  
     static final long serialVersionUID = 2824226005990582538L;   
 39  
      
 40  
     /**The number of values that have been added */
 41  
     private long n;
 42  
     
 43  
     /**
 44  
      * The current Running Product.
 45  
      */
 46  
     private double value;
 47  
 
 48  
     /**
 49  
      * Create a Product instance
 50  
      */
 51  26
     public Product() {
 52  26
         n = 0;
 53  26
         value = Double.NaN;
 54  26
     }
 55  
     
 56  
     /**
 57  
      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
 58  
      */
 59  
     public void increment(final double d) {
 60  170
         if (n == 0) {
 61  20
             value = d;
 62  
         } else {
 63  150
             value *= d;
 64  
         }
 65  170
         n++;
 66  170
     }
 67  
 
 68  
     /**
 69  
      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
 70  
      */
 71  
     public double getResult() {
 72  98
         return value;
 73  
     }
 74  
 
 75  
     /**
 76  
      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
 77  
      */
 78  
     public long getN() {
 79  58
         return n;
 80  
     }
 81  
     
 82  
     /**
 83  
      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
 84  
      */
 85  
     public void clear() {
 86  22
         value = Double.NaN;
 87  22
         n = 0;
 88  22
     }
 89  
 
 90  
     /**
 91  
      * Returns the product of the entries in the specified portion of
 92  
      * the input array, or <code>Double.NaN</code> if the designated subarray
 93  
      * is empty.
 94  
      * <p>
 95  
      * Throws <code>IllegalArgumentException</code> if the array is null.
 96  
      * 
 97  
      * @param values the input array
 98  
      * @param begin index of the first array element to include
 99  
      * @param length the number of elements to include
 100  
      * @return the product of the values or Double.NaN if length = 0
 101  
      * @throws IllegalArgumentException if the array is null or the array index
 102  
      *  parameters are not valid
 103  
      */
 104  
     public double evaluate(final double[] values, final int begin, final int length) {
 105  28
         double product = Double.NaN;
 106  28
         if (test(values, begin, length)) {
 107  20
             product = 1.0;
 108  144
             for (int i = begin; i < begin + length; i++) {
 109  124
                 product *= values[i];
 110  
             }
 111  
         }
 112  26
         return product;
 113  
     }
 114  
 
 115  
 }