View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.aggregator.functions;
18  
19  import java.util.List;
20  
21  import org.apache.commons.functor.UnaryFunction;
22  
23  /**
24   * Aggregator function to be used with subclasses of
25   * {@link org.apache.commons.functor.aggregator.AbstractListBackedAggregator}
26   * which sums up all the numbers in the list.
27   */
28  public final class DoubleSumAggregatorFunction implements UnaryFunction<List<Double>, Double> {
29      /**
30       * Does the actual adding and returns the result. Please note that caller is
31       * responsible for synchronizing access to the list.
32       *
33       * @param data
34       *            List to traverse and sum
35       * @return arithmetic sum of all the data in the list or null if the list is
36       *         empty.
37       */
38      public Double evaluate(List<Double> data) {
39          if (data == null || data.size() == 0) {
40              return null;
41          }
42          double sum = 0.0;
43          for (Double d : data) {
44              sum += d;
45          }
46          return sum;
47      }
48  
49      @Override
50      public String toString() {
51          return DoubleSumAggregatorFunction.class.getName();
52      }
53  }