View Javadoc

1   /*
2    * Copyright (c) 2001-2004,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package com.unitesk.atp.text.location;
33  
34  /***
35   * Text position is specified by <I>line</I> and <I>column</I>.
36   * Valid <I>lines</I> and <I>columns</I> are counted from 1.
37   *
38   * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
39   * @version $Id: Position.java,v 1.3 2004/11/15 07:53:05 all-x Exp $
40   */
41  public class Position
42  {
43      /***
44       * Stub constructor sets <I>line</I> and <I>column</I> in <code>(0,0)</code>.
45       * They are stub values because real lines and columns are counted from 1.
46       */
47      public Position()
48      {
49          line   = 0;
50          column = 0;
51      }
52  
53      /***
54       * Creates position with the specified <I>line</I> and <I>column</I>.
55       *
56       * @param line   The <I>line</I> of new position.
57       * @param column The <I>column</I> of new position.
58       */
59      public Position( int line, int column )
60      {
61          setLine( line );
62          setColumn( column );
63      }
64  
65      /***
66       * Returns the <I>line</I> of this position.
67       *
68       * @return   The <I>line</I> of this position.
69       */
70      public int getLine()
71      {
72          return line;
73      }
74  
75      /***
76       * Sets the new value of <I>line</I> of this position.
77       *
78       * @param line   value to be set as <I>line</I>.
79       */
80      public void setLine( int line )
81      {
82          this.line = line;
83      }
84  
85      /***
86       * Returns the <I>column</I> of this position.
87       *
88       * @return   The <I>column</I> of this position.
89       */
90      public int getColumn()
91      {
92          return column;
93      }
94  
95  
96      /***
97       * Sets the new value of <I>column</I> of this position.
98       *
99       * @param column   value to be set as <I>column</I>.
100      */
101     public void setColumn( int column )
102     {
103         this.column = column;
104     }
105 
106     /***
107      * Returns the string representation of this position.
108      *
109      * @return   The string representation of this position
110      *           as <code>"(${line},${column})"</code>.
111      *           Invalid (non-positive) value of <I>line</I> or <I>column</I>
112      *           is represented as <code>'?'</code>.
113      */
114     public String toString()
115     {
116         return   "("
117                + (line > 0 ? Integer.toString( line ) : "?")
118                + ","
119                + (column > 0 ? Integer.toString( column ) : "?")
120                + ")";
121     }
122     
123     /***
124      * Positions are equal if lines and columns are equal.
125      * 
126      * @param obj      The object to compare with.
127      */
128     public boolean equals( Object obj )
129     {
130         if( !(obj instanceof Position) ) return false;
131         Position pos = (Position)obj;
132         return    getLine() == pos.getLine()
133                && getColumn() == pos.getColumn();
134     }
135     
136     /***
137      * @return         This implementation returns line of position.
138      */
139     public int hashCode()
140     {
141         return getLine();
142     }
143 
144     //--------------------------------------------------------------------------
145     // implementation
146 
147     private int line;
148     private int column;
149 }