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

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

Introduction

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

Prototype

protected Ordering() 

Source Link

Document

Constructs a new instance of this class (only invokable by the subclass constructor, typically implicit).

Usage

From source file:com.palantir.util.Pair.java

/**
 * Returns a <code>Comparator</code> for the
 * right-hand side object of the <code>Pair</code>.
 *///from   w  w  w  . jav  a2  s.co  m
public static <T, V extends Comparable<? super V>> Ordering<Pair<T, V>> compareRhSide() {
    return new Ordering<Pair<T, V>>() {
        @Override
        public int compare(Pair<T, V> o1, Pair<T, V> o2) {
            return o1.rhSide.compareTo(o2.rhSide);
        }
    };
}

From source file:org.nmdp.ngs.range.Ranges.java

/**
 * Return an ordering by lower endpoint over ranges.
 *
 * @param <C> range endpoint type/*  w w w  .ja v a  2  s .c  om*/
 * @return an ordering by lower endpoint over ranges
 */
public static <C extends Comparable> Ordering<Range<C>> orderingByLowerEndpoint() {
    return new Ordering<Range<C>>() {
        @Override
        public int compare(final Range<C> left, final Range<C> right) {
            return ComparisonChain.start().compare(left.hasLowerBound(), right.hasLowerBound())
                    .compare(left.lowerEndpoint(), right.lowerEndpoint()).result();
        }
    };
}

From source file:com.ninja_squad.core.persistence.Identifiables.java

/**
 * Returns an Ordering which compares Identifiables by their ID using the given ID comparator.<br/>
 * @return the created ordering./*www. jav  a 2  s.  c  o  m*/
 */
public static <T, I extends Identifiable<T>> Ordering<I> byId(@Nonnull final Comparator<T> idComparator) {
    Preconditions.checkNotNull(idComparator, "idComparator may not be null");
    return new Ordering<I>() {
        @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP", justification = "if null, it'll throw a NPE as documented")
        @Override
        public int compare(I left, I right) {
            return idComparator.compare(left.getId(), right.getId());
        }
    };
}

From source file:com.nirima.jenkins.SelectionTypeProject.java

private int getPromotedBuildNumber(final String project, final String promoted) {
    BuildableItemWithBuildWrappers item = getProject(project);

    Iterable<AbstractBuild> promotedItems = Iterables.filter(item.asProject().getBuilds(), new Predicate() {
        public boolean apply(Object o) {
            AbstractBuild abstractBuild = (AbstractBuild) o;

            PromotedBuildAction pba = abstractBuild.getAction(PromotedBuildAction.class);
            return (pba != null && pba.getPromotion(promoted) != null);

        }// w ww .ja  v  a2  s. c  om
    });

    Ordering<AbstractBuild> ordering = new Ordering<AbstractBuild>() {
        @Override
        public int compare(AbstractBuild l, AbstractBuild r) {
            return r.getNumber() - l.getNumber();
        }
    };

    try {
        return ordering.max(promotedItems).getNumber();
    } catch (Exception ex) {
        throw new RuntimeException("No promotion of type " + promoted + " in project " + project);
    }
}

From source file:org.eclipse.buildship.ui.view.task.TaskNodeViewerSorter.java

private static Ordering<TaskNode> createByVisibilityOrdering() {
    return new Ordering<TaskNode>() {

        @Override//from  ww w.j  a va  2 s.  c o m
        public int compare(TaskNode left, TaskNode right) {
            int leftOrdinal = toOrdinal(left);
            int rightOrdinal = toOrdinal(right);
            return (leftOrdinal < rightOrdinal ? -1 : (leftOrdinal == rightOrdinal ? 0 : 1));
        }

        private int toOrdinal(TaskNode node) {
            // public tasks to be shown first
            return node.isPublic() ? 1 : 2;
        }
    };
}

From source file:org.nmdp.ngs.tools.FastqToSsake.java

@Override
public Integer call() throws Exception {
    PrintWriter ssakeWriter = null;
    PrintWriter unpairedWriter = null;
    try {//from   w  w w.j a  v a2 s. c om
        ssakeWriter = writer(ssakeFile);
        unpairedWriter = (unpairedFile == null) ? null : writer(unpairedFile);

        // read both FASTQ files into RAM (ick)
        final List<Fastq> reads = Lists.newArrayList();
        SangerFastqReader fastqReader = new SangerFastqReader();
        fastqReader.stream(reader(firstFastqFile), new StreamListener() {
            @Override
            public void fastq(final Fastq fastq) {
                reads.add(fastq);
            }
        });
        fastqReader.stream(reader(secondFastqFile), new StreamListener() {
            @Override
            public void fastq(final Fastq fastq) {
                reads.add(fastq);
            }
        });

        // .. and sort by description
        Collections.sort(reads, new Ordering<Fastq>() {
            @Override
            public int compare(final Fastq left, final Fastq right) {
                return left.getDescription().compareTo(right.getDescription());
            }
        });

        for (int i = 0, size = reads.size(); i < size;) {
            Fastq left = reads.get(i);
            if ((i + 1) == size) {
                if (unpairedWriter != null) {
                    unpairedWriter.println(left.getDescription());
                }
                break;
            }
            Fastq right = reads.get(i + 1);

            if (isLeft(left)) {
                if (isRight(right)) {
                    // write paired reads to SSAKE-hacked-up version of FASTA format
                    StringBuilder sb = new StringBuilder(512);
                    sb.append(">");
                    sb.append(left.getDescription());
                    sb.append(":");
                    sb.append(right.getDescription());
                    sb.append(":");
                    sb.append(insertSize);
                    sb.append("\n");
                    sb.append(left.getSequence());
                    sb.append(":");
                    sb.append(right.getSequence());
                    ssakeWriter.println(sb.toString());
                    i += 2;
                } else {
                    if (unpairedWriter != null) {
                        unpairedWriter.println(right.getDescription());
                    }
                    i++;
                }
            } else {
                if (unpairedWriter != null) {
                    unpairedWriter.println(left.getDescription());
                }
                i++;
            }
        }
        return 0;
    } finally {
        try {
            ssakeWriter.close();
        } catch (Exception e) {
            // ignore
        }
        try {
            unpairedWriter.close();
        } catch (Exception e) {
            // ignore
        }
    }
}

From source file:com.metamx.druid.query.group.orderby.DefaultLimitSpec.java

private Ordering<Row> metricOrdering(final String column, final Comparator comparator) {
    return new Ordering<Row>() {
        @SuppressWarnings("unchecked")
        @Override//ww  w . j a  va 2  s.  com
        public int compare(Row left, Row right) {
            return comparator.compare(left.getFloatMetric(column), right.getFloatMetric(column));
        }
    };
}

From source file:com.palantir.util.Pair.java

/**
 * Returns a <code>Comparator</code> for the
 * left-hand side object of the <code>Pair</code>.
 *//*  www. j a v a 2  s.c o  m*/
public static <T, W> Ordering<Pair<T, W>> compareLhSide(final Comparator<? super T> comp) {
    return new Ordering<Pair<T, W>>() {
        @Override
        public int compare(Pair<T, W> o1, Pair<T, W> o2) {
            return comp.compare(o1.lhSide, o2.lhSide);
        }
    };
}

From source file:io.druid.query.groupby.orderby.DefaultLimitSpec.java

private Ordering<Row> metricOrdering(final String column, final Comparator comparator) {
    return new Ordering<Row>() {
        @SuppressWarnings("unchecked")
        @Override/*from   w ww  . j a  v a 2  s .c o m*/
        public int compare(Row left, Row right) {
            return comparator.compare(left.getRaw(column), right.getRaw(column));
        }
    };
}

From source file:org.grouplens.lenskit.scored.ScoredIds.java

/**
 * An ordering (comparator) that compares IDs by channel.  Missing channels are less than
 * present channels./*from   www .j a  v  a2s.c  o m*/
 * @param chan The channel to sort by.
 * @return An ordering over scored IDs by channel.
 */
public static Ordering<ScoredId> channelOrder(final Symbol chan) {
    return new Ordering<ScoredId>() {
        @Override
        @SuppressFBWarnings("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE")
        public int compare(ScoredId left, ScoredId right) {
            if (left.hasUnboxedChannel(chan)) {
                if (right.hasUnboxedChannel(chan)) {
                    return Doubles.compare(left.getUnboxedChannelValue(chan),
                            right.getUnboxedChannelValue(chan));
                } else {
                    return 1;
                }
            } else if (right.hasUnboxedChannel(chan)) {
                return -1;
            } else {
                return 0;
            }
        }
    };
}