View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.checkstyle;
32  
33  /**
34   * Represent a line range. For example, a Java method can be described by an
35   * instance of this class. The alpha line could be the method definition and
36   * the omega line could be the end closing bracket.
37   *
38   * @since 0.16
39   */
40  public final class LineRange {
41  
42      /**
43       * The first (alpha) line number in the range.
44       */
45      private final int alpha;
46  
47      /**
48       * The last (omega) line number in the range.
49       */
50      private final int omega;
51  
52      /**
53       * Default constructor.
54       * @param first The alpha line number.
55       * @param last The omega line number.
56       */
57      public LineRange(final int first, final int last) {
58          this.alpha = first;
59          this.omega = last;
60      }
61  
62      /**
63       * Is the given line number within range.
64       * @param line The given line number to check.
65       * @return True if the given line number is within this range.
66       */
67      public boolean within(final int line) {
68          return line >= this.first() && line <= this.last();
69      }
70  
71      /**
72       * Is the given range entirely within the LineRange. Example, given a
73       * LineRange of [10, 50], the given range of [12,48] should be within
74       * side that. And the method should return true.
75       * @param range The given LineRange to check.
76       * @return True if the given is entirely within this LineRange.
77       */
78      public boolean within(final LineRange range) {
79          return range.first() >= this.first()
80              && range.last()  <= this.last();
81      }
82  
83      /**
84       * Get the alpha line number.
85       * @return The alpha line number.
86       */
87      public int first() {
88          return this.alpha;
89      }
90  
91      /**
92       * Get the omega line number.
93       * @return The omega line number.
94       */
95      public int last() {
96          return this.omega;
97      }
98  }