com.hazelcast.mapreduce.aggregation.impl.BigDecimalSumAggregation Maven / Gradle / Ivy
/*
* 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.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 com.hazelcast.nio.serialization.BinaryInterface;
import java.math.BigDecimal;
import java.util.Map;
/**
* The predefined sum aggregation for values of type {@link java.math.BigDecimal}.
*
* @param the input key type
* @param the input value type
*/
public class BigDecimalSumAggregation
implements AggType {
@Override
public Collator, BigDecimal> getCollator() {
return new Collator, BigDecimal>() {
@Override
public BigDecimal collate(Iterable> values) {
BigDecimal sum = BigDecimal.ZERO;
for (Map.Entry entry : values) {
sum = sum.add(entry.getValue());
}
return sum;
}
};
}
@Override
public Mapper getMapper(Supplier supplier) {
return new SupplierConsumingMapper(supplier);
}
@Override
public CombinerFactory getCombinerFactory() {
return new BigDecimalSumCombinerFactory();
}
@Override
public ReducerFactory getReducerFactory() {
return new BigDecimalSumReducerFactory();
}
/**
* Sum CombinerFactory for type {@link java.math.BigDecimal}
*
* @param the key type
*/
@BinaryInterface
static final class BigDecimalSumCombinerFactory
extends AbstractAggregationCombinerFactory {
@Override
public Combiner newCombiner(Key key) {
return new BigDecimalSumCombiner();
}
@Override
public int getId() {
return AggregationsDataSerializerHook.BIG_DECIMAL_SUM_COMBINER_FACTORY;
}
}
/**
* Sum ReducerFactory for type {@link java.math.BigDecimal}
*
* @param the key type
*/
@BinaryInterface
static final class BigDecimalSumReducerFactory
extends AbstractAggregationReducerFactory {
@Override
public Reducer newReducer(Key key) {
return new BigDecimalSumReducer();
}
@Override
public int getId() {
return AggregationsDataSerializerHook.BIG_DECIMAL_SUM_REDUCER_FACTORY;
}
}
/**
* Sum Combiner for type {@link java.math.BigDecimal}
*/
private static final class BigDecimalSumCombiner
extends Combiner {
private BigDecimal sum = BigDecimal.ZERO;
@Override
public void combine(BigDecimal value) {
sum = sum.add(value);
}
@Override
public BigDecimal finalizeChunk() {
return sum;
}
@Override
public void reset() {
sum = BigDecimal.ZERO;
}
}
/**
* Sum Reducer for type {@link java.math.BigDecimal}
*/
private static final class BigDecimalSumReducer
extends Reducer {
private BigDecimal sum = BigDecimal.ZERO;
@Override
public void reduce(BigDecimal value) {
sum = sum.add(value);
}
@Override
public BigDecimal finalizeReduce() {
return sum;
}
}
}