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   * Range is specified by <I>start</I> and <I>end</I> positions.
36   *
37   * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
38   * @version $Id: Range.java,v 1.2 2004/11/15 07:53:05 all-x Exp $
39   *
40   * @see Position
41   */
42  public class Range
43  {
44      /***
45       * Stub constructor sets <I>(start,end)</I> to the stub values.
46       *
47       * @see Position#Position()
48       */
49      public Range()
50      {
51          start = new Position();
52          end   = new Position();
53      }
54  
55      /***
56       * Creates a range with the specified <I>start</I> and <I>end</I> positions.
57       *
58       * @param start   Start position of new range.
59       * @param end     End position of new range.
60       *
61       * @throws NullPointerException if <I>start</I> or <I>end</I> is <code>null</code>.
62       */
63      public Range( Position start, Position end )
64      {
65          if( start == null || end == null ) throw new NullPointerException();
66          this.start = start;
67          this.end   = end;
68      }
69  
70      /***
71       * Returns the start position of this range.
72       *
73       * @return   The <I>start</I> position of this range.
74       */
75      public Position getStart()
76      {
77          return start;
78      }
79  
80      /***
81       * Returns the end position of this range.
82       *
83       * @return   The <I>end</I> position of this range.
84       */
85      public Position getEnd()
86      {
87          return end;
88      }
89  
90      /***
91       * Returns the string representation of this range.
92       *
93       * @return   The string representation of this range
94       *           as <code>"${start}-${end}"</code>.
95       *
96       * @see Position#toString()
97       */
98      public String toString()
99      {
100         return start + "-" + end;
101     }
102     
103     /***
104      * Ranges are equal if lines and columns are equal.
105      * 
106      * @param obj      The object to compare with.
107      */
108     public boolean equals( Object obj )
109     {
110         if( !(obj instanceof Range ) ) return false;
111         Range r = (Range)obj;
112         return    getStart().equals( r.getStart() )
113                && getEnd().equals( r.getEnd() );
114     }
115     
116     /***
117      * @return         This implementation returns line number of start position.
118      */
119     public int hashCode()
120     {
121         return getStart().getLine(); 
122     }
123 
124     //--------------------------------------------------------------------------
125     // implementation
126 
127     private Position start;
128     private Position end;
129 }