Skip to content

Commit 873f582

Browse files
committed
[2.0.0-SNAPSHOT]
Builders NPE fixes Hashcode & Equals improved
1 parent aa25129 commit 873f582

15 files changed

+108
-170
lines changed

src/main/java/io/goodforgod/api/etherscan/model/Abi.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.goodforgod.api.etherscan.model;
22

33
import io.goodforgod.api.etherscan.util.BasicUtils;
4+
import java.util.Objects;
45

56
/**
67
* @author GoodforGod
@@ -40,27 +41,15 @@ public boolean isVerified() {
4041
public boolean equals(Object o) {
4142
if (this == o)
4243
return true;
43-
if (o == null || getClass() != o.getClass())
44+
if (!(o instanceof Abi))
4445
return false;
45-
4646
Abi abi = (Abi) o;
47-
48-
if (isVerified != abi.isVerified)
49-
return false;
50-
return contractAbi != null
51-
? contractAbi.equals(abi.contractAbi)
52-
: abi.contractAbi == null;
47+
return isVerified == abi.isVerified && Objects.equals(contractAbi, abi.contractAbi);
5348
}
5449

5550
@Override
5651
public int hashCode() {
57-
int result = contractAbi != null
58-
? contractAbi.hashCode()
59-
: 0;
60-
result = 31 * result + (isVerified
61-
? 1
62-
: 0);
63-
return result;
52+
return Objects.hash(contractAbi, isVerified);
6453
}
6554

6655
@Override

src/main/java/io/goodforgod/api/etherscan/model/Balance.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,15 @@ public BigInteger getEther() {
4848
public boolean equals(Object o) {
4949
if (this == o)
5050
return true;
51-
if (o == null || getClass() != o.getClass())
51+
if (!(o instanceof Balance))
5252
return false;
53-
5453
Balance balance1 = (Balance) o;
55-
56-
if (!balance.equals(balance1.balance))
57-
return false;
58-
return Objects.equals(address, balance1.address);
54+
return Objects.equals(balance, balance1.balance) && Objects.equals(address, balance1.address);
5955
}
6056

6157
@Override
6258
public int hashCode() {
63-
int result = balance.hashCode();
64-
result = 31 * result + (address != null
65-
? address.hashCode()
66-
: 0);
67-
return result;
59+
return Objects.hash(balance, address);
6860
}
6961

7062
@Override

src/main/java/io/goodforgod/api/etherscan/model/Block.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.math.BigInteger;
66
import java.time.LocalDateTime;
77
import java.time.ZoneOffset;
8+
import java.util.Objects;
89

910
/**
1011
* @author GoodforGod
@@ -40,17 +41,15 @@ public BigInteger getBlockReward() {
4041
public boolean equals(Object o) {
4142
if (this == o)
4243
return true;
43-
if (o == null || getClass() != o.getClass())
44+
if (!(o instanceof Block))
4445
return false;
45-
4646
Block block = (Block) o;
47-
4847
return blockNumber == block.blockNumber;
4948
}
5049

5150
@Override
5251
public int hashCode() {
53-
return (int) (blockNumber ^ (blockNumber >>> 32));
52+
return Objects.hash(blockNumber);
5453
}
5554

5655
@Override
@@ -98,8 +97,10 @@ public Block build() {
9897
Block block = new Block();
9998
block.blockNumber = this.blockNumber;
10099
block.blockReward = this.blockReward;
101-
block._timeStamp = this.timeStamp;
102-
block.timeStamp = String.valueOf(this.timeStamp.toEpochSecond(ZoneOffset.UTC));
100+
if (this.timeStamp != null) {
101+
block._timeStamp = this.timeStamp;
102+
block.timeStamp = String.valueOf(this.timeStamp.toEpochSecond(ZoneOffset.UTC));
103+
}
103104
return block;
104105
}
105106
}

src/main/java/io/goodforgod/api/etherscan/model/BlockUncle.java

+9-42
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.time.LocalDateTime;
66
import java.time.ZoneOffset;
77
import java.util.List;
8+
import java.util.Objects;
89

910
/**
1011
* @author GoodforGod
@@ -38,31 +39,16 @@ public int getUnclePosition() {
3839
public boolean equals(Object o) {
3940
if (this == o)
4041
return true;
41-
if (o == null || getClass() != o.getClass())
42+
if (!(o instanceof Uncle))
4243
return false;
43-
4444
Uncle uncle = (Uncle) o;
45-
if (unclePosition != uncle.unclePosition)
46-
return false;
47-
if (miner != null
48-
? !miner.equals(uncle.miner)
49-
: uncle.miner != null)
50-
return false;
51-
return blockreward != null
52-
? blockreward.equals(uncle.blockreward)
53-
: uncle.blockreward == null;
45+
return unclePosition == uncle.unclePosition && Objects.equals(miner, uncle.miner)
46+
&& Objects.equals(blockreward, uncle.blockreward);
5447
}
5548

5649
@Override
5750
public int hashCode() {
58-
int result = miner != null
59-
? miner.hashCode()
60-
: 0;
61-
result = 31 * result + (blockreward != null
62-
? blockreward.hashCode()
63-
: 0);
64-
result = 31 * result + unclePosition;
65-
return result;
51+
return Objects.hash(miner, blockreward, unclePosition);
6652
}
6753

6854
@Override
@@ -139,27 +125,6 @@ public String getUncleInclusionReward() {
139125
}
140126
// </editor-fold>
141127

142-
@Override
143-
public boolean equals(Object o) {
144-
if (this == o)
145-
return true;
146-
if (o == null || getClass() != o.getClass())
147-
return false;
148-
if (!super.equals(o))
149-
return false;
150-
151-
BlockUncle that = (BlockUncle) o;
152-
153-
return getBlockNumber() != 0 && getBlockNumber() == that.getBlockNumber();
154-
}
155-
156-
@Override
157-
public int hashCode() {
158-
int result = super.hashCode();
159-
result = (int) (31 * result + getBlockNumber());
160-
return result;
161-
}
162-
163128
@Override
164129
public String toString() {
165130
return "UncleBlock{" +
@@ -223,8 +188,10 @@ public BlockUncle build() {
223188
blockUncle.blockNumber = this.blockNumber;
224189
blockUncle.blockReward = this.blockReward;
225190
blockUncle.blockMiner = this.blockMiner;
226-
blockUncle._timeStamp = this.timeStamp;
227-
blockUncle.timeStamp = String.valueOf(this.timeStamp.toEpochSecond(ZoneOffset.UTC));
191+
if (this.timeStamp != null) {
192+
blockUncle._timeStamp = this.timeStamp;
193+
blockUncle.timeStamp = String.valueOf(this.timeStamp.toEpochSecond(ZoneOffset.UTC));
194+
}
228195
return blockUncle;
229196
}
230197
}

src/main/java/io/goodforgod/api/etherscan/model/Log.java

+17-34
Original file line numberDiff line numberDiff line change
@@ -125,40 +125,17 @@ public Long getLogIndex() {
125125
public boolean equals(Object o) {
126126
if (this == o)
127127
return true;
128-
if (o == null || getClass() != o.getClass())
128+
if (!(o instanceof Log))
129129
return false;
130-
131130
Log log = (Log) o;
132-
133-
if (!Objects.equals(blockNumber, log.blockNumber))
134-
return false;
135-
if (!Objects.equals(address, log.address))
136-
return false;
137-
if (!Objects.equals(transactionHash, log.transactionHash))
138-
return false;
139-
if (!Objects.equals(timeStamp, log.timeStamp))
140-
return false;
141-
return Objects.equals(logIndex, log.logIndex);
131+
return Objects.equals(blockNumber, log.blockNumber) && Objects.equals(address, log.address)
132+
&& Objects.equals(transactionHash, log.transactionHash) && Objects.equals(transactionIndex, log.transactionIndex)
133+
&& Objects.equals(logIndex, log.logIndex);
142134
}
143135

144136
@Override
145137
public int hashCode() {
146-
int result = blockNumber != null
147-
? blockNumber.hashCode()
148-
: 0;
149-
result = 31 * result + (address != null
150-
? address.hashCode()
151-
: 0);
152-
result = 31 * result + (transactionHash != null
153-
? transactionHash.hashCode()
154-
: 0);
155-
result = 31 * result + (timeStamp != null
156-
? timeStamp.hashCode()
157-
: 0);
158-
result = 31 * result + (logIndex != null
159-
? logIndex.hashCode()
160-
: 0);
161-
return result;
138+
return Objects.hash(blockNumber, address, transactionHash, transactionIndex, logIndex);
162139
}
163140

164141
@Override
@@ -255,17 +232,23 @@ public LogBuilder withLogIndex(Long logIndex) {
255232
public Log build() {
256233
Log log = new Log();
257234
log.address = this.address;
258-
log.gasPrice = String.valueOf(this.gasPrice);
259-
log._gasPrice = this.gasPrice;
235+
if (this.gasPrice != null) {
236+
log.gasPrice = String.valueOf(this.gasPrice);
237+
log._gasPrice = this.gasPrice;
238+
}
260239
log._logIndex = this.logIndex;
261240
log._transactionIndex = this.transactionIndex;
262-
log._gasUsed = this.gasUsed;
263241
log.blockNumber = String.valueOf(this.blockNumber);
264242
log.transactionIndex = String.valueOf(this.transactionIndex);
265-
log.timeStamp = String.valueOf(this.timeStamp);
243+
if (this.timeStamp != null) {
244+
log.timeStamp = String.valueOf(this.timeStamp.toEpochSecond(ZoneOffset.UTC));
245+
log._timeStamp = this.timeStamp;
246+
}
266247
log.data = this.data;
267-
log.gasUsed = String.valueOf(this.gasUsed);
268-
log._timeStamp = this.timeStamp;
248+
if (this.gasUsed != null) {
249+
log.gasUsed = String.valueOf(this.gasUsed);
250+
log._gasUsed = this.gasUsed;
251+
}
269252
log.logIndex = String.valueOf(this.logIndex);
270253
log._blockNumber = this.blockNumber;
271254
log.topics = this.topics;

src/main/java/io/goodforgod/api/etherscan/model/Price.java

+18-33
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.gson.annotations.Expose;
44
import java.time.LocalDateTime;
55
import java.time.ZoneOffset;
6+
import java.util.Objects;
67

78
/**
89
* @author GoodforGod
@@ -30,54 +31,34 @@ public double inBtc() {
3031
}
3132

3233
public LocalDateTime usdTimestamp() {
33-
if (_ethusd_timestamp == null)
34+
if (_ethusd_timestamp == null && ethusd_timestamp != null) {
3435
_ethusd_timestamp = LocalDateTime.ofEpochSecond(Long.parseLong(ethusd_timestamp), 0, ZoneOffset.UTC);
36+
}
3537
return _ethusd_timestamp;
3638
}
3739

3840
public LocalDateTime btcTimestamp() {
39-
if (_ethbtc_timestamp == null)
41+
if (_ethbtc_timestamp == null && ethbtc_timestamp != null) {
4042
_ethbtc_timestamp = LocalDateTime.ofEpochSecond(Long.parseLong(ethbtc_timestamp), 0, ZoneOffset.UTC);
43+
}
4144
return _ethbtc_timestamp;
4245
}
4346

4447
@Override
4548
public boolean equals(Object o) {
4649
if (this == o)
4750
return true;
48-
if (o == null || getClass() != o.getClass())
51+
if (!(o instanceof Price))
4952
return false;
50-
5153
Price price = (Price) o;
52-
53-
if (Double.compare(price.ethusd, ethusd) != 0)
54-
return false;
55-
if (Double.compare(price.ethbtc, ethbtc) != 0)
56-
return false;
57-
if (ethusd_timestamp != null
58-
? !ethusd_timestamp.equals(price.ethusd_timestamp)
59-
: price.ethusd_timestamp != null)
60-
return false;
61-
return (ethbtc_timestamp != null
62-
? !ethbtc_timestamp.equals(price.ethbtc_timestamp)
63-
: price.ethbtc_timestamp != null);
54+
return Double.compare(price.ethusd, ethusd) == 0 && Double.compare(price.ethbtc, ethbtc) == 0
55+
&& Objects.equals(ethusd_timestamp, price.ethusd_timestamp)
56+
&& Objects.equals(ethbtc_timestamp, price.ethbtc_timestamp);
6457
}
6558

6659
@Override
6760
public int hashCode() {
68-
int result;
69-
long temp;
70-
temp = Double.doubleToLongBits(ethusd);
71-
result = (int) (temp ^ (temp >>> 32));
72-
temp = Double.doubleToLongBits(ethbtc);
73-
result = 31 * result + (int) (temp ^ (temp >>> 32));
74-
result = 31 * result + (ethusd_timestamp != null
75-
? ethusd_timestamp.hashCode()
76-
: 0);
77-
result = 31 * result + (ethbtc_timestamp != null
78-
? ethbtc_timestamp.hashCode()
79-
: 0);
80-
return result;
61+
return Objects.hash(ethusd, ethbtc, ethusd_timestamp, ethbtc_timestamp);
8162
}
8263

8364
@Override
@@ -126,11 +107,15 @@ public PriceBuilder withEthBtcTimestamp(LocalDateTime ethbtcTimestamp) {
126107
public Price build() {
127108
Price price = new Price();
128109
price.ethbtc = this.ethbtc;
129-
price.ethbtc_timestamp = String.valueOf(this.ethbtcTimestamp.toEpochSecond(ZoneOffset.UTC));
130-
price._ethbtc_timestamp = this.ethbtcTimestamp;
131110
price.ethusd = this.ethusd;
132-
price.ethusd_timestamp = String.valueOf(this.ethusdTimestamp.toEpochSecond(ZoneOffset.UTC));
133-
price._ethusd_timestamp = this.ethusdTimestamp;
111+
if (this.ethbtcTimestamp != null) {
112+
price.ethbtc_timestamp = String.valueOf(this.ethbtcTimestamp.toEpochSecond(ZoneOffset.UTC));
113+
price._ethbtc_timestamp = this.ethbtcTimestamp;
114+
}
115+
if (this.ethusdTimestamp != null) {
116+
price.ethusd_timestamp = String.valueOf(this.ethusdTimestamp.toEpochSecond(ZoneOffset.UTC));
117+
price._ethusd_timestamp = this.ethusdTimestamp;
118+
}
134119
return price;
135120
}
136121
}

src/main/java/io/goodforgod/api/etherscan/model/Tx.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,12 @@ public Tx build() {
228228
tx.value = this.value;
229229
tx.transactionIndex = this.transactionIndex;
230230
tx.confirmations = this.confirmations;
231-
tx.timeStamp = String.valueOf(this.timeStamp.toEpochSecond(ZoneOffset.UTC));
231+
if (this.timeStamp != null) {
232+
tx.timeStamp = String.valueOf(this.timeStamp.toEpochSecond(ZoneOffset.UTC));
233+
tx._timeStamp = this.timeStamp;
234+
}
232235
tx.nonce = this.nonce;
233236
tx.blockNumber = this.blockNumber;
234-
tx._timeStamp = this.timeStamp;
235237
tx.to = this.to;
236238
tx.input = this.input;
237239
tx.cumulativeGasUsed = this.cumulativeGasUsed;

src/main/java/io/goodforgod/api/etherscan/model/TxErc1155.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,11 @@ public TxErc1155 build() {
235235
txERC721.contractAddress = this.contractAddress;
236236
txERC721.cumulativeGasUsed = this.cumulativeGasUsed;
237237
txERC721.tokenID = this.tokenID;
238-
txERC721.timeStamp = String.valueOf(this.timeStamp.toEpochSecond(ZoneOffset.UTC));
238+
if (this.timeStamp != null) {
239+
txERC721.timeStamp = String.valueOf(this.timeStamp.toEpochSecond(ZoneOffset.UTC));
240+
txERC721._timeStamp = this.timeStamp;
241+
}
239242
txERC721.blockNumber = this.blockNumber;
240-
txERC721._timeStamp = this.timeStamp;
241243
txERC721.tokenValue = this.tokenValue;
242244
txERC721.transactionIndex = this.transactionIndex;
243245
txERC721.to = this.to;

0 commit comments

Comments
 (0)