TxConfidenceTable: extract private methods getConfidence() and newConfidence()

This makes the code more functional and more readable.
This commit is contained in:
Sean Gilligan 2023-08-15 22:57:26 -07:00 committed by Andreas Schildbach
parent 0a4592f189
commit f998802df7

View file

@ -168,15 +168,10 @@ public class TxConfidenceTable {
Objects.requireNonNull(hash); Objects.requireNonNull(hash);
lock.lock(); lock.lock();
try { try {
WeakConfidenceReference reference = table.get(hash); TransactionConfidence confidence = getConfidence(hash);
if (reference != null) { return (confidence != null)
TransactionConfidence confidence = reference.get(); ? confidence
if (confidence != null) : newConfidence(hash);
return confidence;
}
TransactionConfidence newConfidence = confidenceFactory.createConfidence(hash);
table.put(hash, new WeakConfidenceReference(newConfidence, referenceQueue));
return newConfidence;
} finally { } finally {
lock.unlock(); lock.unlock();
} }
@ -190,16 +185,23 @@ public class TxConfidenceTable {
public TransactionConfidence get(Sha256Hash hash) { public TransactionConfidence get(Sha256Hash hash) {
lock.lock(); lock.lock();
try { try {
WeakConfidenceReference ref = table.get(hash); return getConfidence(hash);
if (ref == null)
return null;
TransactionConfidence confidence = ref.get();
if (confidence != null)
return confidence;
else
return null;
} finally { } finally {
lock.unlock(); lock.unlock();
} }
} }
// Internal: assumes lock is in place
@Nullable
private TransactionConfidence getConfidence(Sha256Hash hash) {
WeakConfidenceReference ref = table.get(hash);
return (ref != null) ? ref.get() : null;
}
// Internal: assumes lock is in place
private TransactionConfidence newConfidence(Sha256Hash hash) {
TransactionConfidence newConfidence = confidenceFactory.createConfidence(hash);
table.put(hash, new WeakConfidenceReference(newConfidence, referenceQueue));
return newConfidence;
}
} }