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:com.cenrise.test.azkaban.Utils.java

/**
 * Equivalent to Object.equals except that it handles nulls. If a and b are both null, true is
 * returned.//from w  w w .j a  va  2  s  .  co m
 */
public static boolean equals(final Object a, final Object b) {
    if (a == null || b == null) {
        return a == b;
    }

    return a.equals(b);
}

From source file:com.redhat.rhn.testing.TestUtils.java

/**
 * Search an array by calling the passsed in method name with the key as the checker
 * @param search array//from   w ww  .  ja va2s  .  c  om
 * @param methodName to call on each object in the array (can be toString())
 * @param key to compare to
 * @return boolean if found or not
 */
public static boolean arraySearch(Object[] search, String methodName, Object key) {
    boolean found = false;
    for (int i = 0; i < search.length; i++) {
        Object value = MethodUtil.callMethod(search[i], methodName, new Object[0]);
        if (value.equals(key)) {
            found = true;
        }
    }
    return found;
}

From source file:gda.util.userOptions.UserOptions.java

public static UserOptions createFromTemplate(String configDir, String configName)
        throws ConfigurationException, IOException, Exception {
    FileConfiguration config = LocalParameters.getXMLConfiguration(configDir, configName, false, true);
    UserOptions options = new UserOptions();
    options.title = config.getString(propTitle);
    options.containsDefault = true;/*from  ww  w. j  a va 2  s .  c o  m*/
    Integer index = 0;
    while (true) {
        String tag = "options.option(" + index + ").";
        Object description = null;
        try {
            String keyName = config.getString(tag + propKeyName);
            /*
             * stop on last key
             */
            if (keyName == null)
                break;
            description = config.getProperty(tag + propDesc);
            Object type = config.getProperty(tag + propType);
            UserOption<? extends Object, ? extends Object> option;
            if (type != null && type.equals(typeBoolean)) {
                option = new UserOption<Object, Boolean>(description, config.getBoolean(tag + propDefValue));
            } else if (type != null && type.equals(typeString)) {
                option = new UserOption<Object, String>(description, config.getString(tag + propDefValue));
            } else if (type != null && type.equals(typeDouble)) {
                option = new UserOption<Object, Double>(description, config.getDouble(tag + propDefValue));
            } else if (type != null && type.equals(typeInteger)) {
                option = new UserOption<Object, Integer>(description, config.getInt(tag + propDefValue));
            } else {
                option = new UserOption<Object, Object>(description, config.getProperty(tag + propDefValue));
            }
            options.put(keyName, option);
            index++;
        } catch (Exception ex) {
            throw new Exception("Error reading option " + index, ex);
        }
    }
    return options;
}

From source file:com.google.android.apps.dashclock.api.ExtensionData.java

private static boolean objectEquals(Object x, Object y) {
    if (x == null || y == null) {
        return x == y;
    } else {/*  ww  w  . jav a  2  s. co m*/
        return x.equals(y);
    }
}

From source file:RandomChooser.java

public static void equals(Object value, Object expected) {
    if (value == expected || value.equals(expected))
        return;//from w ww  .  j  a  v  a2 s  .c  o m
    throw new IllegalArgumentException(value + EQUALS + expected);
}

From source file:com.ms.commons.test.treedb.JsonObjectUtils.java

public static boolean isSameObject(Object obj0, Object obj1, String[] fields, boolean exclusive) {
    if (obj0 == null && obj1 == null) {
        return true;
    }/*from  w  w  w . j  a  v  a 2s.co m*/
    if (obj0 == null || obj1 == null || !obj0.getClass().equals(obj1.getClass())) {
        return false;
    }
    if (JSONUtils.isNumber(obj0) || JSONUtils.isBoolean(obj0) || JSONUtils.isString(obj0)) {
        return obj0.equals(obj1);
    }
    String jsons0 = null;
    String jsons1 = null;
    if (obj0 instanceof Enum || JSONUtils.isArray(obj0)) {
        jsons0 = JSONArray.fromObject(obj0, defaultJsonConfig).toString();
        jsons1 = JSONArray.fromObject(obj1, defaultJsonConfig).toString();
    } else {
        jsons0 = JSONObject.fromObject(obj0, defaultJsonConfig).toString();
        jsons1 = JSONObject.fromObject(obj1, defaultJsonConfig).toString();
    }
    if (log.isDebugEnabled()) {
        log.debug("Raw result0 : " + jsons0);
        log.debug("Raw result1 : " + jsons1);
    }
    System.out.println("Raw result0 : " + jsons0);
    System.out.println("Raw result1 : " + jsons1);
    if (fields == null || fields.length == 0) {
        if (exclusive) {
            return jsons0.equals(jsons1);
        } else {
            return true;
        }
    }
    List<Node> valuelist0 = JsonParser.filterFields(jsons0);
    List<Node> valuelist1 = JsonParser.filterFields(jsons1);

    List<Node> list0 = new ArrayList<Node>();
    List<Node> list1 = new ArrayList<Node>();
    for (String str : fields) {
        String[] strs = str.split("\\.");
        if (strs == null || strs.length == 0) {
            continue;
        }
        list0.addAll(findFieldsStr(strs, valuelist0, jsons0));
        list1.addAll(findFieldsStr(strs, valuelist1, jsons1));
    }
    if (exclusive) {
        return isSameExclusiveObjs(list0, list1, jsons0, jsons1);
    } else {
        return isSameInclusiveObjs(list0, list1, jsons0, jsons1);
    }
}

From source file:com.github.nlloyd.hornofmongo.util.BSONizer.java

public static Object convertJStoBSON(Object jsObject, boolean isJsObj, String dateFormat) {
    Object bsonObject = null;/*from   ww  w . j a v  a  2  s .c  o  m*/
    if (jsObject instanceof NativeArray) {
        NativeArray jsArray = (NativeArray) jsObject;
        List<Object> bsonArray = new ArrayList<Object>(Long.valueOf(jsArray.getLength()).intValue());
        for (Object jsEntry : jsArray) {
            bsonArray.add(convertJStoBSON(jsEntry, isJsObj, dateFormat));
        }
        bsonObject = bsonArray;
    } else if (jsObject instanceof NativeRegExp) {
        Object source = ScriptableObject.getProperty((Scriptable) jsObject, "source");
        String fullRegex = (String) Context.jsToJava(jsObject, String.class);
        String options = fullRegex.substring(fullRegex.lastIndexOf("/") + 1);

        bsonObject = Pattern.compile(source.toString(), Bytes.regexFlags(options));
        ;
    } else if (jsObject instanceof NativeObject) {
        BasicDBObject bson = new BasicDBObject();
        bsonObject = bson;

        NativeObject rawJsObject = (NativeObject) jsObject;
        for (Object key : rawJsObject.keySet()) {
            Object value = extractJSProperty(rawJsObject, key);

            //GC: 17/11/15 allow for UTC $date object
            if (key.equals("$date")) {
                try {
                    bsonObject = DateUtils.parseDate(value.toString(),
                            new String[] { "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" });
                } catch (java.text.ParseException e) {
                    bson.put(key.toString(), convertJStoBSON(value, isJsObj, dateFormat));
                }
            } else {
                bson.put(key.toString(), convertJStoBSON(value, isJsObj, dateFormat));
            }
        }
    } else if (jsObject instanceof ScriptableMongoObject) {
        bsonObject = convertScriptableMongoToBSON((ScriptableMongoObject) jsObject, isJsObj, dateFormat);
    } else if (jsObject instanceof BaseFunction) {
        BaseFunction funcObject = (BaseFunction) jsObject;
        Object classPrototype = ScriptableObject.getClassPrototype(funcObject, funcObject.getFunctionName());
        if ((classPrototype instanceof MinKey) || (classPrototype instanceof MaxKey)) {
            // this is a special case handler for instances where MinKey or
            // MaxKey are provided without explicit constructor calls
            // index_check3.js does this
            bsonObject = convertScriptableMongoToBSON((ScriptableMongoObject) classPrototype, isJsObj,
                    dateFormat);
        } else {
            // comes from eval calls
            String decompiledCode = (String) MongoRuntime.call(new JSDecompileAction(funcObject));
            bsonObject = new Code(decompiledCode);
        }
    } else if (jsObject instanceof ScriptableObject) {
        // we found a ScriptableObject that isn't any of the concrete
        // ScriptableObjects above...
        String jsClassName = ((ScriptableObject) jsObject).getClassName();
        if ("Date".equals(jsClassName)) {
            Date dt = (Date) Context.jsToJava(jsObject, Date.class);
            bsonObject = dt;
            //GC: 18/11/15 use dateFormat parameter to format date fields
            if (dateFormat != null && dateFormat.length() > 0)
                bsonObject = DateFormatUtils.formatUTC(dt, dateFormat);
        } else {
            Context.throwAsScriptRuntimeEx(
                    new MongoScopeException("bsonizer couldnt convert js class: " + jsClassName));
            bsonObject = jsObject;
        }
    } else if (jsObject instanceof ConsString) {
        bsonObject = jsObject.toString();
    } else if (jsObject instanceof Undefined) {
        bsonObject = jsObject;
    } else if (jsObject instanceof Integer) {
        // this may seem strange, but JavaScript only knows about the number
        // type
        // which means in the official client we need to pass a Double
        // this applies to Long and Integer values
        bsonObject = Double.valueOf((Integer) jsObject);
    } else if (jsObject instanceof Long) {
        bsonObject = Double.valueOf((Long) jsObject);
    } else {
        bsonObject = jsObject;
    }

    return bsonObject;
}

From source file:com.ctriposs.rest4j.common.testutils.DataAssert.java

/**
 * Asserts that two {@link DataMap}s are equal, subject to the {@code excludedProperties} and
 * {@code nullShouldEqualEmptyListOrMap} arguments.
 *
 * @param actualMap the {@link DataMap} we are checking
 * @param expectedMap the expected {@link DataMap}
 * @param excludedProperties the properties that will be ignored while checking the two DataMaps
 * @param nullShouldEqualEmptyListOrMap true if null should equal an empty {@link DataMap} or {@link DataList}
 *//*w  ww .j  av a  2 s . co m*/
public static void assertDataMapsEqual(DataMap actualMap, DataMap expectedMap, Set<String> excludedProperties,
        boolean nullShouldEqualEmptyListOrMap) {
    if (excludedProperties == null) {
        excludedProperties = Collections.emptySet();
    }

    if (actualMap == null || expectedMap == null) {
        Assert.assertEquals(actualMap, expectedMap, "Only one of the data maps is null!");
        return;
    }

    Set<String> failKeys = new HashSet<String>();

    // Assert key by key so it's easy to debug on assertion failure
    Set<String> allKeys = new HashSet<String>(actualMap.keySet());
    allKeys.addAll(expectedMap.keySet());
    for (String key : allKeys) {
        if (excludedProperties.contains(key)) {
            continue;
        }

        Object actualObject = actualMap.get(key);
        Object expectedObject = expectedMap.get(key);
        if (actualObject == null) {
            if (nullShouldEqualEmptyListOrMap && isEmptyListOrMap(expectedObject)) {
                continue;
            }
            if (expectedObject != null) {
                failKeys.add(key);
            }
        } else if (!actualObject.equals(expectedObject)) {
            if (nullShouldEqualEmptyListOrMap && expectedObject == null && isEmptyListOrMap(actualObject)) {
                continue;
            }
            failKeys.add(key);
        }
    }

    if (!failKeys.isEmpty()) {
        List<String> errorMessages = new ArrayList<String>();
        errorMessages.add(failKeys.size() + " properties don't match:");
        for (String k : failKeys) {
            errorMessages.add("\tMismatch on property \"" + k + "\", expected:<" + expectedMap.get(k)
                    + "> but was:<" + actualMap.get(k) + ">");
        }
        Assert.fail(StringUtils.join(errorMessages, ERROR_MESSAGE_SEPARATOR));
    }
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static void firePropertyChangeListeners(Object bean, String propertyName, BeanMonitor monitor) {
    if (bean.equals(monitor.getBean())) {
        firePropertyChangeListeners(bean, propertyName, monitor.getPropertyChangeListeners());
    }/*w  w w. j  a  va 2 s.c o m*/
}

From source file:com.plusub.lib.annotate.JsonParserUtils.java

/**
 * ??/*from ww  w .  j a  v  a2s  . c  om*/
 * <p>Title: parserField
 * <p>Description: 
 * @param obj
 * @param jsonObj
 * @return
 * @throws Exception
 */
private static Object parserField(Object obj, JSONObject jsonObj) throws Exception {
    Field[] fields = obj.getClass().getDeclaredFields();
    if (fields != null && fields.length > 0) {
        for (Field field : fields) {
            JsonParserField jsonField = field.getAnnotation(JsonParserField.class);

            //JsonParserField
            if (jsonField != null) {
                String keyName = jsonField.praserKey(); //json
                String defaultValue = jsonField.defaultValue(); //
                boolean isList = jsonField.isList();

                //???
                String name = "";
                if (isEmpty(keyName)) {
                    name = field.getName();
                } else {
                    name = keyName;
                }

                try {
                    field.setAccessible(true);
                    //?
                    if (jsonObj.has(name)) {
                        if (!isList) { //?
                            setSingleValue(obj, field, jsonObj, name);
                        } else { //
                            Class listCls = jsonField.classType();
                            if (listCls.equals(Object.class)) {
                                if (showLog)
                                    Logger.e(TAG, "" + field.getName()
                                            + "classType?" + keyName);
                            }
                            JSONArray ja = null;
                            if (isEmpty(keyName)) {
                                ja = JSONUtils.getJSONArray(jsonObj, field.getName(), null);
                            } else {
                                ja = JSONUtils.getJSONArray(jsonObj, keyName, null);
                            }
                            if (ja != null) {
                                //
                                int size = ja.length();
                                Object[] data = new Object[size];

                                //?
                                for (int index = 0; index < size; index++) {
                                    Object oj = parserField(getInstance(listCls.getName()),
                                            ja.getJSONObject(index));
                                    data[index] = oj;
                                }

                                //?
                                Object listObj = field.getType();
                                if (listObj.equals(List.class)) {
                                    setFieldValue(obj, field, Arrays.asList(data));
                                } else {
                                    setFieldValue(obj, field, data);
                                }
                            }
                        }
                    } else {
                        setFieldValue(obj, field, defaultValue);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(
                            "parser " + obj.getClass().getName() + " error! \n" + e.getMessage());
                }
            } else { //JsonParserField
                setSingleValue(obj, field, jsonObj, field.getName());
            }
        }
    }
    return obj;
}