List of usage examples for com.google.common.primitives Longs compare
public static int compare(long a, long b)
From source file:com.facebook.stats.topk.HashBasedTopK.java
@Override public synchronized List<T> getTopK() { Comparator<T> comparator = new Comparator<T>() { public int compare(T key1, T key2) { return Longs.compare(counts.get(key1), counts.get(key2)); }/*from w w w . jav a2s. com*/ }; PriorityQueue<T> topK = new PriorityQueue<T>(k, comparator); for (Map.Entry<T, Long> entry : counts.entrySet()) { if (topK.size() < k) { topK.offer(entry.getKey()); } else if (entry.getValue() > counts.get(topK.peek())) { topK.offer(entry.getKey()); topK.poll(); } } LinkedList<T> sortedTopK = new LinkedList<T>(); while (!topK.isEmpty()) { sortedTopK.addFirst(topK.poll()); } return sortedTopK; }
From source file:org.jclouds.abiquo.domain.DomainWithTasksWrapper.java
public Iterable<AsyncTask<?, ?>> listTasks() { TasksDto result = context.getApi().getTaskApi().listTasks(target); List<AsyncTask<?, ?>> tasks = Lists.newArrayList(); for (TaskDto dto : result.getCollection()) { tasks.add(newTask(context, dto)); }//from w w w . j a v a2 s . c o m // Return the most recent task first Collections.sort(tasks, new Ordering<AsyncTask<?, ?>>() { @Override public int compare(final AsyncTask<?, ?> left, final AsyncTask<?, ?> right) { return Longs.compare(left.getTimestamp(), right.getTimestamp()); } }.reverse()); return tasks; }
From source file:io.druid.query.aggregation.datasketches.theta.SketchObjectStrategy.java
@Override public int compare(Object s1, Object s2) { if (s1 instanceof Sketch) { if (s2 instanceof Sketch) { return SketchAggregatorFactory.COMPARATOR.compare((Sketch) s1, (Sketch) s2); } else {// www .jav a 2 s . c o m return -1; } } if (s1 instanceof Memory) { if (s2 instanceof Memory) { Memory s1Mem = (Memory) s1; Memory s2Mem = (Memory) s2; // We have two Ordered Compact sketches, so just compare their last entry if they have the size. // This is to produce a deterministic ordering, though it might not match the actual estimate // ordering, but that's ok because this comparator is only used by GenericIndexed int retVal = Longs.compare(s1Mem.getCapacity(), s2Mem.getCapacity()); if (retVal == 0) { retVal = Longs.compare(s1Mem.getLong(s1Mem.getCapacity() - 8), s2Mem.getLong(s2Mem.getCapacity() - 8)); } return retVal; } else { return 1; } } throw new IAE("Unknwon class[%s], toString[%s]", s1.getClass(), s1); }
From source file:com.github.jeluard.stone.storage.memory.MemoryStorage.java
@Override public Iterable<Pair<Long, int[]>> all() throws IOException { final int maxSize = getMaximumSize(); final List<Pair<Long, int[]>> all = new ArrayList<Pair<Long, int[]>>(maxSize); for (int i = 0; i < maxSize; i++) { final long timestamp = this.timestamps.get(i); if (timestamp == 0L) { break; }/*from w w w . j a va2s . co m*/ all.add(new Pair<Long, int[]>(timestamp, this.values.get(i))); } Collections.sort(all, new Comparator<Pair<Long, int[]>>() { @Override public int compare(final Pair<Long, int[]> pair1, Pair<Long, int[]> pair2) { return Longs.compare(pair1.first, pair2.first); } }); return all; }
From source file:com.google.gerrit.pgm.util.SiteLibraryBasedDataSourceProvider.java
private void loadSiteLib() { File[] jars = libdir.listFiles(new FileFilter() { @Override// www .j av a 2 s. co m public boolean accept(File path) { String name = path.getName(); return (name.endsWith(".jar") || name.endsWith(".zip")) && path.isFile(); } }); if (jars != null && 0 < jars.length) { Arrays.sort(jars, new Comparator<File>() { @Override public int compare(File a, File b) { // Sort by reverse last-modified time so newer JARs are first. int cmp = Longs.compare(b.lastModified(), a.lastModified()); if (cmp != 0) { return cmp; } return a.getName().compareTo(b.getName()); } }); IoUtil.loadJARs(jars); } }
From source file:eugene.market.book.impl.DefaultOrderBook.java
/** * {@inheritDoc}//from w w w . jav a 2 s . c o m */ @Override public OrderStatus execute(final Side side, final Long orderQty, final BigDecimal price) { checkNotNull(side); checkNotNull(orderQty); checkNotNull(price); checkArgument(price.compareTo(Order.NO_PRICE) == 1); checkArgument(Longs.compare(orderQty, Order.NO_QTY) == 1); checkState(!getQueue(side).isEmpty()); final Order order = peek(side); final OrderStatus newOrderStatus = getOrderStatus(order).execute(price, orderQty); if (newOrderStatus.isFilled()) { final Order removedOrder = getQueue(side).poll(); checkState(order.equals(removedOrder)); orderStatusMap.remove(order); } else { orderStatusMap.put(order, newOrderStatus); } return newOrderStatus; }
From source file:io.druid.query.aggregation.datasketches.tuple.doublearray.SketchObjectStrategy.java
@Override public int compare(Object s1, Object s2) { if (s1 instanceof ArrayOfDoublesSketch) { if (s2 instanceof ArrayOfDoublesSketch) { return SketchAggregatorFactory.COMPARATOR.compare((ArrayOfDoublesSketch) s1, (ArrayOfDoublesSketch) s2); } else {/* www .j a v a 2 s .c o m*/ return -1; } } if (s1 instanceof Memory) { if (s2 instanceof Memory) { Memory s1Mem = (Memory) s1; Memory s2Mem = (Memory) s2; // We have two Ordered Compact sketches, so just compare their last entry if they have the size. // This is to produce a deterministic ordering, though it might not match the actual estimate // ordering, but that's ok because this comparator is only used by GenericIndexed int retVal = Longs.compare(s1Mem.getCapacity(), s2Mem.getCapacity()); if (retVal == 0) { retVal = Longs.compare(s1Mem.getLong(s1Mem.getCapacity() - 8), s2Mem.getLong(s2Mem.getCapacity() - 8)); } return retVal; } else { return 1; } } throw new IAE("Unknwon class[%s], toString[%s]", s1.getClass(), s1); }
From source file:org.apache.hadoop.hbase.CellComparator.java
public static int compareStatic(Cell a, Cell b, boolean onlyKey) { // row// w ww . j a v a 2 s . c o m int c = compareRows(a, b); if (c != 0) return c; c = compareWithoutRow(a, b); if (c != 0) return c; if (!onlyKey) { // Negate following comparisons so later edits show up first // compare log replay tag value if there is any // when either keyvalue tagged with log replay sequence number, we need to compare them: // 1) when both keyvalues have the tag, then use the tag values for comparison // 2) when one has and the other doesn't have, the one without the log // replay tag wins because // it means the edit isn't from recovery but new one coming from clients during recovery // 3) when both doesn't have, then skip to the next mvcc comparison long leftChangeSeqNum = getReplaySeqNum(a); long RightChangeSeqNum = getReplaySeqNum(b); if (leftChangeSeqNum != Long.MAX_VALUE || RightChangeSeqNum != Long.MAX_VALUE) { return Longs.compare(RightChangeSeqNum, leftChangeSeqNum); } // mvccVersion: later sorts first return Longs.compare(b.getMvccVersion(), a.getMvccVersion()); } else { return c; } }
From source file:org.janusgraph.graphdb.internal.AbstractElement.java
public static int compare(JanusGraphElement e1, JanusGraphElement e2) { long e1id = (e1 instanceof AbstractElement) ? ((AbstractElement) e1).getCompareId() : e1.longId(); long e2id = (e2 instanceof AbstractElement) ? ((AbstractElement) e2).getCompareId() : e2.longId(); return Longs.compare(e1id, e2id); }
From source file:gobblin.source.extractor.extract.LongWatermark.java
@Override public int compareTo(ComparableWatermark other) { Preconditions.checkArgument(other instanceof LongWatermark); return Longs.compare(this.value, ((LongWatermark) other).value); }