Example usage for org.springframework.util ObjectUtils nullSafeEquals

List of usage examples for org.springframework.util ObjectUtils nullSafeEquals

Introduction

In this page you can find the example usage for org.springframework.util ObjectUtils nullSafeEquals.

Prototype

public static boolean nullSafeEquals(@Nullable Object o1, @Nullable Object o2) 

Source Link

Document

Determine if the given objects are equal, returning true if both are null or false if only one is null .

Usage

From source file:org.springframework.cloud.iot.integration.coap.support.DefaultCoapHeaderMapper.java

@Override
public Map<String, Object> toHeaders(CoapHeaders coapHeaders) {
    Map<String, Object> target = new HashMap<String, Object>();
    for (Entry<Integer, List<byte[]>> entry : coapHeaders.entrySet()) {
        if (ObjectUtils.nullSafeEquals(entry.getKey(), 9999)) {
            List<byte[]> values = entry.getValue();
            if (values != null && values.size() == 1) {
                target.put("iotGatewayServiceRoute", new String(values.get(0)));
            }//from ww  w .  jav  a2 s  .c  om
        } else {
            target.put(entry.getKey().toString(), entry.getValue());
        }
    }
    return target;
}

From source file:org.springframework.context.annotation.Spr3775InitDestroyLifecycleTests.java

private void assertMethodOrdering(Class<?> clazz, String category, List<String> expectedMethods,
        List<String> actualMethods) {
    debugMethods(clazz, category, actualMethods);
    assertTrue("Verifying " + category + ": expected<" + expectedMethods + "> but got<" + actualMethods + ">.",
            ObjectUtils.nullSafeEquals(expectedMethods, actualMethods));
}

From source file:org.springframework.core.annotation.AnnotationUtils.java

/**
 * Retrieve the given annotation's attributes as an {@link AnnotationAttributes} map.
 * <p>This method provides fully recursive annotation reading capabilities on par with
 * the reflection-based {@link org.springframework.core.type.StandardAnnotationMetadata}.
 * <p><strong>NOTE</strong>: This variant of {@code getAnnotationAttributes()} is
 * only intended for use within the framework. The following special rules apply:
 * <ol>//from w w w  .j  a va 2  s.  com
 * <li>Default values will be replaced with default value placeholders.</li>
 * <li>The resulting, merged annotation attributes should eventually be
 * {@linkplain #postProcessAnnotationAttributes post-processed} in order to
 * ensure that placeholders have been replaced by actual default values and
 * in order to enforce {@code @AliasFor} semantics.</li>
 * </ol>
 * @param annotatedElement the element that is annotated with the supplied annotation;
 * may be {@code null} if unknown
 * @param annotation the annotation to retrieve the attributes for
 * @param classValuesAsString whether to convert Class references into Strings (for
 * compatibility with {@link org.springframework.core.type.AnnotationMetadata})
 * or to preserve them as Class references
 * @param nestedAnnotationsAsMap whether to convert nested annotations into
 * {@link AnnotationAttributes} maps (for compatibility with
 * {@link org.springframework.core.type.AnnotationMetadata}) or to preserve them as
 * {@code Annotation} instances
 * @return the annotation attributes (a specialized Map) with attribute names as keys
 * and corresponding attribute values as values (never {@code null})
 * @since 4.2
 * @see #postProcessAnnotationAttributes
 */
static AnnotationAttributes retrieveAnnotationAttributes(@Nullable Object annotatedElement,
        Annotation annotation, boolean classValuesAsString, boolean nestedAnnotationsAsMap) {

    Class<? extends Annotation> annotationType = annotation.annotationType();
    AnnotationAttributes attributes = new AnnotationAttributes(annotationType);

    for (Method method : getAttributeMethods(annotationType)) {
        try {
            Object attributeValue = method.invoke(annotation);
            Object defaultValue = method.getDefaultValue();
            if (defaultValue != null && ObjectUtils.nullSafeEquals(attributeValue, defaultValue)) {
                attributeValue = new DefaultValueHolder(defaultValue);
            }
            attributes.put(method.getName(),
                    adaptValue(annotatedElement, attributeValue, classValuesAsString, nestedAnnotationsAsMap));
        } catch (Throwable ex) {
            if (ex instanceof InvocationTargetException) {
                Throwable targetException = ((InvocationTargetException) ex).getTargetException();
                rethrowAnnotationConfigurationException(targetException);
            }
            throw new IllegalStateException("Could not obtain annotation attribute value for " + method, ex);
        }
    }

    return attributes;
}

From source file:org.springframework.core.annotation.AnnotationUtils.java

/**
 * Post-process the supplied {@link AnnotationAttributes}.
 * <p>Specifically, this method enforces <em>attribute alias</em> semantics
 * for annotation attributes that are annotated with {@link AliasFor @AliasFor}
 * and replaces default value placeholders with their original default values.
 * @param annotatedElement the element that is annotated with an annotation or
 * annotation hierarchy from which the supplied attributes were created;
 * may be {@code null} if unknown/*from   w ww. j a v a  2s.com*/
 * @param attributes the annotation attributes to post-process
 * @param classValuesAsString whether to convert Class references into Strings (for
 * compatibility with {@link org.springframework.core.type.AnnotationMetadata})
 * or to preserve them as Class references
 * @param nestedAnnotationsAsMap whether to convert nested annotations into
 * {@link AnnotationAttributes} maps (for compatibility with
 * {@link org.springframework.core.type.AnnotationMetadata}) or to preserve them as
 * {@code Annotation} instances
 * @since 4.2
 * @see #retrieveAnnotationAttributes(Object, Annotation, boolean, boolean)
 * @see #getDefaultValue(Class, String)
 */
static void postProcessAnnotationAttributes(@Nullable Object annotatedElement,
        @Nullable AnnotationAttributes attributes, boolean classValuesAsString,
        boolean nestedAnnotationsAsMap) {

    if (attributes == null) {
        return;
    }

    Class<? extends Annotation> annotationType = attributes.annotationType();

    // Track which attribute values have already been replaced so that we can short
    // circuit the search algorithms.
    Set<String> valuesAlreadyReplaced = new HashSet<>();

    if (!attributes.validated) {
        // Validate @AliasFor configuration
        Map<String, List<String>> aliasMap = getAttributeAliasMap(annotationType);
        for (String attributeName : aliasMap.keySet()) {
            if (valuesAlreadyReplaced.contains(attributeName)) {
                continue;
            }
            Object value = attributes.get(attributeName);
            boolean valuePresent = (value != null && !(value instanceof DefaultValueHolder));

            for (String aliasedAttributeName : aliasMap.get(attributeName)) {
                if (valuesAlreadyReplaced.contains(aliasedAttributeName)) {
                    continue;
                }

                Object aliasedValue = attributes.get(aliasedAttributeName);
                boolean aliasPresent = (aliasedValue != null && !(aliasedValue instanceof DefaultValueHolder));

                // Something to validate or replace with an alias?
                if (valuePresent || aliasPresent) {
                    if (valuePresent && aliasPresent) {
                        // Since annotation attributes can be arrays, we must use ObjectUtils.nullSafeEquals().
                        if (!ObjectUtils.nullSafeEquals(value, aliasedValue)) {
                            String elementAsString = (annotatedElement != null ? annotatedElement.toString()
                                    : "unknown element");
                            throw new AnnotationConfigurationException(String.format(
                                    "In AnnotationAttributes for annotation [%s] declared on %s, "
                                            + "attribute '%s' and its alias '%s' are declared with values of [%s] and [%s], "
                                            + "but only one is permitted.",
                                    attributes.displayName, elementAsString, attributeName,
                                    aliasedAttributeName, ObjectUtils.nullSafeToString(value),
                                    ObjectUtils.nullSafeToString(aliasedValue)));
                        }
                    } else if (aliasPresent) {
                        // Replace value with aliasedValue
                        attributes.put(attributeName, adaptValue(annotatedElement, aliasedValue,
                                classValuesAsString, nestedAnnotationsAsMap));
                        valuesAlreadyReplaced.add(attributeName);
                    } else {
                        // Replace aliasedValue with value
                        attributes.put(aliasedAttributeName, adaptValue(annotatedElement, value,
                                classValuesAsString, nestedAnnotationsAsMap));
                        valuesAlreadyReplaced.add(aliasedAttributeName);
                    }
                }
            }
        }
        attributes.validated = true;
    }

    // Replace any remaining placeholders with actual default values
    for (String attributeName : attributes.keySet()) {
        if (valuesAlreadyReplaced.contains(attributeName)) {
            continue;
        }
        Object value = attributes.get(attributeName);
        if (value instanceof DefaultValueHolder) {
            value = ((DefaultValueHolder) value).defaultValue;
            attributes.put(attributeName,
                    adaptValue(annotatedElement, value, classValuesAsString, nestedAnnotationsAsMap));
        }
    }
}

From source file:org.springframework.core.env.PropertySource.java

/**
 * This {@code PropertySource} object is equal to the given object if:
 * <ul>/*  w w  w.ja  v a2  s .  c om*/
 * <li>they are the same instance
 * <li>the {@code name} properties for both objects are equal
 * </ul>
 * <p>No properties other than {@code name} are evaluated.
 */
@Override
public boolean equals(Object obj) {
    return (this == obj || (obj instanceof PropertySource
            && ObjectUtils.nullSafeEquals(this.name, ((PropertySource<?>) obj).name)));
}

From source file:org.springframework.data.hadoop.store.strategy.naming.UuidFileNamingStrategy.java

@Override
public Path init(Path path) {
    path = super.init(path);
    log.debug("Init using path=[" + path + "]");
    if (path != null && isEnabled()) {
        String oldName = path.getName();
        String newName = StringUtils.replace(oldName, prefix + uuid, "");
        if (!StringUtils.hasText(newName)) {
            path = null;/*from w  w w .j  a  v  a  2s . co m*/
            log.debug("Removed last handled name part, returning null");
        } else if (!ObjectUtils.nullSafeEquals(oldName, newName)) {
            path = new Path(path.getParent(), newName);
            log.debug("Removed handled prefix, path is now " + newName);
        }
    }
    return path;
}

From source file:org.springframework.data.hadoop.test.tests.Assume.java

/**
 * Assume that a particular {@link Version} is currently used.
 *
 * @param version the version to expect//  w  ww  .  j  a  v a  2s .  c o m
 */
public static void hadoopVersion(Version version) {
    Version current = Version.resolveVersion();
    if (ObjectUtils.nullSafeEquals(version, current)) {
        return;
    } else {
        throw new AssumptionViolatedException(
                "specified version [" + version + "] not matched with current version [" + current + "]");
    }
}

From source file:org.springframework.data.mapping.PropertyPath.java

@Override
public boolean equals(Object obj) {

    if (this == obj) {
        return true;
    }/* w  w  w.j a  v  a2s.c  om*/

    if (obj == null || !getClass().equals(obj.getClass())) {
        return false;
    }

    PropertyPath that = (PropertyPath) obj;

    return this.name.equals(that.name) && this.type.equals(that.type)
            && ObjectUtils.nullSafeEquals(this.next, that.next);
}

From source file:org.springframework.data.neo4j.fieldaccess.DetachedEntityState.java

private void checkConcurrentModification(final Object entity,
        final Map.Entry<Neo4jPersistentProperty, ExistingValue> entry, final Neo4jPersistentProperty property,
        final MappingPolicy mappingPolicy) {
    final ExistingValue previousValue = entry.getValue();
    if (previousValue.mustCheckConcurrentModification()) {
        final Object nodeValue = unwrap(delegate.getValue(property, mappingPolicy));
        if (!ObjectUtils.nullSafeEquals(nodeValue, previousValue.value)) {
            throw new ConcurrentModificationException("Node " + entity + " field " + property
                    + " changed in between previous " + previousValue + " current " + nodeValue); // todo or just overwrite
        }//from   w w  w  . j  a v  a2s  . c o  m
    }
}

From source file:org.springframework.data.neo4j.support.DelegatingGraphDatabase.java

private boolean configCheck(Map<String, String> config, Map<String, String> existingConfig, String setting) {
    return ObjectUtils.nullSafeEquals(config.get(setting), existingConfig.get(setting));
}