View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.base.io;
17  
18  import java.io.Serializable;
19  
20  
21  /**
22   * Describes an area of an URL contents.
23   *
24   * @author  Michael Meyling
25   */
26  public final class SourceArea implements Serializable {
27  
28      /** Address of input, for identifying source. */
29      private final String address;
30  
31      /** Start position. Must be not <code>null</code>. */
32      private final SourcePosition startPosition;
33  
34      /** End position. Must be not <code>null</code>. */
35      private final SourcePosition endPosition;
36  
37      /**
38       * Constructs file position object.
39       *
40       * @param   address         For identifying source. Must not be <code>null</code>.
41       * @param   startPosition   Start position. Must not be <code>null</code>.
42       * @param   endPosition     Start position. Must not be <code>null</code>.
43       */
44      public SourceArea(final String address, final SourcePosition startPosition,
45              final SourcePosition endPosition) {
46          this.address = address;
47          if (address == null || startPosition == null || endPosition == null) {
48              throw new NullPointerException();
49          }
50          this.startPosition = startPosition;
51          this.endPosition = endPosition;
52      }
53  
54      /**
55       * Constructs file position object with dummy location at begin of file.
56       *
57       * @param   address         For identifying source. Must not be <code>null</code>.
58       */
59      public SourceArea(final String address) {
60          this(address, SourcePosition.BEGIN, SourcePosition.BEGIN);
61      }
62  
63      /**
64       * Get address (or something to identify it) of input source.
65       *
66       * @return  address of input source
67       */
68      public final String getAddress() {
69          return this.address;
70      }
71  
72      /**
73       * Get start position.
74       *
75       * @return  Start position.
76       */
77      public final SourcePosition getStartPosition() {
78          return startPosition;
79      }
80  
81      /**
82       * Get end position.
83       *
84       * @return  End position.
85       */
86      public final SourcePosition getEndPosition() {
87          return endPosition;
88      }
89  
90      public final int hashCode() {
91          return getAddress().hashCode() ^ getStartPosition().hashCode() ^ getEndPosition().hashCode();
92      }
93  
94      public final boolean equals(final Object obj) {
95          if (!(obj instanceof SourceArea)) {
96              return false;
97          }
98          final SourceArea other = (SourceArea) obj;
99          return getAddress().equals(other.getAddress())
100             && getStartPosition().equals(other.getStartPosition())
101             && getEndPosition().equals(other.getEndPosition());
102     }
103 
104     /**
105      * Get short description of area. Looks like "line1:column1 - line2:column2".
106      *
107      * @return  Textual description of area without address.
108      */
109     public final String getShortDescription() {
110         return getStartPosition().getRow() + ":" + getStartPosition().getColumn()
111             + " - " + getEndPosition().getRow() + ":" + getEndPosition().getColumn();
112     }
113 
114     public final String toString() {
115         return getAddress() + ":" + getStartPosition().getRow() + ":" + getStartPosition().getColumn()
116             + ":" + getEndPosition().getRow() + ":" + getEndPosition().getColumn();
117     }
118 
119 }