Skip to content

Commit

Permalink
Replace Guava primitive compare() with JDK7+ compare().
Browse files Browse the repository at this point in the history
In Java 7, Integer.compare() and Long.compare() were added. Use those
methods rather than the Guava equivalents which are “deprecated” for
JDK7+.

(cherry picked from commit 56443ee7542856af93e2411145e82cfea87152c8)
Signed-off-by: HashEngineering <[email protected]>
  • Loading branch information
msgilligan authored and HashEngineering committed Jun 25, 2024
1 parent 018bef6 commit 9e8bc19
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 22 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/org/bitcoinj/core/Coin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.bitcoinj.utils.MonetaryFormat;
import com.google.common.math.LongMath;
import com.google.common.primitives.Longs;

import java.io.Serializable;
import java.math.BigDecimal;
Expand Down Expand Up @@ -327,6 +326,6 @@ public int hashCode() {

@Override
public int compareTo(final Coin other) {
return Longs.compare(this.value, other.value);
return Long.compare(this.value, other.value);
}
}
9 changes: 2 additions & 7 deletions core/src/main/java/org/bitcoinj/core/PeerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.common.annotations.*;
import com.google.common.base.*;
import com.google.common.collect.*;
import com.google.common.primitives.*;
import com.google.common.util.concurrent.*;
import net.jcip.annotations.*;
import org.bitcoinj.core.listeners.*;
Expand Down Expand Up @@ -409,13 +408,9 @@ private PeerGroup(Context context, @Nullable AbstractBlockChain chain, @Nullable
public int compare(PeerAddress a, PeerAddress b) {
checkState(lock.isHeldByCurrentThread());
int result = backoffMap.get(a).compareTo(backoffMap.get(b));
if (result != 0)
return result;
result = Ints.compare(getPriority(a), getPriority(b));
if (result != 0)
return result;
// Sort by port if otherwise equals - for testing
result = Ints.compare(a.getPort(), b.getPort());
if (result == 0)
result = Integer.compare(a.getPort(), b.getPort());
return result;
}
});
Expand Down
7 changes: 3 additions & 4 deletions core/src/main/java/org/bitcoinj/core/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bouncycastle.crypto.params.KeyParameter;
Expand Down Expand Up @@ -77,7 +76,7 @@ public class Transaction extends ChildMessage {
public int compare(final Transaction tx1, final Transaction tx2) {
final long time1 = tx1.getUpdateTime().getTime();
final long time2 = tx2.getUpdateTime().getTime();
final int updateTimeComparison = -(Longs.compare(time1, time2));
final int updateTimeComparison = -(Long.compare(time1, time2));
//If time1==time2, compare by tx hash to make comparator consistent with equals
return updateTimeComparison != 0 ? updateTimeComparison : tx1.getTxId().compareTo(tx2.getTxId());
}
Expand All @@ -92,7 +91,7 @@ public int compare(final Transaction tx1, final Transaction tx2) {
final TransactionConfidence confidence2 = tx2.getConfidence();
final int height2 = confidence2.getConfidenceType() == ConfidenceType.BUILDING
? confidence2.getAppearedAtChainHeight() : Block.BLOCK_HEIGHT_UNKNOWN;
final int heightComparison = -(Ints.compare(height1, height2));
final int heightComparison = -(Integer.compare(height1, height2));
//If height1==height2, compare by tx hash to make comparator consistent with equals
return heightComparison != 0 ? heightComparison : tx1.getTxId().compareTo(tx2.getTxId());
}
Expand Down
42 changes: 42 additions & 0 deletions core/src/main/java/org/bitcoinj/core/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
Expand All @@ -40,6 +42,7 @@

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.io.Resources;
import com.google.common.primitives.UnsignedLongs;
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
Expand Down Expand Up @@ -614,6 +617,45 @@ public static void finishMockSleep() {
}
}

private static class Pair implements Comparable<Pair> {
int item, count;
public Pair(int item, int count) { this.count = count; this.item = item; }
// note that in this implementation compareTo() is not consistent with equals()
@Override public int compareTo(Pair o) { return -Integer.compare(count, o.count); }
}

public static int maxOfMostFreq(int... items) {
ArrayList<Integer> list = new ArrayList<>(items.length);
for (int item : items) list.add(item);
return maxOfMostFreq(list);
}

public static int maxOfMostFreq(List<Integer> items) {
if (items.isEmpty())
return 0;
// This would be much easier in a functional language (or in Java 8).
items = Ordering.natural().reverse().sortedCopy(items);
LinkedList<Pair> pairs = Lists.newLinkedList();
pairs.add(new Pair(items.get(0), 0));
for (int item : items) {
Pair pair = pairs.getLast();
if (pair.item != item)
pairs.add((pair = new Pair(item, 0)));
pair.count++;
}
// pairs now contains a uniqified list of the sorted inputs, with counts for how often that item appeared.
// Now sort by how frequently they occur, and pick the max of the most frequent.
Collections.sort(pairs);
int maxCount = pairs.getFirst().count;
int maxItem = pairs.getFirst().item;
for (Pair pair : pairs) {
if (pair.count != maxCount)
break;
maxItem = Math.max(maxItem, pair.item);
}
return maxItem;
}

private enum Runtime {
ANDROID, OPENJDK, ORACLE_JAVA
}
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/org/bitcoinj/crypto/ChildNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.util.List;
import java.util.Locale;

import com.google.common.primitives.Ints;

/**
* <p>This is just a wrapper for the i (child number) as per BIP 32 with a boolean getter for the most significant bit
* and a getter for the actual 0-based child number. A {@link List} of these forms a <i>path</i> through a
Expand Down Expand Up @@ -107,6 +105,6 @@ public int hashCode() {
@Override
public int compareTo(ChildNumber other) {
// note that in this implementation compareTo() is not consistent with equals()
return Ints.compare(this.num(), other.num());
return Integer.compare(this.num(), other.num());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.bitcoinj.utils;

import org.bitcoinj.core.Utils;
import com.google.common.primitives.Longs;

import static com.google.common.base.Preconditions.checkArgument;

Expand Down Expand Up @@ -92,7 +91,7 @@ public long getRetryTime() {
@Override
public int compareTo(ExponentialBackoff other) {
// note that in this implementation compareTo() is not consistent with equals()
return Longs.compare(retryTime, other.retryTime);
return Long.compare(retryTime, other.retryTime);
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/bitcoinj/utils/Fiat.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.bitcoinj.core.Monetary;
import com.google.common.base.Objects;
import com.google.common.math.LongMath;
import com.google.common.primitives.Longs;

/**
* Represents a monetary fiat value. It was decided to not fold this into {@link Coin} because of type
Expand Down Expand Up @@ -234,6 +233,6 @@ public int hashCode() {
public int compareTo(final Fiat other) {
if (!this.currencyCode.equals(other.currencyCode))
return this.currencyCode.compareTo(other.currencyCode);
return Longs.compare(this.value, other.value);
return Long.compare(this.value, other.value);
}
}
3 changes: 1 addition & 2 deletions core/src/main/java/org/bitcoinj/wallet/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.google.common.annotations.*;
import com.google.common.collect.*;
import com.google.common.primitives.*;
import com.google.common.util.concurrent.*;
import com.google.protobuf.*;
import net.jcip.annotations.*;
Expand Down Expand Up @@ -4786,7 +4785,7 @@ public TxOffsetPair(Transaction tx, int offset) {

@Override public int compareTo(TxOffsetPair o) {
// note that in this implementation compareTo() is not consistent with equals()
return Ints.compare(offset, o.offset);
return Integer.compare(offset, o.offset);
}
}

Expand Down

0 comments on commit 9e8bc19

Please sign in to comment.