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

The following examples show how to use com.google.common.collect.ComparisonChain#compare() . 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: SingularityTaskHistoryQuery.java    From Singularity with Apache License 2.0 6 votes vote down vote up
public Comparator<SingularityTaskIdHistory> getComparator() {
  final OrderDirection localOrderDirection = orderDirection.orElse(OrderDirection.DESC);

  return new Comparator<SingularityTaskIdHistory>() {

    @Override
    public int compare(SingularityTaskIdHistory o1, SingularityTaskIdHistory o2) {
      ComparisonChain chain = ComparisonChain.start();

      if (localOrderDirection == OrderDirection.ASC) {
        chain = chain.compare(o1.getUpdatedAt(), o2.getUpdatedAt());
      } else {
        chain = chain.compare(o2.getUpdatedAt(), o1.getUpdatedAt());
      }

      return chain
        .compare(o1.getTaskId().getRequestId(), o2.getTaskId().getRequestId())
        .result();
    }
  };
}
 
Example 2
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 3
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 4
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 5
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 6
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 7
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();
}