001: /*
002: * $Id: ContainUtil.java 509959 2007-02-21 10:18:24Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.util;
022:
023: import java.lang.reflect.Array;
024: import java.util.Collection;
025: import java.util.Map;
026:
027: /**
028: * <code>ContainUtil</code> will check if object 1 contains object 2.
029: * Object 1 may be an Object, array, Collection, or a Map
030: *
031: * @version $Date: 2007-02-21 05:18:24 -0500 (Wed, 21 Feb 2007) $ $Id: ContainUtil.java 509959 2007-02-21 10:18:24Z rgielen $
032: */
033: public class ContainUtil {
034:
035: /**
036: * Determine if <code>obj2</code> exists in <code>obj1</code>.
037: *
038: * <table borer="1">
039: * <tr>
040: * <td>Type Of obj1</td>
041: * <td>Comparison type</td>
042: * </tr>
043: * <tr>
044: * <td>null<td>
045: * <td>always return false</td>
046: * </tr>
047: * <tr>
048: * <td>Map</td>
049: * <td>Map containsKey(obj2)</td>
050: * </tr>
051: * <tr>
052: * <td>Collection</td>
053: * <td>Collection contains(obj2)</td>
054: * </tr>
055: * <tr>
056: * <td>Array</td>
057: * <td>there's an array element (e) where e.equals(obj2)</td>
058: * </tr>
059: * <tr>
060: * <td>Object</td>
061: * <td>obj1.equals(obj2)</td>
062: * </tr>
063: * </table>
064: *
065: *
066: * @param obj1
067: * @param obj2
068: * @return
069: */
070: public static boolean contains(Object obj1, Object obj2) {
071: if ((obj1 == null) || (obj2 == null)) {
072: //log.debug("obj1 or obj2 are null.");
073: return false;
074: }
075:
076: if (obj1 instanceof Map) {
077: if (((Map) obj1).containsKey(obj2)) {
078: //log.debug("obj1 is a map and contains obj2");
079: return true;
080: }
081: } else if (obj1 instanceof Collection) {
082: if (((Collection) obj1).contains(obj2)
083: || ((Collection) obj1).contains(obj2.toString())) {
084: //log.debug("obj1 is a collection and contains obj2");
085: return true;
086: }
087: } else if (obj1.getClass().isArray()) {
088: for (int i = 0; i < Array.getLength(obj1); i++) {
089: Object value = null;
090: value = Array.get(obj1, i);
091:
092: if (value.equals(obj2)) {
093: //log.debug("obj1 is an array and contains obj2");
094: return true;
095: }
096: }
097: } else if (obj1.toString().equals(obj2.toString())) {
098: //log.debug("obj1 is an object and it's String representation equals obj2's String representation.");
099: return true;
100: } else if (obj1.equals(obj2)) {
101: //log.debug("obj1 is an object and equals obj2");
102: return true;
103: }
104:
105: //log.debug("obj1 does not contain obj2: " + obj1 + ", " + obj2);
106: return false;
107: }
108: }
|