You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/04/16 10:04:14 UTC

svn commit: r529156 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/converter/ main/java/org/apache/camel/impl/converter/ test/java/org/apache/camel/converter/

Author: jstrachan
Date: Mon Apr 16 01:04:10 2007
New Revision: 529156

URL: http://svn.apache.org/viewvc?view=rev&rev=529156
Log:
added support for more type conversions such as dealing with Arrays

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/CollectionConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToArrayTypeConverter.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/CollectionConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/CollectionConverter.java?view=auto&rev=529156
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/CollectionConverter.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/CollectionConverter.java Mon Apr 16 01:04:10 2007
@@ -0,0 +1,74 @@
+/*
+ * 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.camel.converter;
+
+import org.apache.camel.Converter;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Some core java.util Collection based
+ * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
+ *
+ * @version $Revision: 524215 $
+ */
+@Converter
+public class CollectionConverter {
+
+    /**
+     * Converts a collection to an array
+     */
+    @Converter
+    public static Object[] toArray(Collection value) {
+        if (value == null) {
+            return null;
+        }
+        return value.toArray();
+    }
+
+    /**
+     * Converts an array to a collection
+     */
+    @Converter
+    public static List toList(Object[] array) {
+        return Arrays.asList(array);
+    }
+
+    @Converter
+    public static Set toSet(Object[] array) {
+        Set answer = new HashSet();
+        for (Object element : array) {
+            answer.add(element);
+        }
+        return answer;
+    }
+
+    @Converter
+    public static Set toSet(Collection collection) {
+        return new HashSet(collection);
+    }
+
+    @Converter
+    public static Set toSet(Map map) {
+        return map.entrySet();
+    }
+}

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java?view=diff&rev=529156&r1=529155&r2=529156
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java Mon Apr 16 01:04:10 2007
@@ -46,6 +46,7 @@
         typeConverterLoaders.add(new AnnotationTypeConverterLoader());
         fallbackConverters.add(new PropertyEditorTypeConverter());
         fallbackConverters.add(new ToStringTypeConverter());
+        fallbackConverters.add(new ToArrayTypeConverter());
     }
 
     public DefaultTypeConverter(Injector injector) {
@@ -151,6 +152,22 @@
                 TypeConverter converter = getTypeConverter(toType, type);
                 if (converter != null) {
                     return converter;
+                }
+            }
+
+            // lets test for arrays
+            if (fromType.isArray() && !fromType.getComponentType().isPrimitive()) {
+                // TODO can we try walking the inheritence-tree for the element types?
+                if (!fromType.equals(Object[].class)) {
+                    fromSuperClass = Object[].class;
+
+                    TypeConverter converter = getTypeConverter(toType, fromSuperClass);
+                    if (converter == null) {
+                        converter = findTypeConverter(toType, fromSuperClass, value);
+                    }
+                    if (converter != null) {
+                        return converter;
+                    }
                 }
             }
         }

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToArrayTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToArrayTypeConverter.java?view=auto&rev=529156
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToArrayTypeConverter.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToArrayTypeConverter.java Mon Apr 16 01:04:10 2007
@@ -0,0 +1,76 @@
+/*
+ * 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.camel.impl.converter;
+
+import org.apache.camel.TypeConverter;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * A type converter which is used to create derived types of arrays
+ * from collections
+ *
+ * @version $Revision: $
+ */
+public class ToArrayTypeConverter implements TypeConverter {
+    public <T> T convertTo(Class<T> type, Object value) {
+        if (type.isArray()) {
+            if (value instanceof Collection) {
+                Collection collection = (Collection) value;
+                Object array = Array.newInstance(type.getComponentType(), collection.size());
+                if (array instanceof Object[]) {
+                    collection.toArray((Object[]) array);
+                }
+                else {
+                    int index = 0;
+                    for (Object element : collection) {
+                        Array.set(array, index++, element);
+                    }
+                }
+                return (T) array;
+            }
+            else if (value != null && value.getClass().isArray()) {
+                int size = Array.getLength(value);
+                Object answer = Array.newInstance(type.getComponentType(), size);
+                for (int i = 0; i < size; i++) {
+                    Array.set(answer, i, Array.get(value, i));
+                }
+                return (T) answer;
+            }
+        }
+        else if (Collection.class.isAssignableFrom(type)) {
+            if (value != null) {
+                if (value instanceof Object[]) {
+                    return (T) Arrays.asList((Object[]) value);
+                }
+                else if (value.getClass().isArray()) {
+                    int size = Array.getLength(value);
+                    List answer = new ArrayList(size);
+                    for (int i = 0; i < size; i++) {
+                        answer.add(Array.get(value, i));
+                    }
+                    return (T) answer;
+                }
+            }
+        }
+        return null;
+    }
+}

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java?view=diff&rev=529156&r1=529155&r2=529156
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java Mon Apr 16 01:04:10 2007
@@ -17,17 +17,20 @@
  */
 package org.apache.camel.converter;
 
-import java.beans.PropertyEditorManager;
-import java.beans.PropertyEditorSupport;
-import java.io.InputStream;
-
 import junit.framework.TestCase;
-
 import org.apache.camel.TypeConverter;
 import org.apache.camel.impl.converter.DefaultTypeConverter;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.beans.PropertyEditorManager;
+import java.beans.PropertyEditorSupport;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
 /**
  * @version $Revision$
  */
@@ -35,26 +38,26 @@
     private static final transient Log log = LogFactory.getLog(ConverterTest.class);
 
     protected TypeConverter converter = new DefaultTypeConverter();
-    
-	public static class IntegerPropertyEditor extends PropertyEditorSupport {
-		public void setAsText(String text) throws IllegalArgumentException {
+
+    public static class IntegerPropertyEditor extends PropertyEditorSupport {
+        public void setAsText(String text) throws IllegalArgumentException {
             setValue(new Integer(text));
-		}
+        }
 
-		public String getAsText() {
-			Integer value = (Integer) getValue();
-			return (value != null ? value.toString() : "");
-		}		
-	}
-	
-	
-	@Override
-	protected void setUp() throws Exception {
-		PropertyEditorManager.registerEditor(Integer.class, IntegerPropertyEditor.class);
-	}
+        public String getAsText() {
+            Integer value = (Integer) getValue();
+            return (value != null ? value.toString() : "");
+        }
+    }
+
+
+    @Override
+    protected void setUp() throws Exception {
+        PropertyEditorManager.registerEditor(Integer.class, IntegerPropertyEditor.class);
+    }
 
     public void testIntegerPropertyEditorConversion() throws Exception {
-    	Integer value = converter.convertTo(Integer.class, "1000");
+        Integer value = converter.convertTo(Integer.class, "1000");
         assertNotNull(value);
         assertEquals("Converted to Integer", new Integer(1000), value);
 
@@ -78,6 +81,56 @@
 
         String text = converter.convertTo(String.class, inputStream);
         assertEquals("Converted to String", "bar", text);
+    }
+
+    public void testArrayToListAndSetConversion() throws Exception {
+        String[] array = new String[]{"one", "two"};
+
+        List list = converter.convertTo(List.class, array);
+        assertEquals("List size: " + list, 2, list.size());
+
+        Collection collection = converter.convertTo(Collection.class, array);
+        assertEquals("Collection size: " + collection, 2, collection.size());
+
+        Set set = converter.convertTo(Set.class, array);
+        assertEquals("Set size: " + set, 2, set.size());
+        set = converter.convertTo(Set.class, list);
+        assertEquals("Set size: " + set, 2, set.size());
+    }
+
+
+    public void testCollectionToArrayConversion() throws Exception {
+        List list = new ArrayList();
+        list.add("one");
+        list.add("two");
+
+        Object[] objectArray = converter.convertTo(Object[].class, list);
+        assertEquals("Object[] length", 2, objectArray.length);
+
+        String[] stringArray = converter.convertTo(String[].class, list);
+        assertEquals("String[] length", 2, stringArray.length);
+    }
+
+    public void testCollectionToPrimitiveArrayConversion() throws Exception {
+        List list = new ArrayList();
+        list.add(5);
+        list.add(6);
+
+        Integer[] integerArray = converter.convertTo(Integer[].class, list);
+        assertEquals("Integer[] length", 2, integerArray.length);
+
+        int[] intArray = converter.convertTo(int[].class, list);
+        assertEquals("int[] length", 2, intArray.length);
+
+        // lets convert the typesafe array to a larger primitive type
+        long[] longArray = converter.convertTo(long[].class, intArray);
+        assertEquals("long[] length", 2, longArray.length);
+
+        // now lets go back to a List again
+        List resultList = converter.convertTo(List.class, intArray);
+        assertEquals("List size", 2, resultList.size());
+        log.info("From primitive type array we've created the list: " + resultList);
+
     }
 
 }