com.google.gwt.emul.java.util.Comparator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.servicemix.bundles.gwt-servlet
Show all versions of org.apache.servicemix.bundles.gwt-servlet
This OSGi bundle wraps ${pkgArtifactId} ${pkgVersion} jar file.
/*
* 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 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 keyExtractor,
Comparator keyComparator) {
return thenComparing(comparing(keyExtractor, keyComparator));
}
default > Comparator thenComparing(
Function keyExtractor) {
return thenComparing(comparing(keyExtractor));
}
default Comparator thenComparingInt(ToIntFunction keyExtractor) {
return thenComparing(comparingInt(keyExtractor));
}
default Comparator thenComparingLong(ToLongFunction keyExtractor) {
return thenComparing(comparingLong(keyExtractor));
}
default Comparator thenComparingDouble(ToDoubleFunction keyExtractor) {
return thenComparing(comparingDouble(keyExtractor));
}
static Comparator comparing(Function keyExtractor,
Comparator keyComparator) {
checkNotNull(keyExtractor);
checkNotNull(keyComparator);
return (Comparator & Serializable) (a, b) ->
keyComparator.compare(keyExtractor.apply(a), keyExtractor.apply(b));
}
static > Comparator comparing(
Function keyExtractor) {
return comparing(keyExtractor, naturalOrder());
}
static Comparator comparingDouble(ToDoubleFunction keyExtractor) {
checkNotNull(keyExtractor);
return (Comparator & Serializable) (a, b) ->
Double.compare(keyExtractor.applyAsDouble(a), keyExtractor.applyAsDouble(b));
}
static Comparator comparingInt(ToIntFunction keyExtractor) {
checkNotNull(keyExtractor);
return (Comparator & Serializable) (a, b) ->
Integer.compare(keyExtractor.applyAsInt(a), keyExtractor.applyAsInt(b));
}
static Comparator comparingLong(ToLongFunction 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 comparator) {
return new Comparators.NullComparator<>(true, comparator);
}
static Comparator nullsLast(Comparator comparator) {
return new Comparators.NullComparator<>(false, comparator);
}
static > Comparator reverseOrder() {
return Comparators.reverseNaturalOrder();
}
}