3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 18:25:51 +00:00

Improved consistency and formal correctness of all existing .equals methods to meet IntelliJ's convention.

* added null handling so equals methods return false for null argument
* replaced instanceof with getClass to force strict type equality
* added @Override for equals and hashCode where missing
* minor refactorings in equals methods to simplify and improve consistency
* added missing hashCode for ListMessage based on equals definition

Things that HAVE NOT changed:

* set of attributes used for equality checking
* hashCode calculation (except for added hashCode in ListMessage)
* correlation between equals and hashCode
* no new equals methods added
This commit is contained in:
Piotr Włodarek 2014-05-22 20:32:54 +02:00
parent ff8d76cf7e
commit b32d0cce17
27 changed files with 160 additions and 140 deletions

View File

@ -805,8 +805,8 @@ public class Block extends Message {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof Block)) if (this == o) return true;
return false; if (o == null || getClass() != o.getClass()) return false;
Block other = (Block) o; Block other = (Block) o;
return getHash().equals(other.getHash()); return getHash().equals(other.getHash());
} }

View File

@ -266,11 +266,13 @@ public class BloomFilter extends Message {
} }
@Override @Override
public boolean equals(Object other) { public boolean equals(Object o) {
return other instanceof BloomFilter && if (this == o) return true;
((BloomFilter) other).hashFuncs == this.hashFuncs && if (o == null || getClass() != o.getClass()) return false;
((BloomFilter) other).nTweak == this.nTweak && BloomFilter other = (BloomFilter) o;
Arrays.equals(((BloomFilter) other).data, this.data); return hashFuncs == other.hashFuncs &&
nTweak == other.nTweak &&
Arrays.equals(data, other.data);
} }
@Override @Override

View File

@ -79,19 +79,13 @@ public class DumpedPrivateKey extends VersionedChecksummedBytes {
} }
@Override @Override
public boolean equals(Object other) { public boolean equals(Object o) {
// This odd construction is to avoid anti-symmetry of equality: where a.equals(b) != b.equals(a). if (this == o) return true;
boolean result = false; if (o == null || getClass() != o.getClass()) return false;
if (other instanceof VersionedChecksummedBytes) { DumpedPrivateKey other = (DumpedPrivateKey) o;
result = Arrays.equals(bytes, ((VersionedChecksummedBytes)other).bytes); return Arrays.equals(bytes, other.bytes) &&
} version == other.version &&
if (other instanceof DumpedPrivateKey) { compressed == other.compressed;
DumpedPrivateKey o = (DumpedPrivateKey) other;
result = Arrays.equals(bytes, o.bytes) &&
version == o.version &&
compressed == o.compressed;
}
return result;
} }
@Override @Override

View File

@ -402,13 +402,9 @@ public class ECKey implements Serializable {
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
ECDSASignature other = (ECDSASignature) o;
ECDSASignature signature = (ECDSASignature) o; return r.equals(other.r) &&
s.equals(other.s);
if (!r.equals(signature.r)) return false;
if (!s.equals(signature.s)) return false;
return true;
} }
@Override @Override
@ -828,10 +824,8 @@ public class ECKey implements Serializable {
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
ECKey other = (ECKey) o;
ECKey ecKey = (ECKey) o; return Arrays.equals(pub, other.pub);
return Arrays.equals(pub, ecKey.pub);
} }
@Override @Override

View File

@ -99,11 +99,13 @@ public class GetBlocksMessage extends Message {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o == null || o.getClass() != getClass()) return false; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GetBlocksMessage other = (GetBlocksMessage) o; GetBlocksMessage other = (GetBlocksMessage) o;
return (other.version == version && return version == other.version &&
locator.size() == other.locator.size() && locator.containsAll(other.locator) && locator.size() == other.locator.size() &&
stopHash.equals(other.stopHash)); locator.containsAll(other.locator) &&
stopHash.equals(other.stopHash);
} }
@Override @Override

View File

@ -50,11 +50,13 @@ public class GetHeadersMessage extends GetBlocksMessage {
*/ */
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o == null || o.getClass() != getClass()) return false; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GetHeadersMessage other = (GetHeadersMessage) o; GetHeadersMessage other = (GetHeadersMessage) o;
return (other.version == version && return version == other.version &&
locator.size() == other.locator.size() && locator.containsAll(other.locator) && locator.size() == other.locator.size() &&
stopHash.equals(other.stopHash)); locator.containsAll(other.locator) &&
stopHash.equals(other.stopHash);
} }
@Override @Override

View File

@ -43,13 +43,17 @@ public class InventoryItem {
return type.toString() + ": " + hash; return type.toString() + ": " + hash;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InventoryItem other = (InventoryItem) o;
return type == other.type &&
hash.equals(other.hash);
}
@Override
public int hashCode() { public int hashCode() {
return hash.hashCode() + type.ordinal(); return hash.hashCode() + type.ordinal();
} }
public boolean equals(Object o) {
return o instanceof InventoryItem &&
((InventoryItem)o).type == this.type &&
((InventoryItem)o).hash.equals(this.hash);
}
} }

View File

@ -34,7 +34,6 @@ public abstract class ListMessage extends Message {
public static final long MAX_INVENTORY_ITEMS = 50000; public static final long MAX_INVENTORY_ITEMS = 50000;
public ListMessage(NetworkParameters params, byte[] bytes) throws ProtocolException { public ListMessage(NetworkParameters params, byte[] bytes) throws ProtocolException {
super(params, bytes, 0); super(params, bytes, 0);
} }
@ -44,7 +43,6 @@ public abstract class ListMessage extends Message {
super(params, msg, 0, parseLazy, parseRetain, length); super(params, msg, 0, parseLazy, parseRetain, length);
} }
public ListMessage(NetworkParameters params) { public ListMessage(NetworkParameters params) {
super(params); super(params);
items = new ArrayList<InventoryItem>(); items = new ArrayList<InventoryItem>();
@ -124,7 +122,14 @@ public abstract class ListMessage extends Message {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
return o.getClass() == this.getClass() && if (this == o) return true;
((ListMessage)o).items.equals(this.items); if (o == null || getClass() != o.getClass()) return false;
ListMessage other = (ListMessage) o;
return items.equals(other.items);
}
@Override
public int hashCode() {
return items.hashCode();
} }
} }

View File

@ -181,10 +181,11 @@ public abstract class NetworkParameters implements Serializable {
public abstract String getPaymentProtocolId(); public abstract String getPaymentProtocolId();
@Override @Override
public boolean equals(Object other) { public boolean equals(Object o) {
if (!(other instanceof NetworkParameters)) return false; if (this == o) return true;
NetworkParameters o = (NetworkParameters) other; if (o == null || getClass() != o.getClass()) return false;
return o.getId().equals(getId()); NetworkParameters other = (NetworkParameters) o;
return getId().equals(other.getId());
} }
@Override @Override

View File

@ -246,13 +246,14 @@ public class PeerAddress extends ChildMessage {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof PeerAddress)) return false; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PeerAddress other = (PeerAddress) o; PeerAddress other = (PeerAddress) o;
return other.addr.equals(addr) && return other.addr.equals(addr) &&
other.port == port && other.port == port &&
other.services.equals(services) && other.services.equals(services) &&
other.time == time; other.time == time;
//FIXME including services and time could cause same peer to be added multiple times in collections //TODO: including services and time could cause same peer to be added multiple times in collections
} }
@Override @Override

View File

@ -151,11 +151,13 @@ public class RejectMessage extends Message {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
return o instanceof RejectMessage && if (this == o) return true;
((RejectMessage) o).message.equals(message) && if (o == null || getClass() != o.getClass()) return false;
((RejectMessage) o).code.equals(code) && RejectMessage other = (RejectMessage) o;
((RejectMessage) o).reason.equals(reason) && return message.equals(other.message) &&
((RejectMessage) o).messageHash.equals(messageHash); code.equals(other.code) &&
reason.equals(other.reason) &&
messageHash.equals(other.messageHash);
} }
@Override @Override

View File

@ -88,13 +88,12 @@ public class Sha256Hash implements Serializable, Comparable {
} }
} }
/**
* Returns true if the hashes are equal.
*/
@Override @Override
public boolean equals(Object other) { public boolean equals(Object o) {
if (!(other instanceof Sha256Hash)) return false; if (this == o) return true;
return Arrays.equals(bytes, ((Sha256Hash) other).bytes); if (o == null || getClass() != o.getClass()) return false;
Sha256Hash other = (Sha256Hash) o;
return Arrays.equals(bytes, other.bytes);
} }
/** /**

View File

@ -82,10 +82,13 @@ public class StoredBlock implements Serializable {
} }
@Override @Override
public boolean equals(Object other) { public boolean equals(Object o) {
if (!(other instanceof StoredBlock)) return false; if (this == o) return true;
StoredBlock o = (StoredBlock) other; if (o == null || getClass() != o.getClass()) return false;
return o.header.equals(header) && o.chainWork.equals(chainWork) && o.height == height; StoredBlock other = (StoredBlock) o;
return header.equals(other.header) &&
chainWork.equals(other.chainWork) &&
height == other.height;
} }
@Override @Override
@ -94,7 +97,6 @@ public class StoredBlock implements Serializable {
return header.hashCode() ^ chainWork.hashCode() ^ height; return header.hashCode() ^ chainWork.hashCode() ^ height;
} }
/** /**
* Creates a new StoredBlock, calculating the additional fields by adding to the values in this block. * Creates a new StoredBlock, calculating the additional fields by adding to the values in this block.
*/ */

View File

@ -142,14 +142,18 @@ public class StoredTransactionOutput implements Serializable {
return String.format("Stored TxOut of %s (%s:%d)", Utils.bitcoinValueToFriendlyString(value), hash.toString(), index); return String.format("Stored TxOut of %s (%s:%d)", Utils.bitcoinValueToFriendlyString(value), hash.toString(), index);
} }
@Override
public int hashCode() { public int hashCode() {
return hash.hashCode() + (int)index; return hash.hashCode() + (int)index;
} }
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof StoredTransactionOutput)) return false; if (this == o) return true;
return ((StoredTransactionOutput) o).getIndex() == this.getIndex() && if (o == null || getClass() != o.getClass()) return false;
((StoredTransactionOutput) o).getHash().equals(this.getHash()); StoredTransactionOutput other = (StoredTransactionOutput) o;
return getHash().equals(other.getHash()) &&
getIndex() == other.getIndex();
} }
public void serializeToStream(OutputStream bos) throws IOException { public void serializeToStream(OutputStream bos) throws IOException {
@ -169,4 +173,4 @@ public class StoredTransactionOutput implements Serializable {
bos.write(0xFF & (height >> 16)); bos.write(0xFF & (height >> 16));
bos.write(0xFF & (height >> 24)); bos.write(0xFF & (height >> 24));
} }
} }

View File

@ -22,7 +22,7 @@ import java.util.List;
/** /**
* Contains minimal data neccessary to disconnect/connect the transactions * Contains minimal data neccessary to disconnect/connect the transactions
* in the stored block at will. Can either store the full set of * in the stored block at will. Can either store the full set of
* transactions (if the inputs for the block have not been tested to work) * transactions (if the inputs for the block have not been tested to work)
* or the set of transaction outputs created/destroyed when the block is * or the set of transaction outputs created/destroyed when the block is
* connected. * connected.
@ -70,14 +70,18 @@ public class StoredUndoableBlock implements Serializable {
public Sha256Hash getHash() { public Sha256Hash getHash() {
return blockHash; return blockHash;
} }
@Override
public int hashCode() { public int hashCode() {
return blockHash.hashCode(); return blockHash.hashCode();
} }
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof StoredUndoableBlock)) return false; if (this == o) return true;
return ((StoredUndoableBlock)o).getHash().equals(this.getHash()); if (o == null || getClass() != o.getClass()) return false;
StoredUndoableBlock other = (StoredUndoableBlock) o;
return getHash().equals(other.getHash());
} }
@Override @Override

View File

@ -1176,11 +1176,11 @@ public class Transaction extends ChildMessage implements Serializable {
} }
@Override @Override
public boolean equals(Object other) { public boolean equals(Object o) {
if (!(other instanceof Transaction)) return false; if (this == o) return true;
Transaction t = (Transaction) other; if (o == null || getClass() != o.getClass()) return false;
Transaction other = (Transaction) o;
return t.getHash().equals(getHash()); return getHash().equals(other.getHash());
} }
@Override @Override

View File

@ -89,7 +89,6 @@ public class TransactionInput extends ChildMessage implements Serializable {
scriptBytes = EMPTY_ARRAY; scriptBytes = EMPTY_ARRAY;
sequence = NO_SEQUENCE; sequence = NO_SEQUENCE;
this.parentTransaction = parentTransaction; this.parentTransaction = parentTransaction;
length = 41; length = 41;
} }

View File

@ -190,10 +190,12 @@ public class TransactionOutPoint extends ChildMessage implements Serializable {
} }
@Override @Override
public boolean equals(Object other) { public boolean equals(Object o) {
if (!(other instanceof TransactionOutPoint)) return false; if (this == o) return true;
TransactionOutPoint o = (TransactionOutPoint) other; if (o == null || getClass() != o.getClass()) return false;
return o.getIndex() == getIndex() && o.getHash().equals(getHash()); TransactionOutPoint other = (TransactionOutPoint) o;
return getIndex() == other.getIndex() &&
getHash().equals(other.getHash());
} }
@Override @Override

View File

@ -352,11 +352,11 @@ public class TransactionOutput extends ChildMessage implements Serializable {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
TransactionOutput output = (TransactionOutput) o; TransactionOutput other = (TransactionOutput) o;
if (!Arrays.equals(scriptBytes, output.scriptBytes)) return false; if (!Arrays.equals(scriptBytes, other.scriptBytes)) return false;
if (value != null ? !value.equals(output.value) : output.value != null) return false; if (value != null ? !value.equals(other.value) : other.value != null) return false;
if (parentTransaction != null && parentTransaction != output.parentTransaction) return false; if (parentTransaction != null && parentTransaction != other.parentTransaction) return false;
return true; return true;
} }

View File

@ -201,7 +201,8 @@ public class VersionMessage extends Message {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof VersionMessage)) return false; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VersionMessage other = (VersionMessage) o; VersionMessage other = (VersionMessage) o;
return other.bestHeight == bestHeight && return other.bestHeight == bestHeight &&
other.clientVersion == clientVersion && other.clientVersion == clientVersion &&
@ -213,6 +214,12 @@ public class VersionMessage extends Message {
other.relayTxesBeforeFilter == relayTxesBeforeFilter; other.relayTxesBeforeFilter == relayTxesBeforeFilter;
} }
@Override
public int hashCode() {
return (int) bestHeight ^ clientVersion ^ (int) localServices ^ (int) time ^ subVer.hashCode() ^ myAddr.hashCode()
^ theirAddr.hashCode() * (relayTxesBeforeFilter ? 1 : 2);
}
/** /**
* VersionMessage does not handle cached byte array so should not have a cached checksum. * VersionMessage does not handle cached byte array so should not have a cached checksum.
*/ */
@ -229,12 +236,6 @@ public class VersionMessage extends Message {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public int hashCode() {
return (int) bestHeight ^ clientVersion ^ (int) localServices ^ (int) time ^ subVer.hashCode() ^ myAddr.hashCode()
^ theirAddr.hashCode() * (relayTxesBeforeFilter ? 1 : 2);
}
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("\n"); sb.append("\n");

View File

@ -58,6 +58,7 @@ public class VersionedChecksummedBytes {
return Base58.encode(addressBytes); return Base58.encode(addressBytes);
} }
// TODO: shouldn't hashCode be also based on the version?
@Override @Override
public int hashCode() { public int hashCode() {
return Arrays.hashCode(bytes); return Arrays.hashCode(bytes);
@ -65,9 +66,10 @@ public class VersionedChecksummedBytes {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof VersionedChecksummedBytes)) return false; if (this == o) return true;
VersionedChecksummedBytes vcb = (VersionedChecksummedBytes) o; if (o == null || getClass() != o.getClass()) return false;
return Arrays.equals(vcb.bytes, bytes); VersionedChecksummedBytes other = (VersionedChecksummedBytes) o;
return Arrays.equals(bytes, other.bytes);
} }
/** /**

View File

@ -64,7 +64,10 @@ public class ChildNumber {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
return this == o || !(o == null || getClass() != o.getClass()) && i == ((ChildNumber) o).i; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChildNumber other = (ChildNumber) o;
return i == other.i;
} }
@Override @Override

View File

@ -15,6 +15,8 @@
*/ */
package com.google.bitcoin.crypto; package com.google.bitcoin.crypto;
import com.google.common.base.Objects;
import java.util.Arrays; import java.util.Arrays;
/** /**
@ -99,17 +101,12 @@ public class EncryptedPrivateKey {
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object o) {
if (obj == null) { if (this == o) return true;
return false; if (o == null || getClass() != o.getClass()) return false;
} EncryptedPrivateKey other = (EncryptedPrivateKey) o;
if (getClass() != obj.getClass()) { return Objects.equal(initialisationVector, other.initialisationVector) &&
return false; Objects.equal(encryptedPrivateBytes, other.encryptedPrivateBytes);
}
final EncryptedPrivateKey other = (EncryptedPrivateKey) obj;
return com.google.common.base.Objects.equal(this.initialisationVector, other.initialisationVector)
&& com.google.common.base.Objects.equal(this.encryptedPrivateBytes, other.encryptedPrivateBytes);
} }
@Override @Override

View File

@ -16,6 +16,7 @@
*/ */
package com.google.bitcoin.crypto; package com.google.bitcoin.crypto;
import com.google.common.base.Objects;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import com.lambdaworks.crypto.SCrypt; import com.lambdaworks.crypto.SCrypt;
import org.bitcoinj.wallet.Protos; import org.bitcoinj.wallet.Protos;
@ -255,15 +256,10 @@ public class KeyCrypterScrypt implements KeyCrypter, Serializable {
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object o) {
if (obj == null) { if (this == o) return true;
return false; if (o == null || getClass() != o.getClass()) return false;
} KeyCrypterScrypt other = (KeyCrypterScrypt) o;
if (getClass() != obj.getClass()) { return Objects.equal(scryptParameters, other.scryptParameters);
return false;
}
final KeyCrypterScrypt other = (KeyCrypterScrypt) obj;
return com.google.common.base.Objects.equal(this.scryptParameters, other.scryptParameters);
} }
} }

View File

@ -1279,11 +1279,11 @@ public class Script {
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object o) {
if (!(obj instanceof Script)) if (this == o) return true;
return false; if (o == null || getClass() != o.getClass()) return false;
Script s = (Script)obj; Script other = (Script) o;
return Arrays.equals(getQuickProgram(), s.getQuickProgram()); return Arrays.equals(getQuickProgram(), other.getQuickProgram());
} }
@Override @Override

View File

@ -102,11 +102,11 @@ public class ScriptChunk {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
ScriptChunk chunk = (ScriptChunk) o; ScriptChunk other = (ScriptChunk) o;
if (opcode != chunk.opcode) return false; if (opcode != other.opcode) return false;
if (startLocationInProgram != chunk.startLocationInProgram) return false; if (startLocationInProgram != other.startLocationInProgram) return false;
if (!Arrays.equals(data, chunk.data)) return false; if (!Arrays.equals(data, other.data)) return false;
return true; return true;
} }

View File

@ -60,7 +60,8 @@ class StoredTransactionOutPoint implements Serializable {
long getIndex() { long getIndex() {
return index; return index;
} }
@Override
public int hashCode() { public int hashCode() {
return this.hash.hashCode() + (int)index; return this.hash.hashCode() + (int)index;
} }
@ -68,11 +69,14 @@ class StoredTransactionOutPoint implements Serializable {
public String toString() { public String toString() {
return "Stored transaction out point: " + hash.toString() + ":" + index; return "Stored transaction out point: " + hash.toString() + ":" + index;
} }
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof StoredTransactionOutPoint)) return false; if (this == o) return true;
return ((StoredTransactionOutPoint)o).getIndex() == this.index && if (o == null || getClass() != o.getClass()) return false;
Objects.equal(this.getHash(), ((StoredTransactionOutPoint)o).getHash()); StoredTransactionOutPoint other = (StoredTransactionOutPoint) o;
return getIndex() == other.getIndex() &&
Objects.equal(getHash(), other.getHash());
} }
} }