Example usage for com.google.common.collect ComparisonChain result

List of usage examples for com.google.common.collect ComparisonChain result

Introduction

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

Prototype

public abstract int result();

Source Link

Document

Ends this comparison chain and returns its result: a value having the same sign as the first nonzero comparison result in the chain, or zero if every result was zero.

Usage

From source file:org.apache.isis.applib.util.ObjectContractsLegacy.java

private static <T> int compare(final T p, final T q, final Iterable<String> propertyNamesIter) {
    if (p == null) {
        return -1;
    }//from   w w  w .ja v a 2  s.c  o  m
    if (q == null) {
        return +1;
    }
    if (p.getClass() != q.getClass()) {
        // just sort on the class type
        return Ordering.natural().onResultOf((Function<Object, String>) o -> o.getClass().getSimpleName())
                .compare(p, q);
    }

    final Iterable<Clause> clauses = clausesFor(propertyNamesIter);
    ComparisonChain chain = ComparisonChain.start();
    for (final Clause clause : clauses) {
        final Comparable<T> propertyValueOfP = _Casts.uncheckedCast(clause.getValueOf(p));
        final Comparable<T> propertyValueOfQ = _Casts.uncheckedCast(clause.getValueOf(q));
        chain = chain.compare(propertyValueOfP, propertyValueOfQ, clause.getDirection().getOrdering());
    }
    return chain.result();
}

From source file:org.apache.metron.indexing.dao.InMemoryDao.java

private static Comparator<SearchResult> sorted(final List<SortField> fields) {
    return (o1, o2) -> {
        ComparisonChain chain = ComparisonChain.start();
        for (SortField field : fields) {
            Comparable f1 = (Comparable) o1.getSource().get(field.getField());
            Comparable f2 = (Comparable) o2.getSource().get(field.getField());
            chain = chain.compare(f1, f2, new ComparableComparator(field.getSortOrder()));
        }/*from   www  . j ava  2 s  . c om*/
        return chain.result();
    };
}

From source file:org.apache.isis.applib.util.ObjectContracts2.java

private static <T> int compare(final T p, final T q, final Iterable<String> propertyNamesIter) {
    if (p == null) {
        return -1;
    }// www .j  ava  2s .c o  m
    if (q == null) {
        return +1;
    }
    if (p.getClass() != q.getClass()) {
        // just sort on the class type
        return Ordering.natural().onResultOf(new Function<Object, String>() {
            @Override
            public String apply(final Object o) {
                return o.getClass().getSimpleName();
            }
        }).compare(p, q);
    }

    final Iterable<Clause> clauses = clausesFor(propertyNamesIter);
    ComparisonChain chain = ComparisonChain.start();
    for (final Clause clause : clauses) {
        final Comparable<T> propertyValueOfP = (Comparable<T>) clause.getValueOf(p);
        final Comparable<T> propertyValueOfQ = (Comparable<T>) clause.getValueOf(q);
        chain = chain.compare(propertyValueOfP, propertyValueOfQ, clause.getDirection().getOrdering());
    }
    return chain.result();
}

From source file:edu.byu.nlp.util.DoubleArrays.java

public static int compareTo(double[] arr1, double[] arr2) {
    ComparisonChain chain = ComparisonChain.start().compare(arr1.length, arr2.length);
    for (int i = 0; i < arr1.length; i++) {
        chain = chain.compare(arr1[i], arr2[i]);
    }//  w  ww . j av a2 s  .c  om
    return chain.result();
}

From source file:org.apache.isis.applib.util.ObjectContracts.java

private static <T> int compare(final T p, final T q, final Iterable<String> propertyNamesIter) {
    final Iterable<Clause> clauses = clausesFor(propertyNamesIter);
    ComparisonChain chain = ComparisonChain.start();
    for (final Clause clause : clauses) {
        final Comparable<T> propertyValueOfP = (Comparable<T>) clause.getValueOf(p);
        final Comparable<T> propertyValueOfQ = (Comparable<T>) clause.getValueOf(q);
        chain = chain.compare(propertyValueOfP, propertyValueOfQ, clause.getDirection().getOrdering());
    }// www . j a v a  2  s. co  m
    return chain.result();
}

From source file:com.facebook.buck.android.aapt.AbstractAndroidResourceIndexEntry.java

@Override
public int compareTo(AbstractAndroidResourceIndexEntry that) {
    if (this == that) {
        return 0;
    }/*from   w w w  .j ava  2 s. co m*/

    ComparisonChain comparisonChain = ComparisonChain.start().compare(this.getType(), that.getType())
            .compare(this.getName(), that.getName());

    return comparisonChain.result();
}

From source file:org.pircbotx.hooks.Event.java

/**
 * Compare events by {@link #getTimestamp()} and then {@link #getId()} to
 * order by when they are received. This is useful for sorting lists of
 * Channel objects./*from   w  ww  . jav a 2  s  .  c om*/
 *
 * @param other Other Event to compare to
 * @return the result of the comparison
 */
public int compareTo(Event other) {
    ComparisonChain comparison = ComparisonChain.start().compare(getTimestamp(), other.getTimestamp())
            .compare(getId(), other.getId());
    if (bot != null && other.getBot() != null)
        comparison.compare(bot.getBotId(), other.getBot().getBotId());
    return comparison.result();
}

From source file:com.techcavern.pircbotz.hooks.Event.java

/**
 * Compare events by {@link #getTimestamp()} and then {@link #getId()} to 
 * order by when they are received. This is useful for sorting lists of Channel objects.
 * @param other Other Event to compare to
 * @return the result of the comparison/*from  w  ww. j  av a  2  s . c  om*/
 */
public int compareTo(Event<T> other) {
    ComparisonChain comparison = ComparisonChain.start().compare(getTimestamp(), other.getTimestamp())
            .compare(getId(), other.getId());
    if (bot != null && other.getBot() != null)
        comparison.compare(bot.getBotId(), other.getBot().getBotId());
    return comparison.result();
}

From source file:com.thinkbiganalytics.metadata.jpa.sla.JpaServiceLevelAssessment.java

@Override
public int compareTo(ServiceLevelAssessment sla) {

    ComparisonChain chain = ComparisonChain.start().compare(this.getResult(), sla.getResult())
            .compare(this.getAgreement().getName(), sla.getAgreement().getName());

    if (chain.result() != 0) {
        return chain.result();
    }/* w w w  . j a va 2s  .  co m*/

    List<ObligationAssessment> list1 = new ArrayList<>(this.getObligationAssessments());
    List<ObligationAssessment> list2 = new ArrayList<>(sla.getObligationAssessments());

    chain = chain.compare(list1.size(), list2.size());

    Collections.sort(list1);
    Collections.sort(list2);

    for (int idx = 0; idx < list1.size(); idx++) {
        chain = chain.compare(list1.get(idx), list2.get(idx));
    }

    return chain.result();
}

From source file:com.facebook.buck.rules.BuildTargetSourcePath.java

@Override
protected int compareReferences(BuildTargetSourcePath o) {
    if (o == this) {
        return 0;
    }//from  w w w.  j a v a  2  s. c  o  m

    ComparisonChain comparison = ComparisonChain.start().compare(target, o.target)
            .compareTrueFirst(resolvedPath.isPresent(), o.resolvedPath.isPresent());
    if (resolvedPath.isPresent() && o.resolvedPath.isPresent()) {
        comparison = comparison.compare(resolvedPath.get(), o.resolvedPath.get());
    }
    return comparison.result();
}