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  import com.google.common.base.Predicate;
34  import com.google.common.collect.FluentIterable;
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.Iterator;
38  
39  /**
40   * Represents a set of LineRange objects. For example, an instance of this class
41   * could represent all the line ranges for methods in a given Java source code
42   * file.
43   *
44   * @since 0.16
45   */
46  public final class LineRanges {
47  
48      /**
49       * ArrayList of line ranges.
50       */
51      private final LineRanges.LocalCollection lines =
52          new LineRanges.LocalCollection();
53  
54      /**
55       * Adds a line range to the collection.
56       * @param line The line range to add to the collection
57       */
58      public void add(final LineRange line) {
59          this.lines.collection().add(line);
60      }
61  
62      /**
63       * Returns an iterator for this collection.
64       * @return Iterator pointing to the internal collections elements.
65       */
66      public Iterator<LineRange> iterator() {
67          return this.lines.collection().iterator();
68      }
69  
70      /**
71       * Detects if the given line number is within any of the line ranges.
72       * @param line The given line number to check
73       * @return True if the given line number is within any line range.
74       */
75      public boolean inRange(final int line) {
76          return !this.lines.collection().isEmpty()
77              && FluentIterable.from(this.lines.collection())
78              .anyMatch(new LineRanges.LineWithAny(line));
79      }
80  
81      /**
82       * Gets the subset of LineRanges that are within all given ranges. Does
83       * not return null; instead, returns empty range if there are no matches.
84       * @param ranges The ranges to filter on.
85       * @return Returns all LineRange elements that are within range.
86       */
87      public LineRanges within(final LineRanges ranges) {
88          final LineRanges result = new LineRanges();
89          final Iterator<LineRange> iterator = ranges.iterator();
90          while (iterator.hasNext()) {
91              final LineRange next = iterator.next();
92              for (final LineRange line : this.lines.collection()) {
93                  if (next.within(line)) {
94                      result.add(line);
95                  }
96              }
97          }
98          return result;
99      }
100 
101     /**
102      * Clears the collection.
103      */
104     public void clear() {
105         this.lines.collection().clear();
106     }
107 
108     /**
109      * Predicate to determine if a given line is within range of any of
110      * the line ranges.
111      *
112      * @since 0.1
113      */
114     private static final class LineWithAny implements Predicate<LineRange> {
115 
116         /**
117          * The given line.
118          */
119         private final int given;
120 
121         /**
122          * Default constructor.
123          * @param line The given line to check against all the line ranges.
124          */
125         private LineWithAny(final int line) {
126             this.given = line;
127         }
128 
129         @Override
130         public boolean apply(final LineRange range) {
131             return range != null && range.within(this.given);
132         }
133     }
134 
135     /**
136      * Thread-safe collection of line ranges.
137      *
138      * @since 0.1
139      */
140     private static final class LocalCollection
141         extends ThreadLocal<Collection<LineRange>> {
142 
143         /**
144          * Internal Collection.
145          */
146         private final transient Collection<LineRange> ranges =
147             new ArrayList<>(20);
148 
149         /**
150          * Get the collection specific to the current thread only.
151          * @return The collection for this thread.
152          */
153         public Collection<LineRange> collection() {
154             return this.ranges;
155         }
156     }
157 }