Skip to content

Commit

Permalink
Implement equals() and hashCode() in StateTransition
Browse files Browse the repository at this point in the history
This commit prevents duplicate state transition entries
in flow definitions

Resolves #3674
  • Loading branch information
Kyoungwoong authored and fmbenhassine committed May 29, 2024
1 parent f9a3ca5 commit c2355f7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2024 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.
Expand All @@ -22,6 +22,8 @@
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
* Value object representing a potential transition from one {@link State} to another. The
* originating State name and the next {@link State} to execute are linked by a pattern
Expand All @@ -31,6 +33,7 @@
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Kim Youngwoong
* @since 2.0
*/
public final class StateTransition {
Expand Down Expand Up @@ -159,6 +162,22 @@ public boolean isEnd() {
return next == null;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
StateTransition that = (StateTransition) o;
return Objects.equals(state, that.state) && Objects.equals(pattern, that.pattern)
&& Objects.equals(next, that.next);
}

@Override
public int hashCode() {
return Objects.hash(state, pattern, next);
}

@Override
public String toString() {
return String.format("StateTransition: [state=%s, pattern=%s, next=%s]", state == null ? null : state.getName(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2024 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.
Expand All @@ -15,6 +15,8 @@
*/
package org.springframework.batch.core.job.flow.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -25,7 +27,7 @@
/**
* @author Dave Syer
* @author Michael Minella
*
* @author Kim Youngwoong
*/
class StateTransitionTests {

Expand Down Expand Up @@ -74,6 +76,18 @@ void testMatchesPlaceholder() {
assertTrue(transition.matches("CONTINUABLE"));
}

@Test
void testEquals() {
StateTransition transition1 = StateTransition.createStateTransition(state, "pattern1", "next1");
StateTransition transition2 = StateTransition.createStateTransition(state, "pattern1", "next1");
StateTransition transition3 = StateTransition.createStateTransition(state, "pattern2", "next2");

assertEquals(transition1, transition2);
assertNotEquals(transition1, transition3);
assertEquals(transition1, transition1);
assertNotEquals(null, transition1);
}

@Test
void testToString() {
StateTransition transition = StateTransition.createStateTransition(state, "CONTIN???LE", "start");
Expand Down

0 comments on commit c2355f7

Please sign in to comment.