001    /*--------------------------------------------------------------------------+
002    $Id: PairwiseAggregatorBase.java 28099 2010-06-09 13:17:22Z hummelb $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | 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    package edu.tum.cs.commons.math;
019    
020    import java.util.Collection;
021    
022    /**
023     * This is a base class for aggregators that have a defined neutral element and
024     * operate in pairwise manner.
025     * 
026     * @author Florian Deissenboeck
027     * @author $Author: hummelb $
028     * @version $Rev: 28099 $
029     * @levd.rating GREEN Hash: B262E418EFDA28F4B7634AA5FDDFD68E
030     */
031    public abstract class PairwiseAggregatorBase implements IAggregator {
032    
033            /** {@inheritDoc} */
034            @Override
035            public double aggregate(Collection<? extends Number> values) {
036                    double result = getNeutralElement();
037                    for (Number value : values) {
038                            result = aggregate(result, value.doubleValue());
039                    }
040                    return result;
041            }
042    
043            /**
044             * Calculate aggregate of two values.
045             * 
046             * @param aggregate
047             *            the value that has been aggregated by previous pairwise
048             *            aggregations
049             * @param value
050             *            the new value
051             */
052            protected abstract double aggregate(double aggregate, double value);
053    }