Use Java 8 Map#compute when possible
Change-Id: Ida300c054449047096f355f09b3843e4934dcd18
diff --git a/core/common/src/test/java/org/onosproject/store/trivial/SimpleDeviceStore.java b/core/common/src/test/java/org/onosproject/store/trivial/SimpleDeviceStore.java
index 14e37b8..4218d47 100644
--- a/core/common/src/test/java/org/onosproject/store/trivial/SimpleDeviceStore.java
+++ b/core/common/src/test/java/org/onosproject/store/trivial/SimpleDeviceStore.java
@@ -46,7 +46,6 @@
import org.onosproject.net.provider.ProviderId;
import org.onosproject.store.AbstractStore;
import org.onlab.packet.ChassisId;
-import org.onlab.util.NewConcurrentHashMap;
import org.slf4j.Logger;
import java.util.ArrayList;
@@ -73,7 +72,6 @@
import static com.google.common.base.Verify.verify;
import static org.onosproject.net.device.DeviceEvent.Type.*;
import static org.slf4j.LoggerFactory.getLogger;
-import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
import static org.onosproject.net.DefaultAnnotations.union;
import static org.onosproject.net.DefaultAnnotations.merge;
@@ -347,8 +345,7 @@
// Gets the map of ports for the specified device; if one does not already
// exist, it creates and registers a new one.
private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
- return createIfAbsentUnchecked(devicePorts, deviceId,
- NewConcurrentHashMap.ifNeeded());
+ return devicePorts.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
}
private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptions(
diff --git a/core/common/src/test/java/org/onosproject/store/trivial/SimpleFlowRuleStore.java b/core/common/src/test/java/org/onosproject/store/trivial/SimpleFlowRuleStore.java
index bc8805d..3706b4b 100644
--- a/core/common/src/test/java/org/onosproject/store/trivial/SimpleFlowRuleStore.java
+++ b/core/common/src/test/java/org/onosproject/store/trivial/SimpleFlowRuleStore.java
@@ -30,7 +30,6 @@
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
-import org.onlab.util.NewConcurrentHashMap;
import org.onlab.util.Tools;
import org.onosproject.net.DeviceId;
import org.onosproject.net.flow.CompletedBatchOperation;
@@ -66,7 +65,6 @@
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
-import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
import static org.slf4j.LoggerFactory.getLogger;
@@ -163,10 +161,6 @@
}
}
- private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
- return NewConcurrentHashMap.ifNeeded();
- }
-
/**
* Returns the flow table for specified device.
*
@@ -174,8 +168,7 @@
* @return Map representing Flow Table of given device.
*/
private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
- return createIfAbsentUnchecked(flowEntries,
- deviceId, lazyEmptyFlowTable());
+ return flowEntries.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
}
private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
@@ -317,6 +310,7 @@
return null;
}
+ @Override
public void purgeFlowRule(DeviceId deviceId) {
flowEntries.remove(deviceId);
}
diff --git a/core/common/src/test/java/org/onosproject/store/trivial/SimpleGroupStore.java b/core/common/src/test/java/org/onosproject/store/trivial/SimpleGroupStore.java
index e019ed9..3489bd8 100644
--- a/core/common/src/test/java/org/onosproject/store/trivial/SimpleGroupStore.java
+++ b/core/common/src/test/java/org/onosproject/store/trivial/SimpleGroupStore.java
@@ -15,7 +15,6 @@
*/
package org.onosproject.store.trivial;
-import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.ArrayList;
@@ -34,7 +33,6 @@
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
-import org.onlab.util.NewConcurrentHashMap;
import org.onosproject.core.DefaultGroupId;
import org.onosproject.core.GroupId;
import org.onosproject.net.DeviceId;
@@ -99,26 +97,6 @@
log.info("Stopped");
}
- private static NewConcurrentHashMap<GroupKey, StoredGroupEntry>
- lazyEmptyGroupKeyTable() {
- return NewConcurrentHashMap.ifNeeded();
- }
-
- private static NewConcurrentHashMap<GroupId, StoredGroupEntry>
- lazyEmptyGroupIdTable() {
- return NewConcurrentHashMap.ifNeeded();
- }
-
- private static NewConcurrentHashMap<GroupKey, StoredGroupEntry>
- lazyEmptyPendingGroupKeyTable() {
- return NewConcurrentHashMap.ifNeeded();
- }
-
- private static NewConcurrentHashMap<GroupId, Group>
- lazyEmptyExtraneousGroupIdTable() {
- return NewConcurrentHashMap.ifNeeded();
- }
-
/**
* Returns the group key table for specified device.
*
@@ -126,8 +104,7 @@
* @return Map representing group key table of given device.
*/
private ConcurrentMap<GroupKey, StoredGroupEntry> getGroupKeyTable(DeviceId deviceId) {
- return createIfAbsentUnchecked(groupEntriesByKey,
- deviceId, lazyEmptyGroupKeyTable());
+ return groupEntriesByKey.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
}
/**
@@ -137,8 +114,7 @@
* @return Map representing group key table of given device.
*/
private ConcurrentMap<GroupId, StoredGroupEntry> getGroupIdTable(DeviceId deviceId) {
- return createIfAbsentUnchecked(groupEntriesById,
- deviceId, lazyEmptyGroupIdTable());
+ return groupEntriesById.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
}
/**
@@ -149,8 +125,7 @@
*/
private ConcurrentMap<GroupKey, StoredGroupEntry>
getPendingGroupKeyTable(DeviceId deviceId) {
- return createIfAbsentUnchecked(pendingGroupEntriesByKey,
- deviceId, lazyEmptyPendingGroupKeyTable());
+ return pendingGroupEntriesByKey.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
}
/**
@@ -161,9 +136,7 @@
*/
private ConcurrentMap<GroupId, Group>
getExtraneousGroupIdTable(DeviceId deviceId) {
- return createIfAbsentUnchecked(extraneousGroupEntriesById,
- deviceId,
- lazyEmptyExtraneousGroupIdTable());
+ return extraneousGroupEntriesById.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
}
/**
diff --git a/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java b/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java
index 8e2a370..02f65e4 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java
@@ -28,6 +28,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
@@ -43,7 +44,6 @@
import org.apache.felix.scr.annotations.Service;
import org.onlab.packet.ChassisId;
import org.onlab.util.KryoNamespace;
-import org.onlab.util.NewConcurrentHashMap;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.NodeId;
@@ -106,7 +106,6 @@
import static com.google.common.base.Verify.verify;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
-import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
import static org.onlab.util.Tools.groupedThreads;
import static org.onlab.util.Tools.minPriority;
import static org.onosproject.cluster.ControllerNodeToNodeId.toNodeId;
@@ -541,6 +540,7 @@
}
}
+ @Override
public boolean markOnline(DeviceId deviceId) {
if (devices.containsKey(deviceId)) {
final Timestamp timestamp = deviceClockService.getTimestamp(deviceId);
@@ -782,8 +782,7 @@
// Gets the map of ports for the specified device; if one does not already
// exist, it creates and registers a new one.
private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
- return createIfAbsentUnchecked(devicePorts, deviceId,
- NewConcurrentHashMap.ifNeeded());
+ return devicePorts.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
}
private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptionsMap(
diff --git a/core/store/dist/src/main/java/org/onosproject/store/group/impl/DistributedGroupStore.java b/core/store/dist/src/main/java/org/onosproject/store/group/impl/DistributedGroupStore.java
index 554ef2a..33fa8cd 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/group/impl/DistributedGroupStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/group/impl/DistributedGroupStore.java
@@ -28,7 +28,6 @@
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.util.KryoNamespace;
-import org.onlab.util.NewConcurrentHashMap;
import org.onosproject.cfg.ComponentConfigService;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.NodeId;
@@ -89,7 +88,6 @@
import java.util.stream.Collectors;
import static com.google.common.base.Strings.isNullOrEmpty;
-import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
import static org.onlab.util.Tools.get;
import static org.onlab.util.Tools.groupedThreads;
import static org.slf4j.LoggerFactory.getLogger;
@@ -239,16 +237,6 @@
}
}
- private static NewConcurrentHashMap<GroupId, Group>
- lazyEmptyExtraneousGroupIdTable() {
- return NewConcurrentHashMap.ifNeeded();
- }
-
- private static NewConcurrentHashMap<GroupId, StoredGroupEntry>
- lazyEmptyGroupIdTable() {
- return NewConcurrentHashMap.ifNeeded();
- }
-
/**
* Returns the group store eventual consistent key map.
*
@@ -266,8 +254,7 @@
* @return Map representing group key table of given device.
*/
private ConcurrentMap<GroupId, StoredGroupEntry> getGroupIdTable(DeviceId deviceId) {
- return createIfAbsentUnchecked(groupEntriesById,
- deviceId, lazyEmptyGroupIdTable());
+ return groupEntriesById.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
}
/**
@@ -288,9 +275,7 @@
*/
private ConcurrentMap<GroupId, Group>
getExtraneousGroupIdTable(DeviceId deviceId) {
- return createIfAbsentUnchecked(extraneousGroupEntriesById,
- deviceId,
- lazyEmptyExtraneousGroupIdTable());
+ return extraneousGroupEntriesById.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
}
/**
diff --git a/utils/misc/src/main/java/org/onlab/util/NewConcurrentHashMap.java b/utils/misc/src/main/java/org/onlab/util/NewConcurrentHashMap.java
index 08ad070..03d141f 100644
--- a/utils/misc/src/main/java/org/onlab/util/NewConcurrentHashMap.java
+++ b/utils/misc/src/main/java/org/onlab/util/NewConcurrentHashMap.java
@@ -29,7 +29,10 @@
*
* @param <K> ConcurrentHashMap key type
* @param <V> ConcurrentHashMap value type
+ *
+ * @deprecated in Hummingbird (1.7.0)
*/
+@Deprecated
public final class NewConcurrentHashMap<K, V>
implements ConcurrentInitializer<ConcurrentMap<K, V>> {