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

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

Introduction

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

Prototype

public boolean isOrdered(Iterable<? extends T> iterable) 

Source Link

Document

Returns true if each element in iterable after the first is greater than or equal to the element that preceded it, according to this ordering.

Usage

From source file:org.grouplens.lenskit.data.history.History.java

/**
 * Create a history for a particular user.
 * @param id The user ID./*  w ww.  j  av a  2  s. c o  m*/
 * @param events The events.
 * @param <E> The root type of the events in the history.
 * @return A history object.
 */
@SuppressWarnings("deprecation")
@Nonnull
public static <E extends Event> UserHistory<E> forUser(long id, List<? extends E> events) {
    Ordering<Event> ord = Ordering.from(Events.TIMESTAMP_COMPARATOR);
    if (ord.isOrdered(events)) {
        return new BasicUserHistory<E>(id, events);
    } else {
        return new BasicUserHistory<E>(id, ord.immutableSortedCopy(events));
    }
}

From source file:org.lenskit.data.history.History.java

/**
 * Create a history for a particular user.
 * @param id The user ID.// w w  w.  ja  v a2 s. c  o  m
 * @param events The events.
 * @param <E> The root type of the events in the history.
 * @return A history object.
 */
@SuppressWarnings("deprecation")
@Nonnull
public static <E extends Event> UserHistory<E> forUser(long id, List<? extends E> events) {
    Ordering<Event> ord = Ordering.from(Events.TIMESTAMP_COMPARATOR);
    if (ord.isOrdered(events)) {
        return new BasicUserHistory<>(id, events);
    } else {
        return new BasicUserHistory<>(id, ord.immutableSortedCopy(events));
    }
}

From source file:org.apache.calcite.rel.metadata.RelMdCollation.java

/** Helper method to determine a
 * {@link org.apache.calcite.rel.core.Values}'s collation.
 *
 * <p>We actually under-report the collations. A Values with 0 or 1 rows - an
 * edge case, but legitimate and very common - is ordered by every permutation
 * of every subset of the columns./*from ww w  .j  ava  2s . c o  m*/
 *
 * <p>So, our algorithm aims to:<ul>
 *   <li>produce at most N collations (where N is the number of columns);
 *   <li>make each collation as long as possible;
 *   <li>do not repeat combinations already emitted -
 *       if we've emitted {@code (a, b)} do not later emit {@code (b, a)};
 *   <li>probe the actual values and make sure that each collation is
 *      consistent with the data
 * </ul>
 *
 * <p>So, for an empty Values with 4 columns, we would emit
 * {@code (a, b, c, d), (b, c, d), (c, d), (d)}. */
public static List<RelCollation> values(RelMetadataQuery mq, RelDataType rowType,
        ImmutableList<ImmutableList<RexLiteral>> tuples) {
    Util.discard(mq); // for future use
    final List<RelCollation> list = Lists.newArrayList();
    final int n = rowType.getFieldCount();
    final List<Pair<RelFieldCollation, Ordering<List<RexLiteral>>>> pairs = Lists.newArrayList();
    outer: for (int i = 0; i < n; i++) {
        pairs.clear();
        for (int j = i; j < n; j++) {
            final RelFieldCollation fieldCollation = new RelFieldCollation(j);
            Ordering<List<RexLiteral>> comparator = comparator(fieldCollation);
            Ordering<List<RexLiteral>> ordering;
            if (pairs.isEmpty()) {
                ordering = comparator;
            } else {
                ordering = Util.last(pairs).right.compound(comparator);
            }
            pairs.add(Pair.of(fieldCollation, ordering));
            if (!ordering.isOrdered(tuples)) {
                if (j == i) {
                    continue outer;
                }
                pairs.remove(pairs.size() - 1);
            }
        }
        if (!pairs.isEmpty()) {
            list.add(RelCollations.of(Pair.left(pairs)));
        }
    }
    return list;
}

From source file:com.github.ferstl.maven.pomenforcers.PedanticModuleOrderEnforcer.java

@Override
protected void doEnforce(ErrorReport report) {
    MavenProject project = EnforcerRuleUtils.getMavenProject(getHelper());
    // Do nothing if the project is not a parent project
    if (!isPomProject(project)) {
        return;//from w  w w .  j  a v  a  2 s.  c o  m
    }

    // Remove all modules to be ignored.
    List<String> declaredModules = new ArrayList<>(getProjectModel().getModules());
    declaredModules.removeAll(this.ignoredModules);

    // Enforce the module order
    Ordering<String> moduleOrdering = Ordering.natural();
    if (!moduleOrdering.isOrdered(declaredModules)) {
        reportError(report, declaredModules, moduleOrdering.immutableSortedCopy(declaredModules));
    }
}

From source file:com.github.ferstl.maven.pomenforcers.PedanticPomSectionOrderEnforcer.java

@Override
protected void doEnforce(ErrorReport report) {
    Node docElement = getPom().getDocumentElement();
    NodeList sectionNodes = docElement.getChildNodes();
    ArrayList<PomSection> pomSections = new ArrayList<>();
    for (int i = 0; i < sectionNodes.getLength(); i++) {
        Node node = sectionNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            pomSections.add(PomSection.getBySectionName(node.getNodeName()));
        }/* w w w.  j  a v a 2 s  . c o m*/
    }

    // The default ordering is the order of the PomSection enum.
    Ordering<PomSection> ordering = PomSection.createPriorityOrdering(this.sectionPriorities);

    if (!ordering.isOrdered(pomSections)) {
        List<PomSection> sortedPomSections = ordering.immutableSortedCopy(pomSections);

        report.addLine("Your POM has to be organized this way:").emptyLine().addDiff(pomSections,
                sortedPomSections, "Actual Order", "Required Order", pomSectionToString());
    }
}

From source file:de.johni0702.minecraft.gui.container.AbstractGuiContainer.java

@Override
public T sortElements(final Comparator<GuiElement> comparator) {
    Ordering<Map.Entry<GuiElement, LayoutData>> ordering = new Ordering<Map.Entry<GuiElement, LayoutData>>() {
        @Override//from w  w  w  . ja va2  s  .c o  m
        public int compare(Map.Entry<GuiElement, LayoutData> left, Map.Entry<GuiElement, LayoutData> right) {
            return comparator.compare(left.getKey(), right.getKey());
        }
    };
    if (!ordering.isOrdered(elements.entrySet())) {
        ImmutableList<Map.Entry<GuiElement, LayoutData>> sorted = ordering
                .immutableSortedCopy(elements.entrySet());
        elements.clear();
        for (Map.Entry<GuiElement, LayoutData> entry : sorted) {
            elements.put(entry.getKey(), entry.getValue());
        }
    }
    return getThis();
}