Skip to content

Commit

Permalink
Recipe to migrate from Math.random to ThreadLocalRandom (#543)
Browse files Browse the repository at this point in the history
* 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
3 people authored Sep 3, 2024
1 parent 57a7c3d commit 206a1e5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
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();
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/java-version-7.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ recipeList:
- org.openrewrite.java.migrate.UpgradeToJava6
- org.openrewrite.java.migrate.JREJdbcInterfaceNewMethods
- org.openrewrite.java.migrate.JREThrowableFinalMethods
- org.openrewrite.java.migrate.util.ReplaceMathRandomWithThreadLocalRandomRecipe
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.JREJdbcInterfaceNewMethods
Expand Down
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();
}
}
"""
)
);
}
}

0 comments on commit 206a1e5

Please sign in to comment.