001: //Transmogrify License
002: //
003: //Copyright (c) 2001, ThoughtWorks, Inc.
004: //All rights reserved.
005: //Redistribution and use in source and binary forms, with or without
006: //modification, are permitted provided that the following conditions
007: //are met:
008: //- Redistributions of source code must retain the above copyright notice,
009: //this list of conditions and the following disclaimer.
010: //- Redistributions in binary form must reproduce the above copyright
011: //notice, this list of conditions and the following disclaimer in the
012: //documentation and/or other materials provided with the distribution.
013: //Neither the name of the ThoughtWorks, Inc. nor the names of its
014: //contributors may be used to endorse or promote products derived from this
015: //software without specific prior written permission.
016: //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
017: //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
018: //TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
019: //PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
020: //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
021: //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
022: //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
023: //OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
024: //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
025: //OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
026: //ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:
028: package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
029:
030: public class Span {
031:
032: private int _startLine;
033: private int _startColumn;
034: private int _endLine;
035: private int _endColumn;
036:
037: public Span() {
038: }
039:
040: public Span(int startLine, int startColumn, int endLine,
041: int endColumn) {
042: setStart(startLine, startColumn);
043: setEnd(endLine, endColumn);
044: }
045:
046: public Span(Span span) {
047: this (span.getStartLine(), span.getStartColumn(), span
048: .getEndLine(), span.getEndColumn());
049: }
050:
051: public void setStart(int startLine, int startColumn) {
052: _startLine = startLine;
053: _startColumn = startColumn;
054: }
055:
056: public void setEnd(int endLine, int endColumn) {
057: _endLine = endLine;
058: _endColumn = endColumn;
059: }
060:
061: public int getStartLine() {
062: return _startLine;
063: }
064:
065: public int getStartColumn() {
066: return _startColumn;
067: }
068:
069: public int getEndLine() {
070: return _endLine;
071: }
072:
073: public int getEndColumn() {
074: return _endColumn;
075: }
076:
077: public boolean contains(Span span) {
078: return (contains(span.getStartLine(), span.getStartColumn()) && contains(
079: span.getEndLine(), span.getEndColumn()));
080: }
081:
082: public boolean contains(int line, int column) {
083: boolean afterStart = false;
084: boolean beforeEnd = false;
085:
086: if (getStartLine() < line) {
087: afterStart = true;
088: } else if (getStartLine() == line && getStartColumn() <= column) {
089: afterStart = true;
090: }
091:
092: if (getEndLine() > line) {
093: beforeEnd = true;
094: } else if (getEndLine() == line && getEndColumn() >= column) {
095: beforeEnd = true;
096: }
097:
098: return (afterStart && beforeEnd);
099: }
100:
101: protected boolean startsBefore(Span span) {
102: boolean result = false;
103: if (getStartLine() < span.getStartLine()) {
104: result = true;
105: } else if (getStartLine() == span.getStartLine()
106: && getStartColumn() <= span.getStartColumn()) {
107: result = true;
108: }
109:
110: return result;
111: }
112:
113: protected boolean endsAfter(Span span) {
114: boolean result = false;
115: if (getEndLine() > span.getEndLine()) {
116: result = true;
117: } else if (getEndLine() == span.getEndLine()
118: && getEndColumn() >= span.getEndColumn()) {
119: result = true;
120: }
121:
122: return result;
123: }
124:
125: public void compose(Span span) {
126: if (span.startsBefore(this )) {
127: setStart(span.getStartLine(), span.getStartColumn());
128: }
129: if (span.endsAfter(this )) {
130: setEnd(span.getEndLine(), span.getEndColumn());
131: }
132: }
133:
134: public boolean equals(Object o) {
135: boolean result = false;
136: if (o instanceof Span) {
137: Span span = (Span) o;
138: result = (span.getStartLine() == getStartLine()
139: && span.getStartColumn() == getStartColumn()
140: && span.getEndLine() == getEndLine() && span
141: .getEndColumn() == getEndColumn());
142: }
143: return result;
144: }
145:
146: public String toString() {
147: return "[" + getStartLine() + "," + getStartColumn() + ":"
148: + getEndLine() + "," + getEndColumn() + "]";
149: }
150:
151: }
|