List of usage examples for com.google.common.primitives Longs compare
public static int compare(long a, long b)
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"); }// www.ja v a 2 s.co m // 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.lenskit.util.keys.Keys.java
public static <T> Ordering<T> keyOrdering(final KeyExtractor<T> ex) { return new Ordering<T>() { @Override/*w w w . ja va 2 s. com*/ public int compare(T left, T right) { return Longs.compare(ex.getKey(left), ex.getKey(right)); } }; }
From source file:io.druid.query.groupby.having.HavingSpecMetricComparator.java
static int compare(Row row, String aggregationName, Number value) { Object metricValueObj = row.getRaw(aggregationName); if (metricValueObj != null) { if (metricValueObj instanceof Long) { long l = ((Long) metricValueObj).longValue(); return Longs.compare(l, value.longValue()); } else if (metricValueObj instanceof String) { String metricValueStr = (String) metricValueObj; if (LONG_PAT.matcher(metricValueStr).matches()) { long l = row.getLongMetric(aggregationName); return Longs.compare(l, value.longValue()); }/*from w ww . j a v a 2 s. com*/ } } float f = row.getFloatMetric(aggregationName); return Floats.compare(f, value.floatValue()); }
From source file:com.twitter.hbc.twitter4j.parser.JSONObjectParser.java
public static StatusDeletionNotice parseStatusDelete(JSONObject json) throws JSONException { JSONObject statusDelete = json.getJSONObject("delete").getJSONObject("status"); final long statusId = statusDelete.getLong("id"); final long userId = statusDelete.getLong("user_id"); return new StatusDeletionNotice() { @Override//from w w w. j a va 2 s .c om public long getStatusId() { return statusId; } @Override public long getUserId() { return userId; } @Override public int compareTo(StatusDeletionNotice o) { return Longs.compare(getStatusId(), o.getStatusId()); } }; }
From source file:co.cask.cdap.common.stream.StreamEventComparator.java
@Override public int compare(StreamEvent o1, StreamEvent o2) { return Longs.compare(o1.getTimestamp(), o2.getTimestamp()); }
From source file:org.apache.druid.query.groupby.having.HavingSpecMetricComparator.java
static int compare(String aggregationName, Number value, Map<String, AggregatorFactory> aggregators, Object metricValueObj) {// ww w. j a va 2 s . c o m 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.b2international.snowowl.snomed.reasoner.server.classification.StorageKeyComparator.java
@Override public int compare(final SnomedConceptDocument o1, final SnomedConceptDocument o2) { return Longs.compare(o1.getStorageKey(), o2.getStorageKey()); }
From source file:io.airlift.discovery.store.Version.java
public Occurs compare(Version other) { int comparison = Longs.compare(sequence, other.sequence); if (comparison < 0) { return Occurs.BEFORE; } else if (comparison > 0) { return Occurs.AFTER; }/* w ww . ja v a 2 s. c om*/ return Occurs.SAME; }
From source file:com.foundationdb.server.types.mcompat.mcasts.MKeyComparables.java
private static TKeyComparable[] createIntComparisons() { final TComparison integerComparison = new TComparison() { @Override//from w w w. j a v a2 s .c o m public int compare(TInstance leftInstance, ValueSource left, TInstance rightInstance, ValueSource right) { if (left.isNull()) { if (right.isNull()) { return 0; } return -1; } if (right.isNull()) { return 1; } return Longs.compare(getLong(left), getLong(right)); } @Override public void copyComparables(ValueSource source, ValueTarget target) { ValueTargets.putLong(target, getLong(source)); } }; List<TClass> candidates = Arrays.<TClass>asList(MNumeric.TINYINT, MNumeric.SMALLINT, MNumeric.MEDIUMINT, MNumeric.INT, MNumeric.BIGINT, MNumeric.TINYINT_UNSIGNED, MNumeric.SMALLINT_UNSIGNED, MNumeric.MEDIUMINT_UNSIGNED, MNumeric.INT_UNSIGNED // BIGINT UNSIGNED is not here, because its representation is not a signed long ); Set<Set<TClass>> alreadyCreated = new HashSet<>(); List<TKeyComparable> results = new ArrayList<>(); for (TClass outer : candidates) { for (TClass inner : candidates) { if (inner == outer) continue; Set<TClass> pair = new HashSet<>(Arrays.asList(inner, outer)); if (alreadyCreated.add(pair)) { results.add(new TKeyComparable(outer, inner, integerComparison)); } } } return results.toArray(new TKeyComparable[results.size()]); }
From source file:com.metamx.druid.ResultGranularTimestampComparator.java
@Override public int compare(Result<T> r1, Result<T> r2) { return Longs.compare(gran.truncate(r1.getTimestamp().getMillis()), gran.truncate(r2.getTimestamp().getMillis())); }