List of usage examples for com.google.common.primitives Longs compare
public static int compare(long a, long b)
From source file:org.apache.tajo.datum.TimeDatum.java
@Override public int compareTo(Datum datum) { if (datum.kind() == TajoDataTypes.Type.TIME) { TimeDatum another = (TimeDatum) datum; return Longs.compare(time, another.time); } else if (datum.isNull()) { return -1; } else {/*from ww w . ja va2 s . c o m*/ throw new InvalidOperationException(datum.type()); } }
From source file:org.apache.hadoop.hdfs.server.namenode.RedundantEditLogInputStream.java
RedundantEditLogInputStream(Collection<EditLogInputStream> streams, long startTxId) { this.curIdx = 0; this.prevTxId = (startTxId == HdfsServerConstants.INVALID_TXID) ? HdfsServerConstants.INVALID_TXID : (startTxId - 1);// ww w. j a v a 2 s . c om this.state = (streams.isEmpty()) ? State.EOF : State.SKIP_UNTIL; this.prevException = null; // EditLogInputStreams in a RedundantEditLogInputStream must be finalized, // and can't be pre-transactional. EditLogInputStream first = null; for (EditLogInputStream s : streams) { Preconditions.checkArgument(s.getFirstTxId() != HdfsServerConstants.INVALID_TXID, "invalid first txid in stream: %s", s); Preconditions.checkArgument(s.getLastTxId() != HdfsServerConstants.INVALID_TXID, "invalid last txid in stream: %s", s); if (first == null) { first = s; } else { Preconditions.checkArgument(s.getFirstTxId() == first.getFirstTxId(), "All streams in the RedundantEditLogInputStream must have the same " + "start transaction ID! " + first + " had start txId " + first.getFirstTxId() + ", but " + s + " had start txId " + s.getFirstTxId()); } } this.streams = streams.toArray(new EditLogInputStream[0]); // We sort the streams here so that the streams that end later come first. Arrays.sort(this.streams, new Comparator<EditLogInputStream>() { @Override public int compare(EditLogInputStream a, EditLogInputStream b) { return Longs.compare(b.getLastTxId(), a.getLastTxId()); } }); }
From source file:org.robotninjas.barge.state.Leader.java
/** * Find the median value of the list of matchIndex, this value is the committedIndex since, by definition, half of the * matchIndex values are greater and half are less than this value. So, at least half of the replicas have stored the * median value, this is the definition of committed. *//* w w w . j av a 2s . c o m*/ private void updateCommitted() { List<ReplicaManager> sorted = newArrayList(managers.values()); Collections.sort(sorted, new Comparator<ReplicaManager>() { @Override public int compare(ReplicaManager o, ReplicaManager o2) { return Longs.compare(o.getMatchIndex(), o2.getMatchIndex()); } }); final int middle = (int) Math.ceil(sorted.size() / 2.0); final long committed = sorted.get(middle).getMatchIndex(); log.commitIndex(committed); }
From source file:eugene.market.client.impl.OrderReferenceImpl.java
/** * Executes this <code>quantity</code> of this {@link Order} at this <code>price</code>. * * @param price price to execute at.//from w w w . j ava2s . co m * @param quantity quantity to execute */ public synchronized void execute(final BigDecimal price, final Long quantity) { checkNotNull(price); checkArgument(price.compareTo(Order.NO_PRICE) == 1); checkNotNull(quantity); checkArgument(Longs.compare(quantity, Order.NO_QTY) == 1); checkArgument(Longs.compare(quantity, this.leavesQty) <= 0); checkState(this.ordStatus.isNew() || this.ordStatus.isPartiallyFilled()); final BigDecimal quantityPrice = price.multiply(BigDecimal.valueOf(quantity)); final BigDecimal avgPxCumQty = this.avgPx.multiply(BigDecimal.valueOf(this.cumQty)); final BigDecimal quantityCumQty = BigDecimal.valueOf(quantity).add(BigDecimal.valueOf(this.cumQty)); this.avgPx = quantityPrice.add(avgPxCumQty).divide(quantityCumQty, RoundingMode.HALF_UP); this.leavesQty = this.leavesQty - quantity; this.cumQty = this.cumQty + quantity; this.ordStatus = this.leavesQty.equals(Long.valueOf(0L)) ? FILLED : PARTIALLY_FILLED; }
From source file:org.cinchapi.concourse.Timestamp.java
@Override public boolean equals(Object obj) { if (obj instanceof Timestamp) { return Longs.compare(microseconds, ((Timestamp) obj).microseconds) == 0; }/* w w w . j av a 2 s . c om*/ return false; }
From source file:hihex.cs.PidDatabase.java
/** * Obtains the list of running processes. Each item of the list is a map that can be supplied to a Chunk template * for rendering./*from w ww . ja v a 2 s . c o m*/ */ public synchronized List<HashMap<String, String>> runningProcesses() { final ArrayList<PidEntry> entries = mEntries; Collections.sort(entries, new Comparator<PidEntry>() { @Override public int compare(final PidEntry lhs, final PidEntry rhs) { final File aFile = new File("/proc/" + lhs.pid + "/comm"); final File bFile = new File("/proc/" + rhs.pid + "/comm"); final long aTime = aFile.lastModified(); final long bTime = bFile.lastModified(); int res = Longs.compare(bTime, aTime); if (res == 0) { res = Ints.compare(rhs.pid, lhs.pid); } return res; } }); return Lists.transform(entries, new Function<PidEntry, HashMap<String, String>>() { @Override public HashMap<String, String> apply(final PidEntry input) { final HashMap<String, String> summary = new HashMap<>(3); summary.put("pid", String.valueOf(input.pid)); summary.put("name", input.processName); if (input.writer.isPresent()) { summary.put("recording", "true"); } return summary; } }); }
From source file:org.jclouds.abiquo.environment.CloudTestEnvironment.java
public static Ordering<VirtualMachineTemplate> templateBySize() { return new Ordering<VirtualMachineTemplate>() { @Override//ww w . j a v a2 s .c o m public int compare(final VirtualMachineTemplate left, final VirtualMachineTemplate right) { return Longs.compare(left.getDiskFileSize(), right.getDiskFileSize()); } }; }
From source file:com.fullcontact.cassandra.io.compress.CompressionMetadata.java
/** * @param sections Collection of sections in uncompressed file * @return Array of chunks which corresponds to given sections of uncompressed file, sorted by chunk offset *///from w w w . j a va 2 s .c om public Chunk[] getChunksForSections(Collection<Pair<Long, Long>> sections) { // use SortedSet to eliminate duplicates and sort by chunk offset SortedSet<Chunk> offsets = new TreeSet<Chunk>(new Comparator<Chunk>() { public int compare(Chunk o1, Chunk o2) { return Longs.compare(o1.offset, o2.offset); } }); for (Pair<Long, Long> section : sections) { int startIndex = (int) (section.left / parameters.chunkLength()); int endIndex = (int) (section.right / parameters.chunkLength()); endIndex = section.right % parameters.chunkLength() == 0 ? endIndex - 1 : endIndex; for (int i = startIndex; i <= endIndex; i++) { long offset = i * 8; long chunkOffset = chunkOffsets.getLong(offset); long nextChunkOffset = offset + 8 == chunkOffsets.size() ? compressedFileLength : chunkOffsets.getLong(offset + 8); offsets.add(new Chunk(chunkOffset, (int) (nextChunkOffset - chunkOffset - 4))); // "4" bytes reserved for checksum } } return offsets.toArray(new Chunk[offsets.size()]); }
From source file:org.apache.hadoop.hbase.index.covered.CoveredColumnsIndexBuilder.java
/** * Batch all the {@link KeyValue}s in a {@link Mutation} by timestamp. Updates any * {@link KeyValue} with a timestamp == {@link HConstants#LATEST_TIMESTAMP} to the timestamp at * the time the method is called.//w w w . j av a 2 s. c o m * @param m {@link Mutation} from which to extract the {@link KeyValue}s * @return the mutation, broken into batches and sorted in ascending order (smallest first) */ protected Collection<Batch> createTimestampBatchesFromMutation(Mutation m) { Map<Long, Batch> batches = new HashMap<Long, Batch>(); for (List<KeyValue> family : m.getFamilyMap().values()) { createTimestampBatchesFromKeyValues(family, batches); } // sort the batches List<Batch> sorted = new ArrayList<Batch>(batches.values()); Collections.sort(sorted, new Comparator<Batch>() { @Override public int compare(Batch o1, Batch o2) { return Longs.compare(o1.getTimestamp(), o2.getTimestamp()); } }); return sorted; }
From source file:org.apache.phoenix.hbase.index.covered.CoveredColumnsIndexBuilder.java
/** * Batch all the {@link KeyValue}s in a {@link Mutation} by timestamp. Updates any * {@link KeyValue} with a timestamp == {@link HConstants#LATEST_TIMESTAMP} to the timestamp at * the time the method is called./* ww w .j a v a2 s . c om*/ * @param m {@link Mutation} from which to extract the {@link KeyValue}s * @return the mutation, broken into batches and sorted in ascending order (smallest first) */ protected Collection<Batch> createTimestampBatchesFromMutation(Mutation m) { Map<Long, Batch> batches = new HashMap<Long, Batch>(); for (List<Cell> family : m.getFamilyCellMap().values()) { List<KeyValue> familyKVs = KeyValueUtil.ensureKeyValues(family); createTimestampBatchesFromKeyValues(familyKVs, batches); } // sort the batches List<Batch> sorted = new ArrayList<Batch>(batches.values()); Collections.sort(sorted, new Comparator<Batch>() { @Override public int compare(Batch o1, Batch o2) { return Longs.compare(o1.getTimestamp(), o2.getTimestamp()); } }); return sorted; }