alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  
* <td>Comparison type * </tr> * <tr> * <td>null * </tr> * <tr> * <td>Map * <td>Map containsKey(obj2) * </tr> * <tr> * <td>Collection * <td>Collection contains(obj2) * </tr> * <tr> * <td>Array * <td>there's an array element (e) where e.equals(obj2) * </tr> * <tr> * <td>Object * <td>obj1.equals(obj2) * </tr> * </table> * * * @param obj1 * @param obj2 * @return */ public static boolean contains(Object obj1, Object 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; } } if (obj1 instanceof Iterable) { Iterator iter = ((Iterable) obj1).iterator(); while(iter.hasNext()) { Object value = iter.next(); if (obj2.equals(value) || obj2.toString().equals(value)) { 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 (obj2.equals(value)) { //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; } }

Other Struts examples (source code examples)

Here is a short list of links related to this Struts ContainUtil.java source code file:

Struts example source code file (ContainUtil.java)

This example Struts source code file (ContainUtil.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

containutil, iterable, iterable, iterator, iterator, map, map, object, object, reflection, util

The Struts ContainUtil.java source code

/*
 * $Id: ContainUtil.java 802937 2009-08-10 21:57:37Z musachy $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.struts2.util;

import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.Map;


/**
 * <code>ContainUtil will check if object 1 contains object 2.
 * Object 1 may be an Object, array, Collection, or a Map
 *
 * @version $Date: 2009-08-10 23:57:37 +0200 (Mon, 10 Aug 2009) $ $Id: ContainUtil.java 802937 2009-08-10 21:57:37Z musachy $
 */
public class ContainUtil {

    /**
     * Determine if <code>obj2 exists in obj1.
     *
     * <table borer="1">
     *  <tr>
     *      <td>Type Of obj1
* <td>always return false
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.