Class Range

java.lang.Object
com.github.javaparser.Range

public class Range extends Object
A range of characters in a source file, from "begin" to "end", including the characters at "begin" and "end".
  • Field Details

  • Constructor Details

    • Range

      public Range(Position begin, Position end)
      A range of characters in a source file, from "begin" to "end". This range is inclusive of the characters at the "begin" and "end" positions.

      Note that if the given parameters are reversed (i.e. the end is earlier than begin, then the values are swapped.

      Parameters:
      begin - The starting position of the range.
      end - The end position of the range.
  • Method Details

    • range

      public static Range range(Position begin, Position end)
      Create a new `Range` object using the given begin and end position.
      Parameters:
      begin - The starting position of the range.
      end - The end position of the range.
      Returns:
      A new `Range` object with the given start/end position.
    • range

      public static Range range(int beginLine, int beginColumn, int endLine, int endColumn)
      Create a new `Range` object using the given begin and end line/column values. Valid values for each parameter are per the constructor ofPosition.
      Parameters:
      beginLine - The start line of the range.
      beginColumn - The start line column of the range.
      endLine - The end line of the range.
      endColumn - The end column of the range.
      Returns:
      A new `Range` object with the given start/end position.
    • withBeginColumn

      public Range withBeginColumn(int beginColumn)
      Parameters:
      beginColumn - The value used to replace the current begin column number. Valid values are per the constructor ofPosition.
      Returns:
      A copy of this `Range` object, but with the begin column number replaced with the given column number.
    • withBeginLine

      public Range withBeginLine(int beginLine)
      Parameters:
      beginLine - The value used to replace the current begin line number. Valid values are per the constructor ofPosition.
      Returns:
      A copy of this `Range` object, but with the begin line number replaced with the given line number.
    • withEndColumn

      public Range withEndColumn(int endColumn)
      Parameters:
      endColumn - The value used to replace the current end column number. Valid values are per the constructor ofPosition.
      Returns:
      A copy of this `Range` object, but with the end column number replaced with the given line column.
    • withEndLine

      public Range withEndLine(int endLine)
      Parameters:
      endLine - The value used to replace the current end line number. Valid values are per the constructor ofPosition.
      Returns:
      A copy of this `Range` object, but with the end line number replaced with the given line number.
    • withBegin

      public Range withBegin(Position begin)
      Parameters:
      begin - The value used to replace the current begin position.
      Returns:
      A copy of this `Range` object, but with the begin position replaced with the given position.
    • withEnd

      public Range withEnd(Position end)
      Parameters:
      end - The value used to replace the current end position.
      Returns:
      A copy of this `Range` object, but with the end position replaced with the given position.
    • contains

      public boolean contains(Range other)
      Does this loosely contain the other range?

      As strictlyContains(Range), but also allow ranges which have an equal start and/or end position. In these cases, the `other` range is not strictly "inside" of this range.

    • contains

      public boolean contains(Position position)
      Does this loosely contain the other range?

      As strictlyContains(Position), but a position that is on the "edge" of this range will also pass.

      For example, if the given position is equal to the start or end position of this range. In these cases, the `other` range is not strictly "inside" of this range.

    • strictlyContains

      public boolean strictlyContains(Range other)
      Does this strictly contain the other range?

      It means that this has to be larger than other and it has to start before other and end after other.

    • strictlyContains

      public boolean strictlyContains(Position position)
      Does this strictly contain position.

      It means that the position is after the begin of this range and before the end of this range.

    • overlapsWith

      public boolean overlapsWith(Range other)
      Does the other 'Range' overlap with this 'Range'?

      If two ranges overlap, this range or the other range contains the begin or the end of the other range.

      Note that if the ends are "touching" (i.e. a begin position == end position), this counts as an overlap because the positions refer to characters, as opposed to boundary between characters.

      For example, there is an overlap at "C" in the following ranges, with "C" existing within both ranges:

       Range 1: ABC
       Range 2:   CDE
    • isBefore

      public boolean isBefore(Position position)
      Parameters:
      position - The position to compare against.
      Returns:
      True if the end of this range is before (but not equal to) the given position to compare against.
    • isAfter

      public boolean isAfter(Position position)
      Parameters:
      position - The position to compare against.
      Returns:
      True if the start of this range is after (but not equal to) the given position to compare against.
    • 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
    • getLineCount

      public int getLineCount()
      Returns:
      The number of lines that this range represents.

      If the start line and end line are the same, this range is limited to just one line.