felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1759730 - in /felix/trunk/converter/converter/src: main/java/org/apache/felix/converter/impl/ main/java/org/osgi/service/converter/ test/java/org/apache/felix/converter/impl/
Date Thu, 08 Sep 2016 00:56:35 GMT
Author: davidb
Date: Thu Sep  8 00:56:35 2016
New Revision: 1759730

URL: http://svn.apache.org/viewvc?rev=1759730&view=rev
Log:
Felix Converter - simplify the Adapter API

Added:
    felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/FunctionThrowsException.java
Removed:
    felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/ConvertResult.java
    felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/SimpleConvertFunction.java
Modified:
    felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/AdapterImpl.java
    felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/Adapter.java
    felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/ConvertFunction.java
    felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/Converter.java
    felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/AdapterTest.java

Modified: felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/AdapterImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/AdapterImpl.java?rev=1759730&r1=1759729&r2=1759730&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/AdapterImpl.java
(original)
+++ felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/AdapterImpl.java
Thu Sep  8 00:56:35 2016
@@ -31,11 +31,10 @@ import java.util.concurrent.ConcurrentHa
 import org.osgi.service.converter.Adapter;
 import org.osgi.service.converter.ConversionException;
 import org.osgi.service.converter.ConvertFunction;
-import org.osgi.service.converter.ConvertResult;
 import org.osgi.service.converter.Converter;
 import org.osgi.service.converter.Converting;
+import org.osgi.service.converter.FunctionThrowsException;
 import org.osgi.service.converter.Rule;
-import org.osgi.service.converter.SimpleConvertFunction;
 import org.osgi.service.converter.TypeReference;
 
 public class AdapterImpl implements Adapter, InternalConverter {
@@ -62,28 +61,32 @@ public class AdapterImpl implements Adap
     @Override
     @SuppressWarnings("unchecked")
     public <F, T> Adapter rule(Class<F> fromCls, Class<T> toCls,
-            SimpleConvertFunction<F, T> toFun, SimpleConvertFunction<T, F> fromFun)
{
+            FunctionThrowsException<F, T> toFun, FunctionThrowsException<T, F>
fromFun) {
         if (fromCls.equals(toCls))
             throw new IllegalArgumentException();
 
-        if (toFun != null)
-            classRules.put(new TypePair(fromCls, toCls), (ConvertFunction<Object, Object>)
toFun);
+        if (toFun != null) {
+            ConvertFunction<F, T> f = new ConvertFunctionImpl<>(toFun);
+            classRules.put(new TypePair(fromCls, toCls), (ConvertFunction<Object, Object>)
f);
+        }
 
-        if (fromFun != null)
-            classRules.put(new TypePair(toCls, fromCls), (ConvertFunction<Object, Object>)
fromFun);
+        if (fromFun != null) {
+            ConvertFunction<T, F> f = new ConvertFunctionImpl<>(fromFun);
+            classRules.put(new TypePair(toCls, fromCls), (ConvertFunction<Object, Object>)
f);
+        }
         return this;
     }
 
     @Override
     public <F, T> Adapter rule(TypeReference<F> fromRef, TypeReference<T>
toRef,
-            SimpleConvertFunction<F, T> toFun, SimpleConvertFunction<T, F> fromFun)
{
+            FunctionThrowsException<F, T> toFun, FunctionThrowsException<T, F>
fromFun) {
         // TODO Auto-generated method stub
         return null;
     }
 
     @Override
     public <F, T> Adapter rule(Type fromType, Type toType,
-            SimpleConvertFunction<F, T> toFun, SimpleConvertFunction<T, F> fromFun)
{
+            FunctionThrowsException<F, T> toFun, FunctionThrowsException<T, F>
fromFun) {
         // TODO Auto-generated method stub
         return null;
     }
@@ -168,9 +171,10 @@ public class AdapterImpl implements Adap
                         continue;
 
                     try {
-                        ConvertResult<?> res = func.convert(object, type);
-                        if (res.getStatus() == ConvertResult.Status.CONVERTED)
-                            return res.getResult();
+                        Object res = func.convert(object, type);
+                        if (res != null) {
+                            return res;
+                        }
                     } catch (Exception ex) {
                         if (hasDefault)
                             return defaultValue;
@@ -230,4 +234,17 @@ public class AdapterImpl implements Adap
                     Objects.equals(to, o.to);
         }
     }
+
+    static class ConvertFunctionImpl<F, T> implements ConvertFunction<F, T> {
+        private final FunctionThrowsException<F, T> function;
+
+        public ConvertFunctionImpl(FunctionThrowsException<F, T> function) {
+            this.function = function;
+        }
+
+        @Override
+        public T convert(F obj, Type targetType) throws Exception {
+            return function.apply(obj);
+        }
+    }
 }

Modified: felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/Adapter.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/Adapter.java?rev=1759730&r1=1759729&r2=1759730&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/Adapter.java
(original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/Adapter.java
Thu Sep  8 00:56:35 2016
@@ -59,14 +59,14 @@ public interface Adapter extends Convert
 	 * @return The current adapter, can be used to chain invocations.
 	 */
 	<F, T> Adapter rule(Class<F> fromCls, Class<T> toCls,
-			SimpleConvertFunction<F,T> toFun,
-			SimpleConvertFunction<T,F> fromFun);
+	        FunctionThrowsException<F,T> toFun,
+	        FunctionThrowsException<T,F> fromFun);
 
 	<F, T> Adapter rule(TypeReference<F> fromRef, TypeReference<T> toRef,
-			SimpleConvertFunction<F,T> toFun,
-			SimpleConvertFunction<T,F> fromFun);
+	        FunctionThrowsException<F,T> toFun,
+	        FunctionThrowsException<T,F> fromFun);
 
 	<F, T> Adapter rule(Type fromType, Type toType,
-			SimpleConvertFunction<F,T> toFun,
-			SimpleConvertFunction<T,F> fromFun);
+	        FunctionThrowsException<F,T> toFun,
+	        FunctionThrowsException<T,F> fromFun);
 }

Modified: felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/ConvertFunction.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/ConvertFunction.java?rev=1759730&r1=1759729&r2=1759730&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/ConvertFunction.java
(original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/ConvertFunction.java
Thu Sep  8 00:56:35 2016
@@ -30,11 +30,10 @@ public interface ConvertFunction<F, T> {
 	/**
 	 * Convert the object into the target type.
 	 *
-	 * @param obj The object to be converted.
+	 * @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. A {@link ConvertResult} may indicate that conversion
-	 * was not successful. In this case the next matching rule or adapter will be given a
+	 * @return The conversion result or {@code null} to indicate that the convert function cannot
handle this conversion. In this case the next matching rule or adapter will be given a
 	 * opportunity to convert.
 	 */
-	ConvertResult<T> convert(F obj, Type targetType) throws Exception;
+	T convert(F obj, Type targetType) throws Exception;
 }

Modified: felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/Converter.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/Converter.java?rev=1759730&r1=1759729&r2=1759730&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/Converter.java
(original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/Converter.java
Thu Sep  8 00:56:35 2016
@@ -36,16 +36,6 @@ public interface Converter {
 	Converting convert(Object obj);
 
 	/**
-	 * Discuss: Create a copy of the provided object. This is the same as
-	 *   {@code converter.convert(obj).to(obj.getClass());}
-	 * @param obj The object to copy
-	 * @return The copied object
-	 */
-	default <T> T copy(T obj) {
-	    return obj;
-	}
-
-	/**
 	 * Obtain a new adapter to this converter. The adapter behaves just like the
 	 * converter except for the exception rules registered with is. It is also
 	 * possible to obtain a new Adapter for an existing adapter. This allows for

Added: felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/FunctionThrowsException.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/FunctionThrowsException.java?rev=1759730&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/FunctionThrowsException.java
(added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/FunctionThrowsException.java
Thu Sep  8 00:56:35 2016
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) OSGi Alliance (2014, 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.service.converter;
+
+import org.osgi.annotation.versioning.ConsumerType;
+
+/**
+ * A function that accepts a single argument and produces a result.
+ *
+ * <p>
+ * This is a functional interface and can be used as the assignment target for a
+ * lambda expression or method reference.
+ *
+ * @param <T> The type of the function input.
+ * @param <R> The type of the function output.
+ *
+ * @ThreadSafe
+ * @author $Id: 3d17c97c7dc36185681b98caed5ee10bdeb2cd93 $
+ */
+@ConsumerType
+@FunctionalInterface
+public interface FunctionThrowsException<T, R> {
+    /**
+     * Applies this function to the specified argument.
+     *
+     * @param t The input to this function.
+     * @return The output of this function.
+     * @throws Exception An exception thrown by the method.
+     */
+    R apply(T t) throws Exception;
+}

Modified: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/AdapterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/AdapterTest.java?rev=1759730&r1=1759729&r2=1759730&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/AdapterTest.java
(original)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/AdapterTest.java
Thu Sep  8 00:56:35 2016
@@ -32,7 +32,6 @@ import org.junit.Before;
 import org.junit.Test;
 import org.osgi.service.converter.Adapter;
 import org.osgi.service.converter.ConvertFunction;
-import org.osgi.service.converter.ConvertResult;
 import org.osgi.service.converter.Converter;
 import org.osgi.service.converter.Rule;
 
@@ -83,11 +82,11 @@ public class AdapterTest {
         ca.rule(char[].class, String.class, AdapterTest::convertToString, null);
         ca.rule(new Rule<String, Number>(String.class, Number.class, new ConvertFunction<String,
Number>() {
             @Override
-            public ConvertResult<Number> convert(String obj, Type targetType) throws
Exception {
+            public Number convert(String obj, Type targetType) throws Exception {
                 if (Integer.class.equals(targetType))
-                    return new ConvertResult<Number>(Integer.valueOf(-1));
+                    return Integer.valueOf(-1);
                 else if (Long.class.equals(targetType))
-                    return new ConvertResult<Number>(Long.valueOf(-1));
+                    return Long.valueOf(-1);
                 return null;
             }
         }));
@@ -109,10 +108,10 @@ public class AdapterTest {
 
         ca.rule(new Rule<Integer, Long>(Integer.class, Long.class, new ConvertFunction<Integer,Long>()
{
             @Override
-            public ConvertResult<Long> convert(Integer obj, Type targetType) throws
Exception {
+            public Long convert(Integer obj, Type targetType) throws Exception {
                 if (obj.intValue() != 1)
-                    return new ConvertResult<Long>(new Long(-obj.intValue()));
-                return ConvertResult.cannotConvert();
+                    return new Long(-obj.intValue());
+                return null;
             }
         }));
 
@@ -126,18 +125,18 @@ public class AdapterTest {
     public void testWildcardAdapter() {
         ConvertFunction<List, Object> foo = new ConvertFunction<List, Object>()
{
             @Override
-            public ConvertResult<Object> convert(List t, Type type) throws Exception
{
+            public Object convert(List t, Type type) throws Exception {
                 if (type instanceof Class) {
                     if (Number.class.isAssignableFrom((Class<?>) type))
-                        return new ConvertResult<Object>(converter.convert(t.size()).to(type));
+                        return converter.convert(t.size()).to(type);
                 }
-                return ConvertResult.cannotConvert();
+                return null;
             }
         };
 
         Rule<List, Object> r = new Rule<>(List.class, Object.class, foo);
         Rule<Object, Object> allCatch = new Rule<>(Object.class, Object.class,
-                (v,t) -> new ConvertResult<Object>(v.toString()));
+                (v,t) -> v.toString());
 
         Adapter ca = converter.newAdapter();
         ca.rule(r);
@@ -152,13 +151,13 @@ public class AdapterTest {
     public void testWildcardAdapter2() {
         Map<Object, Object> snooped = new HashMap<>();
         Rule<Object, ArrayList> r = new Rule<>(Object.class, ArrayList.class,
-                (v,t) -> new ConvertResult<ArrayList>(null),
-                (v,t) -> new ConvertResult<Object>("arraylist"));
+                (v,t) -> null,
+                (v,t) -> "arraylist");
         Rule<Object, List> r2 = new Rule<>(Object.class, List.class,
-                (v,t) -> new ConvertResult<List>(null),
-                (v,t) -> new ConvertResult<Object>("list"));
+                (v,t) -> null,
+                (v,t) -> "list");
         Rule<Object, Object> allCatch = new Rule<>(Object.class, Object.class,
-                (v,t) -> {snooped.put(v,t); return ConvertResult.cannotConvert();}, null);
+                (v,t) -> {snooped.put(v,t); return null;}, null);
 
         Adapter ca = converter.newAdapter();
         ca.rule(r);



Mime
View raw message