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 finds the maximum number in a list. It does this by traversing the list
027 * (once) -- so the complexity of this will be <i>O(n)</i>.
028 */
029public class DoubleMaxAggregatorFunction implements Function<List<Double>, Double> {
030    /**
031     * Does the actual traversal of the list and finds the maximum value then
032     * returns the result. Please note that caller is responsible for
033     * synchronizing access to the list.
034     *
035     * @param data
036     *            List to traverse and find max
037     * @return max number in the list or null if the list is empty.
038     */
039    public Double evaluate(List<Double> data) {
040        if (data == null || data.size() == 0) {
041            return null;
042        }
043        Double max = null;
044        for (Double d : data) {
045            if (max == null) {
046                max = d;
047            } else {
048                if (max.doubleValue() < d.doubleValue()) {
049                    max = d;
050                }
051            }
052        }
053        return max;
054    }
055
056    @Override
057    public String toString() {
058        return DoubleMaxAggregatorFunction.class.getName();
059    }
060
061}