[tor-commits] [collector/release] Use Map.putIfAbsent and Map.getOrDefault where possible.

karsten at torproject.org karsten at torproject.org
Mon Oct 15 14:39:13 UTC 2018


commit 4361bd5e5f1d68ba122f01ee0221dc7ab0f9712c
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Tue Aug 21 14:58:17 2018 +0200

    Use Map.putIfAbsent and Map.getOrDefault where possible.
---
 .../collector/relaydescs/ArchiveReader.java        |  7 ++----
 .../collector/relaydescs/ArchiveWriter.java        | 29 ++++++----------------
 .../relaydescs/RelayDescriptorDownloader.java      |  7 ++----
 3 files changed, 12 insertions(+), 31 deletions(-)

diff --git a/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveReader.java b/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveReader.java
index 4e0e28d..27010a9 100644
--- a/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveReader.java
+++ b/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveReader.java
@@ -298,11 +298,8 @@ public class ArchiveReader {
   void haveParsedMicrodescConsensus(String validAfterTime,
       SortedSet<String> microdescriptorDigests) {
     for (String microdescriptor : microdescriptorDigests) {
-      if (!this.microdescriptorValidAfterTimes.containsKey(
-          microdescriptor)) {
-        this.microdescriptorValidAfterTimes.put(microdescriptor,
-            new HashSet<>());
-      }
+      this.microdescriptorValidAfterTimes.putIfAbsent(microdescriptor,
+          new HashSet<>());
       this.microdescriptorValidAfterTimes.get(microdescriptor).add(
           validAfterTime);
     }
diff --git a/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveWriter.java b/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveWriter.java
index aa4c65b..a79f0a6 100644
--- a/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveWriter.java
+++ b/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveWriter.java
@@ -229,9 +229,7 @@ public class ArchiveWriter extends CollecTorMain {
           if (published < this.now - 48L * 60L * 60L * 1000L) {
             continue;
           }
-          if (!this.storedServerDescriptors.containsKey(published)) {
-            this.storedServerDescriptors.put(published, new HashMap<>());
-          }
+          this.storedServerDescriptors.putIfAbsent(published, new HashMap<>());
           String serverDescriptorDigest = parts[1];
           String extraInfoDescriptorDigest = parts[2].equals("NA") ? null
               : parts[2];
@@ -256,9 +254,8 @@ public class ArchiveWriter extends CollecTorMain {
           if (published < this.now - 48L * 60L * 60L * 1000L) {
             continue;
           }
-          if (!this.storedExtraInfoDescriptors.containsKey(published)) {
-            this.storedExtraInfoDescriptors.put(published, new HashSet<>());
-          }
+          this.storedExtraInfoDescriptors.putIfAbsent(published,
+              new HashSet<>());
           String extraInfoDescriptorDigest = parts[1];
           this.storedExtraInfoDescriptors.get(published).add(
               extraInfoDescriptorDigest);
@@ -281,9 +278,7 @@ public class ArchiveWriter extends CollecTorMain {
           if (validAfter < this.now - 40L * 24L * 60L * 60L * 1000L) {
             continue;
           }
-          if (!this.storedMicrodescriptors.containsKey(validAfter)) {
-            this.storedMicrodescriptors.put(validAfter, new HashSet<>());
-          }
+          this.storedMicrodescriptors.putIfAbsent(validAfter, new HashSet<>());
           String microdescriptorDigest = parts[1];
           this.storedMicrodescriptors.get(validAfter).add(
               microdescriptorDigest);
@@ -695,9 +690,7 @@ public class ArchiveWriter extends CollecTorMain {
     }
     if (!tarballFileExistedBefore
         && this.now - validAfter < 3L * 60L * 60L * 1000L) {
-      if (!this.storedVotes.containsKey(validAfter)) {
-        this.storedVotes.put(validAfter, new TreeMap<>());
-      }
+      this.storedVotes.putIfAbsent(validAfter, new TreeMap<>());
       this.storedVotes.get(validAfter).put(fingerprint,
           serverDescriptorDigests);
     }
@@ -738,9 +731,7 @@ public class ArchiveWriter extends CollecTorMain {
     }
     if (!tarballFileExistedBefore
         && this.now - published < 48L * 60L * 60L * 1000L) {
-      if (!this.storedServerDescriptors.containsKey(published)) {
-        this.storedServerDescriptors.put(published, new HashMap<>());
-      }
+      this.storedServerDescriptors.putIfAbsent(published, new HashMap<>());
       this.storedServerDescriptors.get(published).put(digest,
           extraInfoDigest);
     }
@@ -766,9 +757,7 @@ public class ArchiveWriter extends CollecTorMain {
     }
     if (!tarballFileExistedBefore
         && this.now - published < 48L * 60L * 60L * 1000L) {
-      if (!this.storedExtraInfoDescriptors.containsKey(published)) {
-        this.storedExtraInfoDescriptors.put(published, new HashSet<>());
-      }
+      this.storedExtraInfoDescriptors.putIfAbsent(published, new HashSet<>());
       this.storedExtraInfoDescriptors.get(published).add(extraInfoDigest);
     }
   }
@@ -801,9 +790,7 @@ public class ArchiveWriter extends CollecTorMain {
     }
     if (!tarballFileExistedBefore
         && this.now - validAfter < 40L * 24L * 60L * 60L * 1000L) {
-      if (!this.storedMicrodescriptors.containsKey(validAfter)) {
-        this.storedMicrodescriptors.put(validAfter, new HashSet<>());
-      }
+      this.storedMicrodescriptors.putIfAbsent(validAfter, new HashSet<>());
       this.storedMicrodescriptors.get(validAfter).add(
           microdescriptorDigest);
     }
diff --git a/src/main/java/org/torproject/metrics/collector/relaydescs/RelayDescriptorDownloader.java b/src/main/java/org/torproject/metrics/collector/relaydescs/RelayDescriptorDownloader.java
index 564d1bc..4764a4b 100644
--- a/src/main/java/org/torproject/metrics/collector/relaydescs/RelayDescriptorDownloader.java
+++ b/src/main/java/org/torproject/metrics/collector/relaydescs/RelayDescriptorDownloader.java
@@ -380,11 +380,8 @@ public class RelayDescriptorDownloader {
                 String microdescriptorDigest = line.split(",")[3];
                 String microdescriptorKey = line.substring(0,
                     line.lastIndexOf(","));
-                if (!this.microdescriptorKeys.containsKey(
-                    microdescriptorDigest)) {
-                  this.microdescriptorKeys.put(
-                      microdescriptorDigest, new HashSet<>());
-                }
+                this.microdescriptorKeys.putIfAbsent(microdescriptorDigest,
+                    new HashSet<>());
                 this.microdescriptorKeys.get(microdescriptorDigest).add(
                     microdescriptorKey);
                 if (line.endsWith("NA") && !this.missingMicrodescriptors





More information about the tor-commits mailing list