You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2015/08/11 22:42:42 UTC

[04/18] incubator-brooklyn git commit: Adds TypeCoercions.function(Class)

Adds TypeCoercions.function(Class)


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/ce0c4223
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/ce0c4223
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/ce0c4223

Branch: refs/heads/master
Commit: ce0c4223c53aa32e3b15d6654072b4caa27ff834
Parents: 81650a8
Author: Aled Sage <al...@gmail.com>
Authored: Sat Apr 18 14:38:50 2015 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Tue Aug 11 17:51:34 2015 +0100

----------------------------------------------------------------------
 .../java/brooklyn/util/flags/TypeCoercions.java | 69 ++++++++++++++------
 .../util/internal/TypeCoercionsTest.java        |  5 ++
 2 files changed, 55 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ce0c4223/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/flags/TypeCoercions.java b/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
index 3b4ec3d..f1e3992 100644
--- a/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
+++ b/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
@@ -274,6 +274,28 @@ public class TypeCoercions {
     }
 
     /**
+     * Returns a function that does a type coercion to the given type. For example,
+     * {@code TypeCoercions.function(Double.class)} will return a function that will
+     * coerce its input value to a {@link Double} (or throw a {@link ClassCoercionException}
+     * if that is not possible).
+     */
+    public static <T> Function<Object, T> function(final Class<T> type) {
+        return new CoerceFunction<T>(type);
+    }
+    
+    private static class CoerceFunction<T> implements Function<Object, T> {
+        private final Class<T> type;
+
+        public CoerceFunction(Class<T> type) {
+            this.type = type;
+        }
+        @Override
+        public T apply(Object input) {
+            return coerce(input, type);
+        }
+    }
+
+    /**
      * Type coercion {@link Function function} for {@link Enum enums}.
      * <p>
      * Tries to convert the string to {@link CaseFormat#UPPER_UNDERSCORE} first,
@@ -286,27 +308,36 @@ public class TypeCoercions {
      * @see Enum#valueOf(Class, String)
      */
     public static <E extends Enum<E>> Function<String, E> stringToEnum(final Class<E> type, @Nullable final E defaultValue) {
-        return new Function<String, E>() {
-            @Override
-            public E apply(String input) {
-                Preconditions.checkNotNull(input, "input");
-                List<String> options = ImmutableList.of(
-                        input,
-                        CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, input),
-                        CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, input),
-                        CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, input),
-                        CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, input));
-                for (String value : options) {
-                    try {
-                        return Enum.valueOf(type, value);
-                    } catch (IllegalArgumentException iae) {
-                        continue;
-                    }
+        return new StringToEnumFunction<E>(type, defaultValue);
+    }
+    
+    private static class StringToEnumFunction<E extends Enum<E>> implements Function<String, E> {
+        private final Class<E> type;
+        private final E defaultValue;
+        
+        public StringToEnumFunction(Class<E> type, @Nullable E defaultValue) {
+            this.type = type;
+            this.defaultValue = defaultValue;
+        }
+        @Override
+        public E apply(String input) {
+            Preconditions.checkNotNull(input, "input");
+            List<String> options = ImmutableList.of(
+                    input,
+                    CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, input),
+                    CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, input),
+                    CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, input),
+                    CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, input));
+            for (String value : options) {
+                try {
+                    return Enum.valueOf(type, value);
+                } catch (IllegalArgumentException iae) {
+                    continue;
                 }
-                Maybe<E> result = Enums.valueOfIgnoreCase(type, input);
-                return (result.isPresent()) ? result.get() : defaultValue;
             }
-        };
+            Maybe<E> result = Enums.valueOfIgnoreCase(type, input);
+            return (result.isPresent()) ? result.get() : defaultValue;
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ce0c4223/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java b/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java
index 66e7d2c..ecb8c7c 100644
--- a/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java
+++ b/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java
@@ -335,6 +335,11 @@ public class TypeCoercionsTest {
         TypeCoercions.coerce(new Object(), TypeToken.of(Integer.class));
     }
 
+    @Test
+    public void testCoercionFunction() {
+        assertEquals(TypeCoercions.function(Double.class).apply("1"), Double.valueOf(1));
+    }
+
     public static class WithAs {
         String value;
         public WithAs(Object x) { value = ""+x; }