You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by da...@apache.org on 2017/11/17 18:42:45 UTC
svn commit: r1815609 - in /felix/trunk/converter/converter: ./
src/main/java/org/osgi/util/converter/
Author: davidb
Date: Fri Nov 17 18:42:44 2017
New Revision: 1815609
URL: http://svn.apache.org/viewvc?rev=1815609&view=rev
Log:
Bring OSGi converter API to avoid circular dependency
Added:
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConversionException.java
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Converter.java
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConverterBuilder.java
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConverterFunction.java
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Converting.java
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Functioning.java
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Rule.java
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Specifying.java
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TargetRule.java
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TypeReference.java
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TypeRule.java
Modified:
felix/trunk/converter/converter/pom.xml
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
Modified: felix/trunk/converter/converter/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/pom.xml?rev=1815609&r1=1815608&r2=1815609&view=diff
==============================================================================
--- felix/trunk/converter/converter/pom.xml (original)
+++ felix/trunk/converter/converter/pom.xml Fri Nov 17 18:42:44 2017
@@ -106,12 +106,6 @@
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
- <artifactId>org.osgi.util.converter</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </dependency>
-
- <dependency>
- <groupId>org.osgi</groupId>
<artifactId>org.osgi.util.function</artifactId>
<version>1.0.0</version>
</dependency>
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConversionException.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConversionException.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConversionException.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConversionException.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) OSGi Alliance (2017). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+/**
+ * This Runtime Exception is thrown when an object is requested to be converted
+ * but the conversion cannot be done. For example when the String "test" is to
+ * be converted into a Long.
+ * @author $Id: 2e85310dba06f5837f45a670bc9369478da3de71 $
+ */
+public class ConversionException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Create a Conversion Exception with a message.
+ *
+ * @param message The message for this exception.
+ */
+ public ConversionException(String message) {
+ super(message);
+ }
+
+ /**
+ * Create a Conversion Exception with a message and a nested cause.
+ *
+ * @param message The message for this exception.
+ * @param cause The causing exception.
+ */
+ public ConversionException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Converter.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Converter.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Converter.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Converter.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) OSGi Alliance (2017). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+import org.osgi.annotation.versioning.ProviderType;
+
+/**
+ * The Converter service is used to start a conversion. The service is obtained
+ * from the service registry. The conversion is then completed via the
+ * Converting interface that has methods to specify the target type.
+ *
+ * @author $Id: bed4a27f1d9cbff0f3646d6df355843518280e33 $
+ * @ThreadSafe
+ */
+@ProviderType
+public interface Converter {
+ /**
+ * Start a conversion for the given object.
+ *
+ * @param obj The object that should be converted.
+ * @return A {@link Converting} object to complete the conversion.
+ */
+ Converting convert(Object obj);
+
+ /**
+ * Start defining a function that can perform given conversions.
+ * @return A {@link Functioning} object to complete the definition.
+ */
+ Functioning function();
+
+ /**
+ * Obtain a builder to create a modified converter based on this converter.
+ * For more details see the {@link ConverterBuilder} interface.
+ *
+ * @return A new Converter Builder.
+ */
+ ConverterBuilder newConverterBuilder();
+}
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConverterBuilder.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConverterBuilder.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConverterBuilder.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConverterBuilder.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) OSGi Alliance (2017). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+import java.lang.reflect.Type;
+
+import org.osgi.annotation.versioning.ProviderType;
+
+/**
+ * A builder to create a new converter with modified behaviour based on an
+ * existing converter. The modified behaviour is specified by providing rules
+ * and/or conversion functions. If multiple rules match they will be visited
+ * in sequence of registration. If a rule's function returns {@code null} the
+ * next ruld found will be visited. If none of the rules can handle the
+ * conversion, the original converter will be used to perform the conversion.
+ *
+ * @author $Id: 7b467a98fd2cd5107f5a546780abfae21d067a53 $
+ */
+@ProviderType
+public interface ConverterBuilder {
+ /**
+ * Build the specified converter. Each time this method is called a new
+ * custom converter is produced based on the rules registered with the
+ * builder.
+ *
+ * @return A new converter with the rules provided to the builder.
+ */
+ Converter build();
+
+ /**
+ * Register a custom error handler. The custom error handler will be called
+ * when the conversion would otherwise throw an exception. The error handler
+ * can either throw a different exception or return a value to be used for
+ * the failed conversion.
+ *
+ * @param func The function to be used to handle errors.
+ * @return This converter builder for further building.
+ */
+ ConverterBuilder errorHandler(ConverterFunction func);
+
+ /**
+ * Register a conversion rule for this converter. Note that only the target
+ * type is specified, so the rule will be visited for every conversion to
+ * the target type.
+ *
+ * @param type The type that this rule will produce.
+ * @param func The function that will handle the conversion.
+ * @return This converter builder for further building.
+ */
+ ConverterBuilder rule(Type type, ConverterFunction func);
+
+ /**
+ * Register a conversion rule for this converter.
+ *
+ * @param rule A rule implementation.
+ * @return This converter builder for further building.
+ */
+ ConverterBuilder rule(TargetRule rule);
+
+ /**
+ * Register a catch-all rule, will be called of no other rule matches.
+ *
+ * @param func The function that will handle the conversion.
+ * @return This converter builder for further building.
+ */
+ ConverterBuilder rule(ConverterFunction func);
+}
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConverterFunction.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConverterFunction.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConverterFunction.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConverterFunction.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) OSGi Alliance (2017). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+import java.lang.reflect.Type;
+
+import org.osgi.annotation.versioning.ConsumerType;
+
+/**
+ * An functional interface with a convert method that is passed the original
+ * object and the target type to perform a custom conversion.
+ * <P>
+ * This interface can also be used to register a custom error handler.
+ *
+ * @author $Id: 60a17aadcd05222bc5aa57bb77d44e6364ef645d $
+ */
+@ConsumerType
+public interface ConverterFunction {
+ /**
+ * Special object to indicate that a custom converter rule or error
+ * handler cannot handle the conversion.
+ */
+ static final Object CANNOT_HANDLE = new Object();
+
+ /**
+ * Convert the object into the target type.
+ *
+ * @param obj The object to be converted. This object will never be
+ * {@code null} as the convert function will not be invoked for
+ * null values.
+ * @param targetType The target type.
+ * @return The conversion result or {@link #CANNOT_HANDLE} to indicate that the
+ * convert function cannot handle this conversion. In this case the
+ * next matching rule or parent converter will be given a
+ * opportunity to convert.
+ * @throws Exception the operation can throw an exception if the conversion
+ * can not be performed due to incompatible types.
+ */
+ Object apply(Object obj, Type targetType)
+ throws Exception;
+}
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Converting.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Converting.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Converting.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Converting.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) OSGi Alliance (2017). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+import java.lang.reflect.Type;
+
+import org.osgi.annotation.versioning.ProviderType;
+
+/**
+ * This interface is used to specify the target that an object should be
+ * converted to. A {@link Converting} instance can be obtained via the
+ * {@link Converter}.
+ *
+ * @author $Id: 228131c26832a3d3dc016ec3090cf5d3b474df4e $
+ * @NotThreadSafe
+ */
+@ProviderType
+public interface Converting extends Specifying<Converting> {
+ /**
+ * Specify the target object type for the conversion as a class object.
+ *
+ * @param cls The class to convert to.
+ * @return The converted object.
+ */
+ <T> T to(Class<T> cls);
+
+ /**
+ * Specify the target object type as a Java Reflection Type object.
+ *
+ * @param type A Type object to represent the target type to be converted
+ * to.
+ * @return The converted object.
+ */
+ <T> T to(Type type);
+
+ /**
+ * Specify the target object type as a {@link TypeReference}. If the target
+ * class carries generics information a TypeReference should be used as this
+ * preserves the generic information whereas a Class object has this
+ * information erased. Example use:
+ *
+ * <pre>
+ * List<String> result = converter.convert(Arrays.asList(1, 2, 3))
+ * .to(new TypeReference<List<String>>() {});
+ * </pre>
+ *
+ * @param ref A type reference to the object being converted to.
+ * @return The converted object.
+ */
+ <T> T to(TypeReference<T> ref);
+}
Modified: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java?rev=1815609&r1=1815608&r2=1815609&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java Fri Nov 17 18:42:44 2017
@@ -82,15 +82,15 @@ class ConvertingImpl extends AbstractSpe
}
// Interfaces with no methods are also not considered
- private static final Collection<Class< ? >> NO_MAP_VIEW_TYPES =
- Arrays.<Class< ? >> asList(String.class, Class.class, Comparable.class,
+ private static final Collection<Class< ? >> NO_MAP_VIEW_TYPES = Arrays
+ .<Class< ? >> asList(String.class, Class.class, Comparable.class,
CharSequence.class, Map.Entry.class);
- volatile InternalConverter converter;
+ volatile InternalConverter converter;
private volatile Object object;
- private volatile Class< ? > sourceClass;
+ private volatile Class< ? > sourceClass;
private volatile Class<?> targetClass;
- private volatile Type[] typeArguments;
+ private volatile Type[] typeArguments;
ConvertingImpl(InternalConverter c, Object obj) {
converter = c;
@@ -157,14 +157,14 @@ class ConvertingImpl extends AbstractSpe
return convertToArray();
} else if (Collection.class.isAssignableFrom(targetAsClass)) {
return convertToCollection();
- } else if (targetAsDTO || targetAsJavaBean || isMapType(targetAsClass, targetAsJavaBean)) {
+ } else if (isMapType(targetAsClass, targetAsJavaBean, targetAsDTO)) {
return convertToMapType();
}
// At this point we know that the target is a 'singular' type: not a map, collection or array
if (Collection.class.isAssignableFrom(sourceClass)) {
return convertCollectionToSingleValue(targetAsClass);
- } else if (isMapType(sourceClass, sourceAsJavaBean)) {
+ } else if (isMapType(sourceClass, sourceAsJavaBean, sourceAsDTO)) {
return convertMapToSingleValue(targetAsClass);
} else if (object instanceof Map.Entry) {
return convertMapEntryToSingleValue(targetAsClass);
@@ -234,7 +234,7 @@ class ConvertingImpl extends AbstractSpe
@SuppressWarnings("unchecked")
private <T> T convertToArray() {
- Collection<?> collectionView = collectionView(object);
+ Collection< ? > collectionView = collectionView();
Iterator<?> itertor = collectionView.iterator();
try {
Object array = Array.newInstance(targetAsClass.getComponentType(), collectionView.size());
@@ -251,7 +251,7 @@ class ConvertingImpl extends AbstractSpe
@SuppressWarnings({ "unchecked", "rawtypes" })
private <T> T convertToCollection() {
- Collection<?> cv = collectionView(object);
+ Collection< ? > cv = collectionView();
Class<?> targetElementType = null;
if (typeArguments != null && typeArguments.length > 0 && typeArguments[0] instanceof Class) {
targetElementType = (Class<?>) typeArguments[0];
@@ -432,7 +432,7 @@ class ConvertingImpl extends AbstractSpe
@SuppressWarnings("rawtypes")
private Object convertToMapType() {
- if (!sourceAsDTO && !isMapType(sourceClass, sourceAsJavaBean)) {
+ if (!isMapType(sourceClass, sourceAsJavaBean, sourceAsDTO)) {
throw new ConversionException(
"Cannot convert " + object + " to " + targetAsClass);
}
@@ -593,7 +593,11 @@ class ConvertingImpl extends AbstractSpe
return converter.convert(0).to(cls);
}
- private static boolean isMapType(Class<?> cls, boolean asJavaBean) {
+ private static boolean isMapType(Class< ? > cls, boolean asJavaBean,
+ boolean asDTO) {
+ if (asDTO)
+ return true;
+
// All interface types that are not Collections are treated as maps
if (Map.class.isAssignableFrom(cls))
return true;
@@ -680,22 +684,24 @@ class ConvertingImpl extends AbstractSpe
return null;
}
- private static Collection<?> collectionView(Object obj) {
- if (obj == null)
+ private Collection< ? > collectionView() {
+ if (object == null)
return null;
- Collection<?> c = asCollection(obj);
+ Collection< ? > c = asCollection();
if (c == null)
- return Collections.singleton(obj);
+ return Collections.singleton(object);
else
return c;
}
- private static Collection<?> asCollection(Object obj) {
- if (obj instanceof Collection)
- return (Collection<?>) obj;
- else if ((obj = asBoxedArray(obj)) instanceof Object[])
- return Arrays.asList((Object[]) obj);
+ private Collection< ? > asCollection() {
+ if (object instanceof Collection)
+ return (Collection< ? >) object;
+ else if ((object = asBoxedArray(object)) instanceof Object[])
+ return Arrays.asList((Object[]) object);
+ else if (isMapType(sourceClass, sourceAsJavaBean, sourceAsDTO))
+ return mapView(object, sourceClass, converter).entrySet();
else
return null;
}
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Functioning.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Functioning.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Functioning.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Functioning.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) OSGi Alliance (2017). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+import java.lang.reflect.Type;
+
+import org.osgi.annotation.versioning.ProviderType;
+import org.osgi.util.function.Function;
+
+/**
+ * This interface is used to specify the target function to perform
+ * conversions. This function can be used multiple times.
+ * A {@link Functioning} instance can be obtained via the
+ * {@link Converter}.
+ *
+ * @author $Id: 83db128a496153440bbe5c347e3de9a83dca7c59 $
+ * @NotThreadSafe
+ */
+@ProviderType
+public interface Functioning extends Specifying<Functioning> {
+ /**
+ * Specify the target object type for the conversion as a class object.
+ *
+ * @param cls The class to convert to.
+ * @return A function that can perform the conversion.
+ */
+ <T> Function<Object, T> to(Class<T> cls);
+
+ /**
+ * Specify the target object type as a Java Reflection Type object.
+ *
+ * @param type A Type object to represent the target type to be converted
+ * to.
+ * @return A function that can perform the conversion.
+ */
+ <T> Function<Object, T> to(Type type);
+
+ /**
+ * Specify the target object type as a {@link TypeReference}. If the target
+ * class carries generics information a TypeReference should be used as this
+ * preserves the generic information whereas a Class object has this
+ * information erased. Example use:
+ *
+ * <pre>
+ * List<String> result = converter.function()
+ * .to(new TypeReference<List<String>>() {});
+ * </pre>
+ *
+ * @param ref A type reference to the object being converted to.
+ * @return A function that can perform the conversion.
+ */
+ <T> Function<Object, T> to(TypeReference<T> ref);
+}
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Rule.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Rule.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Rule.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Rule.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) OSGi Alliance (2017). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+
+import org.osgi.util.function.Function;
+
+/** A rule implementation that works by capturing the type arguments via
+ * subclassing. The rule supports specifying both <em>from</em> and
+ * <em>to</em> types. Filtering on the <em>from</em> by the {@code Rule}
+ * implementation. Filtering on the <em>to</em> is done by the converter
+ * customization mechanism.
+ *
+ * @author $Id: fe695b76abeeb5597341ce74a4178b8943606140 $
+ *
+ * @param <F> The type to convert from.
+ * @param <T> The type to convert to.
+ */
+public abstract class Rule<F, T> implements TargetRule {
+ private final ConverterFunction function;
+
+ /**
+ * Create an instance with a conversion function.
+ * @param func The conversion function to use.
+ */
+ public Rule(Function<F,T> func) {
+ function = getGenericFunction(func);
+ }
+
+ private ConverterFunction getGenericFunction(final Function<F,T> func) {
+ return new ConverterFunction() {
+ @Override
+ @SuppressWarnings("unchecked")
+ public Object apply(Object obj, Type targetType) throws Exception {
+ Rule<?,?> r = Rule.this;
+ Type type = ((ParameterizedType) r.getClass().getGenericSuperclass())
+ .getActualTypeArguments()[0];
+
+ if (type instanceof ParameterizedType) {
+ type = ((ParameterizedType) type).getRawType();
+ }
+
+ Class<?> cls = null;
+ if (type instanceof Class) {
+ cls = ((Class<?>) type);
+ } else {
+ return ConverterFunction.CANNOT_HANDLE;
+ }
+
+ if (cls.isInstance(obj)) {
+ return func.apply((F) obj);
+ }
+ return ConverterFunction.CANNOT_HANDLE;
+ }
+ };
+ }
+
+ @Override
+ public ConverterFunction getFunction() {
+ return function;
+ }
+
+ @Override
+ public Type getTargetType() {
+ Type type = ((ParameterizedType) getClass().getGenericSuperclass())
+ .getActualTypeArguments()[1];
+ return type;
+ }
+}
\ No newline at end of file
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Specifying.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Specifying.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Specifying.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Specifying.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) OSGi Alliance (2017). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+import org.osgi.annotation.versioning.ProviderType;
+
+/**
+ * This is the base interface for the {@link Converting} and {@link Functioning}
+ * interfaces and defines the common modifiers that can be applied to these.
+ *
+ * @author $Id: e6c20609b4db17a7e45b59975e0c3c6eae9f243a $
+ * @NotThreadSafe
+ */
+@ProviderType
+public interface Specifying<T extends Specifying<T>> {
+ /**
+ * Always return a fully populated copy of the object, however if the object
+ * is an immutable built-in scalar such as String or Long, then a copy is
+ * not needed. By default a wrapped object is returned by the converter if
+ * possible.
+ *
+ * @return The current {@code Converting} object so that additional calls
+ * can be chained.
+ */
+ T copy();
+
+ /**
+ * The default value to use when the object cannot be converted or in case
+ * of conversion from a {@code null} value.
+ *
+ * @param defVal The default value.
+ * @return The current {@code Converting} object so that additional calls
+ * can be chained.
+ */
+ T defaultValue(Object defVal);
+
+ /**
+ * When converting between map-like types use case-insensitive mapping of
+ * keys.
+ *
+ * @return The current {@code Converting} object so that additional calls
+ * can be chained.
+ */
+ T keysIgnoreCase();
+
+ /**
+ * Treat the source object as the specified class. This can be used to
+ * disambiguate a type if it implements multiple interfaces or extends
+ * multiple classes.
+ *
+ * @param cls The class to treat the object as.
+ * @return The current {@code Converting} object so that additional calls
+ * can be chained.
+ */
+ T sourceAs(Class< ? > cls);
+
+ /**
+ * Treat the source object as a JavaBean. By default objects will not be
+ * treated as JavaBeans, this has to be specified using this method.
+ *
+ * @return The current {@code Converting} object so that additional calls
+ * can be chained.
+ */
+ T sourceAsBean();
+
+ /**
+ * Treat the source object as a DTO even if the source object has methods or
+ * is otherwise not recognised as a DTO.
+ *
+ * @return The current {@code Converting} object so that additional calls
+ * can be chained.
+ */
+ T sourceAsDTO();
+
+ /**
+ * Treat the target object as the specified class. This can be used to
+ * disambiguate a type if it implements multiple interfaces or extends
+ * multiple classes.
+ *
+ * @param cls The class to treat the object as.
+ * @return The current {@code Converting} object so that additional calls
+ * can be chained.
+ */
+ T targetAs(Class< ? > cls);
+
+ /**
+ * Treat the target object as a JavaBean. By default objects will not be
+ * treated as JavaBeans, this has to be specified using this method.
+ *
+ * @return The current {@code Converting} object so that additional calls
+ * can be chained.
+ */
+ T targetAsBean();
+
+ /**
+ * Treat the target object as a DTO even if it has methods or is otherwise
+ * not recognized as a DTO.
+ *
+ * @return The current {@code Converting} object so that additional calls
+ * can be chained.
+ */
+ T targetAsDTO();
+}
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TargetRule.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TargetRule.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TargetRule.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TargetRule.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) OSGi Alliance (2017). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+import java.lang.reflect.Type;
+
+/**
+ * Interface for custom conversion rules.
+ *
+ * @author $Id: f028e2a4b4e3f0c9a1ddb14655998e46fa856b37 $
+ */
+public interface TargetRule {
+ /**
+ * The function to perform the conversion.
+ * @return The function.
+ */
+ ConverterFunction getFunction();
+
+ /**
+ * The target type of this rule. The conversion function is invoked for each conversion to the target type.
+ *
+ * @return The target type.
+ */
+ Type getTargetType();
+}
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TypeReference.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TypeReference.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TypeReference.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TypeReference.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) OSGi Alliance (2015, 2016). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+
+import org.osgi.annotation.versioning.ConsumerType;
+
+/**
+ * An object does not carry any runtime information about its generic type.
+ * However sometimes it is necessary to specify a generic type, that is the
+ * purpose of this class. It allows you to specify an generic type by defining a
+ * type T, then subclassing it. The subclass will have a reference to the super
+ * class that contains this generic information. Through reflection, we pick
+ * this reference up and return it with the getType() call.
+ *
+ * <pre>
+ * List<String> result = converter.convert(Arrays.asList(1, 2, 3))
+ * .to(new TypeReference<List<String>>() {});
+ * </pre>
+ *
+ * @param <T> The target type for the conversion.
+ * @author $Id: 1ea7d4297e6ed805ce147d32614f23bb9145b24c $
+ * @Immutable
+ */
+@ConsumerType
+public class TypeReference<T> {
+ /**
+ * A {@link TypeReference} cannot be directly instantiated. To use it, it
+ * has to be extended, typically as an anonymous inner class.
+ */
+ protected TypeReference() {}
+
+ /**
+ * Return the actual type of this Type Reference
+ *
+ * @return the type of this reference.
+ */
+ public Type getType() {
+ return ((ParameterizedType) getClass().getGenericSuperclass())
+ .getActualTypeArguments()[0];
+ }
+}
Added: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TypeRule.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TypeRule.java?rev=1815609&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TypeRule.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/TypeRule.java Fri Nov 17 18:42:44 2017
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) OSGi Alliance (2017). All Rights Reserved.
+ *
+ * Licensed 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.osgi.util.converter;
+
+import java.lang.reflect.Type;
+
+import org.osgi.util.function.Function;
+
+/**
+ * Rule implementation that works by passing in type arguments rather than subclassing.
+ * The rule supports specifying both <em>from</em> and
+ * <em>to</em> types. Filtering on the <em>from</em> by the {@code Rule}
+ * implementation. Filtering on the <em>to</em> is done by the converter
+ * customization mechanism.
+ *
+ * @author $Id: c0eeb7d740d96567bf72dfffdf5a038c60d8a3a4 $
+ *
+ * @param <F> The type to convert from.
+ * @param <T> The type to convert to. */
+public class TypeRule<F, T> implements TargetRule {
+ private final ConverterFunction function;
+ private final Type toType;
+
+ /**
+ * Create an instance based on source, target types and a conversion function.
+ *
+ * @param from The type to convert from.
+ * @param to The type to convert to.
+ * @param func The conversion function to use.
+ */
+ public TypeRule(Type from, Type to, Function<F,T> func) {
+ function = getFunction(from, func);
+ toType = to;
+ }
+
+ private static <F, T> ConverterFunction getFunction(final Type from,
+ final Function<F,T> func) {
+ return new ConverterFunction() {
+ @Override
+ @SuppressWarnings("unchecked")
+ public Object apply(Object obj, Type targetType) throws Exception {
+ if (from instanceof Class) {
+ Class<?> cls = (Class<?>) from;
+ if (cls.isInstance(obj)) {
+ T res = func.apply((F) obj);
+ if (res != null)
+ return res;
+ else
+ return ConverterFunction.CANNOT_HANDLE;
+ }
+ }
+ return ConverterFunction.CANNOT_HANDLE;
+ }
+ };
+ }
+
+ @Override
+ public ConverterFunction getFunction() {
+ return function;
+ }
+
+ @Override
+ public Type getTargetType() {
+ return toType;
+ }
+}