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.UnaryFunction; 022 023/** 024 * Aggregator function to be used with subclasses of 025 * {@link org.apache.commons.functor.aggregator.AbstractListBackedAggregator} 026 * which computes the arithmetic mean of all the numbers in the list. 027 */ 028public final class DoubleMeanValueAggregatorFunction implements UnaryFunction<List<Double>, Double> { 029 /** 030 * Does the actual computation and returns the result. Please note that 031 * caller is responsible for synchronizing access to the list. 032 * 033 * @param data 034 * List to traverse and sum 035 * @return arithmetic mean (average) of all the data in the list or null if 036 * the list is empty. 037 */ 038 public Double evaluate(List<Double> data) { 039 if (data == null || data.size() == 0) { 040 return null; 041 } 042 double mean = 0.0; 043 int n = data.size(); 044 for (Double d : data) { 045 mean += d.doubleValue(); 046 } 047 mean /= n; 048 049 return mean; 050 } 051 052 @Override 053 public String toString() { 054 return DoubleMeanValueAggregatorFunction.class.getName(); 055 } 056}