Example usage for com.google.common.primitives Doubles compare

List of usage examples for com.google.common.primitives Doubles compare

Introduction

In this page you can find the example usage for com.google.common.primitives Doubles compare.

Prototype

public static int compare(double a, double b) 

Source Link

Document

Compares the two specified double values.

Usage

From source file:net.bafeimao.umbrella.support.util.Numbers.java

public static int compare(Object a, Object b) {

    if (!a.getClass().equals(b.getClass())) {
        throw new IllegalArgumentException("class type not the same");
    }/*from w  w w.j a  v  a 2s.com*/

    // TODO use instanceof instead
    Class<?> clazz = a.getClass();

    int result = 0;

    if (clazz.equals(Double.class)) {
        result = Doubles.compare((Double) a, (Double) b);
    } else if (clazz.equals(Float.class)) {
        result = Floats.compare((Float) a, (Float) b);
    } else if (clazz.equals(Long.class)) {
        result = Longs.compare((Long) b, (Long) b);
    } else if (clazz.equals(Integer.class)) {
        result = Ints.compare((Integer) a, (Integer) b);
    } else if (clazz.equals(Short.class)) {
        result = Shorts.compare((Short) a, (Short) b);
    } else if (clazz.equals(Byte.class)) {
        result = Shorts.compare((Short) a, (Short) b);
    }

    return result;
}

From source file:org.grouplens.lenskit.vectors.similarity.SpearmanRankCorrelation.java

static SparseVector rank(final SparseVector vec) {
    long[] ids = vec.keySet().toLongArray();
    // sort ID set by value (decreasing)
    LongArrays.quickSort(ids, new AbstractLongComparator() {
        @Override/*from ww w  . j a v  a  2s . c om*/
        public int compare(long k1, long k2) {
            return Doubles.compare(vec.get(k2), vec.get(k1));
        }
    });

    final int n = ids.length;
    final double[] values = new double[n];
    MutableSparseVector rank = vec.mutableCopy();
    // assign ranks to each item
    for (int i = 0; i < n; i++) {
        rank.set(ids[i], i + 1);
        values[i] = vec.get(ids[i]);
    }

    // average ranks for items with same values
    int i = 0;
    while (i < n) {
        int j;
        for (j = i + 1; j < n; j++) {
            // compare difference to 0 with tolerance - more robust
            if (!Scalars.isZero(values[j] - values[i])) {
                break;
            }
        }
        if (j - i > 1) {
            double r2 = (rank.get(ids[i]) + rank.get(ids[j - 1])) / (j - i);
            for (int k = i; k < j; k++) {
                rank.set(ids[k], r2);
            }
        }
        i = j;
    }

    // Make a sparse vector out of it
    return rank;
}

From source file:org.lenskit.similarity.SpearmanRankCorrelation.java

static Long2DoubleMap rank(final Long2DoubleMap vec) {
    long[] ids = vec.keySet().toLongArray();
    // sort ID set by value (decreasing)
    LongArrays.quickSort(ids, (k1, k2) -> Doubles.compare(vec.get(k2), vec.get(k1)));

    final int n = ids.length;
    final double[] values = new double[n];
    Long2DoubleMap rank = new Long2DoubleOpenHashMap(n);
    // assign ranks to each item
    for (int i = 0; i < n; i++) {
        rank.put(ids[i], i + 1);/*w  ww .j a v a  2  s .  c  om*/
        values[i] = vec.get(ids[i]);
    }

    // average ranks for items with same values
    int i = 0;
    while (i < n) {
        int j;
        for (j = i + 1; j < n; j++) {
            // compare difference to 0 with tolerance - more robust
            if (!Scalars.isZero(values[j] - values[i])) {
                break;
            }
        }
        if (j - i > 1) {
            double r2 = (rank.get(ids[i]) + rank.get(ids[j - 1])) / (j - i);
            for (int k = i; k < j; k++) {
                rank.put(ids[k], r2);
            }
        }
        i = j;
    }

    // Make a sparse vector out of it
    return rank;
}

From source file:org.apache.phoenix.schema.types.PUnsignedDouble.java

@Override
public int compareTo(Object lhs, Object rhs, PDataType rhsType) {
    if (rhsType == PDecimal.INSTANCE) {
        return -((BigDecimal) rhs).compareTo(BigDecimal.valueOf(((Number) lhs).doubleValue()));
    }//from  w  ww .ja  v  a 2s.c o  m
    return Doubles.compare(((Number) lhs).doubleValue(), ((Number) rhs).doubleValue());
}

From source file:org.kalypso.model.wspm.core.profil.ProfileObjectRecordWidthComparator.java

@Override
public int compare(final IProfileObjectRecord record1, final IProfileObjectRecord record2) {
    final Double b1 = record1.getBreite();
    final Double b2 = record2.getBreite();

    if (b1 == null)
        return -1;

    if (b2 == null)
        return +1;

    return Doubles.compare(b1, b2);
}

From source file:com.b2international.commons.http.AcceptHeader.java

@Override
public int compareTo(AcceptHeader<T> other) {
    return Doubles.compare(quality, other.quality);
}

From source file:org.n52.iceland.util.Comparables.java

public static int compare(double a, double b) {
    return Doubles.compare(a, b);
}

From source file:org.apache.druid.query.groupby.having.HavingSpecMetricComparator.java

static int compare(String aggregationName, Number value, Map<String, AggregatorFactory> aggregators,
        Object metricValueObj) {//from w w w. ja  va  2  s . c  om
    if (metricValueObj != null) {
        if (aggregators != null && aggregators.containsKey(aggregationName)) {
            metricValueObj = aggregators.get(aggregationName).finalizeComputation(metricValueObj);
        }

        if (metricValueObj instanceof Long || metricValueObj instanceof Integer) {
            // Cast int metrics to longs, it's fine since longs can represent all int values.
            long n = ((Number) metricValueObj).longValue();

            if (value instanceof Long || value instanceof Integer) {
                return Longs.compare(n, value.longValue());
            } else if (value instanceof Double || value instanceof Float) {
                // Invert the comparison since we're providing n, value backwards.
                return -compareDoubleToLong(value.doubleValue(), n);
            } else {
                throw new ISE("Number was[%s]?!?", value.getClass().getName());
            }
        } else if (metricValueObj instanceof Double || metricValueObj instanceof Float) {
            // Cast floats to doubles, it's fine since doubles can represent all float values.
            double n = ((Number) metricValueObj).doubleValue();

            if (value instanceof Long || value instanceof Integer) {
                return compareDoubleToLong(n, value.longValue());
            } else if (value instanceof Double || value instanceof Float) {
                return Doubles.compare(n, value.doubleValue());
            } else {
                throw new ISE("Number was[%s]?!?", value.getClass().getName());
            }
        } else if (metricValueObj instanceof String) {
            String metricValueStr = (String) metricValueObj;
            if (LONG_PAT.matcher(metricValueStr).matches()) {
                long l = Long.parseLong(metricValueStr);
                return Long.compare(l, value.longValue());
            } else {
                double d = Double.parseDouble(metricValueStr);
                return Double.compare(d, value.doubleValue());
            }
        } else {
            throw new ISE("Unknown type of metric value: %s", metricValueObj);
        }
    } else {
        return Double.compare(0, value.doubleValue());
    }
}

From source file:com.davidbracewell.collection.Sorting.java

/**
 * Compares two objects based on their hashcode.
 *
 * @param <T> the type of object being compared
 * @return A simplistic comparator that compares hash code values
 *//*from  www  .  ja v a 2  s .c o m*/
public static <T> Comparator<T> hashCodeComparator() {
    return new Comparator<T>() {
        @Override
        public int compare(T o1, T o2) {
            if (o1 == o2) {
                return 0;
            } else if (o1 == null) {
                return -1;
            } else if (o2 == null) {
                return 1;
            }
            return Doubles.compare(o1.hashCode(), o2.hashCode());
        }
    };
}

From source file:com.github.autermann.matlab.value.MatlabScalar.java

@Override
public int compareTo(MatlabScalar o) {
    return Doubles.compare(value(), checkNotNull(o).value());
}