Make equals work in DefaultStateMachineContext

- Adding equals and hashCode methods to DefaultStateMachineContext,
  DefaultExtendedState and ObservableMap.
- Fixes #628
This commit is contained in:
Janne Valkealahti
2019-01-26 14:35:29 +00:00
parent 28301a2464
commit 62b57ebb84
4 changed files with 197 additions and 2 deletions

View File

@@ -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 + "]";

View File

@@ -199,6 +199,92 @@ public class DefaultStateMachineContext<S, E> implements StateMachineContext<S,
return extendedState;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((childRefs == null) ? 0 : childRefs.hashCode());
result = prime * result + ((childs == null) ? 0 : childs.hashCode());
result = prime * result + ((event == null) ? 0 : event.hashCode());
result = prime * result + ((eventHeaders == null) ? 0 : eventHeaders.hashCode());
result = prime * result + ((extendedState == null) ? 0 : extendedState.hashCode());
result = prime * result + ((historyStates == null) ? 0 : historyStates.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((state == null) ? 0 : state.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;
}
DefaultStateMachineContext<?, ?> 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="

View File

@@ -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<K, V> implements Map<K, V> {
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.
*

View File

@@ -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<String, String> c1 = new DefaultStateMachineContext<String, String>(null, null, null,
null);
DefaultStateMachineContext<String, String> c2 = new DefaultStateMachineContext<String, String>(null, null, null,
null);
assertThat(c1.equals(c2), is(true));
DefaultStateMachineContext<String, String> c0 = new DefaultStateMachineContext<String, String>(null, null, null,
null);
c1 = new DefaultStateMachineContext<String, String>(Arrays.asList("x", "y"), Arrays.asList(c0), "s1", "e1",
new HashMap<>(), new DefaultExtendedState(), new HashMap<>(), "id");
c2 = new DefaultStateMachineContext<String, String>(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<String, String>(Arrays.asList("d", "y"), Arrays.asList(c0), "s1", "e1",
new HashMap<>(), new DefaultExtendedState(), new HashMap<>(), "id");
assertThat(c1.equals(c2), is(false));
}
}