From 119a193ba98898eb103f36741bea459352e4d620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?ZZQ=E7=9A=84?= <33930711+zzq1314zll@users.noreply.github.com> Date: Fri, 2 Jul 2021 17:52:41 +0800 Subject: [PATCH] [ISSUE #6228] Add RandomUtils/RandomUtilsTest (#6243) * [ISSUE #6228] Add RandomUtils/RandomUtilsTest * [ISSUE #6228] Canonical code --- .../nacos/common/utils/RandomUtils.java | 81 +++++++++++++++++++ .../nacos/common/utils/RandomUtilsTest.java | 39 +++++++++ 2 files changed, 120 insertions(+) create mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/RandomUtils.java create mode 100644 common/src/test/java/com/alibaba/nacos/common/utils/RandomUtilsTest.java diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/RandomUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/RandomUtils.java new file mode 100644 index 000000000..b26178ffd --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/utils/RandomUtils.java @@ -0,0 +1,81 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 com.alibaba.nacos.common.utils; + +import java.util.Random; + +/** + * Random utils. + * + * @author zzq + */ +public class RandomUtils { + + /** + * Random Object for random method. + */ + private static final Random RANDOM = new Random(); + + /** + * Returns a random long within the specified range. + * + * @param startInclusive the smallest value that can be returned, must be non-negative + * @param endExclusive the upper bound (not included) + * @return the random long + * @throws IllegalArgumentException if startInclusive or endExclusive illegal + */ + public static long nextLong(final long startInclusive, final long endExclusive) { + checkParameters(startInclusive, endExclusive); + long diff = endExclusive - startInclusive; + if (diff == 0) { + return startInclusive; + } + return (long) (startInclusive + (diff * RANDOM.nextDouble())); + } + + /** + * Returns a random integer within the specified range. + * + * @param startInclusive lower limit, must be non-negative + * @param endExclusive the upper bound (not included) + * @return the random integer + * @throws IllegalArgumentException if startInclusive or endExclusive illegal + */ + public static int nextInt(final int startInclusive, final int endExclusive) { + checkParameters(startInclusive, endExclusive); + int diff = endExclusive - startInclusive; + if (diff == 0) { + return startInclusive; + } + return startInclusive + RANDOM.nextInt(diff); + } + + /** + * Check input parameters. + * + * @param startInclusive lower limit, must be non-negative + * @param endExclusive the upper bound (not included) + */ + private static void checkParameters(final long startInclusive, final long endExclusive) { + if (endExclusive < startInclusive) { + throw new IllegalArgumentException("startInclusive must be less than or equal to the endExclusive."); + } + if (startInclusive < 0) { + throw new IllegalArgumentException("Both parameters must be non-negative"); + } + } +} diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/RandomUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/RandomUtilsTest.java new file mode 100644 index 000000000..76ec4ecaf --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/utils/RandomUtilsTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 com.alibaba.nacos.common.utils; + +import org.junit.Assert; +import org.junit.Test; + +/** + * test RandomUtils. + * @author zzq + */ +public class RandomUtilsTest { + + @Test + public void nextLong() { + final long result = RandomUtils.nextLong(1L, 199L); + Assert.assertTrue(result >= 1L && result < 199L); + } + + @Test + public void nextInt() { + final int result = RandomUtils.nextInt(1, 199); + Assert.assertTrue(result >= 1 && result < 199); + } +}