下面列出了com.google.common.collect.ComparisonChain#result ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public DalListMerger(List<ColumnOrder> sorters) {
if(sorters == null || sorters.isEmpty()) {
return;
}
this.comparator = (o1, o2) ->{
final Comparable least = (i) -> Integer.MIN_VALUE;
ComparisonChain comparisonChain = ComparisonChain.start();
for(ColumnOrder sorter: sorters){
try {
Object v1 = null;
Object v2 = null;
String colName = sorter.getColumn().getColumn().getColumnName();
if (o1 instanceof Map || o2 instanceof Map) {//Map entity
v1 = Optional.of(((Map)o1).get(colName)).orElse(least);
v2 = Optional.of(((Map)o2).get(colName)).orElse(least);
} else {//POJO entity
Field f = o1.getClass().getDeclaredField(Introspector.decapitalize(colName));
f.setAccessible(true);
v1 = Optional.of(f.get(o1)).orElse(least);
v2 = Optional.of(f.get(o2)).orElse(least);
}
if(sorter.isAsc()) {
comparisonChain = comparisonChain.compare((Comparable)v1, (Comparable)v2);
} else {
comparisonChain = comparisonChain.compare((Comparable)v2, (Comparable)v1);
}
}catch (NoSuchFieldException |IllegalAccessException e){
throw new RuntimeException(e);
}
}
return comparisonChain.result();
};
}
@Override
public int compare(File f1, File f2) {
ComparisonChain chain = ComparisonChain.start();
for (AbstractSourceConnectorConfig.FileAttribute fileAttribute : this.attributes) {
switch (fileAttribute) {
case NameAsc:
chain = chain.compare(f1.getName(), f2.getName());
break;
case NameDesc:
chain = chain.compare(f2.getName(), f1.getName());
break;
case LengthAsc: // We prefer larger files first.
chain = chain.compare(f1.length(), f2.length());
break;
case LengthDesc: // We prefer larger files first.
chain = chain.compare(f2.length(), f1.length());
break;
case LastModifiedAsc:
chain = chain.compare(f1.lastModified(), f2.lastModified());
break;
case LastModifiedDesc:
chain = chain.compare(f2.lastModified(), f1.lastModified());
break;
default:
throw new UnsupportedOperationException(
String.format("%s is not a supported FileAttribute.", fileAttribute)
);
}
}
return chain.result();
}
@Override
public int compareTo(FloatArrayWritable arg0) {
float[] thatVal = arg0.get();
ComparisonChain ch = ComparisonChain.start();
// assuming they have the same size
for (int i = 0; i < this.values.length; i++)
ch = ch.compare(this.values[i], thatVal[i]);
return ch.result();
}
private static Comparator<SearchResult> sorted(final List<SortField> fields) {
return (o1, o2) -> {
ComparisonChain chain = ComparisonChain.start();
for(SortField field : fields) {
Comparable f1 = (Comparable) o1.getSource().get(field.getField());
Comparable f2 = (Comparable) o2.getSource().get(field.getField());
chain = chain.compare(f1, f2, new ComparableComparator(field.getSortOrder()));
}
return chain.result();
};
}
@Override
public int compare(RowSet o1, RowSet o2) {
ComparisonChain chain = ComparisonChain.start();
try {
for (int i = 0; i < sortedColumns.length; i++) {
TableColumn column = sortedColumns[i];
OrderByType orderByType = column.getOrderByType();
Object target1 = o1.getObject(column.getResultIndex());
Object target2 = o2.getObject(column.getResultIndex());
if (target1 == null) {// 将null值转化为string进行处理
target1 = "";
if (target2 != null) {
target2 = target2.toString();
} else {
target2 = "";
}
} else if (target2 == null) {
target2 = "";
target1 = target1.toString();
}
if (orderByType == null || orderByType == OrderByType.ASC) {
chain=chain.compare((Comparable<?>) target1, (Comparable<?>) target2);
} else {
chain=chain.compare((Comparable<?>) target2, (Comparable<?>) target1);
}
}
} catch (Exception e) {
throw new ShardException("compare error!row=" + this, e);
}
return chain.result();
}
@Override
public int compareTo(AndroidResourceIndexEntry that) {
if (this == that) {
return 0;
}
ComparisonChain comparisonChain =
ComparisonChain.start()
.compare(this.getType(), that.getType())
.compare(this.getName(), that.getName());
return comparisonChain.result();
}
/**
* A collection of Resources should be sorted such that Resources of the same type should be
* grouped together, and should be alphabetized within that group.
*/
@Override
public int compareTo(RDotTxtEntry that) {
if (this == that) {
return 0;
}
ComparisonChain comparisonChain =
ComparisonChain.start()
.compare(this.type, that.type)
.compare(this.parent, that.parent)
.compare(this.name, that.name);
return comparisonChain.result();
}
@Override
public int compare(Experiment a, Experiment b) {
ComparisonChain chain = ComparisonChain.start();
for (Entry<Field, Direction> entry : ordering.getFields().entrySet()) {
final Ordering.Field field = entry.getKey();
final Experiment lhs = entry.getValue() == Ordering.Direction.ASCENDING ? a : b;
final Experiment rhs = entry.getValue() == Ordering.Direction.ASCENDING ? b : a;
switch (field) {
case NAME:
chain = chain.compare(lhs.getName(), rhs.getName());
break;
case DESCRIPTION:
chain = chain.compare(lhs.getDescription(), rhs.getDescription());
break;
case ACTIVE:
chain = chain.compare(lhs.isActive(), rhs.isActive());
break;
case CREATED:
chain = chain.compare(lhs.getCreated(), rhs.getCreated());
break;
case MODIFIED:
chain = chain.compare(lhs.getModified(), rhs.getModified());
break;
case ACTIVATED:
chain = chain.compare(lhs.getActivated(), rhs.getActivated());
break;
case DEACTIVATED:
chain = chain.compare(lhs.getDeactivated(), rhs.getDeactivated());
break;
default:
throw new IllegalArgumentException(
String.format(
"Unsupported ordering field: %s (%s)",
field,
field.getName()
)
);
}
}
return chain.result();
}