2 Commits

Author SHA1 Message Date
Fabian Holzwarth
7442880452 feat: limit placeholder generation to uppercase chars 2025-05-18 13:24:29 +02:00
Fabian Holzwarth
c4dc3b4245 feat: replace random based placeholder generation with deterministic approach 2025-05-18 12:41:56 +02:00

View File

@@ -24,17 +24,11 @@ public final class PlaceholderType extends UnifyType{
* Used for generating fresh placeholders.
*/
public static final ArrayList<String> EXISTING_PLACEHOLDERS = new ArrayList<String>();
/**
* Prefix of auto-generated placeholder names.
*/
protected static String nextName = "gen_";
/**
* Random number generator used to generate fresh placeholder name.
*/
protected static Random rnd = new Random(43558747548978L);
private static final char[] PLACEHOLDER_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static int placeholderCount = 0;
/**
* True if this object was auto-generated, false if this object was user-generated.
*/
@@ -100,16 +94,24 @@ public final class PlaceholderType extends UnifyType{
}
/**
* Creates a fresh placeholder type with a name that does so far not exist.
* Creates a fresh placeholder type with a name that does so far not exist from the chars A-Z.
* A user could later instantiate a type using the same name that is equivalent to this type.
* @return A fresh placeholder type.
*/
public synchronized static PlaceholderType freshPlaceholder() {
String name = nextName + (char) (rnd.nextInt(22) + 97); // Returns random char between 'a' and 'z'
// Add random chars while the name is in use.
while(EXISTING_PLACEHOLDERS.contains(name)) {
name += (char) (rnd.nextInt(22) + 97); // Returns random char between 'a' and 'z'
String name;
do {
int pc = PlaceholderType.placeholderCount++;
StringBuilder sb = new StringBuilder();
while (pc >= 0) {
sb.insert(0, PLACEHOLDER_CHARS[pc % 26]);
pc = pc / 26 - 1;
}
name = sb.toString();
}
while (EXISTING_PLACEHOLDERS.contains(name));
return new PlaceholderType(name, true);
}