From 62b57ebb84f494b8c8a25dc65855bf3922c189b9 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sat, 26 Jan 2019 14:35:29 +0000 Subject: [PATCH] Make equals work in DefaultStateMachineContext - Adding equals and hashCode methods to DefaultStateMachineContext, DefaultExtendedState and ObservableMap. - Fixes #628 --- .../support/DefaultExtendedState.java | 32 ++++++- .../support/DefaultStateMachineContext.java | 86 +++++++++++++++++++ .../statemachine/support/ObservableMap.java | 32 ++++++- .../DefaultStateMachineContextTests.java | 49 +++++++++++ 4 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 spring-statemachine-core/src/test/java/org/springframework/statemachine/support/DefaultStateMachineContextTests.java diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/DefaultExtendedState.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/DefaultExtendedState.java index 45fdf76a..8068dade 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/DefaultExtendedState.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/DefaultExtendedState.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,6 +73,36 @@ public class DefaultExtendedState implements ExtendedState { this.listener = listener; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((variables == null) ? 0 : variables.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + DefaultExtendedState other = (DefaultExtendedState) obj; + if (variables == null) { + if (other.variables != null) { + return false; + } + } else if (!variables.equals(other.variables)) { + return false; + } + return true; + } + @Override public String toString() { return "DefaultExtendedState [variables=" + variables + "]"; diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/DefaultStateMachineContext.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/DefaultStateMachineContext.java index 0bd5a909..ccd4e863 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/DefaultStateMachineContext.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/DefaultStateMachineContext.java @@ -199,6 +199,92 @@ public class DefaultStateMachineContext implements StateMachineContext other = (DefaultStateMachineContext) obj; + if (childRefs == null) { + if (other.childRefs != null) { + return false; + } + } else if (!childRefs.equals(other.childRefs)) { + return false; + } + if (childs == null) { + if (other.childs != null) { + return false; + } + } else if (!childs.equals(other.childs)) { + return false; + } + if (event == null) { + if (other.event != null) { + return false; + } + } else if (!event.equals(other.event)) { + return false; + } + if (eventHeaders == null) { + if (other.eventHeaders != null) { + return false; + } + } else if (!eventHeaders.equals(other.eventHeaders)) { + return false; + } + if (extendedState == null) { + if (other.extendedState != null) { + return false; + } + } else if (!extendedState.equals(other.extendedState)) { + return false; + } + if (historyStates == null) { + if (other.historyStates != null) { + return false; + } + } else if (!historyStates.equals(other.historyStates)) { + return false; + } + if (id == null) { + if (other.id != null) { + return false; + } + } else if (!id.equals(other.id)) { + return false; + } + if (state == null) { + if (other.state != null) { + return false; + } + } else if (!state.equals(other.state)) { + return false; + } + return true; + } + @Override public String toString() { return "DefaultStateMachineContext [id=" + id + ", childs=" + childs + ", childRefs=" + childRefs + ", state=" diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/ObservableMap.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/ObservableMap.java index 1ae1c7e0..106d205d 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/ObservableMap.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/ObservableMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -164,6 +164,36 @@ public class ObservableMap implements Map { this.listener = listener; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((delegate == null) ? 0 : delegate.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + ObservableMap other = (ObservableMap) obj; + if (delegate == null) { + if (other.delegate != null) { + return false; + } + } else if (!delegate.equals(other.delegate)) { + return false; + } + return true; + } + /** * The listener interface for receiving map change events. * diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/support/DefaultStateMachineContextTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/support/DefaultStateMachineContextTests.java new file mode 100644 index 00000000..d3afb685 --- /dev/null +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/support/DefaultStateMachineContextTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.statemachine.support; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Arrays; +import java.util.HashMap; + +import org.junit.Test; + +public class DefaultStateMachineContextTests { + + @Test + public void testEquals() { + DefaultStateMachineContext c1 = new DefaultStateMachineContext(null, null, null, + null); + DefaultStateMachineContext c2 = new DefaultStateMachineContext(null, null, null, + null); + assertThat(c1.equals(c2), is(true)); + + DefaultStateMachineContext c0 = new DefaultStateMachineContext(null, null, null, + null); + + c1 = new DefaultStateMachineContext(Arrays.asList("x", "y"), Arrays.asList(c0), "s1", "e1", + new HashMap<>(), new DefaultExtendedState(), new HashMap<>(), "id"); + c2 = new DefaultStateMachineContext(Arrays.asList("x", "y"), Arrays.asList(c0), "s1", "e1", + new HashMap<>(), new DefaultExtendedState(), new HashMap<>(), "id"); + assertThat(c1.equals(c2), is(true)); + + c2 = new DefaultStateMachineContext(Arrays.asList("d", "y"), Arrays.asList(c0), "s1", "e1", + new HashMap<>(), new DefaultExtendedState(), new HashMap<>(), "id"); + assertThat(c1.equals(c2), is(false)); + } +}