Skip to content

Commit

Permalink
Collection construction clean-ups
Browse files Browse the repository at this point in the history
* Provide copied collections to the constructor when possible, instead
  of using .addAll(), .putAll() etc.
* Add collections in one call when possible, instead of looping.

Signed-off-by: Stephen Kitt <[email protected]>
  • Loading branch information
skitt committed May 21, 2019
1 parent 8833b6e commit 1112adc
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ private String getAttribute(BundleCapability capability, String name) {
*/
private void createNode(Node<Bundle> node) {
Bundle bundle = node.getValue();
Collection<Bundle> exporters = new HashSet<>();
exporters.addAll(bundleService.getWiredBundles(bundle).values());
Collection<Bundle> exporters = new HashSet<>(bundleService.getWiredBundles(bundle).values());

for (Bundle exporter : exporters) {
if (node.hasAncestor(exporter)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,8 +944,7 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB
return;
}

toStop = new HashSet<>();
toStop.addAll(toRefresh.keySet());
toStop = new HashSet<>(toRefresh.keySet());
removeFragmentsAndBundlesInState(toStop, UNINSTALLED | RESOLVED | STOPPING);
if (!toStop.isEmpty()) {
print("Stopping bundles:", verbose);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ void bootDone() {
public void registerListener(FeaturesListener listener) {
listeners.add(listener);
try {
Set<String> repositoriesList = new TreeSet<>();
Map<String, Set<String>> installedFeatures = new TreeMap<>();
Set<String> repositoriesList;
Map<String, Set<String>> installedFeatures;
synchronized (lock) {
repositoriesList.addAll(state.repositories);
installedFeatures.putAll(copy(state.installedFeatures));
repositoriesList = new TreeSet<>(state.repositories);
installedFeatures = new TreeMap<>(copy(state.installedFeatures));
}
for (String uri : repositoriesList) {
Repository repository = repositories.create(URI.create(uri), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ private static String explode(Dictionary dictionary) {
private static final Collection<ServiceReference> asCollection(ServiceReference[] references) {
List<ServiceReference> result = new LinkedList<>();
if (references != null) {
for (ServiceReference reference : references) {
result.add(reference);
}
result.addAll(Arrays.asList(references));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Enumeration;
Expand Down Expand Up @@ -201,9 +202,7 @@ private static String explode(Dictionary dictionary) {
private static final Collection<ServiceReference> asCollection(ServiceReference[] references) {
List<ServiceReference> result = new LinkedList<>();
if (references != null) {
for (ServiceReference reference : references) {
result.add(reference);
}
result.addAll(Arrays.asList(references));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public AppConfigurationEntry[] getEntries() {
logOptions.put("logger", properties.get("audit.log.logger"));
logOptions.put("level", properties.get("audit.log.level"));

Map<String, Object> eventadminOptions = new HashMap<>();
eventadminOptions.putAll(properties);
Map<String, Object> eventadminOptions = new HashMap<>(properties);
eventadminOptions.put(BundleContext.class.getName(), bundleContext);
eventadminOptions.put(ProxyLoginModule.PROPERTY_MODULE, EVENTADMIN_AUDIT_MODULE);
eventadminOptions.put(ProxyLoginModule.PROPERTY_BUNDLE, Long.toString(bundleContext.getBundle().getBundleId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ private String[] doGetUserRoles(String user, String userDn, String userDnNamespa
if (roleMappings.isEmpty()) {
rolesList.add(role);
} else {
for (String roleMapped : roleMappings) {
rolesList.add(roleMapped);
}
rolesList.addAll(roleMappings);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.karaf.management.internal;

import java.util.Arrays;
import org.apache.karaf.management.JMXSecurityMBean;
import org.apache.karaf.management.KarafMBeanServerGuard;
import org.slf4j.Logger;
Expand Down Expand Up @@ -124,9 +125,7 @@ private String parseMethodName(String method, List<String> argTypes) {
return method;

String args = method.substring(index + 1, method.length() - 1);
for (String arg : args.split(",")) {
argTypes.add(arg);
}
argTypes.addAll(Arrays.asList(args.split(",")));

return method.substring(0, index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,7 @@ protected MavenRepositoryURL[] repositories(Dictionary<String, Object> config, b
repositories[0] = repositories[0].substring(1);
}

List<String> newRepositories = new LinkedList<>();
newRepositories.addAll(Arrays.asList(repositories));
List<String> newRepositories = new LinkedList<>(Arrays.asList(repositories));

// append all repositories from all active profiles from available settings.xml
if (mavenSettings != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public <T> T instantiate(Class<? extends T> clazz, Registry registry) throws Exc
GenericType type = new GenericType(field.getGenericType());
Object value;
if (type.getRawClass() == List.class) {
Set<Object> set = new HashSet<>();
set.addAll(registry.getServices(type.getActualTypeArgument(0).getRawClass()));
Set<Object> set =
new HashSet<>(registry.getServices(type.getActualTypeArgument(0).getRawClass()));
if (registry != this.dependencies) {
set.addAll(this.dependencies.getServices(type.getActualTypeArgument(0).getRawClass()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public void close() {
}

private void updateState() {
List<T> svcs = new ArrayList<>();
List<T> svcs;
synchronized (refs) {
svcs.addAll(refs.values());
svcs = new ArrayList<>(refs.values());
}
updateState(svcs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ String getHeader() {
}

String getContent(String content) {
List<String> lines = new ArrayList<>();
lines.addAll(Arrays.asList(content.split("\n")));
List<String> lines = new ArrayList<>(Arrays.asList(content.split("\n")));
if (wrap) {
List<String> wrapped = new ArrayList<>();
for (String line : lines) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.karaf.tooling.utils;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -45,12 +46,8 @@ private ManifestUtils() {
* @return the list of imports
*/
public static List<Clause> getImports(Manifest manifest) {
List<Clause> result = new LinkedList<>();
Clause[] clauses = Parser.parseHeader(getHeader(Constants.IMPORT_PACKAGE, manifest));
for (Clause clause : clauses) {
result.add(clause);
}
return result;
Clause[] clauses = Parser.parseHeader(getHeader(Constants.IMPORT_PACKAGE, manifest));
return new LinkedList<>(Arrays.asList(clauses));
}

/**
Expand All @@ -76,12 +73,8 @@ public static List<Clause> getMandatoryImports(Manifest manifest) {
* @return the list of exports
*/
public static List<Clause> getExports(Manifest manifest) {
List<Clause> result = new LinkedList<>();
Clause[] clauses = Parser.parseHeader(getHeader(Constants.EXPORT_PACKAGE, manifest));
for (Clause clause : clauses) {
result.add(clause);
}
return result;
Clause[] clauses = Parser.parseHeader(getHeader(Constants.EXPORT_PACKAGE, manifest));
return new LinkedList<>(Arrays.asList(clauses));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -310,9 +311,7 @@ private List<Repository> getRepositories() {
}

try {
for (Repository r : featuresService.listRepositories()) {
repositories.add(r);
}
repositories.addAll(Arrays.asList(featuresService.listRepositories()));
} catch (Exception e) {
this.log.error(e.getMessage());
}
Expand Down

0 comments on commit 1112adc

Please sign in to comment.