Commit c51c4f20 authored by Karsten Loesing's avatar Karsten Loesing
Browse files

Use Map.putIfAbsent and Map.getOrDefault where possible.

parent 4543dd36
......@@ -181,12 +181,9 @@ public class ClientsHistory implements Comparable<ClientsHistory> {
SortedMap<String, Double> thisResponses,
SortedMap<String, Double> otherResponses) {
for (Map.Entry<String, Double> e : otherResponses.entrySet()) {
if (thisResponses.containsKey(e.getKey())) {
thisResponses.put(e.getKey(), thisResponses.get(e.getKey())
+ e.getValue());
} else {
thisResponses.put(e.getKey(), e.getValue());
}
thisResponses.putIfAbsent(e.getKey(), 0.0);
thisResponses.put(e.getKey(), thisResponses.get(e.getKey())
+ e.getValue());
}
}
......
......@@ -335,11 +335,8 @@ public class NodeStatus extends Document {
addressesAndPorts.add(address + ":" + dirPort);
}
addressesAndPorts.addAll(orAddressesAndPorts);
if (this.lastAddresses.containsKey(lastSeenMillis)) {
this.lastAddresses.get(lastSeenMillis).addAll(addressesAndPorts);
} else {
this.lastAddresses.put(lastSeenMillis, addressesAndPorts);
}
this.lastAddresses.putIfAbsent(lastSeenMillis, new TreeSet<>());
this.lastAddresses.get(lastSeenMillis).addAll(addressesAndPorts);
}
/** Returns the time in milliseconds since the epoch when addresses or
......
......@@ -3,7 +3,6 @@
package org.torproject.onionoo.server;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
......@@ -17,12 +16,8 @@ class MostFrequentString {
Map<String, Integer> stringFrequencies = new HashMap<>();
void addString(String string) {
if (!this.stringFrequencies.containsKey(string)) {
this.stringFrequencies.put(string, 1);
} else {
this.stringFrequencies.put(string,
this.stringFrequencies.get(string) + 1);
}
this.stringFrequencies.put(string,
this.stringFrequencies.getOrDefault(string, 0) + 1);
}
@Override
......@@ -33,12 +28,8 @@ class MostFrequentString {
return "null (0)";
}
for (Map.Entry<String, Integer> e : stringFrequencies.entrySet()) {
if (!sortedFrequencies.containsKey(e.getValue())) {
sortedFrequencies.put(e.getValue(), new TreeSet<>(
Arrays.asList(e.getKey())));
} else {
sortedFrequencies.get(e.getValue()).add(e.getKey());
}
sortedFrequencies.putIfAbsent(e.getValue(), new TreeSet<>());
sortedFrequencies.get(e.getValue()).add(e.getKey());
}
StringBuilder sb = new StringBuilder();
int stringsToAdd = 3;
......
......@@ -209,10 +209,7 @@ public class NodeIndexer implements ServletContextListener, Runnable {
* installations in international waters. */
countryCode = "xz";
}
if (!newRelaysByCountryCode.containsKey(countryCode)) {
newRelaysByCountryCode.put(countryCode,
new HashSet<>());
}
newRelaysByCountryCode.putIfAbsent(countryCode, new HashSet<>());
newRelaysByCountryCode.get(countryCode).add(fingerprint);
newRelaysByCountryCode.get(countryCode).add(hashedFingerprint);
String asNumber;
......@@ -225,22 +222,16 @@ public class NodeIndexer implements ServletContextListener, Runnable {
* shouldn't appear in any lookup databases. */
asNumber = "AS0";
}
if (!newRelaysByAsNumber.containsKey(asNumber)) {
newRelaysByAsNumber.put(asNumber, new HashSet<>());
}
newRelaysByAsNumber.putIfAbsent(asNumber, new HashSet<>());
newRelaysByAsNumber.get(asNumber).add(fingerprint);
newRelaysByAsNumber.get(asNumber).add(hashedFingerprint);
String asName = entry.getAsName();
if (!newRelaysByAsName.containsKey(asName)) {
newRelaysByAsName.put(asName, new HashSet<>());
}
newRelaysByAsName.putIfAbsent(asName, new HashSet<>());
newRelaysByAsName.get(asName).add(fingerprint);
newRelaysByAsName.get(asName).add(hashedFingerprint);
for (String flag : entry.getRelayFlags()) {
String flagLowerCase = flag.toLowerCase();
if (!newRelaysByFlag.containsKey(flagLowerCase)) {
newRelaysByFlag.put(flagLowerCase, new HashSet<>());
}
newRelaysByFlag.putIfAbsent(flagLowerCase, new HashSet<>());
newRelaysByFlag.get(flagLowerCase).add(fingerprint);
newRelaysByFlag.get(flagLowerCase).add(hashedFingerprint);
}
......@@ -258,42 +249,31 @@ public class NodeIndexer implements ServletContextListener, Runnable {
int daysSinceFirstSeen = (int) ((
(specialTime < 0 ? System.currentTimeMillis() : specialTime)
- entry.getFirstSeenMillis()) / ONE_DAY);
if (!newRelaysByFirstSeenDays.containsKey(daysSinceFirstSeen)) {
newRelaysByFirstSeenDays.put(daysSinceFirstSeen,
new HashSet<>());
}
newRelaysByFirstSeenDays.putIfAbsent(daysSinceFirstSeen, new HashSet<>());
newRelaysByFirstSeenDays.get(daysSinceFirstSeen).add(fingerprint);
newRelaysByFirstSeenDays.get(daysSinceFirstSeen).add(
hashedFingerprint);
int daysSinceLastSeen = (int) ((
(specialTime < 0 ? System.currentTimeMillis() : specialTime)
- entry.getLastSeenMillis()) / ONE_DAY);
if (!newRelaysByLastSeenDays.containsKey(daysSinceLastSeen)) {
newRelaysByLastSeenDays.put(daysSinceLastSeen,
new HashSet<>());
}
newRelaysByLastSeenDays.putIfAbsent(daysSinceLastSeen, new HashSet<>());
newRelaysByLastSeenDays.get(daysSinceLastSeen).add(fingerprint);
newRelaysByLastSeenDays.get(daysSinceLastSeen).add(
hashedFingerprint);
String contact = entry.getContact();
if (!newRelaysByContact.containsKey(contact)) {
newRelaysByContact.put(contact, new HashSet<>());
}
newRelaysByContact.putIfAbsent(contact, new HashSet<>());
newRelaysByContact.get(contact).add(fingerprint);
newRelaysByContact.get(contact).add(hashedFingerprint);
String version = entry.getVersion();
if (null != version) {
if (!newRelaysByVersion.containsKey(version)) {
newRelaysByVersion.put(version, new HashSet<>());
}
newRelaysByVersion.putIfAbsent(version, new HashSet<>());
newRelaysByVersion.get(version).add(fingerprint);
newRelaysByVersion.get(version).add(hashedFingerprint);
}
String operatingSystem = entry.getOperatingSystem();
if (null != operatingSystem) {
if (!newRelaysByOperatingSystem.containsKey(operatingSystem)) {
newRelaysByOperatingSystem.put(operatingSystem, new HashSet<>());
}
newRelaysByOperatingSystem.putIfAbsent(operatingSystem,
new HashSet<>());
newRelaysByOperatingSystem.get(operatingSystem).add(fingerprint);
newRelaysByOperatingSystem.get(operatingSystem).add(hashedFingerprint);
}
......@@ -308,9 +288,7 @@ public class NodeIndexer implements ServletContextListener, Runnable {
}
for (String hostName : allHostNames) {
String hostNameLowerCase = hostName.toLowerCase();
if (!newRelaysByHostName.containsKey(hostNameLowerCase)) {
newRelaysByHostName.put(hostNameLowerCase, new HashSet<>());
}
newRelaysByHostName.putIfAbsent(hostNameLowerCase, new HashSet<>());
newRelaysByHostName.get(hostNameLowerCase).add(fingerprint);
newRelaysByHostName.get(hostNameLowerCase).add(hashedFingerprint);
}
......@@ -346,9 +324,7 @@ public class NodeIndexer implements ServletContextListener, Runnable {
entry);
for (String flag : entry.getRelayFlags()) {
String flagLowerCase = flag.toLowerCase();
if (!newBridgesByFlag.containsKey(flagLowerCase)) {
newBridgesByFlag.put(flagLowerCase, new HashSet<>());
}
newBridgesByFlag.putIfAbsent(flagLowerCase, new HashSet<>());
newBridgesByFlag.get(flagLowerCase).add(hashedFingerprint);
newBridgesByFlag.get(flagLowerCase).add(
hashedHashedFingerprint);
......@@ -356,10 +332,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
int daysSinceFirstSeen = (int) ((
(specialTime < 0 ? System.currentTimeMillis() : specialTime)
- entry.getFirstSeenMillis()) / ONE_DAY);
if (!newBridgesByFirstSeenDays.containsKey(daysSinceFirstSeen)) {
newBridgesByFirstSeenDays.put(daysSinceFirstSeen,
new HashSet<>());
}
newBridgesByFirstSeenDays.putIfAbsent(daysSinceFirstSeen,
new HashSet<>());
newBridgesByFirstSeenDays.get(daysSinceFirstSeen).add(
hashedFingerprint);
newBridgesByFirstSeenDays.get(daysSinceFirstSeen).add(
......@@ -367,27 +341,21 @@ public class NodeIndexer implements ServletContextListener, Runnable {
int daysSinceLastSeen = (int) ((
(specialTime < 0 ? System.currentTimeMillis() : specialTime)
- entry.getLastSeenMillis()) / ONE_DAY);
if (!newBridgesByLastSeenDays.containsKey(daysSinceLastSeen)) {
newBridgesByLastSeenDays.put(daysSinceLastSeen,
new HashSet<>());
}
newBridgesByLastSeenDays.putIfAbsent(daysSinceLastSeen, new HashSet<>());
newBridgesByLastSeenDays.get(daysSinceLastSeen).add(
hashedFingerprint);
newBridgesByLastSeenDays.get(daysSinceLastSeen).add(
hashedHashedFingerprint);
String version = entry.getVersion();
if (null != version) {
if (!newBridgesByVersion.containsKey(version)) {
newBridgesByVersion.put(version, new HashSet<>());
}
newBridgesByVersion.putIfAbsent(version, new HashSet<>());
newBridgesByVersion.get(version).add(hashedFingerprint);
newBridgesByVersion.get(version).add(hashedHashedFingerprint);
}
String operatingSystem = entry.getOperatingSystem();
if (null != operatingSystem) {
if (!newBridgesByOperatingSystem.containsKey(operatingSystem)) {
newBridgesByOperatingSystem.put(operatingSystem, new HashSet<>());
}
newBridgesByOperatingSystem.putIfAbsent(operatingSystem,
new HashSet<>());
newBridgesByOperatingSystem.get(operatingSystem)
.add(hashedFingerprint);
newBridgesByOperatingSystem.get(operatingSystem)
......
......@@ -114,12 +114,8 @@ public class ClientsStatusUpdater implements DescriptorListener,
ClientsHistory newResponseHistory = new ClientsHistory(
startMillis, endMillis, totalResponses, responsesByCountry,
responsesByTransport, responsesByVersion);
if (!this.newResponses.containsKey(hashedFingerprint)) {
this.newResponses.put(hashedFingerprint,
new TreeSet<>());
}
this.newResponses.get(hashedFingerprint).add(
newResponseHistory);
this.newResponses.putIfAbsent(hashedFingerprint, new TreeSet<>());
this.newResponses.get(hashedFingerprint).add(newResponseHistory);
}
}
......
......@@ -81,10 +81,7 @@ public class DescriptorSource {
/** Registers a descriptor listener for a given descriptor type. */
public void registerDescriptorListener(DescriptorListener listener,
DescriptorType descriptorType) {
if (!this.descriptorListeners.containsKey(descriptorType)) {
this.descriptorListeners.put(descriptorType,
new HashSet<>());
}
this.descriptorListeners.putIfAbsent(descriptorType, new HashSet<>());
this.descriptorListeners.get(descriptorType).add(listener);
}
......
......@@ -223,16 +223,11 @@ public class NodeDetailsStatusUpdater implements DescriptorListener,
if (scanMillis < this.now - DateTimeHelper.ONE_DAY) {
continue;
}
if (!this.exitListEntries.containsKey(fingerprint)) {
this.exitListEntries.put(fingerprint, new HashMap<>());
}
this.exitListEntries.putIfAbsent(fingerprint, new HashMap<>());
String exitAddress = exitAddressScanMillis.getKey();
if (!this.exitListEntries.get(fingerprint).containsKey(
exitAddress)
|| this.exitListEntries.get(fingerprint).get(exitAddress)
if (this.exitListEntries.get(fingerprint).getOrDefault(exitAddress, 0L)
< scanMillis) {
this.exitListEntries.get(fingerprint).put(exitAddress,
scanMillis);
this.exitListEntries.get(fingerprint).put(exitAddress, scanMillis);
}
}
}
......@@ -288,14 +283,12 @@ public class NodeDetailsStatusUpdater implements DescriptorListener,
nodeStatus.setVersion(version);
}
if (entry.getUnmeasured()) {
if (!this.lastSeenUnmeasured.containsKey(fingerprint)
|| this.lastSeenUnmeasured.get(fingerprint)
if (this.lastSeenUnmeasured.getOrDefault(fingerprint, 0L)
< validAfterMillis) {
this.lastSeenUnmeasured.put(fingerprint, validAfterMillis);
}
} else if (consensus.getConsensusMethod() >= 17) {
if (!this.lastSeenMeasured.containsKey(fingerprint)
|| this.lastSeenMeasured.get(fingerprint)
if (this.lastSeenMeasured.getOrDefault(fingerprint, 0L)
< validAfterMillis) {
this.lastSeenMeasured.put(fingerprint, validAfterMillis);
}
......@@ -846,8 +839,7 @@ public class NodeDetailsStatusUpdater implements DescriptorListener,
this.exitListEntries.get(fingerprint).entrySet()) {
String exitAddress = e.getKey();
long scanMillis = e.getValue();
if (!exitAddresses.containsKey(exitAddress)
|| exitAddresses.get(exitAddress) < scanMillis) {
if (exitAddresses.getOrDefault(exitAddress, 0L) < scanMillis) {
exitAddresses.put(exitAddress, scanMillis);
}
}
......
......@@ -103,10 +103,7 @@ public class UptimeStatusUpdater implements DescriptorListener,
for (NetworkStatusEntry entry :
consensus.getStatusEntries().values()) {
String fingerprint = entry.getFingerprint();
if (!this.newRunningRelays.containsKey(fingerprint)) {
this.newRunningRelays.put(fingerprint,
new TreeMap<>());
}
this.newRunningRelays.putIfAbsent(fingerprint, new TreeMap<>());
this.newRunningRelays.get(fingerprint).put(dateHourMillis,
new Flags(entry.getFlags()));
}
......@@ -124,9 +121,7 @@ public class UptimeStatusUpdater implements DescriptorListener,
long dateHourMillis = (status.getPublishedMillis()
/ DateTimeHelper.ONE_HOUR) * DateTimeHelper.ONE_HOUR;
for (String fingerprint : fingerprints) {
if (!this.newRunningBridges.containsKey(fingerprint)) {
this.newRunningBridges.put(fingerprint, new TreeSet<>());
}
this.newRunningBridges.putIfAbsent(fingerprint, new TreeSet<>());
this.newRunningBridges.get(fingerprint).add(dateHourMillis);
}
this.newBridgeStatuses.add(dateHourMillis);
......
......@@ -19,9 +19,7 @@ public class DummyDocumentStore extends DocumentStore {
private <T extends Document> SortedMap<String, Document>
getStoredDocumentsByClass(Class<T> documentType) {
if (!this.storedDocuments.containsKey(documentType)) {
this.storedDocuments.put(documentType, new TreeMap<>());
}
this.storedDocuments.putIfAbsent(documentType, new TreeMap<>());
return this.storedDocuments.get(documentType);
}
......
......@@ -335,10 +335,7 @@ public class ResourceServletTest {
new HashMap<>();
for (String parameter : uriParts[1].split("&")) {
String[] parameterParts = parameter.split("=");
if (!parameterLists.containsKey(parameterParts[0])) {
parameterLists.put(parameterParts[0],
new ArrayList<>());
}
parameterLists.putIfAbsent(parameterParts[0], new ArrayList<>());
parameterLists.get(parameterParts[0]).add(parameterParts[1]);
}
parameters = new HashMap<>();
......
......@@ -31,9 +31,7 @@ public class DummyDescriptorSource extends DescriptorSource {
private Set<Descriptor> getDescriptorsByType(
DescriptorType descriptorType) {
if (!this.descriptors.containsKey(descriptorType)) {
this.descriptors.put(descriptorType, new HashSet<>());
}
this.descriptors.putIfAbsent(descriptorType, new HashSet<>());
return this.descriptors.get(descriptorType);
}
......@@ -43,10 +41,7 @@ public class DummyDescriptorSource extends DescriptorSource {
/** Register a listener to receive descriptors of the demanded type. */
public void registerDescriptorListener(DescriptorListener listener,
DescriptorType descriptorType) {
if (!this.descriptorListeners.containsKey(descriptorType)) {
this.descriptorListeners.put(descriptorType,
new HashSet<>());
}
this.descriptorListeners.putIfAbsent(descriptorType, new HashSet<>());
this.descriptorListeners.get(descriptorType).add(listener);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment