001: /*
002: * SourceLocation.java - part of the CodeAid plugin.
003: * Copyright (C) 1999 Jason Ginchereau
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: //package codeaid.info;
021: package org.acm.seguin.completer.info;
022:
023: // Collections API
024: // java.lang.Comparable
025:
026: /**
027: * Encapsulates a location (file path, line, and column) in Java source code.
028: * The path may be an absolute path (such as
029: * <code>/home/me/projects/test/Test.java</code>), or it may be a relative
030: * path based on the package which the file belongs to
031: * (such as <code>java/lang/Object.java</code>).
032: *
033: * @author Jason Ginchereau
034: **/
035: public final class SourceLocation implements java.io.Serializable,
036: Comparable {
037: public static final SourceLocation UNKNOWN = new SourceLocation(
038: null, -1, -1);
039:
040: public/*final*/String path;
041: public/*final*/int line;
042: public/*final*/int col;
043: public/*final*/int endLine;
044: public/*final*/int endCol;
045:
046: public SourceLocation(String path, int line, int col) {
047: this (path, line, col, line, col);
048: }
049:
050: public SourceLocation(String path, int line, int col, int endLine,
051: int endCol) {
052: if (line == -1) {
053: endLine = -1;
054: col = -1;
055: endCol = -1;
056: } else {
057: if (col < 0) {
058: col = 0;
059: }
060: if (endCol < 0) {
061: endCol = 0;
062: }
063: }
064: if (line < -1 || endLine < line
065: || (endLine == line && endCol < col)) {
066: throw new IllegalArgumentException();
067: }
068:
069: this .path = path;
070: this .line = line;
071: this .col = col;
072: this .endLine = endLine;
073: this .endCol = endCol;
074: }
075:
076: public String getPath() {
077: return path;
078: }
079:
080: public int getLine() {
081: return line;
082: }
083:
084: public int getColumn() {
085: return col;
086: }
087:
088: public int getEndLine() {
089: return endLine;
090: }
091:
092: public int getEndColumn() {
093: return endCol;
094: }
095:
096: public boolean contains(SourceLocation sl) {
097: return ((line < sl.line || (line == sl.line && col <= sl.col)) && (endLine > sl.endLine || (endLine == sl.endLine && endCol >= sl.col)));
098: }
099:
100: public int compareTo(Object o) {
101: SourceLocation sl = (SourceLocation) o;
102: int pc = (path == null ? pc = (sl.path == null ? 0 : 1)
103: : (sl.path == null ? -1 : path.compareTo(sl.path)));
104: if (pc != 0) {
105: return pc;
106: } else if (line < sl.line) {
107: return -1;
108: } else if (line > sl.line) {
109: return 1;
110: } else if (col < sl.col) {
111: return -1;
112: } else if (col > sl.col) {
113: return 1;
114: } else if (endLine < sl.endLine) {
115: return -1;
116: } else if (endLine > sl.endLine) {
117: return 1;
118: } else if (endCol < sl.endCol) {
119: return -1;
120: } else if (endCol > sl.endCol) {
121: return 1;
122: } else {
123: return 0;
124: }
125: }
126:
127: public boolean equals(Object o) {
128: return compareTo(o) == 0;
129: }
130:
131: public int hashCode() {
132: return path.hashCode() + line + col + endLine + endCol;
133: }
134:
135: public String toString() {
136: if (this == UNKNOWN) {
137: return "UNKNOWN";
138: }
139:
140: String path = this .path;
141: if (path == null) {
142: path = "";
143: }
144: String middle = (path == null ? "" : " ");
145: String lineCol = "";
146: if (line >= 0) {
147: if (endLine == line && endCol == col) {
148: lineCol = middle + "(" + line + ", " + col + ")";
149: } else {
150: lineCol = middle + "(" + line + ", " + col + " -> "
151: + endLine + ", " + endCol + ")";
152: }
153: }
154: return path + lineCol;
155: }
156: }
|