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.statemachine.config.AbstractStateMachineFactory.java

/**
 * Main constructor that create a {@link StateMachine}.
 *
 * @param uuid for internal usage. Can be null, in that case a random one will be generated.
 * @param machineId represent a user Id, up to you to set what you want.
 * @return a {@link StateMachine}//from  w w w. j a  v a  2 s  .  c o  m
 */
@SuppressWarnings("unchecked")
public StateMachine<S, E> getStateMachine(UUID uuid, String machineId) {
    StateMachineModel<S, E> stateMachineModel = resolveStateMachineModel(machineId);
    if (stateMachineModel.getConfigurationData().isVerifierEnabled()) {
        StateMachineModelVerifier<S, E> verifier = stateMachineModel.getConfigurationData().getVerifier();
        if (verifier == null) {
            verifier = new CompositeStateMachineModelVerifier<S, E>();
        }
        verifier.verify(stateMachineModel);
    }

    // shared
    DefaultExtendedState defaultExtendedState = new DefaultExtendedState();

    StateMachine<S, E> machine = null;

    // we store mappings from state id's to states which gets
    // created during the process. This is needed for transitions to
    // find a correct mappings because they use state id's, not actual
    // states.
    final Map<S, State<S, E>> stateMap = new HashMap<S, State<S, E>>();
    Stack<MachineStackItem<S, E>> regionStack = new Stack<MachineStackItem<S, E>>();
    Stack<StateData<S, E>> stateStack = new Stack<StateData<S, E>>();
    Map<Object, StateMachine<S, E>> machineMap = new HashMap<Object, StateMachine<S, E>>();
    Map<S, StateHolder<S, E>> holderMap = new HashMap<S, StateHolder<S, E>>();

    Iterator<Node<StateData<S, E>>> iterator = buildStateDataIterator(stateMachineModel);
    while (iterator.hasNext()) {
        Node<StateData<S, E>> node = iterator.next();
        StateData<S, E> stateData = node.getData();
        StateData<S, E> peek = stateStack.isEmpty() ? null : stateStack.peek();

        // simply push and continue
        if (stateStack.isEmpty()) {
            stateStack.push(stateData);
            continue;
        }

        boolean stackContainsSameParent = false;
        Iterator<StateData<S, E>> ii = stateStack.iterator();
        while (ii.hasNext()) {
            StateData<S, E> sd = ii.next();
            if (stateData != null && ObjectUtils.nullSafeEquals(stateData.getState(), sd.getParent())) {
                stackContainsSameParent = true;
                break;
            }
        }

        if (stateData != null && !stackContainsSameParent) {
            stateStack.push(stateData);
            continue;
        }

        Collection<StateData<S, E>> stateDatas = popSameParents(stateStack);
        int initialCount = getInitialCount(stateDatas);
        Collection<Collection<StateData<S, E>>> regionsStateDatas = splitIntoRegions(stateDatas);
        Collection<TransitionData<S, E>> transitionsData = getTransitionData(iterator.hasNext(), stateDatas,
                stateMachineModel);

        if (initialCount > 1) {
            for (Collection<StateData<S, E>> regionStateDatas : regionsStateDatas) {
                machine = buildMachine(machineMap, stateMap, holderMap, regionStateDatas, transitionsData,
                        resolveBeanFactory(stateMachineModel), contextEvents, defaultExtendedState,
                        stateMachineModel.getTransitionsData(), resolveTaskExecutor(stateMachineModel),
                        resolveTaskScheduler(stateMachineModel), machineId, null, stateMachineModel);
                regionStack.push(new MachineStackItem<S, E>(machine));
            }

            Collection<Region<S, E>> regions = new ArrayList<Region<S, E>>();
            while (!regionStack.isEmpty()) {
                MachineStackItem<S, E> pop = regionStack.pop();
                regions.add(pop.machine);
            }
            S parent = (S) peek.getParent();
            RegionState<S, E> rstate = buildRegionStateInternal(parent, regions, null,
                    stateData != null ? stateData.getEntryActions() : null,
                    stateData != null ? stateData.getExitActions() : null,
                    new DefaultPseudoState<S, E>(PseudoStateKind.INITIAL));
            if (stateData != null) {
                stateMap.put(stateData.getState(), rstate);
            } else {
                // TODO: don't like that we create a last machine here
                Collection<State<S, E>> states = new ArrayList<State<S, E>>();
                states.add(rstate);
                Transition<S, E> initialTransition = new InitialTransition<S, E>(rstate);
                StateMachine<S, E> m = buildStateMachineInternal(states, new ArrayList<Transition<S, E>>(),
                        rstate, initialTransition, null, defaultExtendedState, null, contextEvents,
                        resolveBeanFactory(stateMachineModel), resolveTaskExecutor(stateMachineModel),
                        resolveTaskScheduler(stateMachineModel), beanName,
                        machineId != null ? machineId : stateMachineModel.getConfigurationData().getMachineId(),
                        uuid, stateMachineModel);
                machine = m;
            }
        } else {
            machine = buildMachine(machineMap, stateMap, holderMap, stateDatas, transitionsData,
                    resolveBeanFactory(stateMachineModel), contextEvents, defaultExtendedState,
                    stateMachineModel.getTransitionsData(), resolveTaskExecutor(stateMachineModel),
                    resolveTaskScheduler(stateMachineModel), machineId, uuid, stateMachineModel);
            if (peek.isInitial() || (!peek.isInitial() && !machineMap.containsKey(peek.getParent()))) {
                machineMap.put(peek.getParent(), machine);
            }
        }

        stateStack.push(stateData);
    }

    // setup autostart for top-level machine
    if (machine instanceof LifecycleObjectSupport) {
        ((LifecycleObjectSupport) machine)
                .setAutoStartup(stateMachineModel.getConfigurationData().isAutoStart());
    }

    // set top-level machine as relay
    final StateMachine<S, E> fmachine = machine;
    fmachine.getStateMachineAccessor().doWithAllRegions(new StateMachineFunction<StateMachineAccess<S, E>>() {

        @Override
        public void apply(StateMachineAccess<S, E> function) {
            function.setRelay(fmachine);
        }
    });

    // add monitoring hooks
    final StateMachineMonitor<S, E> stateMachineMonitor = stateMachineModel.getConfigurationData()
            .getStateMachineMonitor();
    if (stateMachineMonitor != null || defaultStateMachineMonitor != null) {
        fmachine.getStateMachineAccessor().doWithRegion(new StateMachineFunction<StateMachineAccess<S, E>>() {

            @Override
            public void apply(StateMachineAccess<S, E> function) {
                if (defaultStateMachineMonitor != null) {
                    function.addStateMachineMonitor(defaultStateMachineMonitor);
                }
                if (stateMachineMonitor != null) {
                    function.addStateMachineMonitor(stateMachineMonitor);
                }
            }
        });
    }

    // TODO: should error out if sec is enabled but spring-security is not in cp
    if (stateMachineModel.getConfigurationData().isSecurityEnabled()) {
        final StateMachineSecurityInterceptor<S, E> securityInterceptor = new StateMachineSecurityInterceptor<S, E>(
                stateMachineModel.getConfigurationData().getTransitionSecurityAccessDecisionManager(),
                stateMachineModel.getConfigurationData().getEventSecurityAccessDecisionManager(),
                stateMachineModel.getConfigurationData().getEventSecurityRule());
        log.info("Adding security interceptor " + securityInterceptor);
        fmachine.getStateMachineAccessor()
                .doWithAllRegions(new StateMachineFunction<StateMachineAccess<S, E>>() {

                    @Override
                    public void apply(StateMachineAccess<S, E> function) {
                        function.addStateMachineInterceptor(securityInterceptor);
                    }
                });
    }

    // setup distributed state machine if needed.
    // we wrap previously build machine with a distributed
    // state machine and set it to use given ensemble.
    if (stateMachineModel.getConfigurationData().getStateMachineEnsemble() != null) {
        DistributedStateMachine<S, E> distributedStateMachine = new DistributedStateMachine<S, E>(
                stateMachineModel.getConfigurationData().getStateMachineEnsemble(), machine);
        distributedStateMachine.setAutoStartup(stateMachineModel.getConfigurationData().isAutoStart());
        distributedStateMachine.afterPropertiesSet();
        machine = distributedStateMachine;
    }

    for (StateMachineListener<S, E> listener : stateMachineModel.getConfigurationData()
            .getStateMachineListeners()) {
        machine.addStateListener(listener);
    }

    // go through holders and fix state references which
    // were not known at a time holder was created
    for (Entry<S, StateHolder<S, E>> holder : holderMap.entrySet()) {
        holder.getValue().setState(stateMap.get(holder.getKey()));
    }

    return delegateAutoStartup(machine);
}

From source file:org.springframework.statemachine.config.AbstractStateMachineFactory.java

private static <S, E> Collection<StateData<S, E>> popSameParents(Stack<StateData<S, E>> stack) {
    Collection<StateData<S, E>> data = new ArrayList<StateData<S, E>>();
    Object parent = null;/*  w  w w  .  j  av  a  2 s  . c o m*/
    if (!stack.isEmpty()) {
        parent = stack.peek().getParent();
    }
    while (!stack.isEmpty() && ObjectUtils.nullSafeEquals(parent, stack.peek().getParent())) {
        data.add(stack.pop());
    }
    return data;
}

From source file:org.springframework.test.annotation.ProfileValueUtils.java

/**
 * Determine if the {@code value} (or one of the {@code values})
 * in the supplied {@link IfProfileValue &#064;IfProfileValue} annotation is
 * <em>enabled</em> in the current environment.
 * @param profileValueSource the ProfileValueSource to use to determine if
 * the test is enabled/*from   w  w  w  . ja  v  a  2 s.c  o m*/
 * @param ifProfileValue the annotation to introspect; may be
 * {@code null}
 * @return {@code true} if the test is <em>enabled</em> in the current
 * environment or if the supplied {@code ifProfileValue} is
 * {@code null}
 */
private static boolean isTestEnabledInThisEnvironment(ProfileValueSource profileValueSource,
        @Nullable IfProfileValue ifProfileValue) {

    if (ifProfileValue == null) {
        return true;
    }

    String environmentValue = profileValueSource.get(ifProfileValue.name());
    String[] annotatedValues = ifProfileValue.values();
    if (StringUtils.hasLength(ifProfileValue.value())) {
        Assert.isTrue(annotatedValues.length == 0, () -> "Setting both the 'value' and 'values' attributes "
                + "of @IfProfileValue is not allowed: choose one or the other.");
        annotatedValues = new String[] { ifProfileValue.value() };
    }

    for (String value : annotatedValues) {
        if (ObjectUtils.nullSafeEquals(value, environmentValue)) {
            return true;
        }
    }
    return false;
}

From source file:org.springframework.test.context.ContextConfigurationAttributes.java

/**
 * Determine if the supplied object is equal to this
 * {@code ContextConfigurationAttributes} instance by comparing both object's
 * {@linkplain #getDeclaringClass() declaring class},
 * {@linkplain #getLocations() locations},
 * {@linkplain #getClasses() annotated classes},
 * {@linkplain #isInheritLocations() inheritLocations flag},
 * {@linkplain #getInitializers() context initializer classes},
 * {@linkplain #isInheritInitializers() inheritInitializers flag}, and the
 * {@link #getContextLoaderClass() ContextLoader class}.
 *///from   w  ww.  jav a  2  s . co  m
@Override
public boolean equals(Object other) {
    if (this == other) {
        return true;
    }
    if (!(other instanceof ContextConfigurationAttributes)) {
        return false;
    }
    ContextConfigurationAttributes otherAttr = (ContextConfigurationAttributes) other;
    return (ObjectUtils.nullSafeEquals(this.declaringClass, otherAttr.declaringClass)
            && Arrays.equals(this.classes, otherAttr.classes))
            && Arrays.equals(this.locations, otherAttr.locations)
            && this.inheritLocations == otherAttr.inheritLocations
            && Arrays.equals(this.initializers, otherAttr.initializers)
            && this.inheritInitializers == otherAttr.inheritInitializers
            && ObjectUtils.nullSafeEquals(this.name, otherAttr.name)
            && ObjectUtils.nullSafeEquals(this.contextLoaderClass, otherAttr.contextLoaderClass);
}

From source file:org.springframework.transaction.interceptor.MethodMapTransactionAttributeSource.java

@Override
public boolean equals(Object other) {
    if (this == other) {
        return true;
    }/* w ww .  j  a va  2  s.  c  o  m*/
    if (!(other instanceof MethodMapTransactionAttributeSource)) {
        return false;
    }
    MethodMapTransactionAttributeSource otherTas = (MethodMapTransactionAttributeSource) other;
    return ObjectUtils.nullSafeEquals(this.methodMap, otherTas.methodMap);
}

From source file:org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource.java

@Override
public boolean equals(Object other) {
    if (this == other) {
        return true;
    }//w  w w.  j  a  va2  s .c om
    if (!(other instanceof NameMatchTransactionAttributeSource)) {
        return false;
    }
    NameMatchTransactionAttributeSource otherTas = (NameMatchTransactionAttributeSource) other;
    return ObjectUtils.nullSafeEquals(this.nameMap, otherTas.nameMap);
}

From source file:org.springframework.yarn.fs.AbstractResourceLocalizer.java

@Override
public void setStagingDirectory(Path stagingDirectory) {
    log.info("Setting stagingDirectory=" + stagingDirectory);
    if (!ObjectUtils.nullSafeEquals(this.stagingDirectory, stagingDirectory)) {
        log.info("Marking distributed state false");
        distributed = false;/*  w w w  .j  a v a 2  s  .com*/
        copied = false;
    }
    this.stagingDirectory = stagingDirectory;
}

From source file:org.springframework.yarn.fs.AbstractResourceLocalizer.java

@Override
public void setStagingId(String stagingId) {
    log.info("Setting stagingId=" + stagingId);
    if (!ObjectUtils.nullSafeEquals(this.stagingId, stagingId)) {
        log.info("Marking distributed state false");
        distributed = false;//from w  w w.  ja v  a  2 s  .  c om
        copied = false;
    }
    this.stagingId = stagingId;
}

From source file:org.springframework.yarn.launch.SimpleJvmExitCodeMapper.java

@Override
public int intValue(Boolean exitCode) {
    return ObjectUtils.nullSafeEquals(exitCode, true) ? JVM_EXITCODE_COMPLETED : JVM_EXITCODE_GENERIC_ERROR;
}

From source file:org.talend.dataprep.conversions.BeanConversionService.java

@Override
public boolean canConvert(Class<?> aClass, Class<?> aClass1) {
    return ObjectUtils.nullSafeEquals(aClass, aClass1) || has(aClass) && of(registrations.get(aClass))
            .anyMatch(registration -> of(registration.convertedClasses).anyMatch(aClass1::equals));
}