Skip to content

Commit 206a1e5

Browse files
sullisgithub-actions[bot]timtebeek
authored
Recipe to migrate from Math.random to ThreadLocalRandom (#543)
* Recipe to migrate from Math.random to ThreadLocalRandom * Update src/main/java/org/openrewrite/java/migrate/util/ReplaceMathRandomWithThreadLocalRandom.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Revert "Update src/main/java/org/openrewrite/java/migrate/util/ReplaceMathRandomWithThreadLocalRandom.java" This reverts commit 527c48c. * tweak imports * Minor polish and include with Java 7 upgrades --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 57a7c3d commit 206a1e5

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.openrewrite.java.migrate.util;
18+
19+
import com.google.errorprone.refaster.annotation.AfterTemplate;
20+
import com.google.errorprone.refaster.annotation.BeforeTemplate;
21+
import org.openrewrite.java.template.RecipeDescriptor;
22+
23+
import java.util.concurrent.ThreadLocalRandom;
24+
25+
@RecipeDescriptor(
26+
name = "Replace `java.lang.Math random()` with `ThreadLocalRandom nextDouble()`",
27+
description = "Replace `java.lang.Math random()` with `ThreadLocalRandom nextDouble()` to reduce contention."
28+
)
29+
public class ReplaceMathRandomWithThreadLocalRandom {
30+
@BeforeTemplate
31+
double javaMathRandom() {
32+
return Math.random();
33+
}
34+
35+
@AfterTemplate
36+
double threadLocalRandomNextDouble() {
37+
return ThreadLocalRandom.current().nextDouble();
38+
}
39+
}

src/main/resources/META-INF/rewrite/java-version-7.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ recipeList:
2626
- org.openrewrite.java.migrate.UpgradeToJava6
2727
- org.openrewrite.java.migrate.JREJdbcInterfaceNewMethods
2828
- org.openrewrite.java.migrate.JREThrowableFinalMethods
29+
- org.openrewrite.java.migrate.util.ReplaceMathRandomWithThreadLocalRandomRecipe
2930
---
3031
type: specs.openrewrite.org/v1beta/recipe
3132
name: org.openrewrite.java.migrate.JREJdbcInterfaceNewMethods
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.util;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.java.Assertions.java;
24+
25+
class ReplaceMathRandomWithThreadLocalRandomRecipeTest implements RewriteTest {
26+
27+
@Override
28+
public void defaults(RecipeSpec spec) {
29+
spec.recipe(new ReplaceMathRandomWithThreadLocalRandomRecipe());
30+
}
31+
32+
@Test
33+
@DocumentExample
34+
void replacesMathRandom() {
35+
rewriteRun(
36+
//language=java
37+
java(
38+
"""
39+
class Example {
40+
double test() {
41+
return Math.random();
42+
}
43+
}
44+
""",
45+
"""
46+
import java.util.concurrent.ThreadLocalRandom;
47+
48+
class Example {
49+
double test() {
50+
return ThreadLocalRandom.current().nextDouble();
51+
}
52+
}
53+
"""
54+
)
55+
);
56+
}
57+
}

0 commit comments

Comments
 (0)