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(Iterable<E> iterable) 

Source Link

Document

Returns the least of the specified values according to this ordering.

Usage

From source file:task3.Task3.java

private static Book getOldestBook(List<Book> books) {
    Ordering<Book> o = new Ordering<Book>() {
        @Override/*  ww  w .j  a  va  2 s. c  o  m*/
        public int compare(Book left, Book right) {
            return left.getPublishDate().compare(right.getPublishDate());
        }
    };
    return o.min(books);
}

From source file:com.google.gerrit.server.schema.Schema_124.java

private Collection<AccountSshKey> fixInvalidSequenceNumbers(Collection<AccountSshKey> keys) {
    Ordering<AccountSshKey> o = Ordering.from(comparing(k -> k.getKey().get()));
    List<AccountSshKey> fixedKeys = new ArrayList<>(keys);
    AccountSshKey minKey = o.min(keys);
    while (minKey.getKey().get() <= 0) {
        AccountSshKey fixedKey = new AccountSshKey(new AccountSshKey.Id(minKey.getKey().getParentKey(),
                Math.max(o.max(keys).getKey().get() + 1, 1)), minKey.getSshPublicKey());
        Collections.replaceAll(fixedKeys, minKey, fixedKey);
        minKey = o.min(fixedKeys);/*from   www  . j ava  2s .  com*/
    }
    return fixedKeys;
}

From source file:org.antinori.astar.AStar.java

public List<T> findPath(Collection<T> graph, T start, Collection<T> goals) {
    canceled = false;/*from w  w  w  .  ja  v a  2s  . com*/
    Map<T, State> open = new HashMap<T, State>();
    Map<T, State> closed = new HashMap<T, State>();
    State startState = new State(start, 0, null, goals);
    open.put(start, startState);
    Ordering<Map.Entry<T, State>> orderByEntryValue = Utilities.orderByEntryValue();
    while (!(open.isEmpty() || canceled)) {
        final State state = open.remove(orderByEntryValue.min(open.entrySet()).getKey());
        fireConsidered(new PathEvent<T>(this) {

            @Override
            public List<T> getPath() {
                return state.makePath();
            }

        });
        if (goals.contains(state.node)) {
            return state.makePath();
        } else {
            for (T newNode : state.node.neighbors()) {
                double newCost = state.costFromStart + state.node.traverseCost(newNode);
                State openNode = open.get(newNode);
                if (openNode != null && openNode.costFromStart <= newCost) {
                    continue;
                }

                State closedNode = closed.get(newNode);
                if (closedNode != null && closedNode.costFromStart <= newCost) {
                    continue;
                }

                if (closedNode != null) {
                    closed.remove(newNode);
                }

                if (openNode != null) {
                    open.remove(newNode);
                }

                State newState = new State(newNode, newCost, state, goals);
                open.put(newNode, newState);
            }
        }

        closed.put(state.node, state);
    }

    return null;
}

From source file:io.prestosql.sql.planner.iterative.rule.DetermineSemiJoinDistributionType.java

private PlanNode getCostBasedDistributionType(SemiJoinNode node, Context context) {
    if (!canReplicate(node, context)) {
        return node.withDistributionType(PARTITIONED);
    }//from ww  w. j a  v  a  2s. c om

    List<PlanNodeWithCost> possibleJoinNodes = new ArrayList<>();
    possibleJoinNodes.add(getSemiJoinNodeWithCost(node.withDistributionType(REPLICATED), context));
    possibleJoinNodes.add(getSemiJoinNodeWithCost(node.withDistributionType(PARTITIONED), context));

    if (possibleJoinNodes.stream().anyMatch(result -> result.getCost().hasUnknownComponents())) {
        return node.withDistributionType(PARTITIONED);
    }

    // Using Ordering to facilitate rule determinism
    Ordering<PlanNodeWithCost> planNodeOrderings = costComparator.forSession(context.getSession())
            .onResultOf(PlanNodeWithCost::getCost);
    return planNodeOrderings.min(possibleJoinNodes).getPlanNode();
}

From source file:game.pathfinding.AStar.java

@Override
public Optional<List<T>> findPath(final Collection<T> graph, final T start, final Collection<T> goals) {
    canceled = false;/*from w  ww  .j av  a2  s .co m*/
    final Map<T, State> open = new HashMap<T, State>();
    final Map<T, State> closed = new HashMap<T, State>();
    final State startState = new State(start, 0, null, goals);
    open.put(start, startState);
    final Ordering<Map.Entry<T, State>> orderByEntryValue = orderByEntryValue();
    while (!(open.isEmpty() || canceled)) {
        final State state = open.remove(orderByEntryValue.min(open.entrySet()).getKey());
        fireConsidered(new PathEvent<T>(this) {

            private static final long serialVersionUID = 1L;

            @Override
            public List<T> getPath() {
                return state.makePath();
            }

        });
        if (goals.contains(state.node))
            return Optional.of(state.makePath());
        else {
            for (final T newNode : state.node.neighbors()) {
                final double newCost = state.costFromStart + state.node.traverseCost(newNode);
                final State openNode = open.get(newNode);
                if (openNode != null && openNode.costFromStart <= newCost) {
                    continue;
                }

                final State closedNode = closed.get(newNode);
                if (closedNode != null && closedNode.costFromStart <= newCost) {
                    continue;
                }

                if (closedNode != null) {
                    closed.remove(newNode);
                }

                if (openNode != null) {
                    open.remove(newNode);
                }

                final State newState = new State(newNode, newCost, state, goals);
                open.put(newNode, newState);
            }
        }

        closed.put(state.node, state);
    }

    return Optional.empty();
}

From source file:com.facebook.presto.sql.planner.iterative.rule.DetermineJoinDistributionType.java

private PlanNode getCostBasedJoin(JoinNode joinNode, Context context) {
    List<PlanNodeWithCost> possibleJoinNodes = new ArrayList<>();

    addJoinsWithDifferentDistributions(joinNode, possibleJoinNodes, context);
    addJoinsWithDifferentDistributions(joinNode.flipChildren(), possibleJoinNodes, context);

    if (possibleJoinNodes.stream().anyMatch(result -> result.getCost().hasUnknownComponents())
            || possibleJoinNodes.isEmpty()) {
        return getSyntacticOrderJoin(joinNode, context, AUTOMATIC);
    }//from  ww  w .  j  a  v  a 2s .co m

    // Using Ordering to facilitate rule determinism
    Ordering<PlanNodeWithCost> planNodeOrderings = costComparator.forSession(context.getSession())
            .onResultOf(PlanNodeWithCost::getCost);
    return planNodeOrderings.min(possibleJoinNodes).getPlanNode();
}

From source file:org.apache.phoenix.hbase.index.covered.data.LocalTable.java

protected long getOldestTimestamp(Collection<List<Cell>> cellLists) {
    Ordering<List<Cell>> cellListOrdering = new Ordering<List<Cell>>() {
        @Override/*  w ww  .  jav a  2 s.co  m*/
        public int compare(List<Cell> left, List<Cell> right) {
            // compare the last element of each list, since that is the smallest in that list
            return Longs.compare(Iterables.getLast(left).getTimestamp(),
                    Iterables.getLast(right).getTimestamp());
        }
    };
    List<Cell> minList = cellListOrdering.min(cellLists);
    return Iterables.getLast(minList).getTimestamp();
}

From source file:org.apache.arrow.vector.complex.NonNullableStructVector.java

@Override
public int getValueCapacity() {
    if (size() == 0) {
        return 0;
    }//  w  w  w .j ava2s  .  c om

    final Ordering<ValueVector> natural = new Ordering<ValueVector>() {
        @Override
        public int compare(@Nullable ValueVector left, @Nullable ValueVector right) {
            return Ints.compare(checkNotNull(left).getValueCapacity(), checkNotNull(right).getValueCapacity());
        }
    };

    return natural.min(getChildren()).getValueCapacity();
}

From source file:org.apache.arrow.vector.complex.MapVector.java

@Override
public int getValueCapacity() {
    if (size() == 0) {
        return 0;
    }/*from  w  ww  .  j av a2 s.co  m*/

    final Ordering<ValueVector> natural = new Ordering<ValueVector>() {
        @Override
        public int compare(@Nullable ValueVector left, @Nullable ValueVector right) {
            return Ints.compare(Preconditions.checkNotNull(left).getValueCapacity(),
                    Preconditions.checkNotNull(right).getValueCapacity());
        }
    };

    return natural.min(getChildren()).getValueCapacity();
}

From source file:org.apache.drill.exec.vector.complex.MapVector.java

@Override
public int getValueCapacity() {
    if (size() == 0) {
        return Integer.MAX_VALUE;
    }/*from w w  w. j a va  2s  . c om*/

    final Ordering<ValueVector> natural = new Ordering<ValueVector>() {
        @Override
        public int compare(@Nullable ValueVector left, @Nullable ValueVector right) {
            return Ints.compare(Preconditions.checkNotNull(left).getValueCapacity(),
                    Preconditions.checkNotNull(right).getValueCapacity());
        }
    };

    return natural.min(getChildren()).getValueCapacity();
}