struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mr...@apache.org
Subject svn commit: r615175 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/util/ContainUtil.java test/java/org/apache/struts2/util/ContainUtilTest.java
Date Fri, 25 Jan 2008 11:14:15 GMT
Author: mrdon
Date: Fri Jan 25 03:14:15 2008
New Revision: 615175

URL: http://svn.apache.org/viewvc?rev=615175&view=rev
Log:
Adding support for Iteratable
WW-2397

Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ContainUtil.java
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ContainUtilTest.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ContainUtil.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ContainUtil.java?rev=615175&r1=615174&r2=615175&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ContainUtil.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ContainUtil.java Fri Jan
25 03:14:15 2008
@@ -21,7 +21,7 @@
 package org.apache.struts2.util;
 
 import java.lang.reflect.Array;
-import java.util.Collection;
+import java.util.Iterator;
 import java.util.Map;
 
 
@@ -79,11 +79,14 @@
                 //log.debug("obj1 is a map and contains obj2");
                 return true;
             }
-        } else if (obj1 instanceof Collection) {
-            if (((Collection) obj1).contains(obj2) || ((Collection) obj1).contains(obj2.toString()))
{
-                //log.debug("obj1 is a collection and contains obj2");
-                return true;
-            }
+        } if (obj1 instanceof Iterable) {
+        	Iterator iter = ((Iterable) obj1).iterator();
+        	while(iter.hasNext()) {
+        		Object value = iter.next();
+        		if (obj2.equals(value) || obj2.toString().equals(value)) {
+        			return true;
+        		}
+        	}
         } else if (obj1.getClass().isArray()) {
             for (int i = 0; i < Array.getLength(obj1); i++) {
                 Object value = null;

Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ContainUtilTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ContainUtilTest.java?rev=615175&r1=615174&r2=615175&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ContainUtilTest.java (original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ContainUtilTest.java Fri
Jan 25 03:14:15 2008
@@ -21,6 +21,8 @@
 package org.apache.struts2.util;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
@@ -88,6 +90,25 @@
         assertTrue(ContainUtil.contains(new MyObject("tm_jee", 22), new MyObject("tm_jee",
22)));
     }
 
+    public void testIterableObject() throws Exception {
+    	MyIterableObject i = new MyIterableObject("one", "two");
+
+        assertFalse(ContainUtil.contains(i, "thre"));
+        assertTrue(ContainUtil.contains(i, "one"));
+        assertTrue(ContainUtil.contains(i, "two"));
+    }
+    
+    public static class MyIterableObject implements Iterable<String> {
+    	private List<String> values;
+    	
+    	public MyIterableObject(String... strings) {
+    		values = Arrays.asList(strings);
+    	}
+    	
+		public Iterator<String> iterator() {
+			return values.iterator();
+		}
+    }
 
     public static class MyObject {
         private String name;



Mime
View raw message