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

fix the last inconsistencies in generics #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 29 additions & 46 deletions src/java/org/apache/ivy/core/search/SearchEngine.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.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -59,9 +60,8 @@ public String[] listTokenValues(String token, Map<String, Object> otherTokenValu
Set<String> entries = new LinkedHashSet<>();

for (DependencyResolver resolver : settings.getResolvers()) {
Map<String, String>[] values = resolver.listTokenValues(new String[] {token},
otherTokenValues);
for (Map<String, String> value : values) {
for (Map<String, String> value : resolver.listTokenValues(
Collections.singleton(token), otherTokenValues)) {
entries.add(value.get(token));
}
}
Expand All @@ -73,11 +73,10 @@ public OrganisationEntry[] listOrganisationEntries() {
Set<OrganisationEntry> entries = new HashSet<>();

for (DependencyResolver resolver : settings.getResolvers()) {
Map<String, String>[] orgs = resolver.listTokenValues(
new String[] {IvyPatternHelper.ORGANISATION_KEY}, new HashMap<String, Object>());
for (Map<String, String> oe : orgs) {
String org = oe.get(IvyPatternHelper.ORGANISATION_KEY);
entries.add(new OrganisationEntry(resolver, org));
for (Map<String, String> oe : resolver.listTokenValues(
Collections.singleton(IvyPatternHelper.ORGANISATION_KEY),
new HashMap<String, Object>())) {
entries.add(new OrganisationEntry(resolver, oe.get(IvyPatternHelper.ORGANISATION_KEY)));
}
}

Expand All @@ -88,9 +87,9 @@ public String[] listOrganisations() {
Set<String> entries = new HashSet<>();

for (DependencyResolver resolver : settings.getResolvers()) {
Map<String, String>[] orgs = resolver.listTokenValues(
new String[] {IvyPatternHelper.ORGANISATION_KEY}, new HashMap<String, Object>());
for (Map<String, String> org : orgs) {
for (Map<String, String> org : resolver.listTokenValues(
Collections.singleton(IvyPatternHelper.ORGANISATION_KEY),
new HashMap<String, Object>())) {
entries.add(org.get(IvyPatternHelper.ORGANISATION_KEY));
}
}
Expand All @@ -105,11 +104,9 @@ public ModuleEntry[] listModuleEntries(OrganisationEntry org) {
tokenValues.put(IvyPatternHelper.ORGANISATION_KEY, org.getOrganisation());

for (DependencyResolver resolver : settings.getResolvers()) {
Map<String, String>[] modules = resolver.listTokenValues(
new String[] {IvyPatternHelper.MODULE_KEY}, tokenValues);
for (Map<String, String> me : modules) {
String module = me.get(IvyPatternHelper.MODULE_KEY);
entries.add(new ModuleEntry(org, module));
for (Map<String, String> me : resolver.listTokenValues(
Collections.singleton(IvyPatternHelper.MODULE_KEY), tokenValues)) {
entries.add(new ModuleEntry(org, me.get(IvyPatternHelper.MODULE_KEY)));
}
}

Expand All @@ -123,9 +120,8 @@ public String[] listModules(String org) {
tokenValues.put(IvyPatternHelper.ORGANISATION_KEY, org);

for (DependencyResolver resolver : settings.getResolvers()) {
Map<String, String>[] modules = resolver.listTokenValues(
new String[] {IvyPatternHelper.MODULE_KEY}, tokenValues);
for (Map<String, String> module : modules) {
for (Map<String, String> module : resolver.listTokenValues(
Collections.singleton(IvyPatternHelper.MODULE_KEY), tokenValues)) {
entries.add(module.get(IvyPatternHelper.MODULE_KEY));
}
}
Expand All @@ -141,9 +137,8 @@ public RevisionEntry[] listRevisionEntries(ModuleEntry module) {
tokenValues.put(IvyPatternHelper.MODULE_KEY, module.getModule());

for (DependencyResolver resolver : settings.getResolvers()) {
Map<String, String>[] revisions = resolver.listTokenValues(
new String[] {IvyPatternHelper.REVISION_KEY}, tokenValues);
for (Map<String, String> revision : revisions) {
for (Map<String, String> revision : resolver.listTokenValues(
Collections.singleton(IvyPatternHelper.REVISION_KEY), tokenValues)) {
entries.add(new RevisionEntry(module, revision.get(IvyPatternHelper.REVISION_KEY)));
}
}
Expand All @@ -159,9 +154,8 @@ public String[] listRevisions(String org, String module) {
tokenValues.put(IvyPatternHelper.MODULE_KEY, module);

for (DependencyResolver resolver : settings.getResolvers()) {
Map<String, String>[] revisions = resolver.listTokenValues(
new String[] {IvyPatternHelper.REVISION_KEY}, tokenValues);
for (Map<String, String> revision : revisions) {
for (Map<String, String> revision : resolver.listTokenValues(
Collections.singleton(IvyPatternHelper.REVISION_KEY), tokenValues)) {
entries.add(revision.get(IvyPatternHelper.REVISION_KEY));
}
}
Expand All @@ -188,17 +182,13 @@ public ModuleId[] listModules(ModuleId moduleCrit, PatternMatcher matcher) {
IvyPatternHelper.ORGANISATION_KEY);
addMatcher(matcher, moduleCrit.getName(), criteria, IvyPatternHelper.MODULE_KEY);

String[] tokensToList = new String[] {IvyPatternHelper.ORGANISATION_KEY,
IvyPatternHelper.MODULE_KEY};

for (DependencyResolver resolver : settings.getResolvers()) {
Map<String, String>[] moduleIdAsMap = resolver.listTokenValues(tokensToList, criteria);
for (Map<String, String> moduleId : moduleIdAsMap) {
String org = moduleId.get(IvyPatternHelper.ORGANISATION_KEY);
String name = moduleId.get(IvyPatternHelper.MODULE_KEY);
ModuleId modId = ModuleId.newInstance(org, name);
ret.add(NameSpaceHelper.transform(modId, resolver.getNamespace()
.getToSystemTransformer()));
for (Map<String, String> moduleId : resolver.listTokenValues(new HashSet<>(
Arrays.asList(IvyPatternHelper.ORGANISATION_KEY, IvyPatternHelper.MODULE_KEY)), criteria)) {
ModuleId modId = ModuleId.newInstance(moduleId.get(IvyPatternHelper.ORGANISATION_KEY),
moduleId.get(IvyPatternHelper.MODULE_KEY));
ret.add(NameSpaceHelper.transform(modId,
resolver.getNamespace().getToSystemTransformer()));
}
}

Expand All @@ -224,12 +214,9 @@ public ModuleRevisionId[] listModules(ModuleRevisionId moduleCrit, PatternMatche
addMatcher(matcher, entry.getValue(), criteria, entry.getKey());
}

String[] tokensToList = moduleCrit.getAttributes().keySet()
.toArray(new String[moduleCrit.getAttributes().size()]);

for (DependencyResolver resolver : settings.getResolvers()) {
Map<String, String>[] moduleIdAsMap = resolver.listTokenValues(tokensToList, criteria);
for (Map<String, String> moduleId : moduleIdAsMap) {
for (Map<String, String> moduleId : resolver.listTokenValues(moduleCrit.getAttributes().keySet(),
criteria)) {
String org = moduleId.get(IvyPatternHelper.ORGANISATION_KEY);
String name = moduleId.get(IvyPatternHelper.MODULE_KEY);
String branch = moduleId.get(IvyPatternHelper.BRANCH_KEY);
Expand Down Expand Up @@ -280,12 +267,9 @@ public ModuleRevisionId[] listModules(DependencyResolver resolver, ModuleRevisio
addMatcher(matcher, entry.getValue(), criteria, entry.getKey());
}

String[] tokensToList = moduleCrit.getAttributes().keySet()
.toArray(new String[moduleCrit.getAttributes().size()]);

Map<String, String>[] moduleIdAsMap = resolver.listTokenValues(tokensToList, criteria);
Set<ModuleRevisionId> result = new LinkedHashSet<>(); // we use a Set to remove duplicates
for (Map<String, String> moduleId : moduleIdAsMap) {
for (Map<String, String> moduleId : resolver.listTokenValues(moduleCrit.getAttributes().keySet(),
criteria)) {
String org = moduleId.get(IvyPatternHelper.ORGANISATION_KEY);
String name = moduleId.get(IvyPatternHelper.MODULE_KEY);
String branch = moduleId.get(IvyPatternHelper.BRANCH_KEY);
Expand Down Expand Up @@ -368,7 +352,6 @@ public Collection<ModuleRevisionId> findModuleRevisionIds(DependencyResolver res
+ " on " + resolverName);
boolean foundModule = false;
for (ModuleEntry mEntry : modules) {

ModuleId foundMid = new ModuleId(mEntry.getOrganisation(), mEntry.getModule());
ModuleId systemMid = foundMid;
if (fromNamespace != null) {
Expand Down
9 changes: 1 addition & 8 deletions src/java/org/apache/ivy/osgi/repo/AbstractOSGiResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,8 @@ private void filterCapabilityValues(Set<String> capabilityValues,
}
}

@SuppressWarnings("unchecked")
@Override
public Map<String, String>[] listTokenValues(String[] tokens, Map<String, Object> criteria) {
Set<String> tokenSet = new HashSet<>(Arrays.asList(tokens));
Set<Map<String, String>> listTokenValues = listTokenValues(tokenSet, criteria);
return listTokenValues.toArray(new Map[listTokenValues.size()]);
}

private Set<Map<String, String>> listTokenValues(Set<String> tokens,
public Set<Map<String, String>> listTokenValues(Set<String> tokens,
Map<String, Object> criteria) {
Map<String, String> stringCriteria = new HashMap<>();
for (Map.Entry<String, Object> entry : criteria.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ protected Collection<String> findArtifactNames(Map<String, String> tokenValues,
return names;
}

@SuppressWarnings("unchecked")
@Override
public Map<String, String>[] listTokenValues(String[] tokens, Map<String, Object> criteria) {
public Set<Map<String, String>> listTokenValues(Set<String> tokens, Map<String, Object> criteria) {
Set<Map<String, String>> result = new LinkedHashSet<>();

// use ivy patterns
Expand All @@ -184,17 +183,17 @@ public Map<String, String>[] listTokenValues(String[] tokens, Map<String, Object
}
}

return result.toArray(new Map[result.size()]);
return result;
}

protected String getModuleDescriptorExtension() {
return "xml";
}

private Set<Map<String, String>> resolveTokenValues(String[] tokens, String pattern,
private Set<Map<String, String>> resolveTokenValues(Set<String> tokens, String pattern,
Map<String, Object> criteria, boolean noMd) {
Set<Map<String, String>> result = new LinkedHashSet<>();
Set<String> tokenSet = new HashSet<>(Arrays.asList(tokens));
Set<String> tokenSet = new HashSet<>(tokens);

Map<String, String> tokenValues = new HashMap<>();
for (Map.Entry<String, Object> entry : criteria.entrySet()) {
Expand Down Expand Up @@ -250,8 +249,7 @@ private Set<Map<String, String>> resolveTokenValues(String[] tokens, String patt
} else if (noMd && "module".equals(token)) {
newCriteria.put("artifact", value);
}
result.addAll(resolveTokenValues(tokenSet.toArray(new String[tokenSet.size()]),
moreResolvedPattern, newCriteria, noMd));
result.addAll(resolveTokenValues(tokenSet, moreResolvedPattern, newCriteria, noMd));
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ public String[] listTokenValues(String token, Map<String, String> otherTokenValu
return new String[0];
}

// old API
@SuppressWarnings("unchecked")
@Deprecated
public Map<String, String>[] listTokenValues(String[] tokens, Map<String, Object> criteria) {
return new Map[0];
}
Expand Down
10 changes: 3 additions & 7 deletions src/java/org/apache/ivy/plugins/resolver/ChainResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,13 @@ public ResolvedResource findIvyFileRef(DependencyDescriptor dd, ResolveData data
return null;
}

@SuppressWarnings("unchecked")
@Override
public Map<String, String>[] listTokenValues(String[] tokens, Map<String, Object> criteria) {
public Set<Map<String, String>> listTokenValues(Set<String> tokens, Map<String, Object> criteria) {
Set<Map<String, String>> result = new HashSet<>();
for (DependencyResolver resolver : chain) {
Map<String, String>[] temp = resolver.listTokenValues(tokens,
new HashMap<>(criteria));
result.addAll(Arrays.asList(temp));
result.addAll(resolver.listTokenValues(tokens, new HashMap<>(criteria)));
}

return result.toArray(new Map[result.size()]);
return result;
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/java/org/apache/ivy/plugins/resolver/DependencyResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.ivy.core.cache.ArtifactOrigin;
import org.apache.ivy.core.cache.RepositoryCacheManager;
Expand Down Expand Up @@ -188,6 +190,17 @@ ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data)
* the token which have values
* @return the list of token values, must not be <code>null</code>
*/
default Set<Map<String, String>> listTokenValues(Set<String> tokens, Map<String, Object> criteria) {
Set<Map<String, String>> tokenValueSet = new HashSet<>();
for (Map<String, String> tokenValue :
listTokenValues(tokens.toArray(new String[tokens.size()]), criteria)) {
tokenValueSet.add(tokenValue);
}
return tokenValueSet;
}

// http://www.tothenew.com/blog/why-is-generic-array-creation-not-allowed-in-java/
@Deprecated
Map<String, String>[] listTokenValues(String[] tokens, Map<String, Object> criteria);

OrganisationEntry[] listOrganisations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -155,7 +156,6 @@ public void testInitFromConf() throws Exception {
l.get(0));
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void testMaven2Listing() {
IBiblioResolver resolver = new IBiblioResolver();
Expand All @@ -173,20 +173,22 @@ public void testMaven2Listing() {
RevisionEntry[] revisions = resolver.listRevisions(modules[0]);
assertTrue(revisions.length > 0);

Map otherTokenValues = new HashMap();
Map<String, String> otherTokenValues = new HashMap<>();
otherTokenValues.put(IvyPatternHelper.ORGANISATION_KEY, "commons-lang");
String[] values = resolver.listTokenValues(IvyPatternHelper.MODULE_KEY, otherTokenValues);
assertNotNull(values);
assertEquals(1, values.length);
assertEquals("commons-lang", values[0]);

Map[] valuesMaps = resolver.listTokenValues(new String[] {IvyPatternHelper.MODULE_KEY},
otherTokenValues);
Set vals = new HashSet();
for (Map valuesMap : valuesMaps) {
Map<String, Object> criteria = new HashMap<>();
criteria.putAll(otherTokenValues);
Set<Map<String, String>> valuesMaps = resolver.listTokenValues(
Collections.singleton(IvyPatternHelper.MODULE_KEY), criteria);
Set<String> vals = new HashSet<>();
for (Map<String, String> valuesMap : valuesMaps) {
vals.add(valuesMap.get(IvyPatternHelper.MODULE_KEY));
}
values = (String[]) vals.toArray(new String[vals.size()]);
values = vals.toArray(new String[vals.size()]);
assertEquals(1, values.length);
assertEquals("commons-lang", values[0]);
}
Expand Down