Simplify adding elements to Collections (#4006)

* Simplify adding elements to Collections

This optimizes and simplifies the code that adds elements to Collections.

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2024-01-03 14:51:33 +01:00
committed by GitHub
parent afd1d4726c
commit 81a91ee9ae
9 changed files with 12 additions and 34 deletions
@@ -164,8 +164,7 @@ public class ScriptedAutomationManager {
// triggers are optional
}
List<Action> actions = new ArrayList<>();
actions.addAll(element.getActions());
List<Action> actions = new ArrayList<>(element.getActions());
if (element instanceof SimpleRuleActionHandler handler) {
String privId = addPrivateActionHandler(new SimpleRuleActionHandlerDelegate(handler));
@@ -87,9 +87,7 @@ public class RulePredicates {
*/
public static Predicate<Rule> hasAnyOfPrefixes(String... prefixes) {
final HashSet<String> namespaceSet = new HashSet<>(prefixes.length);
for (final String namespace : prefixes) {
namespaceSet.add(namespace);
}
namespaceSet.addAll(Arrays.asList(prefixes));
// this will even work for null namespace
return r -> namespaceSet.contains(getPrefix(r));
@@ -396,8 +396,8 @@ public class Printer {
int[] columnWidths = new int[] { COLUMN_PROPERTY_VALUE };
List<String> columnValues = new ArrayList<>();
columnValues.add(module.getId());
List<String> moduleContent = new ArrayList<>();
moduleContent.addAll(Utils.getTableTitle(Utils.getRow(columnWidths, columnValues), COLUMN_PROPERTY_VALUE));
List<String> moduleContent = new ArrayList<>(
Utils.getTableTitle(Utils.getRow(columnWidths, columnValues), COLUMN_PROPERTY_VALUE));
columnWidths = new int[] { COLUMN_CONFIG_PARAMETER, COLUMN_CONFIG_PARAMETER_VALUE };
columnValues.set(0, ID);
@@ -126,12 +126,7 @@ public class ConfigDescriptionRegistry {
}
// Now convert the map into the collection
Collection<ConfigDescription> configDescriptions = new ArrayList<>(configMap.size());
for (ConfigDescription configDescription : configMap.values()) {
configDescriptions.add(configDescription);
}
return Collections.unmodifiableCollection(configDescriptions);
return Collections.unmodifiableCollection(new ArrayList<>(configMap.values()));
}
/**
@@ -245,10 +240,8 @@ public class ConfigDescriptionRegistry {
*/
private ConfigDescriptionParameter getConfigOptions(URI uri, Set<URI> aliases, ConfigDescriptionParameter parameter,
@Nullable Locale locale) {
List<ParameterOption> options = new ArrayList<>();
// Add all the existing options that may be provided by the initial config description provider
options.addAll(parameter.getOptions());
List<ParameterOption> options = new ArrayList<>(parameter.getOptions());
boolean found = fillFromProviders(uri, parameter, locale, options);
@@ -71,9 +71,7 @@ public class MetadataConfigDescriptionProviderImpl implements ConfigDescriptionP
@Override
public Collection<ConfigDescription> getConfigDescriptions(@Nullable Locale locale) {
List<ConfigDescription> ret = new LinkedList<>();
ret.addAll(getValueConfigDescriptions(locale));
return ret;
return new LinkedList<>(getValueConfigDescriptions(locale));
}
@Override
@@ -17,7 +17,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
@@ -64,9 +63,7 @@ public class ConsoleSupportEclipse implements CommandProvider {
}
private Collection<ConsoleCommandExtension> getConsoleCommandExtensions() {
final Set<ConsoleCommandExtension> set = new HashSet<>();
set.addAll(consoleCommandExtensions.values());
return set;
return new HashSet<>(consoleCommandExtensions.values());
}
/**
@@ -175,9 +175,7 @@ public class ChannelTypeResource implements RESTResource {
for (ProfileType profileType : profileTypeRegistry.getProfileTypes()) {
if (profileType instanceof TriggerProfileType type) {
if (type.getSupportedChannelTypeUIDs().contains(ctUID)) {
for (String itemType : profileType.getSupportedItemTypes()) {
result.add(itemType);
}
result.addAll(profileType.getSupportedItemTypes());
}
}
}
@@ -175,11 +175,7 @@ public class ExpiringCacheMap<K, V> {
* @return the set of all keys
*/
public synchronized Set<K> keys() {
final Set<K> keys = new LinkedHashSet<>();
for (final K key : items.keySet()) {
keys.add(key);
}
return keys;
return new LinkedHashSet<>(items.keySet());
}
/**
@@ -13,6 +13,7 @@
package org.openhab.core.internal.i18n;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
@@ -127,9 +128,7 @@ public class ResourceBundleTracker extends BundleTracker {
List<Bundle> hosts = new ArrayList<>();
Bundle[] bundles = pkgAdmin.getHosts(fragment);
if (bundles != null) {
for (int i = 0; i < bundles.length; i++) {
hosts.add(bundles[i]);
}
hosts.addAll(Arrays.asList(bundles));
}
return hosts;
}