
com.hazelcast.mapreduce.aggregation.impl.BigIntegerAvgAggregation Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2016, 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.mapreduce.aggregation.impl;
import com.hazelcast.mapreduce.Collator;
import com.hazelcast.mapreduce.Combiner;
import com.hazelcast.mapreduce.CombinerFactory;
import com.hazelcast.mapreduce.Mapper;
import com.hazelcast.mapreduce.Reducer;
import com.hazelcast.mapreduce.ReducerFactory;
import com.hazelcast.mapreduce.aggregation.Supplier;
import java.math.BigInteger;
import java.util.Map;
/**
* The predefined average aggregation for values of type {@link java.math.BigInteger}.
*
* @param the input key type
* @param the input value type
*/
public class BigIntegerAvgAggregation
implements AggType, AvgTuple, BigInteger> {
@Override
public Collator>, BigInteger> getCollator() {
return new Collator>, BigInteger>() {
@Override
public BigInteger collate(Iterable>> values) {
long count = 0;
BigInteger amount = BigInteger.ZERO;
for (Map.Entry> entry : values) {
AvgTuple tuple = entry.getValue();
count += tuple.getFirst();
amount = amount.add(tuple.getSecond());
}
return amount.divide(BigInteger.valueOf(count));
}
};
}
@Override
public Mapper getMapper(Supplier supplier) {
return new SupplierConsumingMapper(supplier);
}
@Override
public CombinerFactory> getCombinerFactory() {
return new BigIntegerAvgCombinerFactory();
}
@Override
public ReducerFactory, AvgTuple> getReducerFactory() {
return new BigIntegerAvgReducerFactory();
}
/**
* Average CombinerFactory for type {@link java.math.BigInteger}
*
* @param the key type
*/
static final class BigIntegerAvgCombinerFactory
extends AbstractAggregationCombinerFactory> {
@Override
public Combiner> newCombiner(Key key) {
return new BigIntegerAvgCombiner();
}
@Override
public int getId() {
return AggregationsDataSerializerHook.BIG_INTEGER_AVG_COMBINER_FACTORY;
}
}
/**
* Average ReducerFactory for type {@link java.math.BigInteger}
*
* @param the key type
*/
static final class BigIntegerAvgReducerFactory
extends AbstractAggregationReducerFactory, AvgTuple> {
@Override
public Reducer, AvgTuple> newReducer(Key key) {
return new BigIntegerAvgReducer();
}
@Override
public int getId() {
return AggregationsDataSerializerHook.BIG_INTEGER_AVG_REDUCER_FACTORY;
}
}
/**
* Average Combiner for type {@link java.math.BigInteger}
*/
private static final class BigIntegerAvgCombiner
extends Combiner> {
private long count;
private BigInteger amount = BigInteger.ZERO;
@Override
public void combine(BigInteger value) {
count++;
amount = amount.add(value);
}
@Override
public AvgTuple finalizeChunk() {
return new AvgTuple(count, amount);
}
@Override
public void reset() {
count = 0;
amount = BigInteger.ZERO;
}
}
/**
* Average Reducer for type {@link java.math.BigInteger}
*/
private static final class BigIntegerAvgReducer
extends Reducer, AvgTuple> {
private long count;
private BigInteger amount = BigInteger.ZERO;
@Override
public void reduce(AvgTuple value) {
count += value.getFirst();
amount = amount.add(value.getSecond());
}
@Override
public AvgTuple finalizeReduce() {
return new AvgTuple(count, amount);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy