com.landawn.abacus.util.Comparators Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android-se-jdk7 Show documentation
Show all versions of abacus-android-se-jdk7 Show documentation
A general programming library in Java
/*
* 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.landawn.abacus.util;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.ToBooleanFunction;
import com.landawn.abacus.util.function.ToByteFunction;
import com.landawn.abacus.util.function.ToCharFunction;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToFloatFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.function.ToShortFunction;
/**
*
* Factory utility class for Comparator.
*
* @author haiyangl
*
*/
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;
static final Comparator COMPARING_IGNORE_CASE = new Comparator() {
@Override
public int compare(String a, String b) {
return N.compareIgnoreCase(a, b);
}
};
private Comparators() {
// Singleton
}
@SuppressWarnings("unchecked")
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) {
N.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return N.compare(keyExtractor.apply(a), keyExtractor.apply(b));
}
};
}
@SuppressWarnings("rawtypes")
public static Comparator reversedComparingBy(final Function super T, ? extends U> keyExtractor) {
N.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return N.compare(keyExtractor.apply(b), keyExtractor.apply(a));
}
};
}
public static Comparator comparingBy(final Function super T, ? extends U> keyExtractor, final Comparator super U> keyComparator) {
N.requireNonNull(keyExtractor);
N.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 comparingBoolean(final ToBooleanFunction super T> keyExtractor) {
N.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Boolean.compare(keyExtractor.applyAsBoolean(a), keyExtractor.applyAsBoolean(b));
}
};
}
public static Comparator comparingChar(final ToCharFunction super T> keyExtractor) {
N.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Character.compare(keyExtractor.applyAsChar(a), keyExtractor.applyAsChar(b));
}
};
}
public static Comparator comparingByte(final ToByteFunction super T> keyExtractor) {
N.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Byte.compare(keyExtractor.applyAsByte(a), keyExtractor.applyAsByte(b));
}
};
}
public static Comparator comparingShort(final ToShortFunction super T> keyExtractor) {
N.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Short.compare(keyExtractor.applyAsShort(a), keyExtractor.applyAsShort(b));
}
};
}
public static Comparator comparingInt(final ToIntFunction super T> keyExtractor) {
N.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) {
N.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 comparingFloat(final ToFloatFunction super T> keyExtractor) {
N.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Float.compare(keyExtractor.applyAsFloat(a), keyExtractor.applyAsFloat(b));
}
};
}
public static Comparator comparingDouble(final ToDoubleFunction super T> keyExtractor) {
N.requireNonNull(keyExtractor);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Double.compare(keyExtractor.applyAsDouble(a), keyExtractor.applyAsDouble(b));
}
};
}
public static Comparator comparingIgnoreCase() {
return COMPARING_IGNORE_CASE;
}
@SuppressWarnings("rawtypes")
public static Comparator> comparingByKey() {
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return N.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 N.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 N.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 N.compare(b.getValue(), a.getValue());
}
};
}
public static Comparator> comparingByKey(final Comparator super K> cmp) {
N.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) {
N.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) {
N.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) {
N.requireNonNull(cmp);
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return cmp.compare(b.getValue(), a.getValue());
}
};
}
}