This commit is contained in:
Looly
2023-05-19 22:28:39 +08:00
parent 586b29f3d1
commit ea1c49a93b
23 changed files with 53 additions and 41 deletions

View File

@@ -48,9 +48,13 @@ public class RandomUtil {
*/
public static final String BASE_CHAR = "abcdefghijklmnopqrstuvwxyz";
/**
* 用于随机选的字符和数字
* 用于随机选的字符和数字(小写)
*/
public static final String BASE_CHAR_NUMBER = BASE_CHAR + BASE_NUMBER;
public static final String BASE_CHAR_NUMBER_LOWER = BASE_CHAR + BASE_NUMBER;
/**
* 用于随机选的字符和数字(包括大写和小写字母)
*/
public static final String BASE_CHAR_NUMBER = BASE_CHAR.toUpperCase() + BASE_CHAR_NUMBER_LOWER;
/**
* 获取随机数生成器对象<br>
@@ -528,13 +532,23 @@ public class RandomUtil {
}
/**
* 获得一个随机的字符串(只包含数字和字符
* 获得一个随机的字符串(只包含数字和大小写字母
*
* @param length 字符串的长度
* @return 随机字符串
*/
public static String randomString(final int length) {
return randomString(BASE_CHAR_NUMBER, length);
return randomStringLower(BASE_CHAR_NUMBER, length);
}
/**
* 获得一个随机的字符串(只包含数字和小写字母)
*
* @param length 字符串的长度
* @return 随机字符串
*/
public static String randomStringLower(final int length) {
return randomStringLower(BASE_CHAR_NUMBER_LOWER, length);
}
/**
@@ -545,7 +559,7 @@ public class RandomUtil {
* @since 4.0.13
*/
public static String randomStringUpper(final int length) {
return randomString(BASE_CHAR_NUMBER, length).toUpperCase();
return randomStringLower(BASE_CHAR_NUMBER_LOWER, length).toUpperCase();
}
/**
@@ -556,9 +570,9 @@ public class RandomUtil {
* @return 随机字符串
*/
public static String randomStringWithoutStr(final int length, final String elemData) {
String baseStr = BASE_CHAR_NUMBER;
String baseStr = BASE_CHAR_NUMBER_LOWER;
baseStr = StrUtil.removeAll(baseStr, elemData.toLowerCase().toCharArray());
return randomString(baseStr, length);
return randomStringLower(baseStr, length);
}
/**
@@ -568,7 +582,7 @@ public class RandomUtil {
* @return 随机字符串
*/
public static String randomNumbers(final int length) {
return randomString(BASE_NUMBER, length);
return randomStringLower(BASE_NUMBER, length);
}
/**
@@ -578,7 +592,7 @@ public class RandomUtil {
* @param length 字符串的长度
* @return 随机字符串
*/
public static String randomString(final String baseString, int length) {
public static String randomStringLower(final String baseString, int length) {
if (StrUtil.isEmpty(baseString)) {
return StrUtil.EMPTY;
}
@@ -612,7 +626,7 @@ public class RandomUtil {
* @since 3.1.2
*/
public static char randomChar() {
return randomChar(BASE_CHAR_NUMBER);
return randomChar(BASE_CHAR_NUMBER_LOWER);
}
/**

View File

@@ -25,7 +25,7 @@ public class LRUCacheTest {
final LRUCache<String, String> cache = CacheUtil.newLRUCache(100, 10);
for (int i = 0; i < 10000; i++) {
//ThreadUtil.execute(()-> cache.put(RandomUtil.randomString(5), "1243", 10));
ThreadUtil.execute(()-> cache.get(RandomUtil.randomString(5), ()->RandomUtil.randomString(10)));
ThreadUtil.execute(()-> cache.get(RandomUtil.randomStringLower(5), ()->RandomUtil.randomStringLower(10)));
}
ThreadUtil.sleep(3000);
}

View File

@@ -38,7 +38,7 @@ public class Base32Test {
@Test
public void encodeAndDecodeRandomTest(){
final String a = RandomUtil.randomString(RandomUtil.randomInt(1000));
final String a = RandomUtil.randomStringLower(RandomUtil.randomInt(1000));
final String encode = Base32.encode(a);
final String decodeStr = Base32.decodeStr(encode);
Assertions.assertEquals(a, decodeStr);

View File

@@ -35,7 +35,7 @@ public class Base62Test {
@Test
public void encodeAndDecodeRandomTest() {
final String a = RandomUtil.randomString(RandomUtil.randomInt(1000));
final String a = RandomUtil.randomStringLower(RandomUtil.randomInt(1000));
final String encode = Base62.encode(a);
final String decodeStr = Base62.decodeStr(encode);
Assertions.assertEquals(a, decodeStr);
@@ -43,7 +43,7 @@ public class Base62Test {
@Test
public void encodeAndDecodeInvertedRandomTest() {
final String a = RandomUtil.randomString(RandomUtil.randomInt(1000));
final String a = RandomUtil.randomStringLower(RandomUtil.randomInt(1000));
final String encode = Base62.encodeInverted(a);
final String decodeStr = Base62.decodeStrInverted(encode);
Assertions.assertEquals(a, decodeStr);

View File

@@ -17,7 +17,7 @@ public class Base64Test {
@Test
public void isTypeBase64Test(){
Assertions.assertTrue(Base64.isTypeBase64(Base64.encode(RandomUtil.randomString(1000))));
Assertions.assertTrue(Base64.isTypeBase64(Base64.encode(RandomUtil.randomStringLower(1000))));
}
@Test

View File

@@ -97,7 +97,7 @@ public class MetroHashTest {
final String[] result = new String[10000000];
int index = 0;
while (index < 10000000) {
result[index++] = RandomUtil.randomString(RandomUtil.randomInt(64));
result[index++] = RandomUtil.randomStringLower(RandomUtil.randomInt(64));
}
return result;
}

View File

@@ -11,10 +11,10 @@ public class MemorySafeLinkedBlockingQueueTest {
public void offerTest(){
// 设置初始值达到最大,这样任何时候元素都无法加入队列
final MemorySafeLinkedBlockingQueue<String> queue = new MemorySafeLinkedBlockingQueue<>(Long.MAX_VALUE);
Assertions.assertFalse(queue.offer(RandomUtil.randomString(RandomUtil.randomInt(100))));
Assertions.assertFalse(queue.offer(RandomUtil.randomStringLower(RandomUtil.randomInt(100))));
// 设定一个很小的值,可以成功加入
queue.setMaxFreeMemory(10);
Assertions.assertTrue(queue.offer(RandomUtil.randomString(RandomUtil.randomInt(100))));
Assertions.assertTrue(queue.offer(RandomUtil.randomStringLower(RandomUtil.randomInt(100))));
}
}

View File

@@ -15,7 +15,6 @@ package org.dromara.hutool.core.func;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.date.StopWatch;
import org.dromara.hutool.core.func.FunctionPool;
import org.dromara.hutool.core.util.RandomUtil;
import org.junit.jupiter.api.Test;
@@ -31,7 +30,7 @@ public class FunctionPoolTest {
// 测试数据
final ArrayList<char[]> list = ListUtil.of();
for (int i = 0; i < 100000; i++) {
list.add(RandomUtil.randomString(100).toCharArray());
list.add(RandomUtil.randomStringLower(100).toCharArray());
}
final StopWatch stopWatch = DateUtil.createStopWatch();

View File

@@ -13,7 +13,7 @@ public class InternUtilTest {
@Test
public void weakTest(){
final Intern<String> intern = InternUtil.ofWeak();
final String a1 = RandomUtil.randomString(RandomUtil.randomInt(100));
final String a1 = RandomUtil.randomStringLower(RandomUtil.randomInt(100));
final String a2 = new String(a1);
Assertions.assertNotSame(a1, a2);

View File

@@ -39,6 +39,6 @@ public class TolerantMapTest {
map.put("tuesday", "星期二");
assert "星期二".equals(map.get("tuesday"));
assert "default".equals(map.get(RandomUtil.randomString(6)));
assert "default".equals(map.get(RandomUtil.randomStringLower(6)));
}
}

View File

@@ -461,7 +461,7 @@ public class StrUtilTest {
@Test
public void briefTest() {
// case: 1 至 str.length - 1
final String str = RandomUtil.randomString(RandomUtil.randomInt(1, 100));
final String str = RandomUtil.randomStringLower(RandomUtil.randomInt(1, 100));
for (int maxLength = 1; maxLength < str.length(); maxLength++) {
final String brief = StrUtil.brief(str, maxLength);
Assertions.assertEquals(brief.length(), maxLength);

View File

@@ -47,9 +47,9 @@ public class TextSimilarityTest {
@Disabled
void longestCommonSubstringLengthTest() {
// https://github.com/dromara/hutool/issues/3045
final String strCommon = RandomUtil.randomString(1024 * 32);
final String strA = RandomUtil.randomString(1024 * 32) + strCommon;
final String strB = RandomUtil.randomString(1024 * 32) + strCommon;
final String strCommon = RandomUtil.randomStringLower(1024 * 32);
final String strA = RandomUtil.randomStringLower(1024 * 32) + strCommon;
final String strB = RandomUtil.randomStringLower(1024 * 32) + strCommon;
final int i = TextSimilarity.longestCommonSubstringLength(strA, strB);
Console.log(i);

View File

@@ -16,7 +16,6 @@ import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.core.util.RandomUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -90,7 +89,7 @@ public class RandomUtilTest {
@Test
public void randomStringOfLengthTest(){
final String s = RandomUtil.randomString("123", -1);
final String s = RandomUtil.randomStringLower("123", -1);
Assertions.assertNotNull(s);
}