下面列出了com.google.common.collect.Ordering#compound ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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));
}
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);
}
/**
* 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;
}
@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());
}
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));
}
/**
* 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;
}
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);
}
}
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);
}
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);
}
@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));
}
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);
}
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);
}
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));
}
public Ordering<ItemStack> build() {
return Ordering.compound(result);
}