com.github.xphsc.bean.Comparators Maven / Gradle / Ivy
package com.github.xphsc.bean;
import com.github.xphsc.bean.comparator.*;
import java.util.Collection;
import java.util.Comparator;
/**
* Created by ${huipei.x} on 2017-8-7.
*/
public class Comparators {
public static final Comparator NATURAL_COMPARATOR = ComparableComparator.comparableComparator();
private Comparators() {
}
public static > Comparator naturalComparator() {
return NATURAL_COMPARATOR;
}
public static Comparator chainedComparator(Comparator... comparators) {
ComparatorChain chain = new ComparatorChain();
Comparator[] arr$ = comparators;
int len$ = comparators.length;
for(int i$ = 0; i$ < len$; ++i$) {
Comparator comparator = arr$[i$];
if(comparator == null) {
throw new NullPointerException("Comparator cannot be null");
}
chain.addComparator(comparator);
}
return chain;
}
public static Comparator chainedComparator(Collection> comparators) {
return chainedComparator((Comparator[])((Comparator[])comparators.toArray(new Comparator[comparators.size()])));
}
public static Comparator reversedComparator(Comparator comparator) {
return new ReverseComparator(comparator);
}
public static Comparator booleanComparator(boolean trueFirst) {
return BooleanComparator.booleanComparator(trueFirst);
}
public static Comparator nullLowComparator(Comparator comparator) {
if(comparator == null) {
comparator = NATURAL_COMPARATOR;
}
return new NullComparator(comparator, false);
}
public static Comparator nullHighComparator(Comparator comparator) {
if(comparator == null) {
comparator = NATURAL_COMPARATOR;
}
return new NullComparator(comparator, true);
}
public static Comparator transformedComparator(Comparator comparator, Transformer super I, ? extends O> transformer) {
if(comparator == null) {
comparator = NATURAL_COMPARATOR;
}
return new TransformingComparator(transformer, comparator);
}
public static E min(E o1, E o2, Comparator comparator) {
if(comparator == null) {
comparator = NATURAL_COMPARATOR;
}
int c = comparator.compare(o1, o2);
return c < 0?o1:o2;
}
public static E max(E o1, E o2, Comparator comparator) {
if(comparator == null) {
comparator = NATURAL_COMPARATOR;
}
int c = comparator.compare(o1, o2);
return c > 0?o1:o2;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy