001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.mapreduce.lib.aggregate;
020    
021    import java.util.ArrayList;
022    
023    import org.apache.hadoop.classification.InterfaceAudience;
024    import org.apache.hadoop.classification.InterfaceStability;
025    
026    
027    /**
028     * This class implements a value aggregator that sums up a sequence of double
029     * values.
030     * 
031     */
032    @InterfaceAudience.Public
033    @InterfaceStability.Stable
034    public class DoubleValueSum implements ValueAggregator<String> {
035    
036      double sum = 0;
037    
038      /**
039       * The default constructor
040       * 
041       */
042      public DoubleValueSum() {
043        reset();
044      }
045    
046      /**
047       * add a value to the aggregator
048       * 
049       * @param val
050       *          an object whose string representation represents a double value.
051       * 
052       */
053      public void addNextValue(Object val) {
054        this.sum += Double.parseDouble(val.toString());
055      }
056    
057      /**
058       * add a value to the aggregator
059       * 
060       * @param val
061       *          a double value.
062       * 
063       */
064      public void addNextValue(double val) {
065        this.sum += val;
066      }
067    
068      /**
069       * @return the string representation of the aggregated value
070       */
071      public String getReport() {
072        return "" + sum;
073      }
074    
075      /**
076       * @return the aggregated value
077       */
078      public double getSum() {
079        return this.sum;
080      }
081    
082      /**
083       * reset the aggregator
084       */
085      public void reset() {
086        sum = 0;
087      }
088    
089      /**
090       * @return return an array of one element. The element is a string
091       *         representation of the aggregated value. The return value is
092       *         expected to be used by the a combiner.
093       */
094      public ArrayList<String> getCombinerOutput() {
095        ArrayList<String> retv = new ArrayList<String>(1);
096        retv.add("" + sum);
097        return retv;
098      }
099    
100    }