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

com.hazelcast.mapreduce.aggregation.impl.LongMinAggregation 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.util.Map;

/**
 * The predefined minimum aggregation for values of type long.
 *
 * @param    the input key type
 * @param  the input value type
 */
public class LongMinAggregation
        implements AggType {

    @Override
    public Collator, Long> getCollator() {
        return new Collator, Long>() {
            @Override
            public Long collate(Iterable> values) {
                long min = Long.MAX_VALUE;
                for (Map.Entry entry : values) {
                    long value = entry.getValue();
                    if (value < min) {
                        min = value;
                    }
                }
                return min;
            }
        };
    }

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

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

    @Override
    public ReducerFactory getReducerFactory() {
        return new LongMinReducerFactory();
    }

    /**
     * Minimum CombinerFactory for type long
     *
     * @param  the key type
     */
    @BinaryInterface
    static final class LongMinCombinerFactory
            extends AbstractAggregationCombinerFactory {

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

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

    /**
     * Minimum ReducerFactory for type long
     *
     * @param  the key type
     */
    @BinaryInterface
    static final class LongMinReducerFactory
            extends AbstractAggregationReducerFactory {

        @Override
        public Reducer newReducer(Key key) {
            return new LongMinReducer();
        }

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

    /**
     * Minimum Combiner for type long
     */
    private static final class LongMinCombiner
            extends Combiner {

        private long chunkMin = Long.MAX_VALUE;

        @Override
        public void combine(Long value) {
            if (value < chunkMin) {
                chunkMin = value;
            }
        }

        @Override
        public Long finalizeChunk() {
            long value = chunkMin;
            chunkMin = Long.MAX_VALUE;
            return value;
        }
    }

    /**
     * Minimum Reducer for type long
     */
    private static final class LongMinReducer
            extends Reducer {

        private long min = Long.MAX_VALUE;

        @Override
        public void reduce(Long value) {
            if (value < min) {
                min = value;
            }
        }

        @Override
        public Long finalizeReduce() {
            return min;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy