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

com.hazelcast.mapreduce.aggregation.impl.IntegerAvgAggregation Maven / Gradle / Ivy

There is a newer version: 5.0-BETA-1
Show newest version
/*
 * Copyright (c) 2008-2015, 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.util.Map;

/**
 * The predefined average aggregation for values of type integer.
 *
 * @param    the input key type
 * @param  the input value type
 */
public class IntegerAvgAggregation
        implements AggType, AvgTuple, Integer> {

    @Override
    public Collator>, Integer> getCollator() {
        return new Collator>, Integer>() {
            @Override
            public Integer collate(Iterable>> values) {
                int count = 0;
                int amount = 0;
                for (Map.Entry> entry : values) {
                    AvgTuple tuple = entry.getValue();
                    count += tuple.getFirst();
                    amount += tuple.getSecond();
                }
                return (int) ((double) amount / count);
            }
        };
    }

    @Override
    public Mapper getMapper(Supplier supplier) {
        return new SupplierConsumingMapper(supplier);
    }

    @Override
    public CombinerFactory> getCombinerFactory() {
        return new IntegerAvgCombinerFactory();
    }

    @Override
    public ReducerFactory, AvgTuple> getReducerFactory() {
        return new IntegerAvgReducerFactory();
    }

    /**
     * Average CombinerFactory for type long
     *
     * @param  the key type
     */
    static final class IntegerAvgCombinerFactory
            extends AbstractAggregationCombinerFactory> {

        @Override
        public Combiner> newCombiner(Key key) {
            return new IntegerAvgCombiner();
        }

        @Override
        public int getId() {
            return AggregationsDataSerializerHook.INTEGER_AVG_COMBINER_FACTORY;
        }
    }

    /**
     * Average ReducerFactory for type integer
     *
     * @param  the key type
     */
    static final class IntegerAvgReducerFactory
            extends AbstractAggregationReducerFactory, AvgTuple> {

        @Override
        public Reducer, AvgTuple> newReducer(Key key) {
            return new IntegerAvgReducer();
        }

        @Override
        public int getId() {
            return AggregationsDataSerializerHook.INTEGER_AVG_REDUCER_FACTORY;
        }
    }

    /**
     * Average Combiner for type integer
     */
    private static final class IntegerAvgCombiner
            extends Combiner> {

        private int count;
        private int amount;

        @Override
        public void combine(Integer value) {
            count++;
            amount += value;
        }

        @Override
        public AvgTuple finalizeChunk() {
            int count = this.count;
            int amount = this.amount;
            this.count = 0;
            this.amount = 0;
            return new AvgTuple(count, amount);
        }
    }

    /**
     * Average Reducer for type integer
     */
    private static final class IntegerAvgReducer
            extends Reducer, AvgTuple> {

        private int count;
        private int amount;

        @Override
        public void reduce(AvgTuple value) {
            count += value.getFirst();
            amount += value.getSecond();
        }

        @Override
        public AvgTuple finalizeReduce() {
            return new AvgTuple(count, amount);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy