Return-Path: X-Original-To: apmail-camel-dev-archive@www.apache.org Delivered-To: apmail-camel-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CC134EF9B for ; Thu, 10 Jan 2013 20:55:19 +0000 (UTC) Received: (qmail 18821 invoked by uid 500); 10 Jan 2013 20:55:19 -0000 Delivered-To: apmail-camel-dev-archive@camel.apache.org Received: (qmail 18775 invoked by uid 500); 10 Jan 2013 20:55:19 -0000 Mailing-List: contact dev-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list dev@camel.apache.org Received: (qmail 18764 invoked by uid 99); 10 Jan 2013 20:55:19 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Jan 2013 20:55:19 +0000 X-ASF-Spam-Status: No, hits=0.7 required=5.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [64.85.173.253] (HELO server.dankulp.com) (64.85.173.253) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Jan 2013 20:55:11 +0000 Received: by server.dankulp.com (Postfix, from userid 5000) id 73B21180A4B; Thu, 10 Jan 2013 15:54:50 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on server.dankulp.com X-Spam-Level: X-Msg-File: /tmp/mailfilter-dev@camel.apache.org.K4yN5nhgW1 Received: from macbook.house.dankulp.com (c-24-91-72-253.hsd1.ma.comcast.net [24.91.72.253]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by server.dankulp.com (Postfix) with ESMTPSA id 130331809D6; Thu, 10 Jan 2013 15:54:44 -0500 (EST) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Subject: Re: svn commit: r1431626 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/util/ObjectHelper.java test/java/org/apache/camel/util/ObjectHelperTest.java From: Daniel Kulp In-Reply-To: <20130110204401.62E7D23889D7@eris.apache.org> Date: Thu, 10 Jan 2013 15:54:43 -0500 Content-Transfer-Encoding: quoted-printable Message-Id: References: <20130110204401.62E7D23889D7@eris.apache.org> To: dev@camel.apache.org, bvahdat@apache.org X-Mailer: Apple Mail (2.1499) X-Virus-Checked: Checked by ClamAV on apache.org X-Old-Spam-Status: No, score=-2.9 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham version=3.3.2 Any particular reason for not just doing: java.lang.reflect.Array.getLength(array) to get the length and: java.lang.reflect.Array.get(array, index); to avoid all the casting, instanceofs, etc=85.? The "get" method will = automatically handle the boxing as well. Dan On Jan 10, 2013, at 3:44 PM, bvahdat@apache.org wrote: > Author: bvahdat > Date: Thu Jan 10 20:44:00 2013 > New Revision: 1431626 >=20 > URL: http://svn.apache.org/viewvc?rev=3D1431626&view=3Drev > Log: > CAMEL-5948: ObjectHelper.createIterator() should properly handle the = array of primitive types e.g. int[]. >=20 > Modified: > = camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.ja= va > = camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTes= t.java >=20 > Modified: = camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.ja= va > URL: = http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apac= he/camel/util/ObjectHelper.java?rev=3D1431626&r1=3D1431625&r2=3D1431626&vi= ew=3Ddiff > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D > --- = camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.ja= va (original) > +++ = camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.ja= va Thu Jan 10 20:44:00 2013 > @@ -64,6 +64,12 @@ import org.slf4j.LoggerFactory; > public final class ObjectHelper { > private static final transient Logger LOG =3D = LoggerFactory.getLogger(ObjectHelper.class); > private static final String DEFAULT_DELIMITER =3D ","; > + @SuppressWarnings("unchecked") > + private static final List PRIMITIVE_TYPES =3D = Arrays.asList(byte.class, short.class, int.class, long.class, > + = float.class, double.class, char.class, boolean.class, void.class); > + @SuppressWarnings("unchecked") > + private static final List PRIMITIVE_ARRAY_TYPES =3D = Arrays.asList(byte[].class, short[].class, int[].class, long[].class, > + = float[].class, double[].class, char[].class, boolean[].class); >=20 > /** > * Utility classes should not have a public constructor. > @@ -493,7 +499,12 @@ public final class ObjectHelper { > * Object[], a String with values separated by the given = delimiter, > * or a primitive type array; otherwise to simplify the caller's > * code, we just create a singleton collection iterator over a = single value > - * > + *=20 > + *

In case of primitive type arrays the returned {@code = Iterator} iterates > + * over the corresponding Java primitive wrapper objects of the = given elements > + * inside the {@code value} array. That's we get an autoboxing of = the primitive > + * types here for free as it's also the case in Java language = itself. > + *=20 > * @param value the value > * @param delimiter delimiter for separating String values > * @param allowEmptyValues whether to allow empty values > @@ -514,9 +525,79 @@ public final class ObjectHelper { > } else if (value instanceof Iterable) { > return ((Iterable)value).iterator(); > } else if (value.getClass().isArray()) { > - // TODO we should handle primitive array types? > - List list =3D Arrays.asList((Object[])value); > - return list.iterator(); > + if (isPrimitiveArrayType(value.getClass())) { > + final Object array =3D value; > + return new Iterator() { > + int idx =3D -1; > + > + public boolean hasNext() { > + return (idx + 1) < length(); > + } > + > + public Object next() { > + idx++; > + return current(idx); > + } > + > + public void remove() { > + throw new UnsupportedOperationException(); > + } > + > + private int length() { > + int answer =3D 0; > + if (array instanceof byte[]) { > + answer =3D ((byte[]) array).length; > + } else if (array instanceof short[]) { > + answer =3D ((short[]) array).length; > + } else if (array instanceof int[]) { > + answer =3D ((int[]) array).length; > + } else if (array instanceof long[]) { > + answer =3D ((long[]) array).length; > + } else if (array instanceof float[]) { > + answer =3D ((float[]) array).length; > + } else if (array instanceof double[]) { > + answer =3D ((double[]) array).length; > + } else if (array instanceof char[]) { > + answer =3D ((char[]) array).length; > + } else if (array instanceof boolean[]) { > + answer =3D ((boolean[]) array).length; > + } else { > + throw new = IllegalStateException("Unexpected type for " + array); > + } > + > + return answer; > + } > + > + private Object current(int index) { > + Object answer =3D 0; > + if (array instanceof byte[]) { > + answer =3D Byte.valueOf(((byte[]) = array)[index]); > + } else if (array instanceof short[]) { > + answer =3D Short.valueOf(((short[]) = array)[index]); > + } else if (array instanceof int[]) { > + answer =3D Integer.valueOf(((int[]) = array)[index]); > + } else if (array instanceof long[]) { > + answer =3D Long.valueOf(((long[]) = array)[index]); > + } else if (array instanceof float[]) { > + answer =3D Float.valueOf(((float[]) = array)[index]); > + } else if (array instanceof double[]) { > + answer =3D Double.valueOf(((double[]) = array)[index]); > + } else if (array instanceof char[]) { > + answer =3D Character.valueOf(((char[]) = array)[index]); > + } else if (array instanceof boolean[]) { > + answer =3D Boolean.valueOf(((boolean[]) = array)[index]); > + } else { > + throw new = IllegalStateException("Unexpected type for " + array); > + } > + > + return answer; > + } > + > + }; > + } else { > + List list =3D Arrays.asList((Object[]) = value); > + return list.iterator(); > + } > } else if (value instanceof NodeList) { > // lets iterate through DOM results after performing = XPaths > final NodeList nodeList =3D (NodeList) value; > @@ -1054,6 +1135,26 @@ public final class ObjectHelper { > } >=20 > /** > + * Returns if the given {@code clazz} type is a Java primitive = type. > + *=20 > + * @param clazz the Java type to be checked > + * @return {@code true} if the given type is a Java primitive = type > + */ > + public static boolean isPrimitiveType(Class clazz) { > + return PRIMITIVE_TYPES.contains(clazz); > + } > + > + /** > + * Returns if the given {@code clazz} type is a Java primitive = array type. > + *=20 > + * @param clazz the Java type to be checked > + * @return {@code true} if the given type is a Java primitive = array type > + */ > + public static boolean isPrimitiveArrayType(Class clazz) { > + return PRIMITIVE_ARRAY_TYPES.contains(clazz); > + } > + > + /** > * Converts primitive types such as int to its wrapper type like > * {@link Integer} > */ >=20 > Modified: = camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTes= t.java > URL: = http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apac= he/camel/util/ObjectHelperTest.java?rev=3D1431626&r1=3D1431625&r2=3D143162= 6&view=3Ddiff > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D > --- = camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTes= t.java (original) > +++ = camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTes= t.java Thu Jan 10 20:44:00 2013 > @@ -25,6 +25,8 @@ import java.util.Iterator; > import java.util.List; >=20 > import junit.framework.TestCase; > + > +import org.apache.camel.CamelContext; > import org.apache.camel.Exchange; > import org.apache.camel.Message; > import org.apache.camel.component.bean.MyStaticClass; > @@ -184,6 +186,173 @@ public class ObjectHelperTest extends Te > assertEquals("bean:bar?method=3Dcool('A','Hello,World')", = it.next()); > } >=20 > + public void testCreateIteratorWithPrimitiveArrayTypes() { > + Iterator it =3D ObjectHelper.createIterator(new byte[] = {13, Byte.MAX_VALUE, 7, Byte.MIN_VALUE}, null); > + assertTrue(it.hasNext()); > + assertEquals(Byte.valueOf((byte) 13), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Byte.MAX_VALUE, it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Byte.valueOf((byte) 7), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Byte.MIN_VALUE, it.next()); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new byte[] {}, null); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new short[] {13, = Short.MAX_VALUE, 7, Short.MIN_VALUE}, null); > + assertTrue(it.hasNext()); > + assertEquals(Short.valueOf((short) 13), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Short.MAX_VALUE, it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Short.valueOf((short) 7), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Short.MIN_VALUE, it.next()); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new short[] {}, null); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new int[] {13, = Integer.MAX_VALUE, 7, Integer.MIN_VALUE}, null); > + assertTrue(it.hasNext()); > + assertEquals(Integer.valueOf(13), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Integer.MAX_VALUE, it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Integer.valueOf(7), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Integer.MIN_VALUE, it.next()); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new int[] {}, null); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new long[] {13, = Long.MAX_VALUE, 7, Long.MIN_VALUE}, null); > + assertTrue(it.hasNext()); > + assertEquals(Long.valueOf(13), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Long.MAX_VALUE, it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Long.valueOf(7), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Long.MIN_VALUE, it.next()); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new long[] {}, null); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new float[] {13.7F, = Float.MAX_VALUE, 7.13F, Float.MIN_VALUE}, null); > + assertTrue(it.hasNext()); > + assertEquals(Float.valueOf(13.7F), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Float.MAX_VALUE, it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Float.valueOf(7.13F), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Float.MIN_VALUE, it.next()); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new float[] {}, null); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new double[] {13.7D, = Double.MAX_VALUE, 7.13D, Double.MIN_VALUE}, null); > + assertTrue(it.hasNext()); > + assertEquals(Double.valueOf(13.7D), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Double.MAX_VALUE, it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Double.valueOf(7.13D), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Double.MIN_VALUE, it.next()); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new double[] {}, null); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new char[] {'C', 'a', 'm', = 'e', 'l'}, null); > + assertTrue(it.hasNext()); > + assertEquals(Character.valueOf('C'), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Character.valueOf('a'), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Character.valueOf('m'), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Character.valueOf('e'), it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Character.valueOf('l'), it.next()); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new char[] {}, null); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new boolean[] {false, = true, false, true, true}, null); > + assertTrue(it.hasNext()); > + assertEquals(Boolean.FALSE, it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Boolean.TRUE, it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Boolean.FALSE, it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Boolean.TRUE, it.next()); > + assertTrue(it.hasNext()); > + assertEquals(Boolean.TRUE, it.next()); > + assertFalse(it.hasNext()); > + > + it =3D ObjectHelper.createIterator(new boolean[] {}, null); > + assertFalse(it.hasNext()); > + } > + > + public void testIsPrimitiveType() { > + assertTrue(ObjectHelper.isPrimitiveType(byte.class)); > + assertTrue(ObjectHelper.isPrimitiveType(short.class)); > + assertTrue(ObjectHelper.isPrimitiveType(int.class)); > + assertTrue(ObjectHelper.isPrimitiveType(long.class)); > + assertTrue(ObjectHelper.isPrimitiveType(float.class)); > + assertTrue(ObjectHelper.isPrimitiveType(double.class)); > + assertTrue(ObjectHelper.isPrimitiveType(char.class)); > + assertTrue(ObjectHelper.isPrimitiveType(boolean.class)); > + assertTrue(ObjectHelper.isPrimitiveType(void.class)); > + > + assertFalse(ObjectHelper.isPrimitiveType(Object.class)); > + assertFalse(ObjectHelper.isPrimitiveType(Byte.class)); > + assertFalse(ObjectHelper.isPrimitiveType(Short.class)); > + assertFalse(ObjectHelper.isPrimitiveType(Integer.class)); > + assertFalse(ObjectHelper.isPrimitiveType(Long.class)); > + assertFalse(ObjectHelper.isPrimitiveType(Float.class)); > + assertFalse(ObjectHelper.isPrimitiveType(Double.class)); > + assertFalse(ObjectHelper.isPrimitiveType(Character.class)); > + assertFalse(ObjectHelper.isPrimitiveType(Boolean.class)); > + assertFalse(ObjectHelper.isPrimitiveType(Void.class)); > + = assertFalse(ObjectHelper.isPrimitiveType(CamelContext.class)); > + assertFalse(ObjectHelper.isPrimitiveType(null)); > + } > + > + public void testIsPrimitiveArrayType() { > + assertTrue(ObjectHelper.isPrimitiveArrayType(byte[].class)); > + assertTrue(ObjectHelper.isPrimitiveArrayType(short[].class)); > + assertTrue(ObjectHelper.isPrimitiveArrayType(int[].class)); > + assertTrue(ObjectHelper.isPrimitiveArrayType(long[].class)); > + assertTrue(ObjectHelper.isPrimitiveArrayType(float[].class)); > + = assertTrue(ObjectHelper.isPrimitiveArrayType(double[].class)); > + assertTrue(ObjectHelper.isPrimitiveArrayType(char[].class)); > + = assertTrue(ObjectHelper.isPrimitiveArrayType(boolean[].class)); > + > + = assertFalse(ObjectHelper.isPrimitiveArrayType(Object[].class)); > + assertFalse(ObjectHelper.isPrimitiveArrayType(Byte[].class)); > + = assertFalse(ObjectHelper.isPrimitiveArrayType(Short[].class)); > + = assertFalse(ObjectHelper.isPrimitiveArrayType(Integer[].class)); > + assertFalse(ObjectHelper.isPrimitiveArrayType(Long[].class)); > + = assertFalse(ObjectHelper.isPrimitiveArrayType(Float[].class)); > + = assertFalse(ObjectHelper.isPrimitiveArrayType(Double[].class)); > + = assertFalse(ObjectHelper.isPrimitiveArrayType(Character[].class)); > + = assertFalse(ObjectHelper.isPrimitiveArrayType(Boolean[].class)); > + assertFalse(ObjectHelper.isPrimitiveArrayType(Void[].class)); > + = assertFalse(ObjectHelper.isPrimitiveArrayType(CamelContext[].class)); > + assertFalse(ObjectHelper.isPrimitiveArrayType(null)); > + } > + > public void testBefore() { > assertEquals("Hello ", ObjectHelper.before("Hello World", = "World")); > assertEquals("Hello ", ObjectHelper.before("Hello World = Again", "World")); >=20 >=20 --=20 Daniel Kulp dkulp@apache.org - http://dankulp.com/blog Talend Community Coder - http://coders.talend.com