SourceArea.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.base.io;
017 
018 import java.io.Serializable;
019 
020 
021 
022 /**
023  * Describes an area of an URL contents.
024  *
025  @author  Michael Meyling
026  */
027 public final class SourceArea implements Serializable {
028 
029     /** Address of input, for identifying source. */
030     private final String address;
031 
032     /** Start position. */
033     private final SourcePosition startPosition;
034 
035     /** End position. Might be <code>null</code>. */
036     private final SourcePosition endPosition;
037 
038     /**
039      * Constructs file position object.
040      *
041      @param   address         For identifying source. Must not be <code>null</code>.
042      @param   startPosition   Start position. Must not be <code>null</code>.
043      @param   endPosition     Start position. Must not be <code>null</code>.
044      */
045     public SourceArea(final String address, final SourcePosition startPosition,
046             final SourcePosition endPosition) {
047         this.address = address;
048         if (address == null || startPosition == null || endPosition == null) {
049             throw new NullPointerException();
050         }
051         this.startPosition = startPosition;
052         this.endPosition = endPosition;
053     }
054 
055     /**
056      * Constructs file position object with dummy location at begin of file.
057      *
058      @param   address         For identifying source. Must not be <code>null</code>.
059      */
060     public SourceArea(final String address) {
061         this(address, SourcePosition.BEGIN, SourcePosition.BEGIN);
062     }
063 
064     /**
065      * Get address (or something to identify it) of input source.
066      *
067      @return  address of input source
068      */
069     public final String getAddress() {
070         return this.address;
071     }
072 
073     /**
074      * Get start position.
075      *
076      @return  Start position.
077      */
078     public final SourcePosition getStartPosition() {
079         return startPosition;
080     }
081 
082     /**
083      * Get end position.
084      *
085      @return  End position.
086      */
087     public final SourcePosition getEndPosition() {
088         return endPosition;
089     }
090 
091     public final int hashCode() {
092         return getAddress().hashCode() ^ getStartPosition().hashCode() ^ getEndPosition().hashCode();
093     }
094 
095     public final boolean equals(final Object obj) {
096         if (!(obj instanceof SourceArea)) {
097             return false;
098         }
099         final SourceArea other = (SourceAreaobj;
100         return getAddress().equals(other.getAddress())
101             && getStartPosition().equals(other.getStartPosition())
102             && getEndPosition().equals(other.getEndPosition());
103     }
104 
105     /**
106      * Get short description of area. Looks like "line1:column1 - line2:column2".
107      *
108      @return  Textual description of area without address.
109      */
110     public final String getShortDescription() {
111         return getStartPosition().getRow() ":" + getStartPosition().getColumn()
112             " - " + getEndPosition().getRow() ":" + getEndPosition().getColumn();
113     }
114 
115     public final String toString() {
116         return getAddress() ":" + getStartPosition().getRow() ":" + getStartPosition().getColumn()
117             ":" + getEndPosition().getRow() ":" + getEndPosition().getColumn();
118     }
119 
120 }