Class CharRange

java.lang.Object
org.apache.commons.lang3.CharRange
All Implemented Interfaces:
Serializable, Iterable<Character>

final class CharRange extends Object implements Iterable<Character>, Serializable
A contiguous range of characters, optionally negated.

Instances are immutable.

#ThreadSafe#

Since:
1.0, 3.21.0 serialVersionUID changed from 8270183163158333422L to 2L.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final class 
    Character Iterator.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static final CharRange[]
    Empty array.
    private final char
    The last character, inclusive, in the range.
    private String
    Cached toString.
    private final boolean
    True if the range is everything except the characters specified.
    private static final long
    Required for serialization support.
    private final char
    The first character, inclusive, in the range.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    CharRange(char start, char end, boolean negated)
    Constructs a CharRange over a set of characters, optionally negating the range.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    contains(char ch)
    Is the character specified contained in this range.
    boolean
    Are all the characters of the passed in range contained in this range.
    boolean
    Compares two CharRange objects, returning true if they represent exactly the same range of characters defined in the same way.
    char
    Gets the end character for this character range.
    char
    Gets the start character for this character range.
    int
    Gets a hashCode compatible with the equals method.
    static CharRange
    is(char ch)
    Constructs a CharRange over a single character.
    (package private) boolean
    Tests whether this range denotes the empty set.
    private boolean
     
    static CharRange
    isIn(char start, char end)
    Constructs a CharRange over a set of characters.
    boolean
    Tests whether this CharRange is negated.
    static CharRange
    isNot(char ch)
    Constructs a negated CharRange over a single character.
    static CharRange
    isNotIn(char start, char end)
    Constructs a negated CharRange over a set of characters.
    private boolean
     
    Returns an iterator which can be used to walk through the characters described by this range.
    private void
    Re-asserts the start <= end invariant after default deserialization.
    Gets a string representation of the character range.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Required for serialization support. Lang version 2.0.
      Since:
      3.21.0 serialVersionUID changed from 8270183163158333422L to 2L.
      See Also:
    • EMPTY_ARRAY

      static final CharRange[] EMPTY_ARRAY
      Empty array.
    • start

      private final char start
      The first character, inclusive, in the range.
    • end

      private final char end
      The last character, inclusive, in the range.
    • negated

      private final boolean negated
      True if the range is everything except the characters specified.
    • iToString

      private transient String iToString
      Cached toString.
  • Constructor Details

    • CharRange

      private CharRange(char start, char end, boolean negated)
      Constructs a CharRange over a set of characters, optionally negating the range.

      A negated range includes everything except that defined by the start and end characters.

      If start and end are in the wrong order, they are reversed. Thus a-e is the same as e-a.

      Parameters:
      start - first character, inclusive, in this range.
      end - last character, inclusive, in this range.
      negated - true to express everything except the range.
  • Method Details

    • is

      public static CharRange is(char ch)
      Constructs a CharRange over a single character.
      Parameters:
      ch - only character in this range.
      Returns:
      The new CharRange object.
      Since:
      2.5
    • isIn

      public static CharRange isIn(char start, char end)
      Constructs a CharRange over a set of characters.

      If start and end are in the wrong order, they are reversed. Thus a-e is the same as e-a.

      Parameters:
      start - first character, inclusive, in this range.
      end - last character, inclusive, in this range.
      Returns:
      The new CharRange object.
      Since:
      2.5
    • isNot

      public static CharRange isNot(char ch)
      Constructs a negated CharRange over a single character.

      A negated range includes everything except that defined by the single character.

      Parameters:
      ch - only character in this range.
      Returns:
      The new CharRange object.
      Since:
      2.5
    • isNotIn

      public static CharRange isNotIn(char start, char end)
      Constructs a negated CharRange over a set of characters.

      A negated range includes everything except that defined by the start and end characters.

      If start and end are in the wrong order, they are reversed. Thus a-e is the same as e-a.

      Parameters:
      start - first character, inclusive, in this range.
      end - last character, inclusive, in this range.
      Returns:
      The new CharRange object.
      Since:
      2.5
    • contains

      public boolean contains(char ch)
      Is the character specified contained in this range.
      Parameters:
      ch - The character to check.
      Returns:
      true if this range contains the input character.
    • contains

      public boolean contains(CharRange range)
      Are all the characters of the passed in range contained in this range.
      Parameters:
      range - The range to check against.
      Returns:
      true if this range entirely contains the input range.
      Throws:
      NullPointerException - Thrown if null input.
    • equals

      public boolean equals(Object obj)
      Compares two CharRange objects, returning true if they represent exactly the same range of characters defined in the same way.
      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare to.
      Returns:
      true if equal.
    • getEnd

      public char getEnd()
      Gets the end character for this character range.
      Returns:
      The end char (inclusive).
    • getStart

      public char getStart()
      Gets the start character for this character range.
      Returns:
      The start char (inclusive).
    • hashCode

      public int hashCode()
      Gets a hashCode compatible with the equals method.
      Overrides:
      hashCode in class Object
      Returns:
      A suitable hashCode.
    • isEmpty

      boolean isEmpty()
      Tests whether this range denotes the empty set.

      A plain (non-negated) range always contains at least one character and is therefore never empty. A negated range is empty if and only if it excludes the entire character space, i.e. if it was created via isNotIn(Character.MIN_VALUE, Character.MAX_VALUE).

      Returns:
      true if this range contains no characters, false otherwise.
    • isEndMax

      private boolean isEndMax()
    • isNegated

      public boolean isNegated()
      Tests whether this CharRange is negated.

      A negated range includes everything except that defined by the start and end characters.

      Returns:
      true if negated.
    • isStartMin

      private boolean isStartMin()
    • iterator

      public Iterator<Character> iterator()
      Returns an iterator which can be used to walk through the characters described by this range.

      #NotThreadSafe# the iterator is not thread-safe

      Specified by:
      iterator in interface Iterable<Character>
      Returns:
      An iterator to the chars represented by this range
      Since:
      2.5
    • readObject

      private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
      Re-asserts the start <= end invariant after default deserialization. The constructor reverses reversed endpoints, so a legitimately serialized instance always has start <= end; a stream that violates this did not come from the constructor and is rejected.
      Parameters:
      in - See Serializable.
      Throws:
      IOException - Thrown as described in Serializable.
      ClassNotFoundException - Thrown as described in Serializable.
      InvalidObjectException - Thrown if start is greater than end.
    • toString

      public String toString()
      Gets a string representation of the character range.
      Overrides:
      toString in class Object
      Returns:
      string representation of this range.