com.annimon.stream.Comparators Maven / Gradle / Ivy
/*
* Copyright (C) 2017 HaiYang Li
*
* 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 com.annimon.stream;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import com.annimon.stream.function.Function;
import com.annimon.stream.function.ToDoubleFunction;
import com.annimon.stream.function.ToIntFunction;
import com.annimon.stream.function.ToLongFunction;
/**
* Factory utility class for Comparator.
*
*
* @author haiyang li
*
* @since 0.8.5
*/
@SuppressWarnings("unchecked")
public final class Comparators {
@SuppressWarnings("rawtypes")
private static final Comparator NULL_FIRST_COMPARATOR = new Comparator() {
@Override
public int compare(final Comparable a, final Comparable b) {
return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : a.compareTo(b));
}
};
@SuppressWarnings("rawtypes")
private static final Comparator NULL_LAST_COMPARATOR = new Comparator() {
@Override
public int compare(final Comparable a, final Comparable b) {
return a == null ? (b == null ? 0 : 1) : (b == null ? -1 : a.compareTo(b));
}
};
@SuppressWarnings("rawtypes")
private static final Comparator NATURAL_ORDER = NULL_FIRST_COMPARATOR;
@SuppressWarnings("rawtypes")
private static final Comparator REVERSED_ORDER = Collections.reverseOrder(NATURAL_ORDER);
@SuppressWarnings("rawtypes")
static final Comparator OBJ_COMPARATOR = NATURAL_ORDER;
private Comparators() {
// Singleton
}
public static Comparator naturalOrder() {
return NATURAL_ORDER;
}
public static Comparator reversedOrder() {
return REVERSED_ORDER;
}
public static Comparator reversedOrder(final Comparator cmp) {
if (cmp == null || cmp == NATURAL_ORDER) {
return REVERSED_ORDER;
} else if (cmp == REVERSED_ORDER) {
return NATURAL_ORDER;
}
return Collections.reverseOrder(cmp);
}
public static Comparator nullsFirst() {
return NULL_FIRST_COMPARATOR;
}
public static Comparator nullsFirst(final Comparator cmp) {
if (cmp == null || cmp == NULL_FIRST_COMPARATOR) {
return NULL_FIRST_COMPARATOR;
}
return new Comparator() {
@Override
public int compare(T a, T b) {
return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : cmp.compare(a, b));
}
};
}
public static Comparator nullsLast() {
return NULL_LAST_COMPARATOR;
}
public static Comparator nullsLast(final Comparator cmp) {
if (cmp == null || cmp == NULL_LAST_COMPARATOR) {
return NULL_LAST_COMPARATOR;
}
return new Comparator() {
@Override
public int compare(T a, T b) {
return a == null ? (b == null ? 0 : 1) : (b == null ? -1 : cmp.compare(a, b));
}
};
}
@SuppressWarnings("rawtypes")
public static Comparator comparingBy(final Function super T, ? extends U> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Objects.compare(keyExtractor.apply(a), keyExtractor.apply(b));
}
};
}
@SuppressWarnings("rawtypes")
public static Comparator reversedComparingBy(final Function super T, ? extends U> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Objects.compare(keyExtractor.apply(b), keyExtractor.apply(a));
}
};
}
public static Comparator comparingBy(final Function super T, ? extends U> keyExtractor, final Comparator super U> keyComparator) {
Objects.requireNonNull(keyExtractor);
Objects.requireNonNull(keyComparator);
return new Comparator() {
@Override
public int compare(T a, T b) {
return keyComparator.compare(keyExtractor.apply(a), keyExtractor.apply(b));
}
};
}
public static Comparator comparingInt(final ToIntFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Integer.compare(keyExtractor.applyAsInt(a), keyExtractor.applyAsInt(b));
}
};
}
public static Comparator comparingLong(final ToLongFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Long.compare(keyExtractor.applyAsLong(a), keyExtractor.applyAsLong(b));
}
};
}
public static Comparator comparingDouble(final ToDoubleFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Double.compare(keyExtractor.applyAsDouble(a), keyExtractor.applyAsDouble(b));
}
};
}
@SuppressWarnings("rawtypes")
public static Comparator> comparingByKey() {
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return Objects.compare(a.getKey(), b.getKey());
}
};
}
@SuppressWarnings("rawtypes")
public static Comparator> reversedComparingByKey() {
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return Objects.compare(b.getKey(), a.getKey());
}
};
}
@SuppressWarnings("rawtypes")
public static Comparator> comparingByValue() {
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return Objects.compare(a.getValue(), b.getValue());
}
};
}
@SuppressWarnings("rawtypes")
public static Comparator> reversedComparingByValue() {
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return Objects.compare(b.getValue(), a.getValue());
}
};
}
public static Comparator> comparingByKey(final Comparator super K> cmp) {
Objects.requireNonNull(cmp);
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return cmp.compare(a.getKey(), b.getKey());
}
};
}
public static Comparator> comparingByValue(final Comparator super V> cmp) {
Objects.requireNonNull(cmp);
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return cmp.compare(a.getValue(), b.getValue());
}
};
}
public static Comparator> reversedComparingByKey(final Comparator super K> cmp) {
Objects.requireNonNull(cmp);
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return cmp.compare(b.getKey(), a.getKey());
}
};
}
public static Comparator> reversedComparingByValue(final Comparator super V> cmp) {
Objects.requireNonNull(cmp);
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return cmp.compare(b.getValue(), a.getValue());
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy