View Javadoc

1   /*
2    * $Id: ContainUtil.java 802937 2009-08-10 21:57:37Z musachy $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.util;
23  
24  import java.lang.reflect.Array;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  
29  /***
30   * <code>ContainUtil</code> will check if object 1 contains object 2.
31   * Object 1 may be an Object, array, Collection, or a Map
32   *
33   * @version $Date: 2009-08-10 17:57:37 -0400 (Mon, 10 Aug 2009) $ $Id: ContainUtil.java 802937 2009-08-10 21:57:37Z musachy $
34   */
35  public class ContainUtil {
36  
37      /***
38       * Determine if <code>obj2</code> exists in <code>obj1</code>.
39       *
40       * <table borer="1">
41       *  <tr>
42       *      <td>Type Of obj1</td>
43       *      <td>Comparison type</td>
44       *  </tr>
45       *  <tr>
46       *      <td>null<td>
47       *      <td>always return false</td>
48       *  </tr>
49       *  <tr>
50       *      <td>Map</td>
51       *      <td>Map containsKey(obj2)</td>
52       *  </tr>
53       *  <tr>
54       *      <td>Collection</td>
55       *      <td>Collection contains(obj2)</td>
56       *  </tr>
57       *  <tr>
58       *      <td>Array</td>
59       *      <td>there's an array element (e) where e.equals(obj2)</td>
60       *  </tr>
61       *  <tr>
62       *      <td>Object</td>
63       *      <td>obj1.equals(obj2)</td>
64       *  </tr>
65       * </table>
66       *
67       *
68       * @param obj1
69       * @param obj2
70       * @return
71       */
72      public static boolean contains(Object obj1, Object obj2) {
73          if ((obj1 == null) || (obj2 == null)) {
74              //log.debug("obj1 or obj2 are null.");
75              return false;
76          }
77  
78          if (obj1 instanceof Map) {
79              if (((Map) obj1).containsKey(obj2)) {
80                  //log.debug("obj1 is a map and contains obj2");
81                  return true;
82              }
83          } if (obj1 instanceof Iterable) {
84          	Iterator iter = ((Iterable) obj1).iterator();
85          	while(iter.hasNext()) {
86          		Object value = iter.next();
87          		if (obj2.equals(value) || obj2.toString().equals(value)) {
88          			return true;
89          		}
90          	}
91          } else if (obj1.getClass().isArray()) {
92              for (int i = 0; i < Array.getLength(obj1); i++) {
93                  Object value = null;
94                  value = Array.get(obj1, i);
95  
96                  if (obj2.equals(value)) {
97                      //log.debug("obj1 is an array and contains obj2");
98                      return true;
99                  }
100             }
101         } else if (obj1.toString().equals(obj2.toString())) {
102             //log.debug("obj1 is an object and it's String representation equals obj2's String representation.");
103             return true;
104         } else if (obj1.equals(obj2)) {
105             //log.debug("obj1 is an object and equals obj2");
106             return true;
107         }
108 
109         //log.debug("obj1 does not contain obj2: " + obj1 + ", " + obj2);
110         return false;
111     }
112 }