-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 <[email protected]>
- Loading branch information
1 parent
57a7c3d
commit 206a1e5
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/org/openrewrite/java/migrate/util/ReplaceMathRandomWithThreadLocalRandom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.openrewrite.java.migrate.util; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import org.openrewrite.java.template.RecipeDescriptor; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
@RecipeDescriptor( | ||
name = "Replace `java.lang.Math random()` with `ThreadLocalRandom nextDouble()`", | ||
description = "Replace `java.lang.Math random()` with `ThreadLocalRandom nextDouble()` to reduce contention." | ||
) | ||
public class ReplaceMathRandomWithThreadLocalRandom { | ||
@BeforeTemplate | ||
double javaMathRandom() { | ||
return Math.random(); | ||
} | ||
|
||
@AfterTemplate | ||
double threadLocalRandomNextDouble() { | ||
return ThreadLocalRandom.current().nextDouble(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...a/org/openrewrite/java/migrate/util/ReplaceMathRandomWithThreadLocalRandomRecipeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class ReplaceMathRandomWithThreadLocalRandomRecipeTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new ReplaceMathRandomWithThreadLocalRandomRecipe()); | ||
} | ||
|
||
@Test | ||
@DocumentExample | ||
void replacesMathRandom() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
class Example { | ||
double test() { | ||
return Math.random(); | ||
} | ||
} | ||
""", | ||
""" | ||
import java.util.concurrent.ThreadLocalRandom; | ||
class Example { | ||
double test() { | ||
return ThreadLocalRandom.current().nextDouble(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |