Class CodePointSet

java.lang.Object
opennlp.tools.util.normalizer.CodePointSet

public final class CodePointSet extends Object
An immutable set of Unicode code points with O(1) membership.

Backed by a BitSet keyed directly by code point, so contains(int) is a single array-word read with no boxing, hashing, or branching beyond a range check. Memory is bounded by the largest member code point (the whole of Unicode would cost about 136 KiB, and the standard whitespace and dash sets are entirely or almost entirely in the Basic Multilingual Plane, so a few kilobytes in practice).

This type carries no opinion about what the code points mean. It is the explicit, standards-sourced data layer that CharClass and the reference tables (UnicodeWhitespace, UnicodeDash) are built from, and that users extend or override through fromFile(Path, String).

Since:
3.0.0
  • Field Details

    • members

      private final BitSet members
  • Constructor Details

    • CodePointSet

      private CodePointSet(BitSet members)
  • Method Details

    • of

      public static CodePointSet of(int... codePoints)
      Creates a set from explicit code points.
      Parameters:
      codePoints - The code points to include.
      Returns:
      The set.
      Throws:
      IllegalArgumentException - Thrown if codePoints is null, or if any value is not a valid Unicode code point (outside [0, U+10FFFF]).
    • ofRange

      public static CodePointSet ofRange(int firstInclusive, int lastInclusive)
      Creates a set covering an inclusive code point range.
      Parameters:
      firstInclusive - The first code point in the range.
      lastInclusive - The last code point in the range.
      Returns:
      The set.
      Throws:
      IllegalArgumentException - Thrown if either bound is invalid or firstInclusive is greater than lastInclusive.
    • fromFile

      public static CodePointSet fromFile(Path definitions, String section) throws IOException
      Loads the code points declared under one section of a user definitions file.

      The format is line oriented: a [name] line opens a section; a # begins a comment that runs to end of line; each remaining line is a single hex code point (U+00A0, 0x00A0, or 00A0) or an inclusive range (U+2000-U+200A). Section names match case insensitively. Only entries under the requested section are returned, so one file can carry, for example, both [whitespace] and [dash] sections.

      Parameters:
      definitions - The file to read (UTF-8).
      section - The section whose entries should be loaded.
      Returns:
      The code points declared under section, or an empty set if the section is absent.
      Throws:
      IOException - Thrown if the file cannot be read.
      IllegalArgumentException - Thrown if definitions or section is null, or if a line is malformed, naming the offending line.
    • parse

      static CodePointSet parse(List<String> lines, String section)
    • addEntry

      private static void addEntry(BitSet members, String line, int lineNumber, String raw)
    • parseCodePoint

      private static int parseCodePoint(String token, int lineNumber, String raw)
      Parses one code point entry in hex, optionally prefixed with U+ or 0x.
      Parameters:
      token - The entry text, already stripped of surrounding whitespace.
      lineNumber - The one-based line number, for the error message.
      raw - The complete line, for the error message.
      Returns:
      The code point.
      Throws:
      IllegalArgumentException - Thrown if the entry has no digits, holds a character that is not a hex digit, or names a value outside [0, U+10FFFF].
    • hasCodePointPrefix

      private static boolean hasCodePointPrefix(String token)
      Checks for the optional U+ or 0x prefix of a code point entry.
      Parameters:
      token - The entry text.
      Returns:
      true if token starts with U+ or 0x, in either case.
    • malformed

      private static IllegalArgumentException malformed(String what, int lineNumber, String raw)
    • requireValid

      static void requireValid(int codePoint)
      Validates that codePoint is a Unicode code point.
      Parameters:
      codePoint - The value to validate.
      Throws:
      IllegalArgumentException - Thrown if codePoint is negative or greater than Character.MAX_CODE_POINT.
    • contains

      public boolean contains(int codePoint)
      Tests membership.
      Parameters:
      codePoint - The code point to test. Out-of-range values return false.
      Returns:
      true if the code point is in this set.
    • union

      public CodePointSet union(CodePointSet other)
      Returns a new set containing every code point in this set or other.
      Parameters:
      other - The set to union with.
      Returns:
      The union, a new set; neither input is modified.
      Throws:
      IllegalArgumentException - Thrown if other is null.
    • size

      public int size()
      Returns the number of code points in this set.
      Returns:
      the number of code points in this set
    • isEmpty

      public boolean isEmpty()
      Returns whether this set is empty.
      Returns:
      whether this set is empty
    • toArray

      public int[] toArray()
      Returns the member code points, in ascending order.
      Returns:
      the member code points, in ascending order
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object