Skip to content

Commit

Permalink
[improve][plugin][streamreader] Remove deprecated RandomUtils class
Browse files Browse the repository at this point in the history
    Deprecate RandomUtils in favor of Apache Commons RNG UniformRandomProvider #942.
  • Loading branch information
wgzhao committed Dec 6, 2023
1 parent e5161fd commit d0e3e23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions plugin/reader/streamreader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-rng-simple</artifactId>
<version>${commons.rng.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
import com.wgzhao.addax.common.spi.Reader;
import com.wgzhao.addax.common.util.Configuration;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -437,24 +438,25 @@ private Column buildOneColumn(Configuration eachColumnConfig, int columnIndex)
int scale = eachColumnConfig.getInt(StreamConstant.MIXUP_FUNCTION_SCALE, -1);
boolean isColumnMixup = StringUtils.isNotBlank(columnRandom);
boolean isIncr = StringUtils.isNotBlank(columnIncr);
UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_128_PP.create();
if (isColumnMixup) {
switch (columnType) {
case STRING:
return new StringColumn(RandomStringUtils.randomAlphanumeric(
(int) RandomUtils.nextLong(param1Int, param2Int + 1)));
(int) rng.nextLong(param1Int, param2Int + 1)));
case LONG:
return new LongColumn(RandomUtils.nextLong(param1Int, param2Int + 1));
return new LongColumn(rng.nextLong(param1Int, param2Int + 1));
case DOUBLE:
// specify fixed scale or not ?
if (scale > 0) {
BigDecimal b = BigDecimal.valueOf(RandomUtils.nextDouble(param1Int, param2Int + 1))
BigDecimal b = BigDecimal.valueOf(rng.nextDouble(param1Int, param2Int + 1))
.setScale(scale, BigDecimal.ROUND_HALF_UP);
return new DoubleColumn(b.doubleValue());
} else {
return new DoubleColumn(RandomUtils.nextDouble(param1Int, param2Int + 1));
return new DoubleColumn(rng.nextDouble(param1Int, param2Int + 1));
}
case DATE:
return new DateColumn(new Date(RandomUtils.nextLong(param1Int, param2Int + 1)));
return new DateColumn(new Date(rng.nextLong(param1Int, param2Int + 1)));
case BOOL:
// warn: no concern -10 etc..., how about (0, 0)(0, 1)(1,2)
if (param1Int == param2Int) {
Expand All @@ -468,14 +470,14 @@ else if (param2Int == 0) {
return new BoolColumn(false);
}
else {
long randomInt = RandomUtils.nextLong(0, param1Int + param2Int + 1);
long randomInt = rng.nextLong(0, param1Int + param2Int + 1);
return new BoolColumn(randomInt > param1Int);
}
case BYTES:
return new BytesColumn(RandomStringUtils.randomAlphanumeric((int)
RandomUtils.nextLong(param1Int, param2Int + 1)).getBytes());
rng.nextLong(param1Int, param2Int + 1)).getBytes());
case TIMESTAMP:
return new TimestampColumn(RandomUtils.nextLong(1_100_000_000_000L, 2_100_000_000_000L));
return new TimestampColumn(rng.nextLong(1_100_000_000_000L, 2_100_000_000_000L));
default:
// in fact,never to be here
throw new Exception(String.format("不支持类型[%s]", columnType.name()));
Expand Down

0 comments on commit d0e3e23

Please sign in to comment.