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 org.apache.commons.functor.BinaryFunction;
020
021/**
022 * Aggregator function to be used with subclasses of
023 * {@link org.apache.commons.functor.aggregator.AbstractNoStoreAggregator} which
024 * simply increments the first argument by 1 and returns it. The reason behind
025 * this class is to provide a simple counter implementation: users call
026 * {@link org.apache.commons.functor.aggregator.AbstractNoStoreAggregator#add(Object)}
027 * with some data (which will be ignored) and the class will simply count how
028 * many times this call was made. Such an implementation can be used for
029 * instance to keep track for instance of number of transactions going through a
030 * system, whereas every time a transaction was made we just increment a
031 * counter. This can be achieved still using a
032 * {@link org.apache.commons.functor.aggregator.AbstractNoStoreAggregator} and a
033 * {@link DoubleSumAggregatorBinaryFunction}-like function (but using int) and always
034 * supplying the second parameter as 1 (one). However, using this might make the
035 * code clearer.
036 */
037public final class IntegerCountAggregatorBinaryFunction implements BinaryFunction<Integer, Integer, Integer> {
038    /**
039     * Increments <code>left</code> by one and returns it.
040     *
041     * @param left
042     *            Value to be incremented by 1 and returned. If
043     *            <code>null</code>, <code>null</code> is returned.
044     * @param right
045     *            ignored
046     * @return <code>left + 1</code> if <code>left != null</code> otherwise it
047     *         returns <code>null</code>.
048     */
049    public Integer evaluate(Integer left, Integer right) {
050        if (left == null) {
051            return null;
052        }
053        return left + 1;
054    }
055
056    @Override
057    public String toString() {
058        return IntegerCountAggregatorBinaryFunction.class.getName();
059    }
060}