Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Map operations using computeIfAbsent #4020

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -154,11 +155,8 @@ public Collection<String> getTypes() {
return obj;
}

Map<String, Object> objects = objectCache.get(scriptIdentifier);
if (objects == null) {
objects = new HashMap<>();
objectCache.put(scriptIdentifier, objects);
}
Map<String, Object> objects = Objects
.requireNonNull(objectCache.computeIfAbsent(scriptIdentifier, k -> new HashMap<>()));

obj = objects.get(type);
if (obj != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -376,11 +377,8 @@ private Rule resolveRuleByTemplate(Rule rule) {
*/
private void updateRuleTemplateMapping(String templateUID, String ruleUID, boolean resolved) {
synchronized (this) {
Set<String> ruleUIDs = mapTemplateToRules.get(templateUID);
if (ruleUIDs == null) {
ruleUIDs = new HashSet<>();
mapTemplateToRules.put(templateUID, ruleUIDs);
}
Set<String> ruleUIDs = Objects
.requireNonNull(mapTemplateToRules.computeIfAbsent(templateUID, k -> new HashSet<>()));
if (resolved) {
ruleUIDs.remove(ruleUID);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -252,11 +253,7 @@ protected void importFile(String parserType, URL url) {
}
} else {
synchronized (urls) {
List<URL> value = urls.get(parserType);
if (value == null) {
value = new ArrayList<>();
urls.put(parserType, value);
}
List<URL> value = Objects.requireNonNull(urls.computeIfAbsent(parserType, k -> new ArrayList<>()));
value.add(url);
}
logger.debug("Parser {} not available", parserType, new Exception());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.service.WatchService;
Expand All @@ -35,11 +36,8 @@ public static void initializeWatchService(String watchingDir, AbstractFileProvid
WatchService watchService) {
AutomationWatchService aws = null;
synchronized (WATCH_SERVICES) {
Map<String, AutomationWatchService> watchers = WATCH_SERVICES.get(provider);
if (watchers == null) {
watchers = new HashMap<>();
WATCH_SERVICES.put(provider, watchers);
}
Map<String, AutomationWatchService> watchers = Objects
.requireNonNull(WATCH_SERVICES.computeIfAbsent(provider, k -> new HashMap<>()));
if (watchers.get(watchingDir) == null) {
aws = new AutomationWatchService(provider, watchService, watchingDir);
watchers.put(watchingDir, aws);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

Expand Down Expand Up @@ -78,26 +79,15 @@ public final synchronized void addAll(Bundle bundle, Collection<T_OBJECT> object
if (objectList.isEmpty()) {
return;
}
List<T_OBJECT> objects = acquireObjects(bundle);
if (objects == null) {
return;
}
List<T_OBJECT> objects = Objects
.requireNonNull(bundleObjectMap.computeIfAbsent(bundle, k -> new CopyOnWriteArrayList<>()));
objects.addAll(objectList);
for (T_OBJECT object : objectList) {
// just make sure no old entry remains in the cache
removeCachedEntries(object);
}
}

private @Nullable List<T_OBJECT> acquireObjects(Bundle bundle) {
List<T_OBJECT> objects = bundleObjectMap.get(bundle);
if (objects == null) {
objects = new CopyOnWriteArrayList<>();
bundleObjectMap.put(bundle, objects);
}
return objects;
}

/**
* Gets the object with the given key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -89,24 +90,17 @@ private void createItemChannelLink(String context, String itemName, String chann
}
ItemChannelLink itemChannelLink = new ItemChannelLink(itemName, channelUIDObject, configuration);

Set<String> itemNames = contextMap.get(context);
if (itemNames == null) {
itemNames = new HashSet<>();
contextMap.put(context, itemNames);
}
Set<String> itemNames = Objects.requireNonNull(contextMap.computeIfAbsent(context, k -> new HashSet<>()));
itemNames.add(itemName);
if (previousItemNames != null) {
previousItemNames.remove(itemName);
}

Map<ChannelUID, ItemChannelLink> links = itemChannelLinkMap.get(itemName);
if (links == null) {
// Create a HashMap with an initial capacity of 2 (the default is 16) to save memory
// because most items have only one channel. A capacity of 2 is enough to avoid
// resizing the HashMap in most cases, whereas 1 would trigger a resize as soon as
// one element is added.
itemChannelLinkMap.put(itemName, links = new HashMap<>(2));
}
// Create a HashMap with an initial capacity of 2 (the default is 16) to save memory because most items have
// only one channel. A capacity of 2 is enough to avoid resizing the HashMap in most cases, whereas 1 would
// trigger a resize as soon as one element is added.
Map<ChannelUID, ItemChannelLink> links = Objects
.requireNonNull(itemChannelLinkMap.computeIfAbsent(itemName, k -> new HashMap<>(2)));

ItemChannelLink oldLink = links.put(channelUIDObject, itemChannelLink);
if (oldLink == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -264,11 +265,8 @@ protected ServiceRegistration<?> registerService(final Object service, final Str
}

private void saveServiceRegistration(final String interfaceName, final ServiceRegistration<?> srvReg) {
List<ServiceRegistration<?>> regs = registeredServices.get(interfaceName);
if (regs == null) {
regs = new ArrayList<>();
registeredServices.put(interfaceName, regs);
}
List<ServiceRegistration<?>> regs = Objects
.requireNonNull(registeredServices.computeIfAbsent(interfaceName, k -> new ArrayList<>()));
regs.add(srvReg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -73,18 +74,10 @@ private void toLinkAdded(final L element) {
try {
Set<L> set;

set = itemNameToLink.get(itemName);
if (set == null) {
set = new HashSet<>();
itemNameToLink.put(itemName, set);
}
set = Objects.requireNonNull(itemNameToLink.computeIfAbsent(itemName, k -> new HashSet<>()));
set.add(element);

set = linkedUidToLink.get(linkedUid);
if (set == null) {
set = new HashSet<>();
linkedUidToLink.put(linkedUid, set);
}
set = Objects.requireNonNull(linkedUidToLink.computeIfAbsent(linkedUid, k -> new HashSet<>()));
set.add(element);
} finally {
toLinkLock.writeLock().unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -86,11 +87,8 @@ public void recordCallEnd(Invocation invocation) {
@Override
public void enqueue(Invocation invocation) {
synchronized (queues) {
Queue<Invocation> queue = queues.get(invocation.getIdentifier());
if (queue == null) {
queue = new LinkedList<>();
queues.put(invocation.getIdentifier(), queue);
}
Queue<Invocation> queue = Objects
.requireNonNull(queues.computeIfAbsent(invocation.getIdentifier(), k -> new LinkedList<>()));
queue.add(invocation);
}
trigger(invocation.getIdentifier());
Expand Down