Skip to content

Commit

Permalink
Performance improvement in Resource.Builder#mergeResources (#4300)
Browse files Browse the repository at this point in the history
* Performance improvement in Resource.Builder#mergeResources

Signed-off-by: Jan Supol <jan.supol@oracle.com>
  • Loading branch information
jansupol authored Nov 20, 2019
1 parent eb39448 commit b86c80e
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.ws.rs.Path;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

import org.glassfish.jersey.Severity;
import org.glassfish.jersey.internal.Errors;
Expand Down Expand Up @@ -200,7 +203,7 @@ public static final class Builder {
private Builder(final Resource.Builder parentResource) {
this.methodBuilders = new LinkedHashSet<>();
this.childResourceBuilders = new LinkedHashSet<>();
this.childResources = new ArrayList<>();
this.childResources = new LinkedList<>();
this.resourceMethods = new LinkedList<>();
this.handlerClasses = Collections.newSetFromMap(new IdentityHashMap<>());
this.handlerInstances = Collections.newSetFromMap(new IdentityHashMap<>());
Expand Down Expand Up @@ -543,27 +546,29 @@ private void onBuildChildResource(Builder childResourceBuilder, Resource.Data ch
}

private static List<Resource.Data> mergeResources(List<Resource.Data> resources) {
List<Resource.Data> mergedResources = new ArrayList<>();
for (int i = 0; i < resources.size(); i++) {
Resource.Data outer = resources.get(i);
Resource.Builder builder = null;
final MultivaluedMap<String, Data> resourceData = new MultivaluedHashMap<>();
final List<Resource.Data> mergedResources = new ArrayList<>();

for (int j = i + 1; j < resources.size(); j++) {
Resource.Data inner = resources.get(j);
for (Iterator<Resource.Data> resourcesIterator = resources.iterator(); resourcesIterator.hasNext(); ) {
Resource.Data data = resourcesIterator.next();
if (resourceData.containsKey(data.path)) {
resourcesIterator.remove();
}
resourceData.add(data.path, data);
}

if (outer.path.equals(inner.path)) {
if (builder == null) {
builder = Resource.builder(outer);
for (Map.Entry<String, List<Resource.Data>> entry : resourceData.entrySet()) {
if (entry.getValue().size() == 1) {
mergedResources.add(entry.getValue().get(0));
} else {
Resource.Builder builder = null;
for (Resource.Data data : entry.getValue()) {
if (builder != null) {
builder.mergeWith(data);
} else {
builder = Resource.builder(data);
}
builder.mergeWith(inner);
resources.remove(j);
//noinspection AssignmentToForLoopParameter
j--;
}
}
if (builder == null) {
mergedResources.add(outer);
} else {
mergedResources.add(builder.buildResourceData());
}
}
Expand Down

0 comments on commit b86c80e

Please sign in to comment.