Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/134#discussion_r64402941
--- Diff:
utils/common/src/main/java/org/apache/brooklyn/util/text/Identifiers.java ---
@@ -87,29 +87,34 @@
* in general this is preferable to base64 as is more portable,
* can be used throughout javascript (as ID's which don't allow +)
* or as java identifiers (which don't allow numbers in the first char)
- **/
- public static String makeRandomId(int l) {
- //this version is 30-50% faster than the old double-based one,
- //which computed a random every 3 turns --
- //takes about 600 ns to do id of len 10, compared to 10000 ns for
old version [on 1.6ghz machine]
- if (l<=0) return "";
+ * <p>
+ * <b>NOTE</b> This version is 30-50% faster than the old double-based
one,
+ * which computed a random every 3 turns -- takes about 600 ns to do id
+ * of len 10, compared to 10000 ns for old version [on 1.6ghz machine]
+ */
+ public static String makeRandomId(int l, String validStartChars,
String validNonStartChars) {
+ if (l <= 0) return "";
char[] id = new char[l];
- int d = random.nextInt( (26+26) * (26+26+10) * (26+26+10) *
(26+26+10) * (26+26+10));
+ int s = validStartChars.length();
+ int n = validNonStartChars.length();
+ int d = random.nextInt(s * n * n * n * n);
--- End diff --
Failing on too many characters is better I think
One more option - if input is too long switch to a slower implementation,
but one which can handle the input correctly.
For this case though suggest making the parametrized `makeRandomId`
private, adding the proper javadoc warnings and adding another method for lower
case IDs. It requires the least effort and is good enough for now.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---