Skip to content

Commit c2355f7

Browse files
Kyoungwoongfmbenhassine
authored andcommitted
Implement equals() and hashCode() in StateTransition
This commit prevents duplicate state transition entries in flow definitions Resolves #3674
1 parent f9a3ca5 commit c2355f7

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/StateTransition.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@
2222
import org.springframework.util.Assert;
2323
import org.springframework.util.StringUtils;
2424

25+
import java.util.Objects;
26+
2527
/**
2628
* Value object representing a potential transition from one {@link State} to another. The
2729
* originating State name and the next {@link State} to execute are linked by a pattern
@@ -31,6 +33,7 @@
3133
* @author Dave Syer
3234
* @author Michael Minella
3335
* @author Mahmoud Ben Hassine
36+
* @author Kim Youngwoong
3437
* @since 2.0
3538
*/
3639
public final class StateTransition {
@@ -159,6 +162,22 @@ public boolean isEnd() {
159162
return next == null;
160163
}
161164

165+
@Override
166+
public boolean equals(Object o) {
167+
if (this == o)
168+
return true;
169+
if (o == null || getClass() != o.getClass())
170+
return false;
171+
StateTransition that = (StateTransition) o;
172+
return Objects.equals(state, that.state) && Objects.equals(pattern, that.pattern)
173+
&& Objects.equals(next, that.next);
174+
}
175+
176+
@Override
177+
public int hashCode() {
178+
return Objects.hash(state, pattern, next);
179+
}
180+
162181
@Override
163182
public String toString() {
164183
return String.format("StateTransition: [state=%s, pattern=%s, next=%s]", state == null ? null : state.getName(),

spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/support/StateTransitionTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.batch.core.job.flow.support;
1717

18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1820
import static org.junit.jupiter.api.Assertions.assertNull;
1921
import static org.junit.jupiter.api.Assertions.assertTrue;
2022

@@ -25,7 +27,7 @@
2527
/**
2628
* @author Dave Syer
2729
* @author Michael Minella
28-
*
30+
* @author Kim Youngwoong
2931
*/
3032
class StateTransitionTests {
3133

@@ -74,6 +76,18 @@ void testMatchesPlaceholder() {
7476
assertTrue(transition.matches("CONTINUABLE"));
7577
}
7678

79+
@Test
80+
void testEquals() {
81+
StateTransition transition1 = StateTransition.createStateTransition(state, "pattern1", "next1");
82+
StateTransition transition2 = StateTransition.createStateTransition(state, "pattern1", "next1");
83+
StateTransition transition3 = StateTransition.createStateTransition(state, "pattern2", "next2");
84+
85+
assertEquals(transition1, transition2);
86+
assertNotEquals(transition1, transition3);
87+
assertEquals(transition1, transition1);
88+
assertNotEquals(null, transition1);
89+
}
90+
7791
@Test
7892
void testToString() {
7993
StateTransition transition = StateTransition.createStateTransition(state, "CONTIN???LE", "start");

0 commit comments

Comments
 (0)