001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.functor.aggregator.functions;
018
019import java.util.List;
020
021import org.apache.commons.functor.Function;
022
023/**
024 * Aggregator function to be used with subclasses of
025 * {@link org.apache.commons.functor.aggregator.AbstractListBackedAggregator}
026 * which sums up all the numbers in the list.
027 */
028public final class DoubleSumAggregatorFunction implements Function<List<Double>, Double> {
029    /**
030     * Does the actual adding and returns the result. Please note that caller is
031     * responsible for synchronizing access to the list.
032     *
033     * @param data
034     *            List to traverse and sum
035     * @return arithmetic sum of all the data in the list or null if the list is
036     *         empty.
037     */
038    public Double evaluate(List<Double> data) {
039        if (data == null || data.size() == 0) {
040            return null;
041        }
042        double sum = 0.0;
043        for (Double d : data) {
044            sum += d;
045        }
046        return sum;
047    }
048
049    @Override
050    public String toString() {
051        return DoubleSumAggregatorFunction.class.getName();
052    }
053}