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:org.apache.brooklyn.rest.resources.ApplicationResourceTest.java

private static Predicate<? super Map<?, ?>> withValueForKey(final Object key, final Object value) {
    return new Predicate<Object>() {
        public boolean apply(Object input) {
            if (!(input instanceof Map))
                return false;
            return value.equals(((Map<?, ?>) input).get(key));
        }//from w  ww . j  a  v  a  2s.  co m
    };
}

From source file:gov.nasa.ensemble.common.CommonUtils.java

/**
 * Returns true if both objects are null, or if they are equals to each other.
 * /*from  w w w.  java2 s  .c o m*/
 * @param object1
 * @param object2
 * @return true if both objects are null, or if they are equals to each other.
 */
public static final boolean equals(Object object1, Object object2) {
    if (object1 == object2) {
        return true;
    }
    if (object1 == null) {
        return false;
    }
    return object1.equals(object2);
}

From source file:lichen.orm.LichenOrmModule.java

public static DataSource buildDataSource(@Symbol(LichenOrmSymbols.DATABASE_CFG_FILE) String dbCfgFile,
        RegistryShutdownHub shutdownHub) throws IOException, ProxoolException {
    ResourceLoader resourceLoader = new DefaultResourceLoader();
    Resource resource = resourceLoader.getResource(dbCfgFile);
    Properties info = new Properties();
    info.load(resource.getInputStream());
    PropertyConfigurator.configure(info);

    String poolNameKey = F.flow(info.keySet()).filter(new Predicate<Object>() {
        public boolean accept(Object element) {
            return element.toString().contains("alias");
        }/* ww  w.j a  va2s.c  o  m*/
    }).first().toString();

    if (poolNameKey == null) {
        throw new RuntimeException("?poolName");
    }
    final String poolName = info.getProperty(poolNameKey);

    //new datasource
    ProxoolDataSource ds = new ProxoolDataSource(poolName);
    //register to shutdown
    shutdownHub.addRegistryShutdownListener(new RegistryShutdownListener() {
        public void registryDidShutdown() {
            Flow<?> flow = F.flow(ProxoolFacade.getAliases()).filter(new Predicate<String>() {
                public boolean accept(String element) {
                    return element.equals(poolName);
                }
            });
            if (flow.count() == 1) {
                try {
                    ProxoolFacade.removeConnectionPool(poolName);
                } catch (ProxoolException e) {
                    //do nothing
                }
            }
        }
    });
    return ds;
}

From source file:net.sensemaker.snappy.SnappyUtil.java

public static boolean jsonEquals(JSONObject a, JSONObject b) {
    Iterator iter = a.sortedKeys();
    while (iter.hasNext()) {
        String key = (String) iter.next();
        Object valueA = a.get(key);
        if (!b.has(key))
            return false;
        Object valueB = b.get(key);
        if (valueB == null)
            return false;
        if (!valueA.getClass().getName().equals(valueB.getClass().getName()))
            return false;
        if (valueA instanceof JSONObject) {
            if (!jsonEquals((JSONObject) valueA, (JSONObject) valueB))
                return false;
        }/* w ww .  j a  v a 2s .com*/
        if (valueA instanceof JSONArray) {
            if (!jsonEquals((JSONArray) valueA, (JSONArray) valueB))
                return false;
        }
        if (!valueA.equals(valueB))
            return false;
    }
    return true;
}

From source file:Main.java

/**
 * Compares two {@link android.os.Bundle Bundles} to check for equality.
 *
 * @param a the first {@link android.os.Bundle Bundle}
 * @param b the second {@link android.os.Bundle Bundle}
 * @return {@code true} if the two {@link android.os.Bundle Bundles} have identical contents, or are both null
 */// w w w. j ava 2s  .c o  m
public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
    // equal if both null
    if (a == null && b == null)
        return true;

    // unequal if one is null
    if (a == null || b == null)
        return false;

    // check size
    if (a.size() != b.size())
        return false;

    // get key sets
    Set<String> bundleAKeys = a.keySet();
    Object valueA;
    Object valueB;

    // loop keys
    for (String key : bundleAKeys) {
        // check key exists in second bundle
        if (!b.containsKey(key))
            return false;

        // get values
        valueA = a.get(key);
        valueB = b.get(key);

        // check null valued entries
        if (valueA == null && valueB == null)
            continue;
        if (valueA == null || valueB == null)
            return false;

        // compare iteratively if they are both bundles
        if (valueA instanceof Bundle && valueB instanceof Bundle) {
            if (!areEqual((Bundle) valueA, (Bundle) valueB))
                return false;
            continue;
        }

        // check for different values
        if (!valueA.equals(valueB))
            return false;
    }

    // passed!
    return true;
}

From source file:MultiMap.java

public static Object remove(Object list, Object o) {
    if (list == null)
        return null;

    if (list instanceof List) {
        List l = (List) list;
        l.remove(o);//from ww w  .  j  av  a 2s. c  om
        if (l.size() == 0)
            return null;
        return list;
    }

    if (list.equals(o))
        return null;
    return list;
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

private static boolean areEqual(Object o1, Object o2) {
    if (o1 != null) {
        return o1.equals(o2);
    } else if (o2 != null) {
        return o2.equals(o1);
    } else {// w  w w .j a v  a2  s .  co m
        // o1 and o2 are both null
        return true;
    }
}

From source file:com.ponysdk.impl.query.memory.FilteringTools.java

public static <T> List<T> filter(final List<T> datas, final String fieldKey, final Object value) {
    if (value == null || datas == null || fieldKey.equals(EMPTY)) {
        return datas;
    }//  w  ww .j a  v a2 s  . co  m
    final List<T> validData = new ArrayList<>();
    try {
        final String[] pathDetails = fieldKey.split(DOT_REGEX);
        for (final T data : datas) {
            final Object val = getValue(data, pathDetails);
            if (val == null)
                continue;
            if (value.equals(val)) {
                validData.add(data);
                continue;
            }
            // Now we can filter our data against the pattern
            final String text = normalisePattern(value.toString().trim());
            final Pattern pattern = Pattern.compile(REGEX_BEGIN + text + REGEX_END, Pattern.CASE_INSENSITIVE);
            Matcher matcher;
            if (val instanceof Collection<?>) {
                final Collection<?> collection = (Collection<?>) val;
                for (final Object item : collection) {
                    matcher = pattern.matcher(item.toString());
                    if (matcher.find()) {
                        validData.add(data);
                        break;
                    }
                }
                if (collection.isEmpty()) {
                    matcher = pattern.matcher("");
                    if (matcher.find()) {
                        validData.add(data);
                    }
                }
            } else if (val.toString() != null) {
                matcher = pattern.matcher(val.toString());
                if (matcher.find()) {
                    validData.add(data);
                } else {
                    matcher = pattern.matcher("");
                    if (matcher.find()) {
                        validData.add(data);
                    }
                }
            }
        }
    } catch (final PatternSyntaxException e) {
        if (log.isDebugEnabled()) {
            log.debug("bad pattern : " + value);
        }
    } catch (final Exception e) {
        log.error("Filter Error => pattern : " + value + " , property : " + fieldKey, e);
    }
    return validData;
}

From source file:edu.sdsc.scigraph.neo4j.GraphUtil.java

static Object getNewPropertyValue(Object originalValue, Object newValue) {
    Class<?> clazz = checkNotNull(newValue).getClass();
    boolean reduceToString = false;
    if (null != originalValue && originalValue.getClass().isArray()) {
        Class<?> originalClazz = Array.get(originalValue, 0).getClass();
        if (!originalClazz.equals(clazz)) {
            reduceToString = true;//  w  w w.ja  va2s .  c  om
            clazz = String.class;
        }
        newValue = reduceToString ? newValue.toString() : newValue;
        Object newArray = Array.newInstance(clazz, Array.getLength(originalValue) + 1);
        for (int i = 0; i < Array.getLength(originalValue); i++) {
            Object val = Array.get(originalValue, i);
            if (newValue.equals(val)) {
                return originalValue;
            }
            Array.set(newArray, i, reduceToString ? val.toString() : val);
        }
        Array.set(newArray, Array.getLength(originalValue), newValue);
        return newArray;
    } else if (null != originalValue) {
        if (!clazz.equals(originalValue.getClass())) {
            reduceToString = true;
            clazz = String.class;
        }
        originalValue = reduceToString ? originalValue.toString() : originalValue;
        newValue = reduceToString ? newValue.toString() : newValue;
        if (!originalValue.equals(newValue)) {
            Object newArray = Array.newInstance(clazz, 2);
            Array.set(newArray, 0, originalValue);
            Array.set(newArray, 1, newValue);
            return newArray;
        } else {
            return originalValue;
        }
    } else {
        return newValue;
    }
}

From source file:com.ocs.dynamo.domain.model.util.EntityModelUtil.java

/**
 * Compares two entities based on the entity model and reports a list of the differences
 * /*  w  w w . j  a  va  2s .c om*/
 * @param oldEntity
 *            the old entity
 * @param newEntity
 *            the new entity
 * @param model
 *            the entity model
 * @param entityModelFactory
 * @param messageService
 *            the message service
 * @param ignore
 *            the names of the fields to ignore
 */
public static List<String> compare(Object oldEntity, Object newEntity, EntityModel<?> model,
        EntityModelFactory entityModelFactory, MessageService messageService, String... ignore) {
    List<String> results = new ArrayList<>();

    Set<String> toIgnore = new HashSet<>();
    if (ignore != null) {
        toIgnore = Sets.newHashSet(ignore);
    }
    toIgnore.addAll(ALWAYS_IGNORE);

    String noValue = messageService.getMessage("ocs.no.value");

    for (AttributeModel am : model.getAttributeModels()) {
        if ((AttributeType.BASIC.equals(am.getAttributeType())
                || AttributeType.MASTER.equals(am.getAttributeType())) && !toIgnore.contains(am.getName())) {

            Object oldValue = ClassUtils.getFieldValue(oldEntity, am.getName());
            Object newValue = ClassUtils.getFieldValue(newEntity, am.getName());

            if (!ObjectUtils.equals(oldValue, newValue)) {
                String oldValueStr = TableUtils.formatPropertyValue(entityModelFactory, model, messageService,
                        am.getName(), oldValue);
                String newValueStr = TableUtils.formatPropertyValue(entityModelFactory, model, messageService,
                        am.getName(), newValue);
                results.add(messageService.getMessage("ocs.value.changed", am.getDisplayName(),
                        oldValue == null ? noValue : oldValueStr, newValue == null ? noValue : newValueStr));
            }
        } else if (AttributeType.DETAIL.equals(am.getAttributeType())) {
            Collection<?> ocol = (Collection<?>) ClassUtils.getFieldValue(oldEntity, am.getName());
            Collection<?> ncol = (Collection<?>) ClassUtils.getFieldValue(newEntity, am.getName());

            for (Object o : ncol) {
                if (!ocol.contains(o)) {
                    results.add(messageService.getMessage("ocs.value.added",
                            getDescription(o, am.getNestedEntityModel()), am.getDisplayName()));
                }
            }

            for (Object o : ocol) {
                if (!ncol.contains(o)) {
                    results.add(messageService.getMessage("ocs.value.removed",
                            getDescription(o, am.getNestedEntityModel()), am.getDisplayName()));
                }
            }

            for (Object o : ocol) {
                for (Object o2 : ncol) {
                    if (o.equals(o2)) {
                        List<String> nested = compare(o, o2, am.getNestedEntityModel(), entityModelFactory,
                                messageService, ignore);
                        results.addAll(nested);
                    }
                }
            }

        }
    }
    return results;
}