From 927d8e01915733baa86e0fb9842d29487265817e Mon Sep 17 00:00:00 2001 From: Taka Cho Date: Thu, 25 Jun 2020 12:10:00 -0400 Subject: replace an emptyArray and computeIfAbsent instead - some methods should return an empty array. - avoid the result of a java.util.Map.get() against null before proceeding with adding or changing the value in the map. - Declaring a variable only to immediately return or throw it is a bad practice. Issue-ID: POLICY-2616 Change-Id: I21a55c5ea716d8f64dc9155f84cabcfbcf5a22e0 Signed-off-by: Taka Cho --- .../onap/policy/drools/serverpool/TargetLock.java | 37 ++++++++-------------- 1 file changed, 13 insertions(+), 24 deletions(-) (limited to 'feature-server-pool') diff --git a/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/TargetLock.java b/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/TargetLock.java index 66ac58da..ef88abf2 100644 --- a/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/TargetLock.java +++ b/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/TargetLock.java @@ -147,6 +147,9 @@ public class TargetLock implements Lock, Serializable { private static final String QP_SERVER = "server"; private static final String QP_TTL = "ttl"; + // define a constant for empty of byte array + private static final byte[] EMPTY_BYTE_ARRAY = {}; + /** * This method triggers registration of 'eventHandler', and also extracts * property values. @@ -1524,7 +1527,7 @@ public class TargetLock implements Lock, Serializable { logger.error("Couldn't forward 'lock/dumpLocksData to uuid {}", serverUuid); - return null; + return EMPTY_BYTE_ARRAY; } return Base64.getEncoder().encode(Util.serialize(new HostData())); @@ -1641,11 +1644,7 @@ public class TargetLock implements Lock, Serializable { UUID uuid = cdr.identity.uuid; // fetch/generate 'MergeData' instance for this UUID - MergedData md = mergedDataMap.get(uuid); - if (md == null) { - md = new MergedData(uuid); - mergedDataMap.put(uuid, md); - } + MergedData md = mergedDataMap.computeIfAbsent(uuid, key -> new MergedData(uuid)); // update 'MergedData.clientDataRecord' if (md.clientDataRecord == null) { @@ -1680,11 +1679,7 @@ public class TargetLock implements Lock, Serializable { UUID uuid = le.currentOwnerUuid; // fetch/generate 'MergeData' instance for this UUID - MergedData md = mergedDataMap.get(uuid); - if (md == null) { - md = new MergedData(uuid); - mergedDataMap.put(uuid, md); - } + MergedData md = mergedDataMap.computeIfAbsent(uuid, key -> new MergedData(uuid)); // update 'lockEntries' table entry if (lockEntries.get(le.key) != null) { @@ -1728,11 +1723,7 @@ public class TargetLock implements Lock, Serializable { UUID uuid = waiting.ownerUuid; // fetch/generate 'MergeData' instance for this UUID - MergedData md = mergedDataMap.get(uuid); - if (md == null) { - md = new MergedData(uuid); - mergedDataMap.put(uuid, md); - } + MergedData md = mergedDataMap.computeIfAbsent(uuid, key -> new MergedData(uuid)); // update 'MergedData.serverLockEntry' and // 'MergedData.serverWaiting' @@ -2383,7 +2374,7 @@ public class TargetLock implements Lock, Serializable { return Base64.getEncoder().encode(Util.serialize(this)); } catch (IOException e) { logger.error("TargetLock.AuditData.encode Exception", e); - return null; + return EMPTY_BYTE_ARRAY; } } @@ -2557,7 +2548,7 @@ public class TargetLock implements Lock, Serializable { // if we reach this point, we didn't forward for some reason logger.error("Couldn't forward 'lock/audit to uuid {}", serverUuid); - return null; + return EMPTY_BYTE_ARRAY; } AuditData auditData = AuditData.decode(encodedData); @@ -2565,7 +2556,7 @@ public class TargetLock implements Lock, Serializable { AuditData auditResp = auditData.generateResponse(true); return auditResp.encode(); } - return null; + return EMPTY_BYTE_ARRAY; } /** @@ -2650,9 +2641,7 @@ public class TargetLock implements Lock, Serializable { // map 'key -> bucket number', and then 'bucket number' -> 'server' Server server = Bucket.bucketToServer(Bucket.bucketNumber(key)); if (server != null) { - AuditData auditData = - auditMap.computeIfAbsent(server, sk -> new AuditData()); - return auditData; + return auditMap.computeIfAbsent(server, sk -> new AuditData()); } // this happens when the bucket has not been assigned to a server yet @@ -2712,7 +2701,7 @@ public class TargetLock implements Lock, Serializable { // serialize byte[] encodedData = auditData.encode(); - if (encodedData == null) { + if (encodedData.length == 0) { // error has already been displayed return; } @@ -2779,7 +2768,7 @@ public class TargetLock implements Lock, Serializable { // serialize byte[] encodedData = auditData.encode(); - if (encodedData == null) { + if (encodedData.length == 0) { // error has already been displayed return; } -- cgit 1.2.3-korg