Java Code Examples for com.google.common.collect.Ordering#compound()

The following examples show how to use com.google.common.collect.Ordering#compound() . 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: QueryHelper.java    From swellrt with Apache License 2.0 6 votes vote down vote up
/**
 * Computes ordering for the search results. If none are specified - then
 * returns the default ordering. The resulting ordering is always compounded
 * with ordering by wave id for stability.
 */
public static Ordering<WaveViewData> computeSorter(
    Map<TokenQueryType, Set<String>> queryParams) {
  Ordering<WaveViewData> ordering = null;
  Set<String> orderBySet = queryParams.get(TokenQueryType.ORDERBY);
  if (orderBySet != null) {
    for (String orderBy : orderBySet) {
      QueryHelper.OrderByValueType orderingType =
          QueryHelper.OrderByValueType.fromToken(orderBy);
      if (ordering == null) {
        // Primary ordering.
        ordering = orderingType.getOrdering();
      } else {
        // All other ordering are compounded to the primary one.
        ordering = ordering.compound(orderingType.getOrdering());
      }
    }
  } else {
    ordering = QueryHelper.DEFAULT_ORDERING;
  }
  // For stability order also by wave id.
  ordering = ordering.compound(QueryHelper.ID_COMPARATOR);
  return ordering;
}
 
Example 2
Source File: OrderedResultIterator.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Builds a comparator from the list of columns in ORDER BY clause.
 * @param orderByExpressions the columns in ORDER BY clause.
 * @return the comparator built from the list of columns in ORDER BY clause.
 */
// ImmutableBytesWritable.Comparator doesn't implement generics
@SuppressWarnings("unchecked")
private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) {
    Ordering<ResultEntry> ordering = null;
    int pos = 0;
    for (OrderByExpression col : orderByExpressions) {
        Expression e = col.getExpression();
        Comparator<ImmutableBytesWritable> comparator = 
                e.getSortOrder() == SortOrder.DESC && !e.getDataType().isFixedWidth() 
                ? buildDescVarLengthComparator() 
                : new ImmutableBytesWritable.Comparator();
        Ordering<ImmutableBytesWritable> o = Ordering.from(comparator);
        if(!col.isAscending()) o = o.reverse();
        o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst();
        Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++));
        ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering);
    }
    return ordering;
}
 
Example 3
Source File: QueryHelper.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
/**
 * Computes ordering for the search results. If none are specified - then
 * returns the default ordering. The resulting ordering is always compounded
 * with ordering by wave id for stability.
 */
public static Ordering<WaveViewData> computeSorter(
    Map<TokenQueryType, Set<String>> queryParams) {
  Ordering<WaveViewData> ordering = null;
  Set<String> orderBySet = queryParams.get(TokenQueryType.ORDERBY);
  if (orderBySet != null) {
    for (String orderBy : orderBySet) {
      QueryHelper.OrderByValueType orderingType =
          QueryHelper.OrderByValueType.fromToken(orderBy);
      if (ordering == null) {
        // Primary ordering.
        ordering = orderingType.getOrdering();
      } else {
        // All other ordering are compounded to the primary one.
        ordering = ordering.compound(orderingType.getOrdering());
      }
    }
  } else {
    ordering = QueryHelper.DEFAULT_ORDERING;
  }
  // For stability order also by wave id.
  ordering = ordering.compound(QueryHelper.ID_COMPARATOR);
  return ordering;
}
 
Example 4
Source File: OrderedResultIterator.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Builds a comparator from the list of columns in ORDER BY clause.
 * @param orderByExpressions the columns in ORDER BY clause.
 * @return the comparator built from the list of columns in ORDER BY clause.
 */
// ImmutableBytesWritable.Comparator doesn't implement generics
@SuppressWarnings("unchecked")
private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) {
    Ordering<ResultEntry> ordering = null;
    int pos = 0;
    for (OrderByExpression col : orderByExpressions) {
        Ordering<ImmutableBytesWritable> o = Ordering.from(new ImmutableBytesWritable.Comparator());
        if(!col.isAscending()) o = o.reverse();
        o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst();
        Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++));
        ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering);
    }
    return ordering;
}
 
Example 5
Source File: Assignments.java    From core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Ordering<Assignment> orderedByRole() {
    Ordering<Assignment> byType = new Ordering<Assignment>() {
        @Override
        public int compare(final Assignment left, final Assignment right) {
            return left.getRole().getType().compareTo(right.getRole().getType());
        }
    };
    Ordering<Assignment> byName = Ordering.natural().onResultOf(input -> input.getRole().getName());
    return byType.compound(byName);
}
 
Example 6
Source File: Assignments.java    From core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Ordering<Assignment> orderedByPrincipal() {
    Ordering<Assignment> byType = new Ordering<Assignment>() {
        @Override
        public int compare(final Assignment left, final Assignment right) {
            return left.getPrincipal().getType().compareTo(right.getPrincipal().getType());
        }
    };
    Ordering<Assignment> byName = Ordering.natural().onResultOf(input -> input.getPrincipal().getName());
    return byType.compound(byName);
}
 
Example 7
Source File: OrderingByPositionTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleOrderBy() throws Exception {
    Comparator<Object[]> ordering = Ordering.compound(Arrays.asList(
        OrderingByPosition.arrayOrdering(1, false, false),
        OrderingByPosition.arrayOrdering(0, false, false)
    ));

    assertThat(ordering.compare(new Object[]{0, 0}, new Object[]{4, 0}), is(-1));
    assertThat(ordering.compare(new Object[]{4, 0}, new Object[]{1, 1}), is(-1));
    assertThat(ordering.compare(new Object[]{5, 1}, new Object[]{2, 2}), is(-1));
    assertThat(ordering.compare(new Object[]{5, 1}, new Object[]{2, 2}), is(-1));
}
 
Example 8
Source File: OrderingByPosition.java    From crate with Apache License 2.0 5 votes vote down vote up
public static Comparator<Object[]> arrayOrdering(int[] position, boolean[] reverse, boolean[] nullsFirst) {
    if (position.length == 1) {
        return arrayOrdering(position[0], reverse[0], nullsFirst[0]);
    }
    List<Comparator<Object[]>> comparators = new ArrayList<>(position.length);
    for (int i = 0, positionLength = position.length; i < positionLength; i++) {
        comparators.add(arrayOrdering(position[i], reverse[i], nullsFirst[i]));
    }
    return Ordering.compound(comparators);
}
 
Example 9
Source File: Comparators.java    From crate with Apache License 2.0 5 votes vote down vote up
private static <T extends CollectExpression<Row, ?>> Comparator<Object[]> createComparatorWithEval(
    Supplier<InputFactory.Context<T>> createInputFactoryCtx,
    OrderBy orderBy) {

    var orderBySymbols = orderBy.orderBySymbols();
    ArrayList<Comparator<Object[]>> comparators = new ArrayList<>(orderBySymbols.size());
    for (int i = 0; i < orderBySymbols.size(); i++) {
        var orderSymbol = orderBySymbols.get(i);
        var ctx = createInputFactoryCtx.get();
        var input = (Input<Object>) ctx.add(orderSymbol);
        var expressionsInput = new ExpressionsInput<>(input, ctx.expressions());
        var row = new ArrayRow();
        comparators.add(new NullAwareComparator<>(
            cells -> {
                Comparable<Object> value;
                synchronized (row) {
                    row.cells(cells);
                    value = (Comparable<Object>) expressionsInput.value(row);
                }
                return value;
            },
            orderBy.reverseFlags()[i],
            orderBy.nullsFirst()[i]
        ));
    }
    return Ordering.compound(comparators);
}
 
Example 10
Source File: OfferOrderBuilder.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
private static Ordering<HostOffer> getOrdering(Ordering<HostOffer> base, OfferOrder order) {
  // Random is Ordering<Object> so accepting base as a parameter and compounding in here is the
  // cleanest way I could come up with to avoid a whole bunch of type finagling.
  switch(order) {
    case CPU: return base.compound(CPU_COMPARATOR);
    case DISK: return base.compound(DISK_COMPARATOR);
    case MEMORY: return base.compound(RAM_COMPARATOR);
    case REVOCABLE_CPU: return base.compound(REVOCABLE_CPU_COMPARATOR);
    default: return base.compound(RANDOM_COMPARATOR);
  }
}
 
Example 11
Source File: SortNode.java    From Quicksql with MIT License 5 votes vote down vote up
private Comparator<Row> comparator() {
  if (rel.getCollation().getFieldCollations().size() == 1) {
    return comparator(rel.getCollation().getFieldCollations().get(0));
  }
  return Ordering.compound(
      Iterables.transform(rel.getCollation().getFieldCollations(),
          this::comparator));
}
 
Example 12
Source File: SortNode.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Comparator<Row> comparator() {
  if (rel.getCollation().getFieldCollations().size() == 1) {
    return comparator(rel.getCollation().getFieldCollations().get(0));
  }
  return Ordering.compound(
      Iterables.transform(rel.getCollation().getFieldCollations(),
          this::comparator));
}
 
Example 13
Source File: EnumerableValues.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelNode passThrough(final RelTraitSet required) {
  RelCollation collation = required.getCollation();
  if (collation == null || collation.isDefault()) {
    return null;
  }

  // A Values with 0 or 1 rows can be ordered by any collation.
  if (tuples.size() > 1) {
    Ordering<List<RexLiteral>> ordering = null;
    // Generate ordering comparator according to the required collations.
    for (RelFieldCollation fc : collation.getFieldCollations()) {
      Ordering<List<RexLiteral>> comparator = RelMdCollation.comparator(fc);
      if (ordering == null) {
        ordering = comparator;
      } else {
        ordering = ordering.compound(comparator);
      }
    }
    // Check whether the tuples are sorted by required collations.
    if (!ordering.isOrdered(tuples)) {
      return null;
    }
  }

  // The tuples order satisfies the collation, we just create a new
  // relnode with required collation info.
  return copy(traitSet.replace(collation), ImmutableList.of());
}
 
Example 14
Source File: OrderedResultIterator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a comparator from the list of columns in ORDER BY clause.
 * @param orderByExpressions the columns in ORDER BY clause.
 * @return the comparator built from the list of columns in ORDER BY clause.
 */
// ImmutableBytesWritable.Comparator doesn't implement generics
@SuppressWarnings("unchecked")
private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) {
    Ordering<ResultEntry> ordering = null;
    int pos = 0;
    for (OrderByExpression col : orderByExpressions) {
        Ordering<ImmutableBytesWritable> o = Ordering.from(new ImmutableBytesWritable.Comparator());
        if(!col.isAscending()) o = o.reverse();
        o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst();
        Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++));
        ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering);
    }
    return ordering;
}
 
Example 15
Source File: OrderingByPosition.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static Ordering<Object[]> arrayOrdering(int[] position, boolean[] reverse, Boolean[] nullsFirst) {
    if (position.length == 0) {
        return arrayOrdering(position[0], reverse[0], nullsFirst[0]);
    }

    // TODO: if the reverse/nullsFirst flags are the same for all positions this could be optimized
    // to use just one single "inner" Ordering instance that is re-used for all positions
    List<Comparator<Object[]>> comparators = new ArrayList<>(position.length);
    for (int i = 0, positionLength = position.length; i < positionLength; i++) {
        comparators.add(arrayOrdering(position[i], reverse[i], nullsFirst[i]));
    }
    return Ordering.compound(comparators);
}
 
Example 16
Source File: StackComparatorBuilder.java    From OpenModsLib with MIT License 4 votes vote down vote up
public Ordering<ItemStack> build() {
	return Ordering.compound(result);
}
 
Example 17
Source File: CapabilitiesComparator.java    From selenium with Apache License 2.0 4 votes vote down vote up
public CapabilitiesComparator(final Capabilities desiredCapabilities,
                              final Platform currentPlatform) {
  final CapabilityScorer<String> browserNameScorer =
      new CapabilityScorer<>(desiredCapabilities.getBrowserName());
  Comparator<Capabilities> byBrowserName =
      Comparator.comparingInt(c -> browserNameScorer.score(c.getBrowserName()));

  final CapabilityScorer<String> versionScorer =
      new VersionScorer(desiredCapabilities.getVersion());
  Comparator<Capabilities> byVersion =
      Comparator.comparingInt(c -> versionScorer.score(c.getVersion()));

  final CapabilityScorer<Boolean> jsScorer =
      new CapabilityScorer<>(desiredCapabilities.is(SUPPORTS_JAVASCRIPT));
  Comparator<Capabilities> byJavaScript =
      Comparator.comparingInt(c -> jsScorer.score(c.is(SUPPORTS_JAVASCRIPT)));

  Platform desiredPlatform = desiredCapabilities.getPlatform();
  if (desiredPlatform == null) {
    desiredPlatform = Platform.ANY;
  }

  final CapabilityScorer<Platform> currentPlatformScorer =
      new CurrentPlatformScorer(currentPlatform, desiredPlatform);
  Comparator<Capabilities> byCurrentPlatform =
      Comparator.comparingInt(c -> currentPlatformScorer.score(c.getPlatform()));

  final CapabilityScorer<Platform> strictPlatformScorer =
      new CapabilityScorer<>(desiredPlatform);
  Comparator<Capabilities> byStrictPlatform =
      Comparator.comparingInt(c -> strictPlatformScorer.score(c.getPlatform()));

  final CapabilityScorer<Platform> fuzzyPlatformScorer =
      new FuzzyPlatformScorer(desiredPlatform);
  Comparator<Capabilities> byFuzzyPlatform =
      Comparator.comparingInt(c -> fuzzyPlatformScorer.score(c.getPlatform()));

  compareWith = Ordering.compound(Arrays.asList(
      byBrowserName,
      byVersion,
      byJavaScript,
      byCurrentPlatform,
      byStrictPlatform,
      byFuzzyPlatform));
}