001 /*--------------------------------------------------------------------------+ 002 $Id: MeanAggregator.java 29788 2010-08-19 08:46:02Z juergens $ 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 * Average aggregator. 024 * 025 * @author deissenb 026 * @author $Author: juergens $ 027 * @version $Rev: 29788 $ 028 * @levd.rating GREEN Hash: D9F275FB1C3A1E1F36A477AFCB8F75A2 029 */ 030 public class MeanAggregator implements IAggregator { 031 032 /** 033 * Aggregates by finding the average value 034 * 035 * @return {@link Double#NaN} for empty input collection 036 */ 037 @Override 038 public double aggregate(Collection<? extends Number> values) { 039 if (values.isEmpty()) { 040 return Double.NaN; 041 } 042 return MathUtils.sum(values) / values.size(); 043 } 044 045 /** Returns {@link Double#NaN}. */ 046 @Override 047 public double getNeutralElement() { 048 return Double.NaN; 049 } 050 }