You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by "yifan-c (via GitHub)" <gi...@apache.org> on 2024/07/22 18:45:21 UTC
Re: [PR] CASSANDRA-19791: Remove other uses of Apache Commons lang for hashcode, equality and r… [cassandra-analytics]
yifan-c commented on code in PR #67:
URL: https://github.com/apache/cassandra-analytics/pull/67#discussion_r1686998515
##########
cassandra-analytics-common/src/main/java/org/apache/cassandra/spark/utils/RandomUtils.java:
##########
@@ -100,8 +119,18 @@ public static InetAddress randomInet()
return InetAddresses.fromInteger(RANDOM.nextInt());
}
- public static Object randomValue(CqlField.CqlType type)
+ public static String randomAlphanumeric(int minLengthInclusive, int maxLengthExclusive)
+ {
+ return randomAlphanumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
+ }
+
+ public static String randomAlphanumeric(int maxLength)
{
- return type.randomValue(MIN_COLLECTION_SIZE);
+ StringBuilder sb = new StringBuilder(maxLength);
+ IntStream.rangeClosed(0, maxLength)
+ .map(i -> RANDOM.nextInt(ALPHANUMERIC.length()))
+ .mapToObj(ALPHANUMERIC::charAt)
+ .forEach(sb::append);
+ return sb.toString();
Review Comment:
The alternative in my mind is random from [0, 36), and map the lower 10 to numbers and upper 26 to chars. Therefore, you do not need to look up the ALPHANUMERIC string.
It is just a nit, feel free to ignore.
##########
cassandra-analytics-common/src/main/java/org/apache/cassandra/spark/utils/RandomUtils.java:
##########
@@ -63,6 +64,24 @@ public static int randomPositiveInt(int bound)
return RANDOM.nextInt(bound - 1) + 1;
}
+ public static int nextInt(final int startInclusive, final int endExclusive)
+ {
+ if (endExclusive < startInclusive)
+ {
+ throw new IllegalArgumentException("Start value must be smaller or equal to end value.");
+ }
+ if (startInclusive < 0)
+ {
+ throw new IllegalArgumentException("Both range values must be non-negative.");
+ }
+ if (startInclusive == endExclusive)
+ {
+ return startInclusive;
+ }
+
+ return startInclusive + RANDOM.nextInt(endExclusive - startInclusive);
Review Comment:
The bound value for random is exclusive. I think we are missing a value here.
For example, when `endExclusive - startInclusive == 1`, the produce pseudo random values contains only a single value of 0, but it wants both 0 and 1 instead.
The util is only used for testing, hence a minor issue. Let's get it correct still.
##########
cassandra-analytics-common/src/main/java/org/apache/cassandra/spark/utils/RandomUtils.java:
##########
@@ -63,6 +64,24 @@ public static int randomPositiveInt(int bound)
return RANDOM.nextInt(bound - 1) + 1;
}
+ public static int nextInt(final int startInclusive, final int endExclusive)
Review Comment:
please remove `final`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org