Skip to content

Commit

Permalink
Simplify code that creates List, Map and Set objects (#3836)
Browse files Browse the repository at this point in the history
Simplifies the code by using List.of, List.copyOf etc. where possible which results in less code and imports.

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored Oct 9, 2023
1 parent 8c8f411 commit e8e1c9f
Show file tree
Hide file tree
Showing 165 changed files with 387 additions and 594 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected List<Addon> getRemoteAddons() {

List<DiscourseUser> users = pages.stream().flatMap(p -> Stream.of(p.users)).toList();
pages.stream().flatMap(p -> Stream.of(p.topicList.topics))
.filter(t -> showUnpublished || Arrays.asList(t.tags).contains(PUBLISHED_TAG))
.filter(t -> showUnpublished || List.of(t.tags).contains(PUBLISHED_TAG))
.map(t -> Optional.ofNullable(convertTopicItemToAddon(t, users)))
.forEach(a -> a.ifPresent(addons::add));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.core.addon;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -196,7 +195,7 @@ public Builder withConnection(@Nullable String connection) {
}

public Builder withCountries(@Nullable String countries) {
this.countries = countries == null || countries.isBlank() ? List.of() : Arrays.asList(countries.split(","));
this.countries = countries == null || countries.isBlank() ? List.of() : List.of(countries.split(","));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public AudioConsoleCommandExtension(final @Reference AudioManager audioManager,

@Override
public List<String> getUsages() {
return Arrays.asList(new String[] {
return List.of(
buildCommandUsage(SUBCMD_PLAY + " [<sink>] <filename>",
"plays a sound file from the sounds folder through the optionally specified audio sink(s)"),
buildCommandUsage(SUBCMD_PLAY + " <sink> <filename> <volume>",
Expand All @@ -79,7 +79,7 @@ public List<String> getUsages() {
buildCommandUsage(SUBCMD_SYNTHESIZE + " <sink> \"<melody>\" <volume>",
"synthesize a tone melody and play it through the optionally specified audio sink(s) with the specified volume"),
buildCommandUsage(SUBCMD_SOURCES, "lists the audio sources"),
buildCommandUsage(SUBCMD_SINKS, "lists the audio sinks") });
buildCommandUsage(SUBCMD_SINKS, "lists the audio sinks"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.IOException;
import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -236,7 +235,7 @@ private void assertRegisteredSource(boolean isSourceDefault) {
audioManager.modified(Map.of(AudioManagerImpl.CONFIG_DEFAULT_SOURCE, audioSource.getId()));
} else {
// just to make sure there is no default source
audioManager.modified(Collections.emptyMap());
audioManager.modified(Map.of());
}

assertThat(String.format("The source %s was not registered", audioSource.getId()), audioManager.getSource(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.io.IOException;
import java.security.Principal;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -73,7 +72,7 @@ public Authentication authenticate(final Credentials credentials) throws Authent
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Principal userPrincipal = new GenericUser(name);
Subject subject = new Subject(true, Set.of(userPrincipal), Collections.emptySet(), Set.of(userCredentials));
Subject subject = new Subject(true, Set.of(userPrincipal), Set.of(), Set.of(userCredentials));

Thread.currentThread().setContextClassLoader(ManagedUserLoginModule.class.getClassLoader());
LoginContext loginContext = new LoginContext(realmName, subject, new CallbackHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -134,7 +134,7 @@ public RuleSupportScriptExtension(final @Reference RuleRegistry ruleRegistry,

@Override
public Collection<String> getDefaultPresets() {
return Collections.emptyList();
return List.of();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void setConfigurationDescriptions(@Nullable List<ConfigDescriptionParamet

@Override
public List<Condition> getConditions() {
return conditions == null ? Collections.emptyList() : conditions;
return conditions == null ? List.of() : conditions;
}

/**
Expand All @@ -180,12 +180,12 @@ public void setConditions(@Nullable List<Condition> conditions) {

@Override
public List<Action> getActions() {
return actions == null ? Collections.emptyList() : actions;
return actions == null ? List.of() : actions;
}

@Override
public List<Trigger> getTriggers() {
return triggers == null ? Collections.emptyList() : triggers;
return triggers == null ? List.of() : triggers;
}

/**
Expand Down Expand Up @@ -227,7 +227,7 @@ public <T extends Module> List<T> getModules(@Nullable Class<T> moduleClazz) {
} else if (Action.class == moduleClazz) {
result = (List<T>) actions;
} else {
result = Collections.emptyList();
result = List.of();
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -71,7 +70,7 @@ public Map<String, Object> importPreset(String scriptIdentifier, String preset)
}
}

return Collections.emptyMap();
return Map.of();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -257,7 +256,7 @@ public Collection<ConfigDescription> getConfigDescriptions(@Nullable Locale loca
return List.of(configDescription);
}

return Collections.emptyList();
return List.of();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public Map<String, Object> importPreset(String scriptIdentifier, String preset)
if (PRESET_DEFAULT.equals(preset)) {
return Collections.unmodifiableMap(elements);
}
return Collections.emptyMap();
return Map.of();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -75,8 +74,7 @@ public class ScriptTransformationServiceTest {

@BeforeEach
public void setUp() throws ScriptException {
Map<String, Object> properties = new HashMap<>();
properties.put(ScriptTransformationService.SCRIPT_TYPE_PROPERTY_NAME, SCRIPT_LANGUAGE);
Map<String, Object> properties = Map.of(ScriptTransformationService.SCRIPT_TYPE_PROPERTY_NAME, SCRIPT_LANGUAGE);
service = new ScriptTransformationService(transformationRegistry, mock(ConfigDescriptionRegistry.class),
scriptEngineManager, properties);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public Set<String> getTags() {
* @param ruleTags the {@link RuleImpl}'s assigned tags.
*/
public void setTags(@Nullable Set<String> ruleTags) {
tags = ruleTags == null ? Collections.emptySet() : Collections.unmodifiableSet(ruleTags);
tags = ruleTags == null ? Set.of() : Collections.unmodifiableSet(ruleTags);
}

@Override
Expand Down Expand Up @@ -205,7 +205,7 @@ public List<ConfigDescriptionParameter> getConfigurationDescriptions() {
* properties of the {@link RuleImpl}.
*/
public void setConfigurationDescriptions(@Nullable List<ConfigDescriptionParameter> configDescriptions) {
this.configDescriptions = configDescriptions == null ? Collections.emptyList()
this.configDescriptions = configDescriptions == null ? List.of()
: Collections.unmodifiableList(configDescriptions);
}

Expand All @@ -220,7 +220,7 @@ public List<Condition> getConditions() {
* @param conditions a list with the conditions that should belong to this {@link RuleImpl}.
*/
public void setConditions(@Nullable List<Condition> conditions) {
this.conditions = conditions == null ? Collections.emptyList() : Collections.unmodifiableList(conditions);
this.conditions = conditions == null ? List.of() : Collections.unmodifiableList(conditions);
}

@Override
Expand All @@ -239,7 +239,7 @@ public List<Trigger> getTriggers() {
* @param actions a list with the actions that should belong to this {@link RuleImpl}.
*/
public void setActions(@Nullable List<Action> actions) {
this.actions = actions == null ? Collections.emptyList() : Collections.unmodifiableList(actions);
this.actions = actions == null ? List.of() : Collections.unmodifiableList(actions);
}

/**
Expand All @@ -248,7 +248,7 @@ public void setActions(@Nullable List<Action> actions) {
* @param triggers a list with the triggers that should belong to this {@link RuleImpl}.
*/
public void setTriggers(@Nullable List<Trigger> triggers) {
this.triggers = triggers == null ? Collections.emptyList() : Collections.unmodifiableList(triggers);
this.triggers = triggers == null ? List.of() : Collections.unmodifiableList(triggers);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -146,8 +145,7 @@ public Set<ModuleType> importModuleTypes(String parserType, URL url) throws IOEx
@Override
public Collection<ModuleType> getModuleTypes(@Nullable Locale locale) {
synchronized (providedObjectsHolder) {
return !providedObjectsHolder.isEmpty() ? providedObjectsHolder.values()
: Collections.<ModuleType> emptyList();
return !providedObjectsHolder.isEmpty() ? providedObjectsHolder.values() : List.of();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -68,6 +67,6 @@ public Set<Rule> parse(InputStreamReader reader) throws ParsingException {
} catch (IOException e) {
}
}
return Collections.emptySet();
return Set.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -68,6 +67,6 @@ public Set<Template> parse(InputStreamReader reader) throws ParsingException {
} catch (IOException e) {
}
}
return Collections.emptySet();
return Set.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -393,7 +392,7 @@ protected List<ConfigDescriptionParameter> getLocalizedConfigurationDescription(
return config;
}
}
return Collections.emptyList();
return List.of();
}

/**
Expand Down Expand Up @@ -433,7 +432,7 @@ protected Set<E> parseData(Parser<E> parser, URL url, Bundle bundle) {
}
}
}
return Collections.emptySet();
return Set.of();
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -118,11 +117,11 @@ protected void processAutomationProvider(Bundle bundle) {
if (parser != null) {
Set<Rule> parsedObjects = parseData(parser, url, bundle);
if (!parsedObjects.isEmpty()) {
addNewProvidedObjects(Collections.emptyList(), Collections.emptyList(), parsedObjects);
addNewProvidedObjects(List.of(), List.of(), parsedObjects);
}
}
}
putNewPortfolio(vendor, Collections.emptyList());
putNewPortfolio(vendor, List.of());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
package org.openhab.core.automation.internal.provider.file;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -56,7 +56,7 @@ public Collection<ModuleType> getAll() {
public <T extends ModuleType> Collection<T> getModuleTypes(@Nullable Locale locale) {
Collection<ModuleType> values = providedObjectsHolder.values();
if (values.isEmpty()) {
return Collections.emptyList();
return List.of();
}
return (Collection<T>) new LinkedList<>(values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
package org.openhab.core.automation.internal.provider.file;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -55,7 +55,7 @@ public Collection<RuleTemplate> getAll() {
public Collection<RuleTemplate> getTemplates(@Nullable Locale locale) {
Collection<RuleTemplate> values = providedObjectsHolder.values();
if (values.isEmpty()) {
return Collections.emptyList();
return List.of();
}
return new LinkedList<>(values);
}
Expand Down
Loading

0 comments on commit e8e1c9f

Please sign in to comment.