Example usage for java.lang Object equals

List of usage examples for java.lang Object equals

Introduction

In this page you can find the example usage for java.lang Object equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:ArrayUtil.java

/**
 * Search for an object in an array./* w ww  . j  a  v a 2 s. c o m*/
 * 
 * @param target
 *        The target array
 * @param search
 *        The object to search for.
 * @return true if search is "in" (equal to any object in) the target, false if not
 */
public static boolean contains(Object[] target, Object search) {
    if ((target == null) || (search == null))
        return false;
    if (target.length == 0)
        return false;
    try {
        for (int i = 0; i < target.length; i++) {
            if (search.equals(target[i]))
                return true;
        }
    } catch (Throwable e) {
        return false;
    }

    return false;
}

From source file:Main.java

public static String nullConvert(Object src) {
    //if (src != null && src.getClass().getName().equals("java.math.BigDecimal")) {
    if (src != null && src instanceof java.math.BigDecimal) {
        return ((BigDecimal) src).toString();
    }/*from   ww  w. ja v a  2 s.co  m*/

    if (src == null || src.equals("null")) {
        return "";
    } else {
        return ((String) src).trim();
    }
}

From source file:com.android.loganalysis.item.GenericItem.java

/**
 * Helper method to return if two objects are equal.
 *
 * @param object1 The first object/*w  w  w.  j  av  a  2s  .  c  o  m*/
 * @param object2 The second object
 * @return True if object1 and object2 are both null or if object1 is equal to object2, false
 * otherwise.
 */
static protected boolean areEqual(Object object1, Object object2) {
    return object1 == null ? object2 == null : object1.equals(object2);
}

From source file:ArrayUtils.java

/**
 * The array may contain nulls, but <TT>o</TT> must be non-null.
 *///  w w w .j  a v  a2  s .  c o m
public static int indexOf(Object[] array, Object o) {
    for (int i = 0, len = array.length; i < len; ++i)
        if (o.equals(array[i]))
            return i;
    return -1;
}

From source file:com.opensymphony.webwork.util.ContainUtil.java

/**
       * Determine if <code>obj2</code> exists in <code>obj1</code>.
       *//  www . j a v  a2s .  com
       * <table borer="1">
       *  <tr>
       *      <td>Type Of obj1</td>
       *      <td>Comparison type</td>
       *  </tr>
       *  <tr>
       *      <td>null<td>
       *      <td>always return false</td>
       *  </tr>
       *  <tr>
       *      <td>Map</td>
       *      <td>Map containsKey(obj2)</td>
       *  </tr>
       *  <tr>
       *      <td>Collection</td>
       *      <td>Collection contains(obj2)</td>
       *  </tr>
       *  <tr>
       *      <td>Array</td>
       *      <td>there's an array element (e) where e.equals(obj2)</td>
       *  </tr>
       *  <tr>
       *      <td>Object</td>
       *      <td>obj1.equals(obj2)</td>
       *  </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;
            }
        } else if (obj1 instanceof Collection) {
            if (((Collection) obj1).contains(obj2)) {
                //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.equals(obj2)) {
            //log.debug("obj1 is an object and equals obj2");
            return true;
        }

        //log.debug("obj1 does not contain obj2: " + obj1 + ", " + obj2);
        return false;
    }

From source file:eionet.web.util.JstlFunctions.java

/**
 * Returns true if provided array or collection contains the specified element.
 *
 * @param arrayOrCollection array or collection holding list of elements
 * @param object element whose presence in this array is to be tested
 * @return true if this array contains the specified element
 *//*from   ww w  .j  a v a  2s .c o  m*/
public static boolean contains(Object arrayOrCollection, Object object) {

    if (arrayOrCollection != null) {

        if (arrayOrCollection instanceof Object[]) {
            for (Object o : ((Object[]) arrayOrCollection)) {
                if (o.equals(object)) {
                    return true;
                }
            }
        } else if (arrayOrCollection instanceof Collection) {
            for (Object o : ((Collection<?>) arrayOrCollection)) {
                if (o.equals(object)) {
                    return true;
                }
            }
        }
    }

    return false;
}

From source file:com.android.loganalysis.item.GenericItem.java

/**
 * Helper method to return if two objects are consistent.
 *
 * @param object1 The first object// w  w  w  . j av  a  2  s . c  om
 * @param object2 The second object
 * @return True if either object1 or object2 is null or if object1 is equal to object2, false if
 * both objects are not null and not equal.
 */
static protected boolean areConsistent(Object object1, Object object2) {
    return object1 == null || object2 == null ? true : object1.equals(object2);
}

From source file:Main.java

public static int indexOf(Object o, ArrayList<Object> elementData) {
    if (o == null) {
        for (int i = 0; i < elementData.size(); i++)
            if (elementData.get(i) == null)
                return i;
    } else {/*from www .jav a2s  .c  o  m*/
        for (int i = 0; i < elementData.size(); i++)
            if (o.equals(elementData.get(i)))
                return i;
    }
    return -1;
}

From source file:Main.java

/**
 * Compares the two specified maps for equality.  Returns
 * <tt>true</tt> if the two maps represent the same mappings.  More formally, two maps <tt>m1</tt> and
 * <tt>m2</tt> represent the same mappings if
 * <tt>m1.keySet().equals(m2.keySet())</tt> and for every key <tt>k</tt>
 * in <tt>m1.keySet()</tt>, <tt> (m1.get(k)==null ? m2.get(k)==null :
 * m1.get(k).equals(m2.get(k))) </tt>.
 * <p/>/*ww w  .j a  v a  2s . c om*/
 * This implementation first checks if the <tt>m1</tt> and <tt>m2</tt> are the same object;
 * if so it returns <tt>true</tt>.  Then, it checks if the two maps have the same sizw; if
 * not, it returns <tt>false</tt>.  If so, it iterates over <tt>m1</tt>'s
 * <tt>entrySet</tt> collection, and checks that map <tt>m1</tt>
 * contains each mapping that map <tt>m2</tt> contains.  If map <tt>m1</tt>
 * fails to contain such a mapping, <tt>false</tt> is returned.  If the
 * iteration completes, <tt>true</tt> is returned.
 *
 * @return <tt>true</tt> if the specified object is equal to this map.
 */
public static boolean equals(Map m1, Map m2) {
    if (m2 == m1)
        return true;
    if (m1 == null)
        return false;
    if (m2 == null)
        return false;
    if (m2.size() != m1.size())
        return false;

    try {
        for (Iterator it = m1.entrySet().iterator(); it.hasNext();) {
            Map.Entry e = (Map.Entry) it.next();
            Object key = e.getKey();
            Object value = e.getValue();
            if (value == null) {
                if (!(m2.get(key) == null && m2.containsKey(key)))
                    return false;
            } else {
                if (!value.equals(m2.get(key)))
                    return false;
            }
        }
    } catch (ClassCastException unused) {
        return false;
    } catch (NullPointerException unused) {
        return false;
    }
    return true;
}

From source file:de.ncoder.sensorsystem.android.logging.JSONUtils.java

public static Object wrap(Object o) {
    if (o == null) {
        return JSONObject.NULL;
    } else if (o instanceof JSONArray || o instanceof JSONObject) {
        return o;
    } else if (o.equals(JSONObject.NULL)) {
        return o;
    } else if (o instanceof Boolean || o instanceof Byte || o instanceof Character || o instanceof Double
            || o instanceof Float || o instanceof Integer || o instanceof Long || o instanceof Short) {
        return o;
    } else if (o instanceof CharSequence) {
        return JSONObject.quote(o.toString());
    } else if (o instanceof Collection) {
        return new JSONArray((Collection) o);
    } else if (o.getClass().isArray()) {
        final int length = Array.getLength(o);
        List<Object> values = new ArrayList<>(length);
        for (int i = 0; i < length; ++i) {
            values.add(wrap(Array.get(o, i)));
        }// ww  w.  ja  va2  s. c o m
        return new JSONArray(values);
    } else if (o instanceof Map) {
        return new JSONObject((Map) o);
    } else if (o instanceof Location) {
        return wrapLocation((Location) o);
    } else if (o instanceof Bundle) {
        return wrapBundle((Bundle) o);
    }
    return o.toString();
}