diff --git a/src/main/java/org/aksw/gerbil/semantic/sameas/impl/DomainBasedSameAsRetrieverManager.java b/src/main/java/org/aksw/gerbil/semantic/sameas/impl/DomainBasedSameAsRetrieverManager.java index 1ffb9f96..f2a702c0 100644 --- a/src/main/java/org/aksw/gerbil/semantic/sameas/impl/DomainBasedSameAsRetrieverManager.java +++ b/src/main/java/org/aksw/gerbil/semantic/sameas/impl/DomainBasedSameAsRetrieverManager.java @@ -17,7 +17,7 @@ package org.aksw.gerbil.semantic.sameas.impl; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -31,7 +31,7 @@ public class DomainBasedSameAsRetrieverManager implements SameAsRetriever { private List staticRetrievers = new ArrayList(); private List defaultRetrievers = new ArrayList(); - private Map domainRetrieverMapping = new HashMap(); + private Map> domainRetrieverMapping = new HashMap<>(); public void addStaticRetriever(SingleUriSameAsRetriever retriever) { staticRetrievers.add(retriever); @@ -42,15 +42,14 @@ public void addDefaultRetriever(SingleUriSameAsRetriever retriever) { } public void addDomainSpecificRetriever(String domain, SingleUriSameAsRetriever retriever) { - SingleUriSameAsRetriever retrievers[]; + List retrievers; if (domainRetrieverMapping.containsKey(domain)) { retrievers = domainRetrieverMapping.get(domain); - retrievers = Arrays.copyOf(retrievers, retrievers.length + 1); - retrievers[retrievers.length - 1] = retriever; } else { - retrievers = new SingleUriSameAsRetriever[] { retriever }; + retrievers = new ArrayList<>(); + domainRetrieverMapping.put(domain, retrievers); } - domainRetrieverMapping.put(domain, retrievers); + retrievers.add(retriever); } @Override @@ -63,44 +62,35 @@ public Set retrieveSameURIs(String domain, String uri) { if (uri == null) { return null; } - Set result = null, newResult = null; + Set result = null; if ((domain != null) && (domainRetrieverMapping.containsKey(domain))) { - SingleUriSameAsRetriever retrievers[] = domainRetrieverMapping.get(domain); - for (int i = 0; i < retrievers.length; ++i) { - newResult = retrievers[i].retrieveSameURIs(domain, uri); - if (newResult != null) { - if (result != null) { - result.addAll(newResult); - } else { - result = newResult; - } - } - } + result = retrieve(domain, uri, domainRetrieverMapping.get(domain), result); } else { - for (SingleUriSameAsRetriever retriever : defaultRetrievers) { - newResult = retriever.retrieveSameURIs(domain, uri); - if (newResult != null) { - if (result != null) { - result.addAll(newResult); - } else { - result = newResult; - } - } - } + result = retrieve(domain, uri, defaultRetrievers, result); } - for (SingleUriSameAsRetriever retriever : staticRetrievers) { - newResult = retriever.retrieveSameURIs(domain, uri); - if (newResult != null) { - if (result != null) { - result.addAll(newResult); - } else { - result = newResult; - } - } + result = retrieve(domain, uri, staticRetrievers, result); + return result; + } + + protected static Set retrieve(String domain, String uri, Collection retrievers, + Set result) { + for (SingleUriSameAsRetriever retriever : retrievers) { + result = merge(result, retriever.retrieveSameURIs(domain, uri)); } return result; } + public static Set merge(Set oldResults, Set newResults) { + if (newResults != null) { + if (oldResults != null) { + oldResults.addAll(newResults); + } else { + return newResults; + } + } + return oldResults; + } + @Override public void addSameURIs(Set uris) { Set temp = new HashSet();