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

org.danekja.java.misc.serializable.SerializableComparator Maven / Gradle / Ivy

Go to download

A library containing Serializable versions of functional interfaces from java.util.function package.

There is a newer version: 1.9.0
Show newest version
/*
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Jakub Danek
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 *
 *  Please visit https://github.com/danekja/jdk-function-serializable if you need additional information or have any
 *  questions.
 *
 */

package org.danekja.java.misc.serializable;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Objects;

import org.danekja.java.util.function.serializable.SerializableFunction;
import org.danekja.java.util.function.serializable.SerializableToDoubleFunction;
import org.danekja.java.util.function.serializable.SerializableToIntFunction;
import org.danekja.java.util.function.serializable.SerializableToLongFunction;

/**
 * Serializable version of {@link Comparator}.
 *
 * @author Emond Papegaaij
 */
@FunctionalInterface
public interface SerializableComparator extends Comparator, Serializable {
	default SerializableComparator reversed() {
        return Collections.reverseOrder(this)::compare;
    }

    default SerializableComparator thenComparing(SerializableComparator other) {
        Objects.requireNonNull(other);
        return (c1, c2) -> {
            int res = compare(c1, c2);
            return (res != 0) ? res : other.compare(c1, c2);
        };
    }

    default  SerializableComparator thenComparing(
    		SerializableFunction keyExtractor,
            SerializableComparator keyComparator)
    {
        return thenComparing(comparing(keyExtractor, keyComparator));
    }

    default > SerializableComparator thenComparing(
    		SerializableFunction keyExtractor)
    {
        return thenComparing(comparing(keyExtractor));
    }

    default SerializableComparator thenComparingInt(
    		SerializableToIntFunction keyExtractor) {
        return thenComparing(comparingInt(keyExtractor));
    }

    default SerializableComparator thenComparingLong(
    		SerializableToLongFunction keyExtractor) {
        return thenComparing(comparingLong(keyExtractor));
    }

    default SerializableComparator thenComparingDouble(
    		SerializableToDoubleFunction keyExtractor) {
        return thenComparing(comparingDouble(keyExtractor));
    }

    public static > SerializableComparator reverseOrder() {
        return Collections.reverseOrder()::compare;
    }

    public static > SerializableComparator naturalOrder() {
        return Comparator.naturalOrder()::compare;
    }

    public static  SerializableComparator nullsFirst(
    		SerializableComparator comparator) {
        return Comparator.nullsFirst(comparator)::compare;
    }

    public static  SerializableComparator nullsLast(
    		SerializableComparator comparator) {
        return Comparator.nullsLast(comparator)::compare;
    }

    public static  SerializableComparator comparing(
    		SerializableFunction keyExtractor,
            SerializableComparator keyComparator)
    {
        Objects.requireNonNull(keyExtractor);
        Objects.requireNonNull(keyComparator);
        return 
            (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1),
                                              keyExtractor.apply(c2));
    }

    public static > SerializableComparator comparing(
    		SerializableFunction keyExtractor)
    {
        Objects.requireNonNull(keyExtractor);
        return (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
    }

    public static  SerializableComparator comparingInt(
    		SerializableToIntFunction keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
    }

    public static  SerializableComparator comparingLong(
    		SerializableToLongFunction keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
    }

    public static SerializableComparator comparingDouble(
    		SerializableToDoubleFunction keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
    }
}