com.bestvike.collections.generic.Comparer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of linq Show documentation
Show all versions of linq Show documentation
LINQ to Objects for Java.
The newest version!
package com.bestvike.collections.generic;
import com.bestvike.CultureInfo;
import com.bestvike.IComparison;
import com.bestvike.linq.exception.ExceptionArgument;
import com.bestvike.linq.exception.ThrowHelper;
import java.text.Collator;
import java.util.Comparator;
/**
* Created by 许崇雷 on 2017-07-18.
*/
public final class Comparer implements Comparator {
private static final Comparer> DEFAULT = new Comparer<>(null);
private static final Comparer> DEFAULT_INVARIANT = new Comparer<>(CultureInfo.getInvariantCulture());
private static final Comparator FLOAT = new FloatComparer();
private static final Comparator DOUBLE = new DoubleComparer();
private final Collator collator;
private Comparer(Collator collator) {
this.collator = collator;
}
public static Comparator Default() {
//noinspection unchecked
return (Comparator) DEFAULT;
}
public static Comparator DefaultInvariant() {
//noinspection unchecked
return (Comparator) DEFAULT_INVARIANT;
}
public static Comparator Float() {
return FLOAT;
}
public static Comparator Double() {
return DOUBLE;
}
public static Comparator create(Collator collator) {
if (collator == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.collator);
return new Comparer<>(collator);
}
public static Comparator create(IComparison super T> comparison) {
if (comparison == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.comparison);
return new ComparisonComparer<>(comparison);
}
@Override
public int compare(T x, T y) {
if (x == y)
return 0;
if (x == null)
return -1;
if (y == null)
return 1;
if (x instanceof String && y instanceof String)
return (this.collator == null ? CultureInfo.getCurrentCulture() : this.collator).compare((String) x, (String) y);
if (x instanceof Comparable)
//noinspection unchecked
return ((Comparable) x).compareTo(y);
if (y instanceof Comparable)
//noinspection unchecked
return -((Comparable) y).compareTo(x);
ThrowHelper.throwImplementComparableException();
return 0;
}
private static final class FloatComparer implements Comparator {
@Override
public int compare(Float x, Float y) {
if (x != null) {
if (y != null)
return this.comparePrimitive(x, y);
return 1;
}
if (y != null)
return -1;
return 0;
}
private int comparePrimitive(float x, float y) {
if (x < y)
return -1;
if (x > y)
return 1;
if (x == y)
return 0;
// At least one of the values is NaN.
if (Float.isNaN(x))
return Float.isNaN(y) ? 0 : -1;
else // y is NaN.
return 1;
}
}
private static final class DoubleComparer implements Comparator {
@Override
public int compare(Double x, Double y) {
if (x != null) {
if (y != null)
return this.comparePrimitive(x, y);
return 1;
}
if (y != null)
return -1;
return 0;
}
private int comparePrimitive(double x, double y) {
if (x < y)
return -1;
if (x > y)
return 1;
if (x == y)
return 0;
// At least one of the values is NaN.
if (Double.isNaN(x))
return Double.isNaN(y) ? 0 : -1;
else // y is NaN.
return 1;
}
}
}