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

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

Introduction

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

Prototype

public <E extends T> E min(@Nullable E a, @Nullable E b) 

Source Link

Document

Returns the lesser of the two values according to this ordering.

Usage

From source file:com.palantir.common.base.BatchingVisitables.java

/**
 * This will return the first maximal element in the visitable. This method
 * takes a default element and will return that if the visitable is empty.
 * If the visitable is non-empty it will return the largest value in the
 * visitable.//  w w w .java2s  .c  om
 * <p>
 * A common way to use this would be to pass <code>null</code> as the
 * defaultElement and have the ordering throw on null elements so you know
 * the visitable doesn't have any nulls.
 */
public static <T> T getMin(BatchingVisitable<T> v, final Ordering<? super T> o, @Nullable T defaultElement) {
    final Mutable<T> ret = Mutables.newMutable(defaultElement);
    v.batchAccept(DEFAULT_BATCH_SIZE, AbortingVisitors.batching(new AbortingVisitor<T, RuntimeException>() {
        boolean hasSeenFirst = false;

        @Override
        public boolean visit(T item) throws RuntimeException {
            if (hasSeenFirst) {
                ret.set(o.min(ret.get(), item));
            } else {
                // Call o.max here so it will throw if item is null and this
                // ordering hates on nulls.
                ret.set(o.min(item, item));
                hasSeenFirst = true;
            }
            return true;
        }
    }));
    return ret.get();
}

From source file:org.geogit.api.plumbing.diff.DiffCounter.java

private DiffObjectCount countChildrenDiffs(Iterator<Node> leftTree, Iterator<Node> rightTree) {

    final Ordering<Node> storageOrder = new NodeStorageOrder();

    DiffObjectCount count = new DiffObjectCount();

    PeekingIterator<Node> left = Iterators.peekingIterator(leftTree);
    PeekingIterator<Node> right = Iterators.peekingIterator(rightTree);

    while (left.hasNext() && right.hasNext()) {
        Node peekLeft = left.peek();
        Node peekRight = right.peek();

        if (0 == storageOrder.compare(peekLeft, peekRight)) {
            // same path, consume both
            peekLeft = left.next();//  ww  w.jav  a  2s .  co  m
            peekRight = right.next();
            if (!peekLeft.getObjectId().equals(peekRight.getObjectId())) {
                // find the diffs between these two specific refs
                if (RevObject.TYPE.FEATURE.equals(peekLeft.getType())) {
                    checkState(RevObject.TYPE.FEATURE.equals(peekRight.getType()));
                    count.addFeatures(1);
                } else {
                    checkState(RevObject.TYPE.TREE.equals(peekLeft.getType()));
                    checkState(RevObject.TYPE.TREE.equals(peekRight.getType()));
                    ObjectId leftTreeId = peekLeft.getObjectId();
                    ObjectId rightTreeId = peekRight.getObjectId();
                    count.add(countDiffs(leftTreeId, rightTreeId));
                }
            }
        } else if (peekLeft == storageOrder.min(peekLeft, peekRight)) {
            peekLeft = left.next();// consume only the left value
            count.add(aggregateSize(ImmutableList.of(peekLeft)));
        } else {
            peekRight = right.next();// consume only the right value
            count.add(aggregateSize(ImmutableList.of(peekRight)));
        }
    }

    if (left.hasNext()) {
        count.add(countRemaining(left));
    } else if (right.hasNext()) {
        count.add(countRemaining(right));
    }
    Preconditions.checkState(!left.hasNext());
    Preconditions.checkState(!right.hasNext());
    return count;
}