View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   /**
8    * Represent a line range. For example, a Java method can be described by an
9    * instance of this class. The alpha line could be the method definition and
10   * the omega line could be the end closing bracket.
11   *
12   * @since 0.16
13   */
14  public final class LineRange {
15  
16      /**
17       * The first (alpha) line number in the range.
18       */
19      private final int alpha;
20  
21      /**
22       * The last (omega) line number in the range.
23       */
24      private final int omega;
25  
26      /**
27       * Default constructor.
28       *
29       * @param first The alpha line number
30       * @param last The omega line number
31       */
32      public LineRange(final int first, final int last) {
33          this.alpha = first;
34          this.omega = last;
35      }
36  
37      /**
38       * Is the given line number within range.
39       *
40       * @param line The given line number to check
41       * @return True if the given line number is within this range
42       */
43      public boolean within(final int line) {
44          return line >= this.first() && line <= this.last();
45      }
46  
47      /**
48       * Is the given range entirely within the LineRange. Example, given a
49       * LineRange of [10, 50], the given range of [12,48] should be within
50       * side that. And the method should return true.
51       *
52       * @param range The given LineRange to check
53       * @return True if the given is entirely within this LineRange
54       */
55      public boolean within(final LineRange range) {
56          return range.first() >= this.first()
57              && range.last() <= this.last();
58      }
59  
60      /**
61       * Get the alpha line number.
62       *
63       * @return The alpha line number
64       */
65      public int first() {
66          return this.alpha;
67      }
68  
69      /**
70       * Get the omega line number.
71       *
72       * @return The omega line number
73       */
74      public int last() {
75          return this.omega;
76      }
77  }