Class MappedReservedWords

  • All Implemented Interfaces:
    ReservedWords

    public final class MappedReservedWords
    extends java.lang.Object
    implements ReservedWords
    A reserved words implementation that maps known words to other words.

    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"));
     
     
    • Constructor Summary

      Constructors 
      Constructor Description
      MappedReservedWords​(java.util.Map<java.lang.String,​java.lang.String> mappings, java.util.Map<java.lang.String,​java.lang.String> caseInsensitiveMappings)  
    • Constructor Detail

      • MappedReservedWords

        public MappedReservedWords​(java.util.Map<java.lang.String,​java.lang.String> mappings,
                                   java.util.Map<java.lang.String,​java.lang.String> caseInsensitiveMappings)
        Parameters:
        mappings - Map of reserved word to replacement words.
        caseInsensitiveMappings - Map of case-insensitive reserved word to replacement words.
    • Method Detail

      • escape

        public java.lang.String escape​(java.lang.String word)
        Description copied from interface: ReservedWords
        Escapes a reserved word.
        Specified by:
        escape in interface ReservedWords
        Parameters:
        word - Word to escape.
        Returns:
        Returns the converted value.
      • isReserved

        public boolean isReserved​(java.lang.String word)
        Description copied from interface: ReservedWords
        Checks if the given word is reserved.
        Specified by:
        isReserved in interface ReservedWords
        Parameters:
        word - Word to check.
        Returns:
        Returns true if the word is reserved.