Skip to content

Commit

Permalink
Iterate using Map entries (#4003)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
wborn authored Jan 3, 2024
1 parent 106c8b2 commit afd1d47
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1349,10 +1349,10 @@ private boolean addAutoMapConnections(Input input, Map<Set<String>, OutputRef> o
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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;
Expand Down Expand Up @@ -95,9 +95,9 @@ public void ungetHandler(Module module, String childModulePrefix, ModuleHandler
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ static List<Bundle> returnHostBundles(Bundle fragment) {
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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -148,10 +149,9 @@ protected void addNewProvidedObjects(List<String> newPortfolio, List<String> pre
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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -101,35 +102,33 @@ public void validate(Map<String, Object> configurationParameters, URI configDesc

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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -154,14 +155,12 @@ protected void eventReceived(GENASubscription sub) {
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;
Expand Down Expand Up @@ -300,8 +299,8 @@ public Map<String, String> invokeAction(UpnpIOParticipant participant, String se
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());
}
}

Expand All @@ -316,10 +315,11 @@ public Map<String, String> invokeAction(UpnpIOParticipant participant, String se

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -105,8 +106,8 @@ public static Thing createThing(ThingType thingType, ThingUID thingUID, Configur
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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -161,8 +162,8 @@ private Servlet getImpl() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -191,10 +192,11 @@ protected void modified(Map<String, Object> config) {
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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,9 +908,9 @@ public void assertThatApproveAddsAllPropertiesOfDiscoveryResultToThingProperties
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));
Expand Down

0 comments on commit afd1d47

Please sign in to comment.