Class KeywordSet

java.lang.Object
org.apache.juneau.commons.collections.KeywordSet

public class KeywordSet extends Object
A specialized set for storing and efficiently looking up language keywords.

This class provides a lightweight, immutable container optimized for fast keyword lookups using binary search. Keywords are stored in a sorted array, making lookups O(log n) efficient. This is particularly useful for parsers, compilers, and syntax highlighters that need to frequently check if identifiers are reserved keywords.

Features:
  • Immutable after construction - thread-safe for concurrent reads
  • Efficient O(log n) lookups using binary search
  • Compact memory footprint using sorted array
  • Automatic rejection of null and single-character strings
Implementation Details:

Keywords are sorted lexicographically during construction and stored in an internal array. The contains(String) method uses Arrays.binarySearch(Object[], Object) for efficient lookups. Strings shorter than 2 characters are automatically rejected without performing a search, as most programming languages don't have single-character keywords.

Examples:

// Create a keyword set for Java reserved words KeywordSet javaKeywords = new KeywordSet( "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const" // ... more keywords ); // Check if identifiers are keywords if (javaKeywords.contains("class")) { // Handle keyword } // Safe handling of edge cases assertFalse(javaKeywords.contains(null)); // Returns false assertFalse(javaKeywords.contains("a")); // Single char - returns false assertFalse(javaKeywords.contains("myVar")); // Not a keyword // Use in a parser/lexer class Lexer { private static final KeywordSet KEYWORDS = new KeywordSet( "if", "else", "while", "for", "return" ); boolean isKeyword(String token) { return KEYWORDS.contains(token); } }

Use Cases:
  • Programming language parsers - checking if tokens are reserved words
  • Syntax highlighters - identifying keywords for special formatting
  • Code analyzers - distinguishing keywords from identifiers
  • Template engines - recognizing template keywords
  • Query languages - validating reserved words
Notes:
  • This class is immutable and thread-safe after construction. Multiple threads can safely call contains(String) concurrently.
  • Keywords are compared using exact string matching (case-sensitive). For case-insensitive matching, normalize your keywords and input strings to the same case.
  • The minimum keyword length is 2 characters. Single-character strings are automatically rejected.
  • Consider creating KeywordSet instances as static final constants to avoid repeated construction.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    KeywordSet(String... keywords)
    Creates a new keyword set with the specified keywords.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Checks if the specified string is a keyword in this set.
    boolean
    Compares the specified object with this keyword set for equality.
    int
    Returns the hash code value for this keyword set.
    Returns a string representation of this keyword set.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • KeywordSet

      public KeywordSet(String... keywords)
      Creates a new keyword set with the specified keywords.

      Keywords are automatically sorted during construction. Duplicate keywords are allowed but provide no benefit. For best performance, pass unique keywords.

      Example:

      // Create a keyword set for SQL keywords KeywordSet sql = new KeywordSet( "SELECT", "FROM", "WHERE", "INSERT", "UPDATE", "DELETE", "CREATE", "DROP", "TABLE", "INDEX" ); // Keywords can be passed in any order KeywordSet keywords = new KeywordSet("zebra", "apple", "banana"); assertTrue(keywords.contains("apple")); // Sorted internally

      Parameters:
      keywords - The keywords to store. Can be empty but not null. Individual keywords can be any non-null string.
  • Method Details

    • contains

      public boolean contains(String s)
      Checks if the specified string is a keyword in this set.

      This method performs an O(log n) binary search on the sorted keyword array. Null strings and strings with fewer than 2 characters are automatically rejected without performing a search.

      Example:

      KeywordSet keywords = new KeywordSet("class", "interface", "enum"); // Standard checks assertTrue(keywords.contains("class")); // Keyword exists assertFalse(keywords.contains("MyClass")); // Not a keyword // Edge cases handled gracefully assertFalse(keywords.contains(null)); // null returns false assertFalse(keywords.contains("")); // Empty string returns false assertFalse(keywords.contains("a")); // Single char returns false // Case-sensitive matching assertTrue(keywords.contains("class")); assertFalse(keywords.contains("CLASS")); // Different case

      Performance:
      • Time complexity: O(log n) using binary search
      • Space complexity: O(1) - no additional memory allocated
      • Short-circuit: Strings with length < 2 return immediately without searching
      Parameters:
      s - The string to check. Can be null.
      Returns:
      true if the string exists in this keyword set, false if it doesn't exist, is null, or has fewer than 2 characters.
    • toString

      public String toString()
      Returns a string representation of this keyword set.

      The format follows the standard Java set convention: "[keyword1, keyword2, ...]"

      Overrides:
      toString in class Object
      Returns:
      A string representation of this keyword set.
    • equals

      public boolean equals(Object o)
      Compares the specified object with this keyword set for equality.

      Returns true if the given object is also a keyword set and contains the same keywords in the same order. Two keyword sets are equal if their internal arrays are equal.

      Overrides:
      equals in class Object
      Parameters:
      o - Object to be compared for equality with this keyword set.
      Returns:
      true if the specified object is equal to this keyword set.
    • hashCode

      public int hashCode()
      Returns the hash code value for this keyword set.

      The hash code is computed from the internal array of keywords using Arrays.hashCode(Object[]). This ensures that ks1.equals(ks2) implies that ks1.hashCode()==ks2.hashCode() for any two keyword sets ks1 and ks2, as required by the general contract of Object.hashCode().

      Overrides:
      hashCode in class Object
      Returns:
      The hash code value for this keyword set.