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 adding elements to Collections #4006

Merged
merged 2 commits into from
Jan 3, 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 @@ -164,8 +164,7 @@ public Rule addUnmanagedRule(Rule element) {
// 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ public static Predicate<Rule> hasPrefix(final @Nullable String prefix) {
*/
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ private static List<String> getModuleRecords(Module module) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,7 @@ public Collection<ConfigDescription> getConfigDescriptions(@Nullable Locale loca
}

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

/**
Expand Down Expand Up @@ -245,10 +240,8 @@ private boolean fillFromProviders(URI uri, @Nullable Locale locale, List<ConfigD
*/
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

Expand Down Expand Up @@ -64,9 +63,7 @@ private ConsoleCommandExtension getConsoleCommandExtension(final String cmd) {
}

private Collection<ConsoleCommandExtension> getConsoleCommandExtensions() {
final Set<ConsoleCommandExtension> set = new HashSet<>();
set.addAll(consoleCommandExtensions.values());
return set;
return new HashSet<>(consoleCommandExtensions.values());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ public Response getLinkableItemTypes(
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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ public void clear() {
* @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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,9 +128,7 @@ private List<Bundle> returnHostBundles(Bundle fragment) {
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;
}
Expand Down