1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.util;
19
20 import java.lang.reflect.Array;
21 import java.util.Collection;
22 import java.util.Map;
23
24
25 /***
26 * <code>ContainUtil</code> will check if object 1 contains object 2.
27 * Object 1 may be an Object, array, Collection, or a Map
28 *
29 */
30 public class ContainUtil {
31
32 public static boolean contains(Object obj1, Object obj2) {
33 if ((obj1 == null) || (obj2 == null)) {
34
35 return false;
36 }
37
38 if (obj1 instanceof Map) {
39 if (((Map) obj1).containsValue(obj2)) {
40
41 return true;
42 }
43 } else if (obj1 instanceof Collection) {
44 if (((Collection) obj1).contains(obj2)) {
45
46 return true;
47 }
48 } else if (obj1.getClass().isArray()) {
49 for (int i = 0; i < Array.getLength(obj1); i++) {
50 Object value = null;
51 value = Array.get(obj1, i);
52
53 if (value.equals(obj2)) {
54
55 return true;
56 }
57 }
58 } else if (obj1.equals(obj2)) {
59
60 return true;
61 }
62
63
64 return false;
65 }
66 }