CAMEL-10105: [api-component-framework] Reduce object allocation in ApiCollection
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/90ac673b Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/90ac673b Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/90ac673b Branch: refs/heads/master Commit: 90ac673b3b916ad9fe4608b6426b2491e7614bdf Parents: 773e9ee Author: lburgazzoli <lburgazz...@gmail.com> Authored: Wed Jun 29 18:07:40 2016 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Thu Jul 21 11:47:40 2016 +0200 ---------------------------------------------------------------------- .../camel/util/component/ApiCollection.java | 35 +++++++++++++------- .../src/main/resources/api-collection.vm | 10 +++++- 2 files changed, 32 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/90ac673b/camel-core/src/main/java/org/apache/camel/util/component/ApiCollection.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/component/ApiCollection.java b/camel-core/src/main/java/org/apache/camel/util/component/ApiCollection.java index b1e05fc..03fe772 100644 --- a/camel-core/src/main/java/org/apache/camel/util/component/ApiCollection.java +++ b/camel-core/src/main/java/org/apache/camel/util/component/ApiCollection.java @@ -17,25 +17,25 @@ package org.apache.camel.util.component; import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; /** * Base class for a collection of ApiMethods. Meant to be extended by Components to create the api name map. */ public abstract class ApiCollection<E extends Enum<E> & ApiName, T> { - protected final Map<E, ApiMethodHelper<? extends ApiMethod>> apis = new HashMap<E, ApiMethodHelper<? extends ApiMethod>>(); - protected final HashMap<Class<? extends ApiMethod>, E> apiMethods = new HashMap<Class<? extends ApiMethod>, E>(); + private Map<E, ApiMethodHelper<? extends ApiMethod>> apiHelpers = Collections.emptyMap(); + private Map<Class<? extends ApiMethod>, E> apiMethods = Collections.emptyMap(); + private Set<String> apiNames = Collections.emptySet(); public final Map<E, ApiMethodHelper<? extends ApiMethod>> getApiHelpers() { - return Collections.unmodifiableMap(apis); + return apiHelpers; } public final Map<Class<? extends ApiMethod>, E> getApiMethods() { - return Collections.unmodifiableMap(apiMethods); + return apiMethods; } /** @@ -44,7 +44,7 @@ public abstract class ApiCollection<E extends Enum<E> & ApiName, T> { * @return helper class to work with {@link ApiMethod} */ public final ApiMethodHelper<? extends ApiMethod> getHelper(E apiName) { - return apis.get(apiName); + return apiHelpers.get(apiName); } /** @@ -52,11 +52,7 @@ public abstract class ApiCollection<E extends Enum<E> & ApiName, T> { * @return list of API names. */ public final Set<String> getApiNames() { - final Set<String> result = new HashSet<String>(); - for (E api : apis.keySet()) { - result.add(api.getName()); - } - return Collections.unmodifiableSet(result); + return apiNames; } public final E getApiName(Class<? extends ApiMethod> apiMethod) { @@ -69,4 +65,19 @@ public abstract class ApiCollection<E extends Enum<E> & ApiName, T> { * @return Endpoint configuration object for the API. */ public abstract T getEndpointConfiguration(E apiName); + + protected final void setApiHelpers(Map<E, ApiMethodHelper<? extends ApiMethod>> apiHelpers) { + this.apiHelpers = Collections.unmodifiableMap(apiHelpers); + + this.apiNames = Collections.unmodifiableSet( + apiHelpers.keySet() + .stream() + .map(api -> api.getName()) + .collect(Collectors.toSet()) + ); + } + + protected final void setApiMethods(Map<Class<? extends ApiMethod>, E> apiMethods) { + this.apiMethods = Collections.unmodifiableMap(apiMethods); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/90ac673b/tooling/maven/camel-api-component-maven-plugin/src/main/resources/api-collection.vm ---------------------------------------------------------------------- diff --git a/tooling/maven/camel-api-component-maven-plugin/src/main/resources/api-collection.vm b/tooling/maven/camel-api-component-maven-plugin/src/main/resources/api-collection.vm index 3e75702..8152900 100644 --- a/tooling/maven/camel-api-component-maven-plugin/src/main/resources/api-collection.vm +++ b/tooling/maven/camel-api-component-maven-plugin/src/main/resources/api-collection.vm @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.HashMap; #set( $componentConfig = "${componentName}Configuration" ) import ${componentPackage}.${componentConfig}; @@ -33,6 +34,7 @@ import ${componentPackage}.${helper.getEndpointConfig($api.ProxyClass)}; #end import org.apache.camel.util.component.ApiCollection; +import org.apache.camel.util.component.ApiMethod; import org.apache.camel.util.component.ApiMethodHelper; /** @@ -44,6 +46,9 @@ public final class $collectionName extends ApiCollection<${apiNameEnum}, ${compo private ${collectionName}() { final Map<String, String> aliases = new HashMap<String, String>(); + final Map<${apiNameEnum}, ApiMethodHelper<? extends ApiMethod>> apiHelpers = new HashMap<>(); + final Map<Class<? extends ApiMethod>, ${apiNameEnum}> apiMethods = new HashMap<>(); + List<String> nullableArgs; #foreach( $api in $apis ) @@ -54,9 +59,12 @@ public final class $collectionName extends ApiCollection<${apiNameEnum}, ${compo #set( $apiMethod = ${helper.getApiMethod($api.ProxyClass)} ) #set( $apiName = "${apiNameEnum}.${helper.getEnumConstant($api.ApiName)}" ) nullableArgs = Arrays.asList(${helper.getNullableOptionValues($api.NullableOptions)}); - apis.put($apiName, new ApiMethodHelper<$apiMethod>(${apiMethod}.class, aliases, nullableArgs)); + apiHelpers.put($apiName, new ApiMethodHelper<$apiMethod>(${apiMethod}.class, aliases, nullableArgs)); apiMethods.put(${apiMethod}.class, ${apiName}); #end + + setApiHelpers(apiHelpers); + setApiMethods(apiMethods); } public $componentConfig getEndpointConfiguration(${apiNameEnum} apiName) {