This is an automated email from the ASF dual-hosted git repository.
veithen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-axiom.git
The following commit(s) were added to refs/heads/master by this push:
new edee934f8 Move the implementation of the adapter pattern into a
separate class.
edee934f8 is described below
commit edee934f8099de1f2958050cc74ea7f81ff1b5c0
Author: Andreas Veithen-Knowles <[email protected]>
AuthorDate: Tue Oct 21 13:21:55 2025 +0000
Move the implementation of the adapter pattern
into a separate class.
---
.../apache/axiom/testing/multiton/Adaptable.java | 79 ++++++++++++++++++++++
.../axiom/testing/multiton/AdapterFactory.java | 2 +-
.../apache/axiom/testing/multiton/Multiton.java | 54 +--------------
3 files changed, 83 insertions(+), 52 deletions(-)
diff --git
a/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/Adaptable.java
b/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/Adaptable.java
new file mode 100644
index 000000000..f478cb7f1
--- /dev/null
+++
b/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/Adaptable.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.testing.multiton;
+
+import java.lang.reflect.ParameterizedType;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+
+/**
+ * Base class providing a simple mechanism that allows to extend the behavior
of objects using
+ * adapters.
+ */
+@SuppressWarnings({"rawtypes", "unchecked"})
+public abstract class Adaptable {
+ private static final Map<Class, List<AdapterFactory>> adapterFactoryMap =
new HashMap<>();
+ private final Adapters adapters = new Adapters();
+
+ static {
+ for (Iterator<AdapterFactory> it =
+ ServiceLoader.load(AdapterFactory.class,
Adaptable.class.getClassLoader())
+ .iterator();
+ it.hasNext(); ) {
+ AdapterFactory adapterFactory = it.next();
+ // TODO: only works in the basic case where the factory directly
implements
+ // AdapterFactory as the first interface
+ Class clazz =
+ ((Class<?>)
+ ((ParameterizedType)
+ adapterFactory.getClass()
+
.getGenericInterfaces()[0])
+ .getActualTypeArguments()[0])
+ .asSubclass(Adaptable.class);
+ List<AdapterFactory> adapterFactories =
adapterFactoryMap.get(clazz);
+ if (adapterFactories == null) {
+ adapterFactories = new ArrayList<>();
+ adapterFactoryMap.put(clazz, adapterFactories);
+ }
+ adapterFactories.add(adapterFactory);
+ }
+ }
+
+ public final <T> T getAdapter(Class<T> type) {
+ synchronized (adapters) {
+ if (!adapters.initialized()) {
+ Class<?> clazz = getClass();
+ while (clazz != Adaptable.class) {
+ List<AdapterFactory> adapterFactories =
adapterFactoryMap.get(clazz);
+ if (adapterFactories != null) {
+ for (AdapterFactory adapterFactory : adapterFactories)
{
+ adapterFactory.createAdapters(this, adapters);
+ }
+ }
+ clazz = clazz.getSuperclass();
+ }
+ }
+ return adapters.get(type);
+ }
+ }
+}
diff --git
a/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/AdapterFactory.java
b/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/AdapterFactory.java
index 5fb1d28e8..d3c39f36a 100644
---
a/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/AdapterFactory.java
+++
b/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/AdapterFactory.java
@@ -18,6 +18,6 @@
*/
package org.apache.axiom.testing.multiton;
-public interface AdapterFactory<T extends Multiton> {
+public interface AdapterFactory<T extends Adaptable> {
void createAdapters(T instance, Adapters adapters);
}
diff --git
a/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/Multiton.java
b/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/Multiton.java
index 9531dada4..63fecac10 100644
---
a/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/Multiton.java
+++
b/testing/multiton/src/main/java/org/apache/axiom/testing/multiton/Multiton.java
@@ -22,18 +22,14 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
-import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.ServiceLoader;
/**
- * Base class for multitons. A multiton is a class that has a fixed set of
instances. The base class
- * also provides a simple mechanism that allows to extend the behavior of a
multiton using adapters.
+ * Base class for multitons. A multiton is a class that has a fixed set of
instances.
*
* <p>The set of instances of a multiton is determined by:
*
@@ -45,35 +41,9 @@ import java.util.ServiceLoader;
* with an component type assignment compatible with the multiton class.
* </ul>
*/
-@SuppressWarnings({"rawtypes", "unchecked"})
-public abstract class Multiton {
+@SuppressWarnings({"unchecked"})
+public abstract class Multiton extends Adaptable {
private static final Map<Class<?>, List<?>> instancesMap = new HashMap<>();
- private static final Map<Class, List<AdapterFactory>> adapterFactoryMap =
new HashMap<>();
- private final Adapters adapters = new Adapters();
-
- static {
- for (Iterator<AdapterFactory> it =
- ServiceLoader.load(AdapterFactory.class,
Multiton.class.getClassLoader())
- .iterator();
- it.hasNext(); ) {
- AdapterFactory adapterFactory = it.next();
- // TODO: only works in the basic case where the factory directly
implements
- // AdapterFactory as the first interface
- Class clazz =
- ((Class<?>)
- ((ParameterizedType)
- adapterFactory.getClass()
-
.getGenericInterfaces()[0])
- .getActualTypeArguments()[0])
- .asSubclass(Multiton.class);
- List<AdapterFactory> adapterFactories =
adapterFactoryMap.get(clazz);
- if (adapterFactories == null) {
- adapterFactories = new ArrayList<>();
- adapterFactoryMap.put(clazz, adapterFactories);
- }
- adapterFactories.add(adapterFactory);
- }
- }
/**
* Get all instances of the given multiton. See the Javadoc of the {@link
Multiton} class for
@@ -134,22 +104,4 @@ public abstract class Multiton {
}
return instances;
}
-
- public final <T> T getAdapter(Class<T> type) {
- synchronized (adapters) {
- if (!adapters.initialized()) {
- Class<?> clazz = getClass();
- while (clazz != Multiton.class) {
- List<AdapterFactory> adapterFactories =
adapterFactoryMap.get(clazz);
- if (adapterFactories != null) {
- for (AdapterFactory adapterFactory : adapterFactories)
{
- adapterFactory.createAdapters(this, adapters);
- }
- }
- clazz = clazz.getSuperclass();
- }
- }
- return adapters.get(type);
- }
- }
}