Class IdGenerator
- java.lang.Object
-
- org.commonmark.ext.heading.anchor.IdGenerator
-
public class IdGenerator extends java.lang.Object
Generates strings to be used as identifiers.Use
builder()
to create an instance.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
IdGenerator.Builder
-
Field Summary
Fields Modifier and Type Field Description private java.util.regex.Pattern
allowedCharacters
private java.lang.String
defaultIdentifier
private java.util.Map<java.lang.String,java.lang.Integer>
identityMap
private java.lang.String
prefix
private java.lang.String
suffix
-
Constructor Summary
Constructors Modifier Constructor Description private
IdGenerator(IdGenerator.Builder builder)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static IdGenerator.Builder
builder()
private static java.util.regex.Pattern
compileAllowedCharactersPattern()
java.lang.String
generateId(java.lang.String text)
Generate an ID based on the provided text and previously generated IDs.private java.lang.String
normalizeText(java.lang.String text)
Assume we've been given a space separated text.
-
-
-
Field Detail
-
allowedCharacters
private final java.util.regex.Pattern allowedCharacters
-
identityMap
private final java.util.Map<java.lang.String,java.lang.Integer> identityMap
-
prefix
private final java.lang.String prefix
-
suffix
private final java.lang.String suffix
-
defaultIdentifier
private java.lang.String defaultIdentifier
-
-
Constructor Detail
-
IdGenerator
private IdGenerator(IdGenerator.Builder builder)
-
-
Method Detail
-
builder
public static IdGenerator.Builder builder()
- Returns:
- a new builder with default arguments
-
generateId
public java.lang.String generateId(java.lang.String text)
Generate an ID based on the provided text and previously generated IDs.
This method is not thread safe, concurrent calls can end up with non-unique identifiers.
Note that collision can occur in the case that
- Method called with 'X'
- Method called with 'X' again
- Method called with 'X-1'
In that case, the three generated IDs will be:
- X
- X-1
- X-1
Therefore if collisions are unacceptable you should ensure that numbers are stripped from end of
text
.- Parameters:
text
- Text that the identifier should be based on. Will be normalised, then used to generate the identifier.- Returns:
text
if this is the first instance that thetext
has been passed to the method. Otherwise,text + "-" + X
will be returned, where X is the number of times thattext
has previously been passed in. Iftext
is empty, the default identifier given in the constructor will be used.
-
compileAllowedCharactersPattern
private static java.util.regex.Pattern compileAllowedCharactersPattern()
-
normalizeText
private java.lang.String normalizeText(java.lang.String text)
Assume we've been given a space separated text.- Parameters:
text
- Text to normalize to an ID
-
-