Class TokenRange

java.lang.Object
com.blamejared.searchables.api.TokenRange
All Implemented Interfaces:
Comparable<TokenRange>, Iterable<TokenRange>

public final class TokenRange extends Object implements Comparable<TokenRange>, Iterable<TokenRange>
A range denoting the start and end of a Token, as well as any tokens inside that range.
  • Field Details

  • Method Details

    • at

      public static TokenRange at(int position)
      Creates a new TokenRange at the given position
      Parameters:
      position - The position of the token.
      Returns:
      a new TokenRange at the given position
    • between

      public static TokenRange between(int start, int end)
      Creates a new TokenRange with the given start and end.
      Parameters:
      start - The start of the range.
      end - The end of the range.
      Returns:
      A new TokenRange at the given start and end.
    • encompassing

      public static TokenRange encompassing(TokenRange first, TokenRange second)
      Creates a new TokenRange that covers both the first and second token.
      Parameters:
      first - The first token to cover.
      second - The second token to cover.
      Returns:
      A new TokenRange that covers both the first and second token.
    • addRange

      public void addRange(TokenRange range)
      Adds a range as a subrange to this range.
      Parameters:
      range - The range to add.
    • addRanges

      public void addRanges(Collection<TokenRange> ranges)
      Adds a collection of ranges as subranges to this range.
      Parameters:
      ranges - The ranges to add.
    • subRanges

      public Set<TokenRange> subRanges()
    • range

      public TokenRange range(int index)
      Gets the subrange at the given index.
      Parameters:
      index - The index to get.
      Returns:
      The range at the given index
      Throws:
      IndexOutOfBoundsException - if index is outside the bounds of subranges.
    • rangeIndexAtPosition

      public int rangeIndexAtPosition(int position)
      Gets the index of the range at the given position. If there are no sub ranges, this will return 0
      Parameters:
      position - The position to get.
      Returns:
      The index of the range at the given position, or 0 if
      Throws:
      IndexOutOfBoundsException - if the position is not within this range's bounds.
    • rangeAtPosition

      public TokenRange rangeAtPosition(int position)
    • simplify

      public TokenRange simplify()
      Creates a new TokenRange at this range's start and end, without any subranges.
      Returns:
    • covers

      public boolean covers(TokenRange other)
      Checks if this range fully covers the other range.
      Parameters:
      other - The range to see if this range covers.
      Returns:
      true if this range covers the other range, false otherwise.
    • contains

      public boolean contains(int position)
      Checks if this range contains the given position.
      Parameters:
      position - The position to check.
      Returns:
      true if it contains, false otherwise.
    • substring

      public String substring(String of)
      Performs a substring on the given string using this range's start and end.
      Parameters:
      of - The string to substring.
      Returns:
      The substring of the given string using this range's start and end.
    • substring

      public String substring(String of, int end)
      Performs a substring on the given string using this range's start and the given end.
      Parameters:
      of - The string to substring.
      end - The end position.
      Returns:
      The substring of the given string using this range's start and the given end.
    • delete

      public String delete(String from)
      Deletes the content of the given string that this range covers.
      Parameters:
      from - The string to delete from.
      Returns:
      The string with the contents inside this range's position removed.
    • insert

      public String insert(String to, String toInsert)
      Inserts the toInsert into the given string at this range's start position.
      Parameters:
      to - The string to insert into.
      toInsert - The string to insert.
      Returns:
      The given string with the insertion added at this range's start position.
    • replace

      public String replace(String into, String toInsert)
      Replaces the contents of the string at this range's positions with the given toInsert
      Parameters:
      into - The string to insert into
      toInsert - The string to insert.
      Returns:
      The given string with the insertion added at this range's position.
    • isEmpty

      public boolean isEmpty()
      Checks if this range is empty.
      Returns:
      true if empty, false otherwise.
    • length

      public int length()
      Gets the length of this range.
      Returns:
      the length of the range.
    • start

      public int start()
    • end

      public int end()
    • recalculate

      public TokenRange recalculate()
      Creates a new range that fully covers the subranges starts and ends.
      Returns:
      a new range that fully covers the subranges starts and ends.
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • compareTo

      public int compareTo(@NotNull @NotNull TokenRange o)
      Specified by:
      compareTo in interface Comparable<TokenRange>
    • iterator

      @NotNull public @NotNull Iterator<TokenRange> iterator()
      Specified by:
      iterator in interface Iterable<TokenRange>