mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Adding support for Iteratable
WW-2397 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@615175 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -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 @@ public class ContainUtil {
|
||||
//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;
|
||||
|
||||
@@ -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 @@ public class ContainUtilTest extends TestCase {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user