Author: nbelaevski
Date: 2007-07-22 08:17:52 -0400 (Sun, 22 Jul 2007)
New Revision: 1771
Added:
trunk/framework/impl/src/main/java/org/richfaces/component/util/ComponentUtil.java
trunk/framework/impl/src/test/java/org/richfaces/
trunk/framework/impl/src/test/java/org/richfaces/component/
trunk/framework/impl/src/test/java/org/richfaces/component/util/
trunk/framework/impl/src/test/java/org/richfaces/component/util/ComponentUtilTest.java
Modified:
trunk/framework/impl/
Log:
Property changes on: trunk/framework/impl
___________________________________________________________________
Name: svn:ignore
- .classpath
.project
.settings
target
+ .classpath
.project
.settings
target
.clover
Added: trunk/framework/impl/src/main/java/org/richfaces/component/util/ComponentUtil.java
===================================================================
--- trunk/framework/impl/src/main/java/org/richfaces/component/util/ComponentUtil.java
(rev 0)
+++
trunk/framework/impl/src/main/java/org/richfaces/component/util/ComponentUtil.java 2007-07-22
12:17:52 UTC (rev 1771)
@@ -0,0 +1,59 @@
+/**
+ *
+ */
+package org.richfaces.component.util;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * @author Nick Belaevski
+ * mailto:nbelaevski@exadel.com
+ * created 20.07.2007
+ *
+ */
+public class ComponentUtil {
+ public static String[] asArray(Object object) {
+ if (object == null) {
+ return null;
+ }
+
+ Class componentType = object.getClass().getComponentType();
+
+ if (String.class.equals(componentType)) {
+ return (String[]) object;
+ } else if (componentType != null) {
+ Object[] objects = (Object[]) object;
+ String[] result = new String[objects.length];
+ for (int i = 0; i < objects.length; i++) {
+ Object o = objects[i];
+ if (o == null) {
+ continue;
+ }
+
+ result[i] = o.toString();
+ }
+
+ return result;
+ } else if (object instanceof Collection) {
+ Collection collection = (Collection) object;
+ String[] result = new String[collection.size()];
+ Iterator iterator = collection.iterator();
+
+ for (int i = 0; i < result.length; i++) {
+ Object next = iterator.next();
+ if (next == null) {
+ continue;
+ }
+
+ result[i] = next.toString();
+ }
+
+ return result;
+ } else {
+ String string = object.toString().trim();
+ String[] split = string.split("\\s*,\\s*");
+ return split;
+ }
+ }
+}
Added:
trunk/framework/impl/src/test/java/org/richfaces/component/util/ComponentUtilTest.java
===================================================================
---
trunk/framework/impl/src/test/java/org/richfaces/component/util/ComponentUtilTest.java
(rev 0)
+++
trunk/framework/impl/src/test/java/org/richfaces/component/util/ComponentUtilTest.java 2007-07-22
12:17:52 UTC (rev 1771)
@@ -0,0 +1,66 @@
+package org.richfaces.component.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.TreeSet;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Nick Belaevski
+ * mailto:nbelaevski@exadel.com
+ * created 20.07.2007
+ *
+ */
+public class ComponentUtilTest extends TestCase {
+
+ public void testAsArray() {
+ assertNull(ComponentUtil.asArray(null));
+ }
+
+ public void testAsArray1() {
+ String[] strings = new String[] {"a", "b"};
+ String[] array = ComponentUtil.asArray(strings);
+ assertSame(strings, array);
+ }
+
+ public void testAsArray2() {
+ Object[] objects = new Object[] {
+ Integer.valueOf(12), null, Integer.valueOf(22), Integer.valueOf(42)
+ };
+ String[] array = ComponentUtil.asArray(objects);
+ String[] etalon = new String[] {"12", null, "22", "42"};
+ assertTrue(Arrays.equals(etalon, array));
+ }
+
+ public void testAsArray3() {
+ ArrayList list = new ArrayList();
+ list.add(new Integer(12));
+ list.add(null);
+ list.add(new Integer(22));
+ list.add(new Integer(42));
+
+ String[] array = ComponentUtil.asArray(list);
+ String[] etalon = new String[] {"12", null, "22", "42"};
+ assertTrue(Arrays.equals(etalon, array));
+ }
+
+ public void testAsArray31() {
+ Set set = new TreeSet();
+ set.add(new Integer(12));
+ set.add(new Integer(22));
+ set.add(new Integer(42));
+
+ String[] array = ComponentUtil.asArray(set);
+ String[] etalon = new String[] {"12", "22", "42"};
+ assertTrue(Arrays.equals(etalon, array));
+ }
+
+ public void testAsArray4() {
+ String string = " a , \t\n b \n , c ";
+ String[] strings = ComponentUtil.asArray(string);
+ String[] etalon = new String[] {"a", "b", "c"};
+ assertTrue(Arrays.equals(etalon, strings));
+ }
+}
\ No newline at end of file
Show replies by date