View Javadoc
1   package de.fhdw.wtf.common.token;
2   
3   import java.io.Serializable;
4   
5   /**
6    * The position is the begin position of a token in the original input document.
7    */
8   public final class Position implements Serializable {
9   	
10  	/**
11  	 * Serial Version UID.
12  	 */
13  	static final long serialVersionUID = 238623487L;
14  	
15  	/**
16  	 * Line-Number in the model.
17  	 */
18  	private final int lineNumber;
19  	
20  	/**
21  	 * Position in the model.
22  	 */
23  	private final int position;
24  	
25  	/**
26  	 * Column-Number in the model.
27  	 * 
28  	 */
29  	private final int columnNumber;
30  	
31  	/**
32  	 * FilePath.
33  	 */
34  	private final String filePath;
35  	
36  	/**
37  	 * Constructor for {@link Position}.
38  	 * 
39  	 * @param filePath
40  	 *            initial file path
41  	 * @param lineNumber
42  	 *            initial line number
43  	 * @param position
44  	 *            start position of this comment in the original input
45  	 * @param columnNumber
46  	 *            initial column number
47  	 */
48  	private Position(final String filePath, final int lineNumber, final int columnNumber, final int position) {
49  		this.lineNumber = lineNumber;
50  		this.filePath = filePath;
51  		this.columnNumber = columnNumber;
52  		this.position = position;
53  	}
54  	
55  	/**
56  	 * Factory for a {@link Position}.
57  	 * 
58  	 * @param filePath
59  	 *            initial file path
60  	 * @param lineNumber
61  	 *            initial line number
62  	 * @param position
63  	 *            start position in the original input
64  	 * @param columnNumber
65  	 *            initial column number
66  	 * @return a new instance of this position
67  	 */
68  	public static Position create(final String filePath,
69  			final int lineNumber,
70  			final int columnNumber,
71  			final int position) {
72  		return new Position(filePath, lineNumber, columnNumber, position);
73  	}
74  	
75  	/**
76  	 * @return Returns the line number of a token in the original input document. Counting starts at 1.
77  	 */
78  	public int getLineNumber() {
79  		return this.lineNumber;
80  	}
81  	
82  	/**
83  	 * @return Returns the position of a token in the document. (Zero-based counting, Control sequences are counted as
84  	 *         one character)
85  	 */
86  	public int getPosition() {
87  		return this.position;
88  	}
89  	
90  	/**
91  	 * @return Returns the position of a token in the line. The line is given in {@link #getLineNumber()
92  	 *         getLineNumber()} Counting starts at 1.
93  	 */
94  	public int getColumnNumber() {
95  		return this.columnNumber;
96  	}
97  	
98  	/**
99  	 * @return Returns the file path of a token.
100 	 */
101 	public String getFilePath() {
102 		return this.filePath;
103 	}
104 	
105 	@Override
106 	public String toString() {
107 		return "[FilePath:" + this.getFilePath() + ",LineNumber:" + this.getLineNumber() + ",ColumnNumber:"
108 				+ this.getColumnNumber() + ",Position:" + this.getPosition() + "]";
109 	}
110 	
111 	@Override
112 	public boolean equals(final Object o) {
113 		if (o instanceof Position) {
114 			final Position p = (Position) o;
115 			return this.getFilePath().equals(p.getFilePath()) && this.getLineNumber() == p.getLineNumber()
116 					&& this.getPosition() == p.getPosition() && this.getColumnNumber() == p.getColumnNumber();
117 		}
118 		return false;
119 	}
120 	
121 	@Override
122 	public int hashCode() {
123 		return this.getFilePath().hashCode() ^ this.getLineNumber() ^ this.getPosition() ^ this.getColumnNumber();
124 	}
125 	
126 	/**
127 	 * @return creates a non-existent position object
128 	 */
129 	public static Position createDummyPosition() {
130 		return Position.create("", -1, -1, -1);
131 	}
132 }