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;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.commons.functor.Function;
023
024/**
025 * Implementation of an aggregator which stores the data series in an
026 * <code>ArrayList</code>.
027 *
028 * @param <T>
029 *            Type of object stored in the data series.
030 */
031public class ArrayListBackedAggregator<T> extends AbstractListBackedAggregator<T> {
032    /**
033     * Similar to {@link #ArrayListBackedAggregator(Function, long)
034     * ArrayListBackedAggregator(aggregationFunction, 0L)}.
035     *
036     * @param aggregationFunction
037     *            Aggregation function to use in {@link #evaluate()}. Throws
038     *            <code>NullPointerException</code> if this is <code>null</code>
039     */
040    public ArrayListBackedAggregator(Function<List<T>, T> aggregationFunction) {
041        this(aggregationFunction, 0L);
042    }
043
044    /**
045     * Similar to
046     * {@link #ArrayListBackedAggregator(Function, long, boolean)
047     * ArrayListBackedAggregator(aggregationFunction,interval,false)}.
048     *
049     * @param aggregationFunction
050     *            Aggregation function to use in {@link #evaluate()}. Throws
051     *            <code>NullPointerException</code> if this is <code>null</code>
052     * @param interval
053     *            interval in miliseconds to reset this aggregator
054     */
055    public ArrayListBackedAggregator(Function<List<T>, T> aggregationFunction, long interval) {
056        this(aggregationFunction, interval, false);
057    }
058
059    /**
060     * Initializes an aggregator with the given function, interval and decides
061     * whether to use the shared timer or own timer.
062     *
063     * @param aggregationFunction
064     *            Aggregation function to use in {@link #evaluate()}. Throws
065     *            <code>NullPointerException</code> if this is <code>null</code>
066     * @param interval
067     *            interval in miliseconds to reset this aggregator
068     * @param useSharedTimer
069     *            if set to true, it shares a timer across instances as per
070     *            {@link AbstractTimedAggregator#AbstractTimedAggregator(long,boolean)}
071     *            , otherwise this instance will use its private timer
072     */
073    public ArrayListBackedAggregator(Function<List<T>, T> aggregationFunction, long interval,
074            boolean useSharedTimer) {
075        super(aggregationFunction, interval, useSharedTimer);
076    }
077
078    /**
079     * Creates an instance of <code>ArrayList</code> and returns it.
080     *
081     * @return newly created <code>ArrayList</code> (with default initial
082     *         capacity as per JDK).
083     */
084    @Override
085    protected List<T> createList() {
086        return new ArrayList<T>();
087    }
088
089    @Override
090    public String toString() {
091        return ArrayListBackedAggregator.class.getName();
092    }
093}