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.data.redis.connection.lettuce.LettuceClusterConnection.java

@Override
public String ping() {
    Collection<String> ping = clusterCommandExecutor
            .executeCommandOnAllNodes((LettuceClusterCommandCallback<String>) BaseRedisCommands::ping)
            .resultsAsList();/*from w  w  w . j a va  2s  .c  o  m*/

    for (String result : ping) {
        if (!ObjectUtils.nullSafeEquals("PONG", result)) {
            return "";
        }
    }

    return "PONG";
}

From source file:org.springframework.faces.mvc.bind.ReverseDataBinder.java

/**
 * Perform the reverse bind on the <tt>dataBinder</tt> provided in the constructor. Note: Calling with method will
 * also trigger a <tt>bind</tt> operation on the <tt>dataBinder</tt>. This method returns {@link PropertyValues}
 * containing a name/value pairs for each property that can be bound. Property values are encoded as Strings using
 * the property editors bound to the original dataBinder.
 * @return property values that could be re-bound using the data binder
 * @throws IllegalStateException if the target object values cannot be bound
 *///from   www  .j  a v a  2  s.  c  om
public PropertyValues reverseBind() {
    Assert.notNull(dataBinder.getTarget(),
            "ReverseDataBinder.reverseBind can only be used with a DataBinder that has a target object");
    MutablePropertyValues rtn = new MutablePropertyValues();
    BeanWrapper target = PropertyAccessorFactory.forBeanPropertyAccess(dataBinder.getTarget());
    PropertyDescriptor[] propertyDescriptors = target.getPropertyDescriptors();

    BeanWrapper defaultValues = null;
    if (skipDefaultValues) {
        defaultValues = newDefaultTargetValues(dataBinder.getTarget());
    }

    for (int i = 0; i < propertyDescriptors.length; i++) {
        PropertyDescriptor property = propertyDescriptors[i];
        String propertyName = getPropertyName(property);
        Object propertyValue = target.getPropertyValue(propertyName);

        if (isSkippedProperty(property)) {
            continue;
        }

        if (!isMutableProperty(property)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Ignoring '" + propertyName + "' due to missing read/write methods");
            }
            continue;
        }

        if (defaultValues != null
                && ObjectUtils.nullSafeEquals(defaultValues.getPropertyValue(propertyName), propertyValue)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Skipping '" + propertyName + "' as property contains default value");
            }
            continue;
        }

        // Find a property editor
        PropertyEditorRegistrySupport propertyEditorRegistrySupport = null;
        if (target instanceof PropertyEditorRegistrySupport) {
            propertyEditorRegistrySupport = (PropertyEditorRegistrySupport) target;
        }
        PropertyEditor propertyEditor = findEditor(propertyEditorRegistrySupport, target.getWrappedInstance(),
                target.getPropertyType(propertyName), property);

        // Convert and store the value
        String convertedPropertyValue = convertToStringUsingPropertyEditor(propertyValue, propertyEditor);
        if (convertedPropertyValue != null) {
            rtn.addPropertyValue(propertyName, convertedPropertyValue);
        }
    }

    dataBinder.bind(rtn);
    BindingResult bindingResult = dataBinder.getBindingResult();
    if (bindingResult.hasErrors()) {
        throw new IllegalStateException("Unable to reverse bind from target '" + dataBinder.getObjectName()
                + "', the properties '" + rtn + "' will result in binding errors when re-bound "
                + bindingResult.getAllErrors());
    }
    return rtn;
}

From source file:org.springframework.integration.kafka.listener.KafkaTopicOffsetManager.java

private void checkAndAddData(KafkaMessage kafkaMessage) {
    Key key = MessageUtils.decodeKey(kafkaMessage, KEY_CODEC);
    Long value = MessageUtils.decodePayload(kafkaMessage, VALUE_CODEC);
    if (log.isTraceEnabled()) {
        log.trace("Loading key " + key + " with value " + value);
    }/*from   w  w  w .  j  a  v  a  2s .c o  m*/
    // we are only interested for messages that are intended for this consumer id
    if (key != null && ObjectUtils.nullSafeEquals(this.consumerId, key.getConsumerId())) {
        if (null != value) {
            // write data in the cache, overwriting the older values
            this.data.put(key.getPartition(), value);
        } else {
            // a null value means that the data has been deleted, but not compacted yet
            if (this.data.containsKey(key.getPartition())) {
                this.data.remove(key.getPartition());
            }
        }
    }
}

From source file:org.springframework.jca.context.SpringContextResourceAdapter.java

@Override
public boolean equals(Object obj) {
    return (obj instanceof SpringContextResourceAdapter && ObjectUtils.nullSafeEquals(
            getContextConfigLocation(), ((SpringContextResourceAdapter) obj).getContextConfigLocation()));
}

From source file:org.springframework.messaging.support.MessageHeaderAccessor.java

/**
 * Set the value for the given header name.
 * <p>If the provided value is {@code null}, the header will be removed.
 *///w ww  .j  a v  a2 s .  c o  m
public void setHeader(String name, Object value) {
    if (isReadOnly(name)) {
        throw new IllegalArgumentException("'" + name + "' header is read-only");
    }
    verifyType(name, value);
    if (!ObjectUtils.nullSafeEquals(value, getHeader(name))) {
        this.modified = true;
        if (value != null) {
            this.headers.getRawHeaders().put(name, value);
        } else {
            this.headers.getRawHeaders().remove(name);
        }
    }
}

From source file:org.springframework.richclient.command.support.DefaultCommandRegistry.java

/**
 * This method is provided as a private helper so that it can be called by the constructor, 
 * instead of the constructor having to call the public overridable setParent method. 
 *///w w  w .ja  va2s .com
private void internalSetParent(CommandRegistry parentRegistry) {

    if (!ObjectUtils.nullSafeEquals(this.parent, parentRegistry)) {

        if (this.parent != null) {
            this.parent.removeCommandRegistryListener(this);
        }

        this.parent = parentRegistry;

        if (this.parent != null) {
            this.parent.addCommandRegistryListener(this);
        }

    }

}

From source file:org.springframework.richclient.core.LabeledObjectSupport.java

protected boolean hasChanged(Object currentValue, Object proposedValue) {
    return !ObjectUtils.nullSafeEquals(currentValue, proposedValue);
}

From source file:org.springframework.shell.core.JLineShell.java

public void promptLoop() {
    setShellStatus(Status.USER_INPUT);/*  w  w  w  .j  a  va  2  s.c o  m*/
    String line;
    String prompt = getPromptText();

    try {
        while (exitShellRequest == null && (reader != null && ((line = reader.readLine()) != null))) {
            JLineLogHandler.resetMessageTracking();
            setShellStatus(Status.USER_INPUT);

            if ("".equals(line)) {
                continue;
            }

            executeCommand(line);

            String newPrmpt = getPromptText();
            if (!ObjectUtils.nullSafeEquals(prompt, newPrmpt)) {
                prompt = newPrmpt;
                setPromptPath(null);
            }
            //System.out.println("executed command:" + line);
        }
    } catch (IOException ioe) {
        throw new IllegalStateException("Shell line reading failure", ioe);
    }
    setShellStatus(Status.SHUTTING_DOWN);
}

From source file:org.springframework.springfaces.mvc.bind.ReverseDataBinder.java

/**
 * Perform the reverse bind on the <tt>dataBinder</tt> provided in the constructor. Note: Calling with method will
 * also trigger a <tt>bind</tt> operation on the <tt>dataBinder</tt>. This method returns {@link PropertyValues}
 * containing a name/value pairs for each property that can be bound. Property values are encoded as Strings using
 * the property editors bound to the original dataBinder.
 * @return property values that could be re-bound using the data binder
 * @throws IllegalStateException if the target object values cannot be bound
 *///from   ww w  . jav  a  2 s  . c o m
public PropertyValues reverseBind() {
    Assert.notNull(this.dataBinder.getTarget(),
            "ReverseDataBinder.reverseBind can only be used with a DataBinder that has a target object");

    MutablePropertyValues rtn = new MutablePropertyValues();
    BeanWrapper target = PropertyAccessorFactory.forBeanPropertyAccess(this.dataBinder.getTarget());

    ConversionService conversionService = this.dataBinder.getConversionService();
    if (conversionService != null) {
        target.setConversionService(conversionService);
    }

    PropertyDescriptor[] propertyDescriptors = target.getPropertyDescriptors();

    BeanWrapper defaultValues = null;
    if (this.skipDefaultValues) {
        defaultValues = newDefaultTargetValues(this.dataBinder.getTarget());
    }

    for (int i = 0; i < propertyDescriptors.length; i++) {
        PropertyDescriptor property = propertyDescriptors[i];
        String propertyName = PropertyAccessorUtils.canonicalPropertyName(property.getName());
        Object propertyValue = target.getPropertyValue(propertyName);

        if (isSkippedProperty(property)) {
            continue;
        }

        if (!isMutableProperty(property)) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Ignoring '" + propertyName + "' due to missing read/write methods");
            }
            continue;
        }

        if (defaultValues != null
                && ObjectUtils.nullSafeEquals(defaultValues.getPropertyValue(propertyName), propertyValue)) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Skipping '" + propertyName + "' as property contains default value");
            }
            continue;
        }

        // Find a property editor
        PropertyEditorRegistrySupport propertyEditorRegistrySupport = null;
        if (target instanceof PropertyEditorRegistrySupport) {
            propertyEditorRegistrySupport = (PropertyEditorRegistrySupport) target;
        }

        PropertyEditor propertyEditor = findEditor(propertyName, propertyEditorRegistrySupport,
                target.getWrappedInstance(), target.getPropertyType(propertyName),
                target.getPropertyTypeDescriptor(propertyName));

        // Convert and store the value
        String convertedPropertyValue = convertToStringUsingPropertyEditor(propertyValue, propertyEditor);
        if (convertedPropertyValue != null) {
            rtn.addPropertyValue(propertyName, convertedPropertyValue);
        }
    }

    this.dataBinder.bind(rtn);
    BindingResult bindingResult = this.dataBinder.getBindingResult();
    if (bindingResult.hasErrors()) {
        throw new IllegalStateException("Unable to reverse bind from target '" + this.dataBinder.getObjectName()
                + "', the properties '" + rtn + "' will result in binding errors when re-bound "
                + bindingResult.getAllErrors());
    }
    return rtn;
}

From source file:org.springframework.statemachine.action.DistributedLeaderAction.java

@Override
public void execute(StateContext<S, E> context) {
    StateMachine<S, E> left = context.getStateMachine();
    StateMachine<S, E> right = ensemble.getLeader();
    boolean leader = (left != null & right != null)
            && ObjectUtils.nullSafeEquals(left.getUuid(), right.getUuid());
    if (log.isDebugEnabled()) {
        log.debug("Distibuted action leader status is " + leader);
    }//from   ww w  .j a v  a  2  s.  c  o m
    if (leader) {
        action.execute(context);
    }
}