All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.aggregation.Aggregators Maven / Gradle / Ivy

There is a newer version: 4.5.4
Show newest version
/*
 * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.aggregation;

import com.hazelcast.aggregation.impl.BigDecimalAverageAggregator;
import com.hazelcast.aggregation.impl.BigDecimalSumAggregator;
import com.hazelcast.aggregation.impl.BigIntegerAverageAggregator;
import com.hazelcast.aggregation.impl.BigIntegerSumAggregator;
import com.hazelcast.aggregation.impl.CountAggregator;
import com.hazelcast.aggregation.impl.DistinctValuesAggregator;
import com.hazelcast.aggregation.impl.DoubleAverageAggregator;
import com.hazelcast.aggregation.impl.DoubleSumAggregator;
import com.hazelcast.aggregation.impl.FixedSumAggregator;
import com.hazelcast.aggregation.impl.FloatingPointSumAggregator;
import com.hazelcast.aggregation.impl.IntegerAverageAggregator;
import com.hazelcast.aggregation.impl.IntegerSumAggregator;
import com.hazelcast.aggregation.impl.LongAverageAggregator;
import com.hazelcast.aggregation.impl.LongSumAggregator;
import com.hazelcast.aggregation.impl.MaxAggregator;
import com.hazelcast.aggregation.impl.MaxByAggregator;
import com.hazelcast.aggregation.impl.MinAggregator;
import com.hazelcast.aggregation.impl.MinByAggregator;
import com.hazelcast.aggregation.impl.NumberAverageAggregator;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Set;

/**
 * A utility class to create basic {@link com.hazelcast.aggregation.Aggregator} instances.
 * 

* Min/Max/Average aggregators are type specific, so an integerAvg() aggregator expects all elements to be integers. * There is no conversion executed while accumulating, so if there is any other type met an exception will be thrown. *

* In order to operate on a generic Number type use the fixedPointSum(), floatingPointSum() and numberAvg() aggregators. * All of them will convert the given number to either Long or Double during the accumulation phase. * It will result in a lot of allocations since each number has to be converted, but it enables the user * to operate on the whole family of numbers. It is especially useful if the numbers given to the aggregators * may not be of one type only. *

* The attributePath given in the factory method allows the aggregator to operate on the value extracted by navigating * to the given attributePath on each object that has been returned from a query. * The attribute path may be simple, e.g. "name", or nested "address.city". *

* If an aggregator does not accept null values pass a predicate to the aggregate call that will filter them out. *

* If the input value or the extracted value is a collection it won't be "unfolded" - so for example * count aggregation on "person.postalCodes" will return 1 for each input object and not the size of the collection. * In order to calculate the size of the collection use the [any] operator, e.g. "person.postalCodes[any]". * * @since 3.8 */ @SuppressWarnings({"checkstyle:methodcount", "checkstyle:classdataabstractioncoupling"}) public final class Aggregators { private Aggregators() { } /** * @param type of the input object. * @return an aggregator that counts the input values. * Accepts nulls as input values. * Aggregation result type Long. */ public static Aggregator count() { return new CountAggregator(); } /** * @param type of the input object. * @return an aggregator that counts the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Aggregation result type Long. */ public static Aggregator count(String attributePath) { return new CountAggregator(attributePath); } /** * @param type of the input object. * @param type of the return object. * @return an aggregator that calculates the distinct set of input values. * Accepts null input values. * Aggregation result type is a Set of R. */ public static Aggregator> distinct() { return new DistinctValuesAggregator(); } /** * @param type of the input object. * @param type of the return object. * @return an aggregator that calculates the distinct set of input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Aggregation result type is a Set of R. */ public static Aggregator> distinct(String attributePath) { return new DistinctValuesAggregator(attributePath); } // --------------------------------------------------------------------------------------------------------- // average aggregators // --------------------------------------------------------------------------------------------------------- /** * @param type of the input object. * @return an aggregator that calculates the average of the input values. * Does NOT accept null input values. * Accepts only BigDecimal input values. * Aggregation result type is BigDecimal. */ public static Aggregator bigDecimalAvg() { return new BigDecimalAverageAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values extracted from the given attributePath. * Does NOT accept null input values nor null extracted values. * Accepts only BigDecimal input values. * Aggregation result type is BigDecimal. */ public static Aggregator bigDecimalAvg(String attributePath) { return new BigDecimalAverageAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values. * Does NOT accept null input values. * Accepts only BigInteger input values. * Aggregation result type is BigDecimal. */ public static Aggregator bigIntegerAvg() { return new BigIntegerAverageAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values extracted from the given attributePath. * Does NOT accept null input values nor null extracted values. * Accepts only BigInteger input values. * Aggregation result type is BigDecimal. */ public static Aggregator bigIntegerAvg(String attributePath) { return new BigIntegerAverageAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values. * Does NOT accept null input values. * Accepts only Double input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator doubleAvg() { return new DoubleAverageAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values extracted from the given attributePath. * Does NOT accept null input values nor null extracted values. * Accepts only Double input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator doubleAvg(String attributePath) { return new DoubleAverageAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values. * Does NOT accept null input values. * Accepts only Integer input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator integerAvg() { return new IntegerAverageAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values extracted from the given attributePath. * Does NOT accept null input values nor null extracted values. * Accepts only Integer input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator integerAvg(String attributePath) { return new IntegerAverageAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values. * Does NOT accept null input values. * Accepts only Long input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator longAvg() { return new LongAverageAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values extracted from the given attributePath. * Does NOT accept null input values nor null extracted values. * Accepts only Long input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator longAvg(String attributePath) { return new LongAverageAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values. * Does NOT accept null input values. * Accepts generic Number input values. * Aggregation result type is Double. */ public static Aggregator numberAvg() { return new NumberAverageAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the average of the input values extracted from the given attributePath. * Does NOT accept null input values nor null extracted values. * Accepts generic Number input values. * Aggregation result type is Double. */ public static Aggregator numberAvg(String attributePath) { return new NumberAverageAggregator(attributePath); } // --------------------------------------------------------------------------------------------------------- // max aggregators // --------------------------------------------------------------------------------------------------------- /** * @param type of the input object. * @return an aggregator that calculates the max of the input values. * Accepts null input values. * Accepts only BigDecimal input values. * Aggregation result type is BigDecimal. */ public static Aggregator bigDecimalMax() { return new MaxAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the max of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts only BigDecimal input values. * Aggregation result type is BigDecimal. */ public static Aggregator bigDecimalMax(String attributePath) { return new MaxAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the max of the input values. * Accepts null input values and null extracted values. * Accepts only BigInteger input values. * Aggregation result type is BigInteger. */ public static Aggregator bigIntegerMax() { return new MaxAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the max of the input values extracted from the given attributePath. * Accepts null input values nor null extracted values. * Accepts only BigInteger input values. * Aggregation result type is BigInteger. */ public static Aggregator bigIntegerMax(String attributePath) { return new MaxAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the max of the input values. * Accepts null input values and null extracted values. * Accepts only Double input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator doubleMax() { return new MaxAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the max of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts only Double input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator doubleMax(String attributePath) { return new MaxAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the max of the input values. * Accepts null input values. * Accepts only Integer input values (primitive and boxed). * Aggregation result type is Integer. */ public static Aggregator integerMax() { return new MaxAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the max of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts only Integer input values (primitive and boxed). * Aggregation result type is Integer. */ public static Aggregator integerMax(String attributePath) { return new MaxAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the max of the input values. * Accepts null input values. * Accepts only Long input values (primitive and boxed). * Aggregation result type is Long. */ public static Aggregator longMax() { return new MaxAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the max of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts only Long input values (primitive and boxed). * Aggregation result type is Long. */ public static Aggregator longMax(String attributePath) { return new MaxAggregator(attributePath); } /** * @param type of the input object. * @param type of the return object (subtype of Comparable) * @return an aggregator that calculates the max of the input values. * Accepts null input values. * Accepts generic Comparable input values. * Aggregation result type is R. */ public static Aggregator comparableMax() { return new MaxAggregator(); } /** * @param type of the input object. * @param type of the return object (subtype of Comparable) * @return an aggregator that calculates the max of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts generic Comparable input values. * Aggregation result type is R. */ public static Aggregator comparableMax(String attributePath) { return new MaxAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the max of the input values extracted from the given attributePath * and returns the input item containing the maximum value. If multiple items contain the maximum value, * any of them is returned. * Accepts null input values and null extracted values. * Accepts generic Comparable input values. */ public static Aggregator maxBy(String attributePath) { return new MaxByAggregator(attributePath); } // --------------------------------------------------------------------------------------------------------- // min aggregators // --------------------------------------------------------------------------------------------------------- /** * @param type of the input object. * @return an aggregator that calculates the min of the input values. * Accepts null input values and null extracted values. * Accepts only BigDecimal input values. * Aggregation result type is BigDecimal. */ public static Aggregator bigDecimalMin() { return new MinAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the min of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts only BigDecimal input values. * Aggregation result type is BigDecimal. */ public static Aggregator bigDecimalMin(String attributePath) { return new MinAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the min of the input values. * Accepts null input values and null extracted values. * Accepts only BigInteger input values. * Aggregation result type is BigInteger. */ public static Aggregator bigIntegerMin() { return new MinAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the min of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts only BigInteger input values. * Aggregation result type is BigInteger. */ public static Aggregator bigIntegerMin(String attributePath) { return new MinAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the min of the input values. * Accepts null input values and null extracted values. * Accepts only Double input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator doubleMin() { return new MinAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the min of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts only Double input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator doubleMin(String attributePath) { return new MinAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the min of the input values. * Accepts null input values and null extracted values. * Accepts only Integer input values (primitive and boxed). * Aggregation result type is Integer. */ public static Aggregator integerMin() { return new MinAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the min of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts only Integer input values (primitive and boxed). * Aggregation result type is Integer. */ public static Aggregator integerMin(String attributePath) { return new MinAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the min of the input values. * Accepts null input values and null extracted values. * Accepts only Long input values (primitive and boxed). * Aggregation result type is Long. */ public static Aggregator longMin() { return new MinAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the min of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts only Long input values (primitive and boxed). * Aggregation result type is Long. */ public static Aggregator longMin(String attributePath) { return new MinAggregator(attributePath); } /** * @param type of the input object. * @param type of the return object (subtype of Comparable) * @return an aggregator that calculates the min of the input values. * Accepts null input values. * Accepts generic Comparable input values. * Aggregation result type is R. */ public static Aggregator comparableMin() { return new MinAggregator(); } /** * @param type of the input object. * @param type of the return object (subtype of Comparable) * @return an aggregator that calculates the min of the input values extracted from the given attributePath. * Accepts null input values and null extracted values. * Accepts generic Comparable input values. * Aggregation result type is R. */ public static Aggregator comparableMin(String attributePath) { return new MinAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the min of the input values extracted from the given attributePath * and returns the input item containing the minimum value. If multiple items contain the minimum value, * any of them is returned. * Accepts null input values and null extracted values. * Accepts generic Comparable input values. */ public static Aggregator minBy(String attributePath) { return new MinByAggregator(attributePath); } // --------------------------------------------------------------------------------------------------------- // sum aggregators // --------------------------------------------------------------------------------------------------------- /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values. * Does NOT accept null input values. * Accepts only BigDecimal input values. * Aggregation result type is BigDecimal. */ public static Aggregator bigDecimalSum() { return new BigDecimalSumAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values extracted from the given attributePath. * Does NOT accept null input values nor null extracted values. * Accepts only BigDecimal input values. * Aggregation result type is BigDecimal. */ public static Aggregator bigDecimalSum(String attributePath) { return new BigDecimalSumAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values. * Does NOT accept null input values. * Accepts only BigInteger input values. * Aggregation result type is BigInteger. */ public static Aggregator bigIntegerSum() { return new BigIntegerSumAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values extracted from the given attributePath. * Does NOT accept null input values nor null extracted values. * Accepts only BigInteger input values. * Aggregation result type is BigInteger. */ public static Aggregator bigIntegerSum(String attributePath) { return new BigIntegerSumAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values. * Does NOT accept null input values. * Accepts only Double input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator doubleSum() { return new DoubleSumAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values extracted from the given attributePath. * Does NOT accept null input values. * Accepts only Double input values (primitive and boxed). * Aggregation result type is Double. */ public static Aggregator doubleSum(String attributePath) { return new DoubleSumAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values. * Does NOT accept null input values. * Accepts only Integer input values (primitive and boxed). * Aggregation result type is Long. */ public static Aggregator integerSum() { return new IntegerSumAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values extracted from the given attributePath. * Does NOT accept null input values. * Accepts only Integer input values (primitive and boxed). * Aggregation result type is Long. */ public static Aggregator integerSum(String attributePath) { return new IntegerSumAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values. * Does NOT accept null input values. * Accepts only Long input values (primitive and boxed). * Aggregation result type is Long. */ public static Aggregator longSum() { return new LongSumAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values extracted from the given attributePath. * Does NOT accept null input values. * Accepts only Long input values (primitive and boxed). * Aggregation result type is Long. */ public static Aggregator longSum(String attributePath) { return new LongSumAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values. * Does NOT accept null input values. * Accepts generic Number input values. * Aggregation result type is Long. */ public static Aggregator fixedPointSum() { return new FixedSumAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values extracted from the given attributePath. * Does NOT accept null input values. * Accepts generic Number input values. * Aggregation result type is Long. */ public static Aggregator fixedPointSum(String attributePath) { return new FixedSumAggregator(attributePath); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values. * Does NOT accept null input values. * Accepts generic Number input values. * Aggregation result type is Double. */ public static Aggregator floatingPointSum() { return new FloatingPointSumAggregator(); } /** * @param type of the input object. * @return an aggregator that calculates the sum of the input values extracted from the given attributePath. * Does NOT accept null input values. * Accepts generic Number input values. * Aggregation result type is Double. */ public static Aggregator floatingPointSum(String attributePath) { return new FloatingPointSumAggregator(attributePath); } }