Iterate using Map entries (#4003)

* Iterate using Map entries

Iteration using Map entries is preferred because it is more efficient and helps preventing NPEs.

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born 2024-01-03 14:50:50 +01:00 committed by GitHub
parent 106c8b2270
commit afd1d4726c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 62 additions and 59 deletions

View File

@ -1349,10 +1349,10 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
OutputRef outputRef = null;
boolean conflict = false;
if (!inputTags.isEmpty()) {
for (Set<String> outTags : outputTagMap.keySet()) {
if (outTags.containsAll(inputTags)) { // input tags must be subset of the output ones
for (Entry<Set<String>, OutputRef> entry : outputTagMap.entrySet()) {
if (entry.getKey().containsAll(inputTags)) { // input tags must be subset of the output ones
if (outputRef == null) {
outputRef = outputTagMap.get(outTags);
outputRef = entry.getValue();
} else {
conflict = true; // already exist candidate for autoMap
break;

View File

@ -17,7 +17,7 @@ import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -95,9 +95,9 @@ public class CompositeModuleHandlerFactory extends BaseModuleHandlerFactory impl
ModuleHandler handlerOfModule = getHandlers().get(getModuleIdentifier(childModulePrefix, module.getId()));
if (handlerOfModule instanceof AbstractCompositeModuleHandler) {
AbstractCompositeModuleHandler<ModuleImpl, ?, ?> h = (AbstractCompositeModuleHandler<ModuleImpl, ?, ?>) handlerOfModule;
Set<ModuleImpl> modules = h.moduleHandlerMap.keySet();
for (ModuleImpl child : modules) {
ModuleHandler childHandler = h.moduleHandlerMap.get(child);
for (Entry<ModuleImpl, @Nullable ? extends ModuleHandler> entry : h.moduleHandlerMap.entrySet()) {
ModuleImpl child = entry.getKey();
ModuleHandler childHandler = entry.getValue();
if (childHandler == null) {
continue;
}

View File

@ -54,9 +54,9 @@ public class HostFragmentMappingUtil {
if (bundles != null) {
hosts = Arrays.asList(bundles);
} else {
for (Bundle host : hostFragmentMapping.keySet()) {
if (hostFragmentMapping.get(host).contains(fragment)) {
hosts.add(host);
for (Entry<Bundle, List<Bundle>> entry : hostFragmentMapping.entrySet()) {
if (entry.getValue().contains(fragment)) {
hosts.add(entry.getKey());
}
}
}

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -148,10 +149,9 @@ public class RuleResourceBundleImporter extends AbstractResourceBundleProvider<R
protected List<String> getPreviousPortfolio(Vendor vendor) {
List<String> portfolio = providerPortfolio.get(vendor);
if (portfolio == null) {
for (Vendor v : providerPortfolio.keySet()) {
if (v.getVendorSymbolicName().equals(vendor.getVendorSymbolicName())) {
List<String> vendorPortfolio = providerPortfolio.get(v);
return vendorPortfolio == null ? List.of() : vendorPortfolio;
for (Entry<Vendor, List<String>> entry : providerPortfolio.entrySet()) {
if (entry.getKey().getVendorSymbolicName().equals(vendor.getVendorSymbolicName())) {
return entry.getValue();
}
}
}

View File

@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -101,35 +102,33 @@ public final class ConfigDescriptionValidatorImpl implements ConfigDescriptionVa
Collection<ConfigValidationMessage> configDescriptionValidationMessages = new ArrayList<>();
for (String key : map.keySet()) {
ConfigDescriptionParameter configDescriptionParameter = map.get(key);
if (configDescriptionParameter != null) {
// If the parameter supports multiple selection, then it may be provided as an array
if (configDescriptionParameter.isMultiple() && configurationParameters.get(key) instanceof List) {
List<Object> values = (List<Object>) configurationParameters.get(key);
// check if multipleLimit is obeyed
Integer multipleLimit = configDescriptionParameter.getMultipleLimit();
if (multipleLimit != null && values.size() > multipleLimit) {
MessageKey messageKey = MessageKey.MULTIPLE_LIMIT_VIOLATED;
ConfigValidationMessage message = new ConfigValidationMessage(
configDescriptionParameter.getName(), messageKey.defaultMessage, messageKey.key,
multipleLimit, values.size());
configDescriptionValidationMessages.add(message);
}
// Perform validation on each value in the list separately
for (Object value : values) {
ConfigValidationMessage message = validateParameter(configDescriptionParameter, value);
if (message != null) {
configDescriptionValidationMessages.add(message);
}
}
} else {
ConfigValidationMessage message = validateParameter(configDescriptionParameter,
configurationParameters.get(key));
for (Entry<String, ConfigDescriptionParameter> entry : map.entrySet()) {
String key = entry.getKey();
ConfigDescriptionParameter configDescriptionParameter = entry.getValue();
// If the parameter supports multiple selection, then it may be provided as an array
if (configDescriptionParameter.isMultiple() && configurationParameters.get(key) instanceof List) {
List<Object> values = (List<Object>) configurationParameters.get(key);
// check if multipleLimit is obeyed
Integer multipleLimit = configDescriptionParameter.getMultipleLimit();
if (multipleLimit != null && values.size() > multipleLimit) {
MessageKey messageKey = MessageKey.MULTIPLE_LIMIT_VIOLATED;
ConfigValidationMessage message = new ConfigValidationMessage(configDescriptionParameter.getName(),
messageKey.defaultMessage, messageKey.key, multipleLimit, values.size());
configDescriptionValidationMessages.add(message);
}
// Perform validation on each value in the list separately
for (Object value : values) {
ConfigValidationMessage message = validateParameter(configDescriptionParameter, value);
if (message != null) {
configDescriptionValidationMessages.add(message);
}
}
} else {
ConfigValidationMessage message = validateParameter(configDescriptionParameter,
configurationParameters.get(key));
if (message != null) {
configDescriptionValidationMessages.add(message);
}
}
}

View File

@ -15,6 +15,7 @@ package org.openhab.core.io.transport.upnp.internal;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@ -154,14 +155,12 @@ public class UpnpIOServiceImpl implements UpnpIOService, RegistryListener {
deviceRoot.getIdentity().getUdn());
for (UpnpIOParticipant participant : participants) {
if (Objects.equals(getDevice(participant), deviceRoot)) {
for (String stateVariable : values.keySet()) {
StateVariableValue value = values.get(stateVariable);
if (value.getValue() != null) {
try {
participant.onValueReceived(stateVariable, value.getValue().toString(), serviceId);
} catch (Exception e) {
logger.error("Participant threw an exception onValueReceived", e);
}
for (Entry<String, StateVariableValue> entry : values.entrySet()) {
try {
participant.onValueReceived(entry.getKey(), entry.getValue().getValue().toString(),
serviceId);
} catch (Exception e) {
logger.error("Participant threw an exception onValueReceived", e);
}
}
break;
@ -300,8 +299,8 @@ public class UpnpIOServiceImpl implements UpnpIOService, RegistryListener {
if (action != null) {
ActionInvocation invocation = new ActionInvocation(action);
if (inputs != null) {
for (String variable : inputs.keySet()) {
invocation.setInput(variable, inputs.get(variable));
for (Entry<String, String> entry : inputs.entrySet()) {
invocation.setInput(entry.getKey(), entry.getValue());
}
}
@ -316,10 +315,11 @@ public class UpnpIOServiceImpl implements UpnpIOService, RegistryListener {
Map<String, ActionArgumentValue> result = invocation.getOutputMap();
if (result != null) {
for (String variable : result.keySet()) {
for (Entry<String, ActionArgumentValue> entry : result.entrySet()) {
String variable = entry.getKey();
final ActionArgumentValue newArgument;
try {
newArgument = result.get(variable);
newArgument = entry.getValue();
} catch (final Exception ex) {
logger.debug("An exception '{}' occurred, cannot get argument for variable '{}'",
ex.getMessage(), variable);

View File

@ -14,6 +14,7 @@ package org.openhab.core.thing.binding;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -105,8 +106,8 @@ public class ThingFactory {
thingHandlerFactory.getClass(), thingTypeUID);
} else {
if (properties != null) {
for (String key : properties.keySet()) {
thing.setProperty(key, properties.get(key));
for (Entry<String, String> entry : properties.entrySet()) {
thing.setProperty(entry.getKey(), entry.getValue());
}
}
}

View File

@ -21,6 +21,7 @@ import java.util.Base64;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.servlet.Servlet;
@ -161,8 +162,8 @@ public class ProxyServletService extends HttpServlet {
private Hashtable<String, @Nullable String> propsFromConfig(Map<String, Object> config, Servlet servlet) {
Hashtable<String, @Nullable String> props = new Hashtable<>();
for (String key : config.keySet()) {
props.put(key, config.get(key).toString());
for (Entry<String, Object> entry : config.entrySet()) {
props.put(entry.getKey(), entry.getValue().toString());
}
// must specify for Jetty proxy servlet, per http://stackoverflow.com/a/27625380

View File

@ -24,6 +24,7 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.WeakHashMap;
@ -191,10 +192,11 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider, Dia
this.defaultVoice = config.containsKey(CONFIG_DEFAULT_VOICE) ? config.get(CONFIG_DEFAULT_VOICE).toString()
: null;
for (String key : config.keySet()) {
for (Entry<String, Object> entry : config.entrySet()) {
String key = entry.getKey();
if (key.startsWith(CONFIG_PREFIX_DEFAULT_VOICE)) {
String tts = key.substring(CONFIG_PREFIX_DEFAULT_VOICE.length());
defaultVoices.put(tts, config.get(key).toString());
defaultVoices.put(tts, entry.getValue().toString());
}
}
}

View File

@ -908,9 +908,9 @@ public class InboxOSGiTest extends JavaOSGiTest {
assertNotNull(addedThing);
assertNotNull(approvedThing);
assertEquals(approvedThing, addedThing);
discoveryResultProperties.keySet().forEach(key -> {
discoveryResultProperties.forEach((key, value) -> {
String thingProperty = addedThing.getProperties().get(key);
String descResultParam = String.valueOf(discoveryResultProperties.get(key));
String descResultParam = String.valueOf(value);
assertThat(thingProperty, is(notNullValue()));
assertThat(descResultParam, is(notNullValue()));
assertThat(thingProperty, is(descResultParam));