Skip to content

Commit

Permalink
use collections instead of arrays; optimise their use
Browse files Browse the repository at this point in the history
  • Loading branch information
Gintas Grigelionis committed Aug 24, 2017
1 parent 14313cf commit 90c42ec
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 98 deletions.
14 changes: 10 additions & 4 deletions src/java/org/apache/ivy/core/IvyPatternHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static String substitute(String pattern, String org, String module, Strin
tokens.put(ORIGINAL_ARTIFACTNAME_KEY, new OriginalArtifactNameValue(origin));
}

return substituteTokens(pattern, tokens);
return substituteTokens(pattern, tokens, false);
}

// CheckStyle:ParameterNumber ON
Expand Down Expand Up @@ -218,9 +218,15 @@ private static String substituteVariables(String pattern, IvyVariableContainer v
}
}

@SuppressWarnings({"rawtypes", "unchecked"})
public static String substituteTokens(String pattern, Map tokens) {
Map<String, Object> tokensCopy = new HashMap<>(tokens);
// This is a cludge to reconcile different values passed to the method
public static String substituteTokens(String pattern, Map<String, String> tokens) {
Map<String, Object> tokensCopy = new HashMap<>();
tokensCopy.putAll(tokens);
return substituteTokens(pattern, tokensCopy, true);
}

private static String substituteTokens(String pattern, Map<String, Object> tokens, boolean external) {
Map<String, Object> tokensCopy = external ? tokens : new HashMap<>(tokens);
if (tokensCopy.containsKey(ORGANISATION_KEY) && !tokensCopy.containsKey(ORGANISATION_KEY2)) {
tokensCopy.put(ORGANISATION_KEY2, tokensCopy.get(ORGANISATION_KEY));
}
Expand Down
25 changes: 13 additions & 12 deletions src/java/org/apache/ivy/core/module/descriptor/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -74,7 +75,7 @@ public static Collection<Configuration> findConfigurationExtending(String conf,

private String description;

private String[] extendsFrom;
private Set<String> extendsFrom;

private Visibility visibility;

Expand All @@ -95,9 +96,9 @@ public Configuration(String name) {
}

public Configuration(Configuration source, ModuleRevisionId sourceModule) {
this(source.getAttributes(), source.getQualifiedExtraAttributes(), source.getName(), source
.getVisibility(), source.getDescription(), source.getExtends(), source
.isTransitive(), source.getDeprecated(), sourceModule);
this(source.getAttributes(), source.getQualifiedExtraAttributes(), source.getName(),
source.getVisibility(), source.getDescription(), source.getExtends(),
source.isTransitive(), source.getDeprecated(), sourceModule);
}

/**
Expand All @@ -122,7 +123,7 @@ public Configuration(String name, Visibility visibility, String description, Str
}

private Configuration(Map<String, String> attributes, Map<String, String> extraAttributes,
String name, Visibility visibility, String description, String[] ext,
String name, Visibility visibility, String description, String[] exts,
boolean transitive, String deprecated, ModuleRevisionId sourceModule) {
super(attributes, extraAttributes);

Expand All @@ -135,12 +136,12 @@ private Configuration(Map<String, String> attributes, Map<String, String> extraA
this.name = name;
this.visibility = visibility;
this.description = description;
if (ext == null) {
extendsFrom = new String[0];
if (exts == null) {
extendsFrom = Collections.emptySet();
} else {
extendsFrom = new String[ext.length];
for (int i = 0; i < ext.length; i++) {
extendsFrom[i] = ext[i].trim();
extendsFrom = new LinkedHashSet<>();
for (String ext : exts) {
extendsFrom.add(ext.trim());
}
}
this.transitive = transitive;
Expand Down Expand Up @@ -168,7 +169,7 @@ public String getDescription() {
* @return Returns the extends. May be empty, but never null.
*/
public String[] getExtends() {
return extendsFrom;
return extendsFrom.toArray(new String[extendsFrom.size()]);
}

/**
Expand Down Expand Up @@ -232,7 +233,7 @@ public void replaceWildcards(ModuleDescriptor md) {
}
}

this.extendsFrom = newExtends.toArray(new String[newExtends.size()]);
this.extendsFrom = newExtends;
}

private void addOther(Configuration[] allConfigs, Visibility visibility, Set<String> configs) {
Expand Down
6 changes: 3 additions & 3 deletions src/java/org/apache/ivy/core/report/ResolveReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,16 @@ public String getResolveId() {
* specified one.
*
* @param extended String
* @return String[]
* @return Set of String
*/
@SuppressWarnings("unused")
private String[] getExtendingConfs(String extended) {
private Set<String> getExtendingConfs(String extended) {
Set<String> extendingConfs = new HashSet<>();
extendingConfs.add(extended);
for (String conf : md.getConfigurationsNames()) {
gatherExtendingConfs(extendingConfs, conf, extended);
}
return extendingConfs.toArray(new String[extendingConfs.size()]);
return extendingConfs;
}

private boolean gatherExtendingConfs(Set<String> extendingConfs, String conf, String extended) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
* uses only the SAX API, which makes the parsing code harder to understand.
*/
public class XmlModuleDescriptorParser extends AbstractModuleDescriptorParser {
static final String[] DEPENDENCY_REGULAR_ATTRIBUTES = new String[] {"org", "name", "branch",
"branchConstraint", "rev", "revConstraint", "force", "transitive", "changing", "conf"};
static final List<String> DEPENDENCY_REGULAR_ATTRIBUTES = Arrays.asList("org", "name", "branch",
"branchConstraint", "rev", "revConstraint", "force", "transitive", "changing", "conf");

private static final XmlModuleDescriptorParser INSTANCE = new XmlModuleDescriptorParser();

Expand Down Expand Up @@ -845,8 +845,8 @@ protected void confStarted(Attributes attributes) {
settings.substitute(attributes.getValue("description")),
(ext == null) ? null : ext.split(","), transitive, deprecated);
ExtendableItemHelper.fillExtraAttributes(settings, configuration, attributes,
new String[] {"name", "visibility", "extends", "transitive", "description",
"deprecated"});
Arrays.asList("name", "visibility", "extends", "transitive", "description",
"deprecated"));
getMd().addConfiguration(configuration);
break;
case State.PUB:
Expand Down Expand Up @@ -954,7 +954,7 @@ protected void artifactStarted(String qName, Attributes attributes)
String url = settings.substitute(attributes.getValue("url"));
artifact = new MDArtifact(getMd(), artName, type, ext, url == null ? null
: new URL(url), ExtendableItemHelper.getExtraAttributes(settings,
attributes, new String[] {"ext", "type", "name", "conf"}));
attributes, Arrays.asList("ext", "type", "name", "conf")));
String confs = settings.substitute(attributes.getValue("conf"));
// only add confs if they are specified. if they aren't, endElement will
// handle this
Expand Down Expand Up @@ -1013,9 +1013,9 @@ protected void infoStarted(Attributes attributes) {
module,
branch,
revision,
ExtendableItemHelper.getExtraAttributes(settings, attributes, new String[] {
ExtendableItemHelper.getExtraAttributes(settings, attributes, Arrays.asList(
"organisation", "module", "revision", "status", "publication",
"branch", "namespace", "default", "resolver"})));
"branch", "namespace", "default", "resolver"))));

String namespace = settings.substitute(attributes.getValue("namespace"));
if (namespace != null) {
Expand Down Expand Up @@ -1107,7 +1107,7 @@ protected void parseRule(String tag, Attributes attributes) throws MalformedURLE
if (state == State.DEP_ARTIFACT) {
String url = settings.substitute(attributes.getValue("url"));
Map<String, String> extraAtt = ExtendableItemHelper.getExtraAttributes(settings,
attributes, new String[] {"name", "type", "ext", "url", "conf"});
attributes, Arrays.asList("name", "type", "ext", "url", "conf"));
confAware = new DefaultDependencyArtifactDescriptor(dd, name, type, ext,
url == null ? null : new URL(url), extraAtt);
} else if (state == State.ARTIFACT_INCLUDE) {
Expand All @@ -1118,8 +1118,8 @@ protected void parseRule(String tag, Attributes attributes) throws MalformedURLE
module = module == null ? PatternMatcher.ANY_EXPRESSION : module;
ArtifactId aid = new ArtifactId(new ModuleId(org, module), name, type, ext);
Map<String, String> extraAtt = ExtendableItemHelper.getExtraAttributes(settings,
attributes, new String[] {"org", "module", "name", "type", "ext", "matcher",
"conf"});
attributes, Arrays.asList("org", "module", "name", "type", "ext", "matcher",
"conf"));
confAware = new DefaultIncludeRule(aid, matcher, extraAtt);
} else { // _state == ARTIFACT_EXCLUDE || EXCLUDE
PatternMatcher matcher = getPatternMatcher(attributes.getValue("matcher"));
Expand All @@ -1129,8 +1129,8 @@ protected void parseRule(String tag, Attributes attributes) throws MalformedURLE
module = module == null ? PatternMatcher.ANY_EXPRESSION : module;
ArtifactId aid = new ArtifactId(new ModuleId(org, module), name, type, ext);
Map<String, String> extraAtt = ExtendableItemHelper.getExtraAttributes(settings,
attributes, new String[] {"org", "module", "name", "type", "ext", "matcher",
"conf"});
attributes, Arrays.asList("org", "module", "name", "type", "ext", "matcher",
"conf"));
confAware = new DefaultExcludeRule(aid, matcher, extraAtt);
}
String confs = settings.substitute(attributes.getValue("conf"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,9 @@ private void infoStarted(Attributes attributes) {

// if necessary translate mrid using optional namespace argument
ModuleRevisionId localMid = ModuleRevisionId.newInstance(organisation, module, branch,
rev, ExtendableItemHelper
.getExtraAttributes(settings, attributes, new String[] {"organisation",
"module", "revision", "status", "publication", "namespace"}));
rev, ExtendableItemHelper.getExtraAttributes(settings, attributes,
Arrays.asList("organisation", "module", "revision", "status",
"publication", "namespace")));
ModuleRevisionId systemMid = (ns == null) ? localMid : ns.getToSystemTransformer()
.transform(localMid);

Expand Down
30 changes: 15 additions & 15 deletions src/java/org/apache/ivy/plugins/version/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*/
package org.apache.ivy.plugins.version;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
Expand Down Expand Up @@ -76,16 +77,16 @@ public void setRevision(String revision) {
public Matcher getPatternMatcher(ModuleRevisionId askedMrid) {
String revision = askedMrid.getRevision();

String[] args = split(getArgs());
String[] argValues = getRevisionArgs(revision);
List<String> args = split(getArgs());
List<String> argValues = getRevisionArgs(revision);

if (args.length != argValues.length) {
if (args.size() != argValues.size()) {
return new NoMatchMatcher();
}

Map<String, String> variables = new HashMap<>();
for (int i = 0; i < args.length; i++) {
variables.put(args[i], argValues[i]);
for (String arg : args) {
variables.put(arg, argValues.get(args.indexOf(arg)));
}

String pattern = getPattern();
Expand All @@ -95,33 +96,32 @@ public Matcher getPatternMatcher(ModuleRevisionId askedMrid) {
return pMatcher.getMatcher(pattern);
}

private String[] getRevisionArgs(String revision) {
private List<String> getRevisionArgs(String revision) {
int bracketStartIndex = revision.indexOf('(');
if (bracketStartIndex == -1) {
return new String[0];
return Collections.emptyList();
}

int bracketEndIndex = revision.indexOf(')');
if (bracketEndIndex <= (bracketStartIndex + 1)) {
return new String[0];
return Collections.emptyList();
}

String args = revision.substring(bracketStartIndex + 1, bracketEndIndex);
return split(args);
return split(revision.substring(bracketStartIndex + 1, bracketEndIndex));
}

private static String[] split(String string) {
private static List<String> split(String string) {
if (string == null) {
return new String[0];
return Collections.emptyList();
}

StringTokenizer tokenizer = new StringTokenizer(string, ", ");
List<String> tokens = new ArrayList<>();
List<String> tokens = new LinkedList<>();
while (tokenizer.hasMoreTokens()) {
tokens.add(tokenizer.nextToken());
}

return tokens.toArray(new String[tokens.size()]);
return tokens;
}

private static class NoMatchMatcher implements Matcher {
Expand Down
21 changes: 16 additions & 5 deletions src/java/org/apache/ivy/util/extendable/ExtendableItemHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package org.apache.ivy.util.extendable;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

Expand All @@ -40,6 +40,12 @@ public static Map<String, String> getExtraAttributes(Attributes attributes, Stri
return ret;
}

@Deprecated
public static Map<String, String> getExtraAttributes(ParserSettings settings,
Attributes attributes, String[] ignoredAttNames) {
return getExtraAttributes(settings, attributes, Arrays.asList(ignoredAttNames));
}

/**
* Extract from the XML attribute the extra Ivy ones
*
Expand All @@ -50,19 +56,24 @@ public static Map<String, String> getExtraAttributes(Attributes attributes, Stri
* @return Map&lt;String,String&gt;
*/
public static Map<String, String> getExtraAttributes(ParserSettings settings,
Attributes attributes, String[] ignoredAttNames) {
Attributes attributes, List<String> ignoredAttNames) {
Map<String, String> ret = new HashMap<>();
Collection<String> ignored = Arrays.asList(ignoredAttNames);
for (int i = 0; i < attributes.getLength(); i++) {
if (!ignored.contains(attributes.getQName(i))) {
if (!ignoredAttNames.contains(attributes.getQName(i))) {
ret.put(attributes.getQName(i), settings.substitute(attributes.getValue(i)));
}
}
return ret;
}

@Deprecated
public static void fillExtraAttributes(ParserSettings settings, DefaultExtendableItem item,
Attributes attributes, String[] ignoredAttNames) {
fillExtraAttributes(settings, item, attributes, Arrays.asList(ignoredAttNames));
}

public static void fillExtraAttributes(ParserSettings settings, DefaultExtendableItem item,
Attributes attributes, String[] ignoredAttNames) {
Attributes attributes, List<String> ignoredAttNames) {
Map<String, String> att = getExtraAttributes(settings, attributes, ignoredAttNames);
for (Entry<String, String> entry : att.entrySet()) {
item.setExtraAttribute(entry.getKey(), entry.getValue());
Expand Down
Loading

0 comments on commit 90c42ec

Please sign in to comment.