org.danekja.java.misc.serializable.SerializableComparator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jdk-serializable-functional Show documentation
Show all versions of jdk-serializable-functional Show documentation
A library containing Serializable versions of functional interfaces from
java.util.function package.
/*
*
* 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 super T> other) {
Objects.requireNonNull(other);
return (c1, c2) -> {
int res = compare(c1, c2);
return (res != 0) ? res : other.compare(c1, c2);
};
}
default SerializableComparator thenComparing(
SerializableFunction super T, ? extends U> keyExtractor,
SerializableComparator super U> keyComparator)
{
return thenComparing(comparing(keyExtractor, keyComparator));
}
default > SerializableComparator thenComparing(
SerializableFunction super T, ? extends U> keyExtractor)
{
return thenComparing(comparing(keyExtractor));
}
default SerializableComparator thenComparingInt(
SerializableToIntFunction super T> keyExtractor) {
return thenComparing(comparingInt(keyExtractor));
}
default SerializableComparator thenComparingLong(
SerializableToLongFunction super T> keyExtractor) {
return thenComparing(comparingLong(keyExtractor));
}
default SerializableComparator thenComparingDouble(
SerializableToDoubleFunction super T> 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 super T> comparator) {
return Comparator.nullsFirst(comparator)::compare;
}
public static SerializableComparator nullsLast(
SerializableComparator super T> comparator) {
return Comparator.nullsLast(comparator)::compare;
}
public static SerializableComparator comparing(
SerializableFunction super T, ? extends U> keyExtractor,
SerializableComparator super U> keyComparator)
{
Objects.requireNonNull(keyExtractor);
Objects.requireNonNull(keyComparator);
return
(c1, c2) -> keyComparator.compare(keyExtractor.apply(c1),
keyExtractor.apply(c2));
}
public static > SerializableComparator comparing(
SerializableFunction super T, ? extends U> keyExtractor)
{
Objects.requireNonNull(keyExtractor);
return (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}
public static SerializableComparator comparingInt(
SerializableToIntFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
}
public static SerializableComparator comparingLong(
SerializableToLongFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
}
public static SerializableComparator comparingDouble(
SerializableToDoubleFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy