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

[tor-commits] [metrics-lib/master] Use Map.putIfAbsent and Map.getOrDefault where possible.



commit 7c26323811d733643f9042611c55cdaeec4e3cc4
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Tue Aug 21 15:05:26 2018 +0200

    Use Map.putIfAbsent and Map.getOrDefault where possible.
---
 .../org/torproject/descriptor/impl/DescriptorImpl.java  | 17 +++++------------
 .../descriptor/impl/DescriptorReaderImpl.java           |  3 +--
 .../descriptor/impl/NetworkStatusEntryImpl.java         |  4 +---
 3 files changed, 7 insertions(+), 17 deletions(-)

diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java
index 1ae85d6..b6dbf74 100644
--- a/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java
+++ b/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java
@@ -279,11 +279,7 @@ public abstract class DescriptorImpl implements Descriptor {
           this.firstKey = key;
         }
         lastKey = key;
-        if (parsedKeys.containsKey(key)) {
-          parsedKeys.put(key, parsedKeys.get(key) + 1);
-        } else {
-          parsedKeys.put(key, 1);
-        }
+        parsedKeys.put(key, parsedKeys.getOrDefault(key, 0) + 1);
       }
     }
   }
@@ -307,10 +303,7 @@ public abstract class DescriptorImpl implements Descriptor {
   protected void checkExactlyOnceKeys(Set<Key> keys)
       throws DescriptorParseException {
     for (Key key : keys) {
-      int contained = 0;
-      if (this.parsedKeys.containsKey(key)) {
-        contained = this.parsedKeys.get(key);
-      }
+      int contained = this.parsedKeys.getOrDefault(key, 0);
       if (contained != 1) {
         throw new DescriptorParseException("Keyword '" + key.keyword + "' is "
             + "contained " + contained + " times, but must be contained "
@@ -332,10 +325,10 @@ public abstract class DescriptorImpl implements Descriptor {
   protected void checkAtMostOnceKeys(Set<Key> keys)
       throws DescriptorParseException {
     for (Key key : keys) {
-      if (this.parsedKeys.containsKey(key)
-          && this.parsedKeys.get(key) > 1) {
+      int contained = this.parsedKeys.getOrDefault(key, 0);
+      if (contained > 1) {
         throw new DescriptorParseException("Keyword '" + key.keyword + "' is "
-            + "contained " + this.parsedKeys.get(key) + " times, "
+            + "contained " + contained + " times, "
             + "but must be contained at most once.");
       }
     }
diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java
index bbe467e..b8eb191 100644
--- a/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java
+++ b/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java
@@ -229,8 +229,7 @@ public class DescriptorReaderImpl implements DescriptorReader {
           try {
             String absolutePath = file.getAbsolutePath();
             long lastModifiedMillis = file.lastModified();
-            if (this.excludedFilesBefore.containsKey(absolutePath)
-                && this.excludedFilesBefore.get(absolutePath)
+            if (this.excludedFilesBefore.getOrDefault(absolutePath, 0L)
                 == lastModifiedMillis) {
               this.excludedFilesAfter.put(absolutePath, lastModifiedMillis);
               continue;
diff --git a/src/main/java/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java b/src/main/java/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java
index 9ebedbd..2ddaaa0 100644
--- a/src/main/java/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java
+++ b/src/main/java/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java
@@ -217,9 +217,7 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry {
     if (pairs.containsKey("Unmeasured")) {
       this.unmeasured = pairs.remove("Unmeasured") == 1L;
     }
-    if (!pairs.isEmpty()) {
-      /* Ignore unknown key-value pair. */
-    }
+    /* Ignore unknown key-value pair. */
   }
 
   private void parsePLine(String line, String[] parts)

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