Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.landawn.abacus.util.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.landawn.abacus.util;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
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 abstract class Comparators {
@SuppressWarnings("rawtypes")
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")
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")
static final Comparator NATURAL_ORDER = NULL_FIRST_COMPARATOR;
@SuppressWarnings("rawtypes")
static final Comparator REVERSED_ORDER = Collections.reverseOrder(NATURAL_ORDER);
static final Comparator COMPARING_IGNORE_CASE = new Comparator() {
@Override
public int compare(String a, String b) {
return N.compareIgnoreCase(a, b);
}
};
static final Comparator COMPARING_BY_LENGTH = new Comparator() {
@Override
public int compare(CharSequence a, CharSequence b) {
return (a == null ? 0 : a.length()) - (b == null ? 0 : b.length());
}
};
@SuppressWarnings("rawtypes")
static final Comparator COMPARING_BY_SIZE = new Comparator() {
@Override
public int compare(Collection a, Collection b) {
return (a == null ? 0 : a.size()) - (b == null ? 0 : b.size());
}
};
@SuppressWarnings("rawtypes")
static final Comparator COMPARING_BY_MAP_SIZE = new Comparator() {
@Override
public int compare(Map a, Map b) {
return (a == null ? 0 : a.size()) - (b == null ? 0 : b.size());
}
};
Comparators() {
// Singleton
}
public static final Comparator BOOLEAN_ARRAY_COMPARATOR = new Comparator() {
@Override
public int compare(final boolean[] a, final boolean[] b) {
final int lenA = N.len(a);
final int lenB = N.len(b);
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
if (a[i] != b[i]) {
return a[i] ? 1 : -1;
}
}
return N.compare(lenA, lenB);
}
};
public static final Comparator CHAR_ARRAY_COMPARATOR = new Comparator() {
@Override
public int compare(final char[] a, final char[] b) {
final int lenA = N.len(a);
final int lenB = N.len(b);
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
if (a[i] != b[i]) {
return a[i] > b[i] ? 1 : -1;
}
}
return N.compare(lenA, lenB);
}
};
public static final Comparator BYTE_ARRAY_COMPARATOR = new Comparator() {
@Override
public int compare(final byte[] a, final byte[] b) {
final int lenA = N.len(a);
final int lenB = N.len(b);
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
if (a[i] != b[i]) {
return a[i] > b[i] ? 1 : -1;
}
}
return N.compare(lenA, lenB);
}
};
public static final Comparator SHORT_ARRAY_COMPARATOR = new Comparator() {
@Override
public int compare(final short[] a, final short[] b) {
final int lenA = N.len(a);
final int lenB = N.len(b);
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
if (a[i] != b[i]) {
return a[i] > b[i] ? 1 : -1;
}
}
return N.compare(lenA, lenB);
}
};
public static final Comparator INT_ARRAY_COMPARATOR = new Comparator() {
@Override
public int compare(final int[] a, final int[] b) {
final int lenA = N.len(a);
final int lenB = N.len(b);
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
if (a[i] != b[i]) {
return a[i] > b[i] ? 1 : -1;
}
}
return N.compare(lenA, lenB);
}
};
public static final Comparator LONG_ARRAY_COMPARATOR = new Comparator() {
@Override
public int compare(final long[] a, final long[] b) {
final int lenA = N.len(a);
final int lenB = N.len(b);
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
if (a[i] != b[i]) {
return a[i] > b[i] ? 1 : -1;
}
}
return N.compare(lenA, lenB);
}
};
public static final Comparator FLOAT_ARRAY_COMPARATOR = new Comparator() {
@Override
public int compare(final float[] a, final float[] b) {
final int lenA = N.len(a);
final int lenB = N.len(b);
int result = 0;
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
result = Float.compare(a[i], b[i]);
if (result != 0) {
return result;
}
}
return N.compare(lenA, lenB);
}
};
public static final Comparator DOUBLE_ARRAY_COMPARATOR = new Comparator() {
@Override
public int compare(final double[] a, final double[] b) {
final int lenA = N.len(a);
final int lenB = N.len(b);
int result = 0;
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
result = Double.compare(a[i], b[i]);
if (result != 0) {
return result;
}
}
return N.compare(lenA, lenB);
}
};
public static final Comparator OBJECT_ARRAY_COMPARATOR = new Comparator() {
@Override
public int compare(final Object[] a, final Object[] b) {
final int lenA = N.len(a);
final int lenB = N.len(b);
int result = 0;
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
result = NATURAL_ORDER.compare(a[i], b[i]);
if (result != 0) {
return result;
}
}
return N.compare(lenA, lenB);
}
};
@SuppressWarnings("rawtypes")
public static final Comparator COLLECTION_COMPARATOR = new Comparator() {
@Override
public int compare(final Collection a, final Collection b) {
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(b) ? 0 : -1;
} else if (N.isNullOrEmpty(b)) {
return 1;
}
final Iterator iterA = a.iterator();
final Iterator iterB = b.iterator();
final int lenA = N.size(a);
final int lenB = N.size(b);
int result = 0;
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
result = NATURAL_ORDER.compare(iterA.next(), iterB.next());
if (result != 0) {
return result;
}
}
return N.compare(lenA, lenB);
}
};
/**
*
* @param
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Comparator naturalOrder() {
return NATURAL_ORDER;
}
/**
*
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static Comparator reversedOrder() {
return REVERSED_ORDER;
}
/**
*
* @param
* @param cmp
* @return
*/
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);
}
/**
*
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static Comparator nullsFirst() {
return NULL_FIRST_COMPARATOR;
}
/**
*
* @param
* @param cmp
* @return
*/
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));
}
};
}
/**
*
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static Comparator nullsLast() {
return NULL_LAST_COMPARATOR;
}
/**
*
* @param
* @param cmp
* @return
*/
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));
}
};
}
/**
*
* @param
* @param
* @param keyMapper
* @return
*/
public static > Comparator comparingBy(final Function super T, ? extends U> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return N.compare(keyMapper.apply(a), keyMapper.apply(b));
}
};
}
/**
* Reversed comparing by.
*
* @param
* @param
* @param keyMapper
* @return
*/
public static > Comparator reversedComparingBy(final Function super T, ? extends U> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return N.compare(keyMapper.apply(b), keyMapper.apply(a));
}
};
}
/**
*
* @param
* @param
* @param keyMapper
* @param keyComparator
* @return
*/
public static Comparator comparingBy(final Function super T, ? extends U> keyMapper, final Comparator super U> keyComparator) {
N.checkArgNotNull(keyMapper);
N.checkArgNotNull(keyComparator);
return new Comparator() {
@Override
public int compare(T a, T b) {
return keyComparator.compare(keyMapper.apply(a), keyMapper.apply(b));
}
};
}
/**
*
* @param
* @param keyMapper
* @return
*/
public static Comparator comparingBoolean(final ToBooleanFunction super T> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Boolean.compare(keyMapper.applyAsBoolean(a), keyMapper.applyAsBoolean(b));
}
};
}
/**
*
* @param
* @param keyMapper
* @return
*/
public static Comparator comparingChar(final ToCharFunction super T> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Character.compare(keyMapper.applyAsChar(a), keyMapper.applyAsChar(b));
}
};
}
/**
*
* @param
* @param keyMapper
* @return
*/
public static Comparator comparingByte(final ToByteFunction super T> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Byte.compare(keyMapper.applyAsByte(a), keyMapper.applyAsByte(b));
}
};
}
/**
*
* @param
* @param keyMapper
* @return
*/
public static Comparator comparingShort(final ToShortFunction super T> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Short.compare(keyMapper.applyAsShort(a), keyMapper.applyAsShort(b));
}
};
}
/**
*
* @param
* @param keyMapper
* @return
*/
public static Comparator comparingInt(final ToIntFunction super T> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Integer.compare(keyMapper.applyAsInt(a), keyMapper.applyAsInt(b));
}
};
}
/**
*
* @param
* @param keyMapper
* @return
*/
public static Comparator comparingLong(final ToLongFunction super T> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Long.compare(keyMapper.applyAsLong(a), keyMapper.applyAsLong(b));
}
};
}
/**
*
* @param
* @param keyMapper
* @return
*/
public static Comparator comparingFloat(final ToFloatFunction super T> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Float.compare(keyMapper.applyAsFloat(a), keyMapper.applyAsFloat(b));
}
};
}
/**
*
* @param
* @param keyMapper
* @return
*/
public static Comparator comparingDouble(final ToDoubleFunction super T> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return Double.compare(keyMapper.applyAsDouble(a), keyMapper.applyAsDouble(b));
}
};
}
/**
* Comparing ignore case.
*
* @return
*/
public static Comparator comparingIgnoreCase() {
return COMPARING_IGNORE_CASE;
}
/**
* Comparing ignore case.
*
* @param
* @param keyMapper
* @return
*/
public static Comparator comparingIgnoreCase(final Function super T, String> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Comparator() {
@Override
public int compare(T a, T b) {
return N.compareIgnoreCase(keyMapper.apply(a), keyMapper.apply(b));
}
};
}
/**
* Comparing by key.
*
* @param the key type
* @param the value type
* @return
*/
public static , V> Comparator> comparingByKey() {
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return N.compare(a.getKey(), b.getKey());
}
};
}
/**
* Reversed comparing by key.
*
* @param the key type
* @param the value type
* @return
*/
public static , V> Comparator> reversedComparingByKey() {
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return N.compare(b.getKey(), a.getKey());
}
};
}
/**
* Comparing by value.
*
* @param the key type
* @param the value type
* @return
*/
public static > Comparator> comparingByValue() {
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return N.compare(a.getValue(), b.getValue());
}
};
}
/**
* Reversed comparing by value.
*
* @param the key type
* @param the value type
* @return
*/
public static > Comparator> reversedComparingByValue() {
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return N.compare(b.getValue(), a.getValue());
}
};
}
/**
* Comparing by key.
*
* @param the key type
* @param the value type
* @param cmp
* @return
*/
public static Comparator> comparingByKey(final Comparator super K> cmp) {
N.checkArgNotNull(cmp);
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return cmp.compare(a.getKey(), b.getKey());
}
};
}
/**
* Comparing by value.
*
* @param the key type
* @param the value type
* @param cmp
* @return
*/
public static Comparator> comparingByValue(final Comparator super V> cmp) {
N.checkArgNotNull(cmp);
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return cmp.compare(a.getValue(), b.getValue());
}
};
}
/**
* Reversed comparing by key.
*
* @param the key type
* @param the value type
* @param cmp
* @return
*/
@Deprecated
public static Comparator> reversedComparingByKey(final Comparator super K> cmp) {
N.checkArgNotNull(cmp);
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return cmp.compare(b.getKey(), a.getKey());
}
};
}
/**
* Reversed comparing by value.
*
* @param the key type
* @param the value type
* @param cmp
* @return
*/
@Deprecated
public static Comparator> reversedComparingByValue(final Comparator super V> cmp) {
N.checkArgNotNull(cmp);
return new Comparator>() {
@Override
public int compare(Map.Entry a, Map.Entry b) {
return cmp.compare(b.getValue(), a.getValue());
}
};
}
/**
* Comparing by length.
*
* @param
* @return
*/
public static Comparator comparingByLength() {
return (Comparator) COMPARING_BY_LENGTH;
}
/**
* Comparing by size.
*
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static Comparator comparingBySize() {
return (Comparator) COMPARING_BY_SIZE;
}
/**
* Comparing by sizee.
*
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static Comparator comparingByMapSize() {
return (Comparator) COMPARING_BY_MAP_SIZE;
}
public static Comparator comparingArray(final Comparator cmp) {
N.checkArgNotNull(cmp);
return new Comparator() {
@Override
public int compare(final T[] a, final T[] b) {
final int lenA = N.len(a);
final int lenB = N.len(b);
int result = 0;
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
result = cmp.compare(a[i], b[i]);
if (result != 0) {
return result;
}
}
return N.compare(lenA, lenB);
}
};
}
public static > Comparator comparingCollection(final Comparator cmp) {
N.checkArgNotNull(cmp);
return new Comparator() {
@Override
public int compare(final C a, final C b) {
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(b) ? 0 : -1;
} else if (N.isNullOrEmpty(b)) {
return 1;
}
final Iterator iterA = a.iterator();
final Iterator iterB = b.iterator();
final int lenA = N.size(a);
final int lenB = N.size(b);
int result = 0;
for (int i = 0, minLen = N.min(lenA, lenB); i < minLen; i++) {
result = cmp.compare(iterA.next(), iterB.next());
if (result != 0) {
return result;
}
}
return N.compare(lenA, lenB);
}
};
}
}