com.google.gwt.emul.java.util.Comparator Maven / Gradle / Ivy
/*
* Copyright 2007 Google Inc.
*
* 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 java.util;
import static javaemul.internal.InternalPreconditions.checkNotNull;
import java.io.Serializable;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
/**
* An interface used a basis for implementing custom ordering. [Sun
* docs]
*
* @param the type to be compared.
*/
@FunctionalInterface
public interface Comparator {
int compare(T a, T b);
@Override
boolean equals(Object other);
default Comparator reversed() {
return new Comparators.ReversedComparator(this);
}
default Comparator thenComparing(Comparator super T> other) {
checkNotNull(other);
return (Comparator & Serializable) (a, b) -> {
int c = compare(a, b);
return (c != 0) ? c : other.compare(a, b);
};
}
default Comparator thenComparing(Function super T, ? extends U> keyExtractor,
Comparator super U> keyComparator) {
return thenComparing(comparing(keyExtractor, keyComparator));
}
default > Comparator thenComparing(
Function super T, ? extends U> keyExtractor) {
return thenComparing(comparing(keyExtractor));
}
default Comparator thenComparingInt(ToIntFunction super T> keyExtractor) {
return thenComparing(comparingInt(keyExtractor));
}
default Comparator thenComparingLong(ToLongFunction super T> keyExtractor) {
return thenComparing(comparingLong(keyExtractor));
}
default Comparator thenComparingDouble(ToDoubleFunction super T> keyExtractor) {
return thenComparing(comparingDouble(keyExtractor));
}
static Comparator comparing(Function super T, ? extends U> keyExtractor,
Comparator super U> keyComparator) {
checkNotNull(keyExtractor);
checkNotNull(keyComparator);
return (Comparator & Serializable) (a, b) ->
keyComparator.compare(keyExtractor.apply(a), keyExtractor.apply(b));
}
static > Comparator comparing(
Function super T, ? extends U> keyExtractor) {
return comparing(keyExtractor, naturalOrder());
}
static Comparator comparingDouble(ToDoubleFunction super T> keyExtractor) {
checkNotNull(keyExtractor);
return (Comparator & Serializable) (a, b) ->
Double.compare(keyExtractor.applyAsDouble(a), keyExtractor.applyAsDouble(b));
}
static Comparator comparingInt(ToIntFunction super T> keyExtractor) {
checkNotNull(keyExtractor);
return (Comparator & Serializable) (a, b) ->
Integer.compare(keyExtractor.applyAsInt(a), keyExtractor.applyAsInt(b));
}
static Comparator comparingLong(ToLongFunction super T> keyExtractor) {
checkNotNull(keyExtractor);
return (Comparator & Serializable) (a, b) ->
Long.compare(keyExtractor.applyAsLong(a), keyExtractor.applyAsLong(b));
}
static > Comparator naturalOrder() {
return Comparators.naturalOrder();
}
static Comparator nullsFirst(Comparator super T> comparator) {
return new Comparators.NullComparator<>(true, comparator);
}
static Comparator nullsLast(Comparator super T> comparator) {
return new Comparators.NullComparator<>(false, comparator);
}
static > Comparator reverseOrder() {
return Comparators.reverseNaturalOrder();
}
}