ContainUtilpublic class ContainUtil extends Object ContainUtil will check if object 1 contains object 2.
Object 1 may be an Object, array, Collection, or a Map |
Methods Summary |
---|
public static boolean | contains(java.lang.Object obj1, java.lang.Object obj2)Determine if obj2 exists in obj1 .
Type Of obj1 |
Comparison type |
null |
| always return false |
Map |
Map containsKey(obj2) |
Collection |
Collection contains(obj2) |
Array |
there's an array element (e) where e.equals(obj2) |
Object |
obj1.equals(obj2) |
if ((obj1 == null) || (obj2 == null)) {
//log.debug("obj1 or obj2 are null.");
return false;
}
if (obj1 instanceof Map) {
if (((Map) obj1).containsKey(obj2)) {
//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;
}
} else if (obj1.getClass().isArray()) {
for (int i = 0; i < Array.getLength(obj1); i++) {
Object value = null;
value = Array.get(obj1, i);
if (value.equals(obj2)) {
//log.debug("obj1 is an array and contains obj2");
return true;
}
}
} else if (obj1.toString().equals(obj2.toString())) {
//log.debug("obj1 is an object and it's String representation equals obj2's String representation.");
return true;
} else if (obj1.equals(obj2)) {
//log.debug("obj1 is an object and equals obj2");
return true;
}
//log.debug("obj1 does not contain obj2: " + obj1 + ", " + obj2);
return false;
|
|