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

com.jparams.store.index.reducer.MinReducer Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
package com.jparams.store.index.reducer;

import java.util.Comparator;
import java.util.function.Function;

/**
 * Reduces all elements for a key retaining the min value
 *
 * @param  key type
 * @param  value type
 */
public class MinReducer extends ComparingReducer
{
    public > MinReducer(final Function valueProvider, final boolean nullGreater)
    {
        super(valueProvider, nullGreater);
    }

    public  MinReducer(final Function valueProvider, final Comparator comparator, final boolean nullGreater)
    {
        super(valueProvider, comparator, nullGreater);
    }

    @Override
    int compare(final Object value1, final Object value2, final Comparator comparator, final boolean nullGreater)
    {
        if (value1 == value2)
        {
            return 0;
        }

        if (value1 == null)
        {
            return nullGreater ? -1 : 1;
        }

        if (value2 == null)
        {
            return nullGreater ? 1 : -1;
        }

        return comparator.compare(value2, value1);
    }
}