Example usage for com.google.common.collect Ordering compound

List of usage examples for com.google.common.collect Ordering compound

Introduction

In this page you can find the example usage for com.google.common.collect Ordering compound.

Prototype

@GwtCompatible(serializable = true)
public static <T> Ordering<T> compound(Iterable<? extends Comparator<? super T>> comparators) 

Source Link

Document

Returns an ordering which tries each given comparator in order until a non-zero result is found, returning that result, and returning zero only if all comparators return zero.

Usage

From source file:com.intel.podm.allocation.mappers.Sorter.java

@SafeVarargs
public static <T> List<T> sort(Collection<T> toBeSorted, Ordering<T>... orderings) {
    if (orderings == null || orderings.length == 0) {
        throw new IllegalArgumentException("At least one ordering method must be passed to sort.");
    }/*from   ww  w  .  j  ava  2s . c om*/

    Ordering<T> ordering = orderings[0];
    for (int i = 1; i < orderings.length; i++) {
        ordering = ordering.compound(orderings[i]);
    }

    return ordering.sortedCopy(toBeSorted);
}

From source file:org.apache.aurora.scheduler.offers.OfferOrderBuilder.java

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://from  w w  w.ja va 2 s . c om
        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);
    }
}

From source file:com.seven7.util.PropertyOrderingFactory.java

public static <T> Ordering<T> createOrderingFromSorters(Collection<SortInfo> sortInfos) {
    Ordering<T> ordering = null;

    if (sortInfos != null) {
        for (SortInfo sorter : sortInfos) {
            Ordering<T> propertyOrdering = createOrdering(sorter.getProperty(), sorter.getDirection());
            if (ordering == null) {
                ordering = propertyOrdering;
            } else {
                ordering = ordering.compound(propertyOrdering);
            }/*from  w ww .  j  av  a 2s .c om*/
        }
    }

    return ordering;
}

From source file:org.jboss.as.console.client.administration.accesscontrol.store.Assignments.java

public static Ordering<Assignment> orderedByRole() {
    Ordering<Assignment> byType = new Ordering<Assignment>() {
        @Override/* ww w . j a v a  2s. c o  m*/
        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);
}

From source file:org.jboss.as.console.client.administration.accesscontrol.store.Assignments.java

public static Ordering<Assignment> orderedByPrincipal() {
    Ordering<Assignment> byType = new Ordering<Assignment>() {
        @Override// www  . j  ava 2  s  .c  om
        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);
}

From source file:org.artifactory.common.wicket.util.ListPropertySorter.java

private static Ordering createOrdering(SortParam[] sortParams, ListSorterOrderingStrategy strategy) {
    Ordering ordering = null;
    for (SortParam sortParam : sortParams) {
        if (sortParam != null) {
            final String property = sortParam.getProperty();
            if (StringUtils.hasText(property)) {
                if (ordering == null) {
                    ordering = strategy.createOrdering(sortParam);
                } else {
                    ordering = ordering.compound(strategy.createOrdering(sortParam));
                }/*from ww  w  .ja  v  a 2  s.co m*/
            }
        }
    }
    return ordering;
}

From source file:org.sonar.server.measure.ws.ComponentTreeSort.java

public static List<ComponentDto> sortComponents(List<ComponentDto> components, ComponentTreeWsRequest wsRequest,
        List<MetricDto> metrics,
        Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric) {
    List<String> sortParameters = wsRequest.getSort();
    if (sortParameters == null || sortParameters.isEmpty()) {
        return components;
    }//from   ww w  .  j  av a 2  s  . c  o m
    boolean isAscending = wsRequest.getAsc();
    Map<String, Ordering<ComponentDto>> orderingsBySortField = ImmutableMap
            .<String, Ordering<ComponentDto>>builder().put(NAME_SORT, componentNameOrdering(isAscending))
            .put(QUALIFIER_SORT, componentQualifierOrdering(isAscending))
            .put(PATH_SORT, componentPathOrdering(isAscending))
            .put(METRIC_SORT, metricValueOrdering(wsRequest, metrics, measuresByComponentUuidAndMetric))
            .put(METRIC_PERIOD_SORT, metricPeriodOrdering(wsRequest, metrics, measuresByComponentUuidAndMetric))
            .build();

    String firstSortParameter = sortParameters.get(0);
    Ordering<ComponentDto> primaryOrdering = orderingsBySortField.get(firstSortParameter);
    if (sortParameters.size() > 1) {
        for (int i = 1; i < sortParameters.size(); i++) {
            String secondarySortParameter = sortParameters.get(i);
            Ordering<ComponentDto> secondaryOrdering = orderingsBySortField.get(secondarySortParameter);
            primaryOrdering = primaryOrdering.compound(secondaryOrdering);
        }
    }

    return primaryOrdering.immutableSortedCopy(components);
}

From source file:org.sonar.server.component.ws.TreeAction.java

public static List<ComponentDto> sortComponents(List<ComponentDto> components, TreeWsRequest wsRequest) {
    List<String> sortParameters = wsRequest.getSort();
    if (sortParameters == null || sortParameters.isEmpty()) {
        return components;
    }/*  w  w  w.  j a va  2 s  .c om*/
    boolean isAscending = wsRequest.getAsc();
    Map<String, Ordering<ComponentDto>> orderingsBySortField = ImmutableMap
            .<String, Ordering<ComponentDto>>builder()
            .put(NAME_SORT, stringOrdering(isAscending, ComponentDto::name))
            .put(QUALIFIER_SORT, stringOrdering(isAscending, ComponentDto::qualifier))
            .put(PATH_SORT, stringOrdering(isAscending, ComponentDto::path)).build();

    String firstSortParameter = sortParameters.get(0);
    Ordering<ComponentDto> primaryOrdering = orderingsBySortField.get(firstSortParameter);
    if (sortParameters.size() > 1) {
        for (int i = 1; i < sortParameters.size(); i++) {
            String secondarySortParameter = sortParameters.get(i);
            Ordering<ComponentDto> secondaryOrdering = orderingsBySortField.get(secondarySortParameter);
            primaryOrdering = primaryOrdering.compound(secondaryOrdering);
        }
    }
    return primaryOrdering.immutableSortedCopy(components);
}

From source file:org.waveprotocol.box.server.waveserver.QueryHelper.java

/**
 * 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.
 *///www.j  a va  2  s .  com
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;
}

From source file:org.apache.phoenix.iterate.OrderedResultIterator.java

/**
 * 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.
 */// w w  w.  j a  v  a 2  s  . co  m
// 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;
}