Source code: com/puppycrawl/tools/checkstyle/checks/usage/transmogrify/Span.java
1 //Transmogrify License
2 //
3 //Copyright (c) 2001, ThoughtWorks, Inc.
4 //All rights reserved.
5 //Redistribution and use in source and binary forms, with or without
6 //modification, are permitted provided that the following conditions
7 //are met:
8 //- Redistributions of source code must retain the above copyright notice,
9 //this list of conditions and the following disclaimer.
10 //- Redistributions in binary form must reproduce the above copyright
11 //notice, this list of conditions and the following disclaimer in the
12 //documentation and/or other materials provided with the distribution.
13 //Neither the name of the ThoughtWorks, Inc. nor the names of its
14 //contributors may be used to endorse or promote products derived from this
15 //software without specific prior written permission.
16 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 //TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 //PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 //OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 //OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 //ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
29
30 public class Span {
31
32 private int _startLine;
33 private int _startColumn;
34 private int _endLine;
35 private int _endColumn;
36
37 public Span() {}
38
39 public Span(int startLine, int startColumn, int endLine, int endColumn) {
40 setStart(startLine, startColumn);
41 setEnd(endLine, endColumn);
42 }
43
44 public Span( Span span ) {
45 this( span.getStartLine(), span.getStartColumn(),
46 span.getEndLine(), span.getEndColumn() );
47 }
48
49 public void setStart(int startLine, int startColumn) {
50 _startLine = startLine;
51 _startColumn = startColumn;
52 }
53
54 public void setEnd(int endLine, int endColumn) {
55 _endLine = endLine;
56 _endColumn = endColumn;
57 }
58
59 public int getStartLine() {
60 return _startLine;
61 }
62
63 public int getStartColumn() {
64 return _startColumn;
65 }
66
67 public int getEndLine() {
68 return _endLine;
69 }
70
71 public int getEndColumn() {
72 return _endColumn;
73 }
74
75 public boolean contains( Span span ) {
76 return ( contains(span.getStartLine(), span.getStartColumn())
77 && contains( span.getEndLine(), span.getEndColumn()) );
78 }
79
80 public boolean contains(int line, int column) {
81 boolean afterStart = false;
82 boolean beforeEnd = false;
83
84 if ( getStartLine() < line ) {
85 afterStart = true;
86 }
87 else if ( getStartLine() == line && getStartColumn() <= column ) {
88 afterStart = true;
89 }
90
91 if ( getEndLine() > line ) {
92 beforeEnd = true;
93 }
94 else if ( getEndLine() == line && getEndColumn() >= column ) {
95 beforeEnd = true;
96 }
97
98 return ( afterStart && beforeEnd );
99 }
100
101 protected boolean startsBefore( Span span ) {
102 boolean result = false;
103 if ( getStartLine() < span.getStartLine() ) {
104 result = true;
105 }
106 else if ( getStartLine() == span.getStartLine()
107 && getStartColumn() <= span.getStartColumn() ) {
108 result = true;
109 }
110
111 return result;
112 }
113
114 protected boolean endsAfter( Span span ) {
115 boolean result = false;
116 if ( getEndLine() > span.getEndLine() ) {
117 result = true;
118 }
119 else if ( getEndLine() == span.getEndLine()
120 && getEndColumn() >= span.getEndColumn() ) {
121 result = true;
122 }
123
124 return result;
125 }
126
127 public void compose( Span span ) {
128 if ( span.startsBefore( this ) ) {
129 setStart( span.getStartLine(), span.getStartColumn() );
130 }
131 if ( span.endsAfter( this ) ) {
132 setEnd( span.getEndLine(), span.getEndColumn() );
133 }
134 }
135
136 public boolean equals(Object o) {
137 boolean result = false;
138 if ( o instanceof Span ) {
139 Span span = (Span)o;
140 result = ( span.getStartLine() == getStartLine()
141 && span.getStartColumn() == getStartColumn()
142 && span.getEndLine() == getEndLine()
143 && span.getEndColumn() == getEndColumn() );
144 }
145 return result;
146 }
147
148 public String toString() {
149 return "[" + getStartLine() + "," + getStartColumn()
150 + ":" + getEndLine() + "," + getEndColumn() + "]";
151 }
152
153 }