FileDocCategorySizeDatePackage
ContainUtil.javaAPI DocExample3672Mon Jul 23 13:26:56 BST 2007org.apache.struts2.util

ContainUtil

public class ContainUtil extends Object
ContainUtil will check if object 1 contains object 2. Object 1 may be an Object, array, Collection, or a Map
version
$Date: 2007-02-21 05:18:24 -0500 (Wed, 21 Feb 2007) $ $Id: ContainUtil.java 509959 2007-02-21 10:18:24Z rgielen $

Fields Summary
Constructors Summary
Methods Summary
public static booleancontains(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)

param
obj1
param
obj2
return

        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;