public final class MappedReservedWords extends java.lang.Object implements ReservedWords
The following example shows how to use this class to make reserved words safe for the targeted code:
ReservedWords reserved = MappedReservedWords.builder()
.put("exception", "apiException")
.put("void", "void_")
.build();
String safeWord = reserved.escape("exception");
System.out.println(safeWord); // outputs "apiException"
The detection of reserved words can be made case-insensitive such that "bar", "BAR", "Bar", etc., can be detected as reserved words.
ReservedWords reserved = MappedReservedWords.builder()
.put("foo", "Hi")
.putCaseInsensitive("bar", "bam")
.build();
assert(reserved.escape("foo").equals("Hi"));
assert(reserved.escape("Foo").equals("Foo"));
assert(reserved.escape("BAR").equals("bam"));
Modifier and Type | Class and Description |
---|---|
static class |
MappedReservedWords.Builder
Builder to create a new
MappedReservedWords instance. |
Constructor and Description |
---|
MappedReservedWords(java.util.Map<java.lang.String,java.lang.String> mappings,
java.util.Map<java.lang.String,java.lang.String> caseInsensitiveMappings) |
Modifier and Type | Method and Description |
---|---|
static MappedReservedWords.Builder |
builder() |
java.lang.String |
escape(java.lang.String word)
Escapes a reserved word.
|
boolean |
isReserved(java.lang.String word)
Checks if the given word is reserved.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
compose, identity
public MappedReservedWords(java.util.Map<java.lang.String,java.lang.String> mappings, java.util.Map<java.lang.String,java.lang.String> caseInsensitiveMappings)
mappings
- Map of reserved word to replacement words.caseInsensitiveMappings
- Map of case-insensitive reserved word to replacement words.public static MappedReservedWords.Builder builder()
public java.lang.String escape(java.lang.String word)
ReservedWords
escape
in interface ReservedWords
word
- Word to escape.public boolean isReserved(java.lang.String word)
ReservedWords
isReserved
in interface ReservedWords
word
- Word to check.