Skip to content

Commit 0d5d668

Browse files
authored
Merge pull request #28 from adlange/4-add-combinations-calculation
#4 add getNumberOfPossibleCombinations method to ReadableIdGenerator …
2 parents c048bbf + 171d43e commit 0d5d668

File tree

7 files changed

+50
-0
lines changed

7 files changed

+50
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
* new module `readable-ids-dictionary-english-adjective-noun`
88
* fix parent BOM creation and artifact packaging
9+
* add `getNumberOfPossibleCombinations()` method to `ReadableIdGenerator`
10+
* add `getNumberOfPossibleCombinations()` method to `TokenDictionary` interface
911
* improve documentation
1012

1113
## 0.0.1

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ generated. Because all dictionaries have the same probability of being selected
209209
duplicates among the IDs can be much higher when using another significantly smaller dictionary compared to the use of a
210210
single, extensive dictionary.
211211

212+
You can get the estimated number of possible combinations possible from the given dictionaries, assuming that there are
213+
no duplicates / overlaps within the dictionaries and that a good `RandomGenerator` is used:
214+
215+
```java
216+
long possibleCombinations = generator.getNumberOfPossibleCombinations();
217+
```
218+
212219
### Using Multiple Modules of readable-ids
213220

214221
To avoid having to maintain the module version individually, it is advisable to use the BOM.

readable-ids-core/src/main/java/de/adrianlange/readableids/ReadableIdGenerator.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ public String nextId() {
9797
return tokenJoiner.joinTokens(idFromDictionary);
9898
}
9999

100+
/**
101+
* Returns the number of possible combinations of tokens for all dictionaries.
102+
*
103+
* @return Number of possible combinations
104+
*/
105+
public long getNumberOfPossibleCombinations() {
106+
107+
return tokenDictionaries.stream()
108+
.map(TokenDictionary::getNumberOfPossibleCombinations)
109+
.reduce(0L, Long::sum);
110+
}
111+
100112
private String[] getIdFromDictionary() {
101113

102114
var dictionary = tokenDictionaries.get(randomGenerator.nextInt(tokenDictionaries.size()));

readable-ids-core/src/main/java/de/adrianlange/readableids/tokendictionary/AppendNumberDictionary.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ public String[] getTokensAtPositions(int[] positions) {
5252
tokensAtPositions[positionsForParent.length] = String.valueOf(positions[positionsForParent.length] + minimalValueInc);
5353
return tokensAtPositions;
5454
}
55+
56+
@Override
57+
public long getNumberOfPossibleCombinations() {
58+
59+
return parentTokenDictionary.getNumberOfPossibleCombinations() * (long) (maximalValueExc - minimalValueInc);
60+
}
5561
}

readable-ids-core/src/main/java/de/adrianlange/readableids/tokendictionary/PrependAmountGermanDictionary.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public String[] getTokensAtPositions(int[] positions) {
3737
return tokensAtPositions;
3838
}
3939

40+
@Override
41+
public long getNumberOfPossibleCombinations() {
42+
43+
return parentTokenDictionary.getNumberOfPossibleCombinations() * (long) (MAXIMAL_VALUE - MINIMAL_VALUE);
44+
}
45+
4046
private static String getAmountString(int amount) {
4147

4248
return switch (amount) {

readable-ids-core/src/main/java/de/adrianlange/readableids/tokendictionary/SimpleTokenDictionary.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ public String[] getTokensAtPositions(int[] positions) {
3232
}
3333
return tokens;
3434
}
35+
36+
@Override
37+
public long getNumberOfPossibleCombinations() {
38+
39+
long numberOfCombinations = 1;
40+
for (var tokenArray : dictionary) {
41+
numberOfCombinations *= tokenArray.length;
42+
}
43+
return numberOfCombinations;
44+
}
3545
}

readable-ids-core/src/main/java/de/adrianlange/readableids/tokendictionary/TokenDictionary.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@ public interface TokenDictionary {
2020
* @return Array of tokens, which form the ID in the end
2121
*/
2222
String[] getTokensAtPositions(int[] positions);
23+
24+
/**
25+
* Returns the number of possible combinations of tokens in the dictionary. If the dictionary is empty, it also returns <var>1</var>, since it returns the singe empty value.
26+
*
27+
* @return Number of possible combinations
28+
*/
29+
long getNumberOfPossibleCombinations();
2330
}

0 commit comments

Comments
 (0)