View Javadoc

1   package org.modeshape.common.text;
2   
3   import net.jcip.annotations.Immutable;
4   
5   /**
6    * A class that represents the position of a particular character in terms of the lines and columns of a character sequence.
7    */
8   @Immutable
9   public final class Position {
10  	/**
11  	 * The position is used when there is no content.
12  	 */
13  	public final static Position EMPTY_CONTENT_POSITION = new Position(-1, 1, 0);
14  	
15      private final int line;
16      private final int column;
17      private final int indexInContent;
18  
19      public Position( int indexInContent, 
20      				 int line,
21                       int column ) {
22      	this.indexInContent = indexInContent < 0 ? -1:indexInContent;
23          this.line = line;
24          this.column = column;
25          
26          assert this.indexInContent >= -1;
27          assert this.line > 0;
28          assert this.column >= 0;
29          // make sure that negative index means an EMPTY_CONTENT_POSITION
30          assert this.indexInContent < 0 ? this.line == 1 && this.column == 0: true;
31      }
32      
33      /**
34       * Get the 0-based index of this position in the content character array.
35       * 
36       * @return the index; never negative except for the first position in an empty content.
37       */
38      public int getIndexInContent() {
39          return indexInContent;
40      }
41  
42      /**
43       * Get the 1-based column number of the character.
44       * 
45       * @return the column number; always positive
46       */
47      public int getColumn() {
48          return column;
49      }
50  
51      /**
52       * Get the 1-based line number of the character.
53       * 
54       * @return the line number; always positive
55       */
56      public int getLine() {
57          return line;
58      }
59  
60      /**
61       * {@inheritDoc}
62       * 
63       * @see java.lang.Object#hashCode()
64       */
65      @Override
66      public int hashCode() {
67          return indexInContent;
68      }
69  
70      /**
71       * {@inheritDoc}
72       * 
73       * @see java.lang.Object#toString()
74       */
75      @Override
76      public String toString() {
77          return "" + indexInContent + ':' + line + ':' + column;
78      }
79      
80      /**
81       * Return a new position that is the addition of this position and that supplied.
82       * 
83       * @param position the position to add to this object; may not be null
84       * @return the combined position
85       */
86      public Position add(Position position) {
87      	if( this.getIndexInContent() < 0 ) {
88      		return position.getIndexInContent() < 0 ? EMPTY_CONTENT_POSITION:position;
89      	}
90      	
91      	if( position.getIndexInContent() < 0 ) {
92      		return this;
93      	}
94  
95          int index = this.getIndexInContent() + position.getIndexInContent();
96          int line = position.getLine() + this.getLine() - 1;
97          int column = this.getLine() == 1 ? this.getColumn() + position.getColumn() : this.getColumn();
98          
99          return new Position(index, line, column);
100     }
101 }