com.hazelcast.mapreduce.aggregation.impl.ComparableMaxAggregation 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 maximum aggregation for values of type {@link java.lang.Comparable}.
*
* @param the input key type
* @param the input value type
*/
public class ComparableMaxAggregation
implements AggType {
@Override
public Collator, Comparable> getCollator() {
return new Collator, Comparable>() {
@Override
public Comparable collate(Iterable> values) {
Comparable max = null;
for (Map.Entry entry : values) {
Comparable value = entry.getValue();
if (max == null || value.compareTo(max) > 0) {
max = value;
}
}
return max;
}
};
}
@Override
public Mapper getMapper(Supplier supplier) {
return new SupplierConsumingMapper(supplier);
}
@Override
public CombinerFactory getCombinerFactory() {
return new ComparableMaxCombinerFactory();
}
@Override
public ReducerFactory getReducerFactory() {
return new ComparableMaxReducerFactory();
}
/**
* Maximum CombinerFactory for type {@link java.lang.Comparable}
*
* @param the key type
*/
@BinaryInterface
static final class ComparableMaxCombinerFactory
extends AbstractAggregationCombinerFactory {
@Override
public Combiner newCombiner(Key key) {
return new ComparableMaxCombiner();
}
@Override
public int getId() {
return AggregationsDataSerializerHook.COMPARABLE_MAX_COMBINER_FACTORY;
}
}
/**
* Maximum ReducerFactory for type {@link java.lang.Comparable}
*
* @param the key type
*/
@BinaryInterface
static final class ComparableMaxReducerFactory
extends AbstractAggregationReducerFactory {
@Override
public Reducer newReducer(Key key) {
return new ComparableMaxReducer();
}
@Override
public int getId() {
return AggregationsDataSerializerHook.COMPARABLE_MAX_REDUCER_FACTORY;
}
}
/**
* Maximum Combiner for type {@link java.lang.Comparable}
*/
private static final class ComparableMaxCombiner
extends Combiner {
private Comparable max;
@Override
public void combine(Comparable value) {
if (max == null || value.compareTo(max) > 0) {
max = value;
}
}
@Override
public Comparable finalizeChunk() {
Comparable value = max;
max = null;
return value;
}
}
/**
* Maximum Reducer for type {@link java.lang.Comparable}
*/
private static final class ComparableMaxReducer
extends Reducer {
private Comparable max;
@Override
public void reduce(Comparable value) {
if (max == null || value.compareTo(max) > 0) {
max = value;
}
}
@Override
public Comparable finalizeReduce() {
return max;
}
}
}