[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

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



commit 5e4ef944eccf18b5276647828162fa2bc76a2f8f
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Tue Aug 21 15:12:01 2018 +0200

    Use Map.putIfAbsent and Map.getOrDefault where possible.
---
 src/main/java/org/torproject/metrics/stats/connbidirect/Main.java | 7 ++-----
 .../java/org/torproject/metrics/stats/hidserv/Aggregator.java     | 8 ++------
 .../java/org/torproject/metrics/stats/hidserv/DocumentStore.java  | 4 +---
 .../java/org/torproject/metrics/stats/hidserv/Extrapolator.java   | 4 +---
 .../metrics/stats/servers/ConsensusStatsFileHandler.java          | 8 ++------
 src/main/java/org/torproject/metrics/web/MetricServlet.java       | 4 +---
 6 files changed, 9 insertions(+), 26 deletions(-)

diff --git a/src/main/java/org/torproject/metrics/stats/connbidirect/Main.java b/src/main/java/org/torproject/metrics/stats/connbidirect/Main.java
index a069d84..b2dc1b6 100644
--- a/src/main/java/org/torproject/metrics/stats/connbidirect/Main.java
+++ b/src/main/java/org/torproject/metrics/stats/connbidirect/Main.java
@@ -447,11 +447,8 @@ public class Main {
             rawStat.fractionWrite, rawStat.fractionBoth };
         for (int i = 0; i < directions.length; i++) {
           String dateAndDirection = date + "," + directions[i];
-          if (!fractionsByDateAndDirection.containsKey(
-              dateAndDirection)) {
-            fractionsByDateAndDirection.put(dateAndDirection,
-                new ArrayList<>());
-          }
+          fractionsByDateAndDirection.putIfAbsent(dateAndDirection,
+              new ArrayList<>());
           fractionsByDateAndDirection.get(dateAndDirection).add(
               fractions[i]);
         }
diff --git a/src/main/java/org/torproject/metrics/stats/hidserv/Aggregator.java b/src/main/java/org/torproject/metrics/stats/hidserv/Aggregator.java
index 14fc31f..36e7967 100644
--- a/src/main/java/org/torproject/metrics/stats/hidserv/Aggregator.java
+++ b/src/main/java/org/torproject/metrics/stats/hidserv/Aggregator.java
@@ -88,17 +88,13 @@ public class Aggregator {
         continue;
       }
       if (extrapolated.getFractionRendRelayedCells() > 0.0) {
-        if (!extrapolatedCells.containsKey(date)) {
-          extrapolatedCells.put(date, new ArrayList<>());
-        }
+        extrapolatedCells.putIfAbsent(date, new ArrayList<>());
         extrapolatedCells.get(date).add(new double[] {
             extrapolated.getExtrapolatedRendRelayedCells(),
             extrapolated.getFractionRendRelayedCells() });
       }
       if (extrapolated.getFractionDirOnionsSeen() > 0.0) {
-        if (!extrapolatedOnions.containsKey(date)) {
-          extrapolatedOnions.put(date, new ArrayList<>());
-        }
+        extrapolatedOnions.putIfAbsent(date, new ArrayList<>());
         extrapolatedOnions.get(date).add(new double[] {
             extrapolated.getExtrapolatedDirOnionsSeen(),
             extrapolated.getFractionDirOnionsSeen() });
diff --git a/src/main/java/org/torproject/metrics/stats/hidserv/DocumentStore.java b/src/main/java/org/torproject/metrics/stats/hidserv/DocumentStore.java
index f73812a..61b9fa8 100644
--- a/src/main/java/org/torproject/metrics/stats/hidserv/DocumentStore.java
+++ b/src/main/java/org/torproject/metrics/stats/hidserv/DocumentStore.java
@@ -58,9 +58,7 @@ public class DocumentStore<T extends Document> {
     SortedMap<String, SortedSet<String>> formattedDocuments = new TreeMap<>();
     for (T retrieveDocument : retrievedDocuments) {
       String[] formattedDocument = retrieveDocument.format();
-      if (!formattedDocuments.containsKey(formattedDocument[0])) {
-        formattedDocuments.put(formattedDocument[0], new TreeSet<>());
-      }
+      formattedDocuments.putIfAbsent(formattedDocument[0], new TreeSet<>());
       formattedDocuments.get(formattedDocument[0]).add(
           formattedDocument[1]);
     }
diff --git a/src/main/java/org/torproject/metrics/stats/hidserv/Extrapolator.java b/src/main/java/org/torproject/metrics/stats/hidserv/Extrapolator.java
index 2c4a8f8..0dcc638 100644
--- a/src/main/java/org/torproject/metrics/stats/hidserv/Extrapolator.java
+++ b/src/main/java/org/torproject/metrics/stats/hidserv/Extrapolator.java
@@ -99,9 +99,7 @@ public class Extrapolator {
         new TreeMap<>();
     for (ReportedHidServStats stat : reportedStats) {
       String fingerprint = stat.getFingerprint();
-      if (!parsedStatsByFingerprint.containsKey(fingerprint)) {
-        parsedStatsByFingerprint.put(fingerprint, new HashSet<>());
-      }
+      parsedStatsByFingerprint.putIfAbsent(fingerprint, new HashSet<>());
       parsedStatsByFingerprint.get(fingerprint).add(stat);
     }
 
diff --git a/src/main/java/org/torproject/metrics/stats/servers/ConsensusStatsFileHandler.java b/src/main/java/org/torproject/metrics/stats/servers/ConsensusStatsFileHandler.java
index f4f99cd..491170e 100644
--- a/src/main/java/org/torproject/metrics/stats/servers/ConsensusStatsFileHandler.java
+++ b/src/main/java/org/torproject/metrics/stats/servers/ConsensusStatsFileHandler.java
@@ -257,13 +257,9 @@ public class ConsensusStatsFileHandler {
         continue;
       }
       String date = bridgesRawLine.substring(0, 10);
-      if (!bridgesPerDayAndAuthority.containsKey(date)) {
-        bridgesPerDayAndAuthority.put(date, new TreeMap<>());
-      }
+      bridgesPerDayAndAuthority.putIfAbsent(date, new TreeMap<>());
       String authority = parts[1];
-      if (!bridgesPerDayAndAuthority.get(date).containsKey(authority)) {
-        bridgesPerDayAndAuthority.get(date).put(authority, new int[3]);
-      }
+      bridgesPerDayAndAuthority.get(date).putIfAbsent(authority, new int[3]);
       int[] bridges = bridgesPerDayAndAuthority.get(date).get(authority);
       bridges[0] += brunning;
       bridges[1] += Integer.parseInt(parts[3]);
diff --git a/src/main/java/org/torproject/metrics/web/MetricServlet.java b/src/main/java/org/torproject/metrics/web/MetricServlet.java
index 3b279a9..353af63 100644
--- a/src/main/java/org/torproject/metrics/web/MetricServlet.java
+++ b/src/main/java/org/torproject/metrics/web/MetricServlet.java
@@ -44,9 +44,7 @@ public abstract class MetricServlet extends AnyServlet {
       String id = metric.getId();
       String title = metric.getTitle();
       String type = metric.getType();
-      if (!this.idsByType.containsKey(type)) {
-        this.idsByType.put(type, new HashSet<>());
-      }
+      this.idsByType.putIfAbsent(type, new HashSet<>());
       this.idsByType.get(type).add(id);
       this.titles.put(id, title);
       this.descriptions.put(id, metric.getDescription());



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits