Skip to content

Commit 3fa8fe7

Browse files
committed
Refactor StateTransition class(spring-projects#3674)
Implemented equals() and hashCode() methods to prevent duplicate entries in transitionMap. fix: spring-projects#3674
1 parent 6bd148c commit 3fa8fe7

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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

Lines changed: 19 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,21 @@ public boolean isEnd() {
159162
return next == null;
160163
}
161164

165+
@Override
166+
public boolean equals(Object o) {
167+
if (this == o) return true;
168+
if (o == null || getClass() != o.getClass()) return false;
169+
StateTransition that = (StateTransition) o;
170+
return Objects.equals(state, that.state) &&
171+
Objects.equals(pattern, that.pattern) &&
172+
Objects.equals(next, that.next);
173+
}
174+
175+
@Override
176+
public int hashCode() {
177+
return Objects.hash(state, pattern, next);
178+
}
179+
162180
@Override
163181
public String toString() {
164182
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: 15 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,7 @@
1515
*/
1616
package org.springframework.batch.core.job.flow.support;
1717

18+
import static org.junit.Assert.assertFalse;
1819
import static org.junit.jupiter.api.Assertions.assertNull;
1920
import static org.junit.jupiter.api.Assertions.assertTrue;
2021

@@ -25,7 +26,7 @@
2526
/**
2627
* @author Dave Syer
2728
* @author Michael Minella
28-
*
29+
* @author Kim Youngwoong
2930
*/
3031
class StateTransitionTests {
3132

@@ -74,6 +75,18 @@ void testMatchesPlaceholder() {
7475
assertTrue(transition.matches("CONTINUABLE"));
7576
}
7677

78+
@Test
79+
public void testEquals() {
80+
StateTransition A = StateTransition.createStateTransition(state, "pattern1", "next1");
81+
StateTransition B = StateTransition.createStateTransition(state, "pattern1", "next1");
82+
StateTransition C = StateTransition.createStateTransition(state, "pattern2", "next2");
83+
84+
assertTrue(A.equals(B));
85+
assertFalse(A.equals(C));
86+
assertTrue(A.equals(A));
87+
assertFalse(A.equals(null));
88+
}
89+
7790
@Test
7891
void testToString() {
7992
StateTransition transition = StateTransition.createStateTransition(state, "CONTIN???LE", "start");

0 commit comments

Comments
 (0)