Java Code Examples for com.google.common.collect.ComparisonChain#result()

The following examples show how to use com.google.common.collect.ComparisonChain#result() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DalListMerger.java    From das with Apache License 2.0 5 votes vote down vote up
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();
	};
}
 
Example 2
Source File: FileComparator.java    From kafka-connect-spooldir with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 3
Source File: FrameworkUtils.java    From data-polygamy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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();
}
 
Example 4
Source File: InMemoryDao.java    From metron with Apache License 2.0 5 votes vote down vote up
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();
  };
}
 
Example 5
Source File: RowSetComparator.java    From Oceanus with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 6
Source File: AndroidResourceIndexEntry.java    From buck with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 7
Source File: RDotTxtEntry.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 8
Source File: MemoryExperimentsStore.java    From alchemy with MIT License 4 votes vote down vote up
@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();
}