Skip to content

Commit 9bc5e2e

Browse files
committed
Make Singleton safe to cache
1 parent b52a552 commit 9bc5e2e

1 file changed

Lines changed: 1 addition & 5 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/Singleton.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,8 @@ public class Singleton extends Recipe {
8181
"The easiest way is to use Lombok's `@Value` annotation on your recipe class, which automatically " +
8282
"generates correct `equals()` and `hashCode()` implementations based on all fields.";
8383

84-
@Nullable Integer recipeIndex;
85-
8684
public boolean isSingleton(ExecutionContext ctx) {
87-
if (recipeIndex == null) {
88-
recipeIndex = ctx.getCycleDetails().getRecipePosition();
89-
}
85+
Integer recipeIndex = ctx.computeMessageIfAbsent(getClass().getName() + "@" + Integer.toHexString(hashCode()), key -> ctx.getCycleDetails().getRecipePosition());
9086
return recipeIndex == ctx.getCycleDetails().getRecipePosition();
9187
}
9288

0 commit comments

Comments
 (0)