SourceArea.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  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  * Describes an area of an URL contents.
023  *
024  @author  Michael Meyling
025  */
026 public final class SourceArea implements Serializable {
027 
028     /** Address of input, for identifying source. */
029     private final String address;
030 
031     /** Start position. Must be not <code>null</code>. */
032     private final SourcePosition startPosition;
033 
034     /** End position. Must be not <code>null</code>. */
035     private final SourcePosition endPosition;
036 
037     /**
038      * Constructs file position object.
039      *
040      @param   address         For identifying source. Must not be <code>null</code>.
041      @param   startPosition   Start position. Must not be <code>null</code>.
042      @param   endPosition     Start position. Must not be <code>null</code>.
043      */
044     public SourceArea(final String address, final SourcePosition startPosition,
045             final SourcePosition endPosition) {
046         this.address = address;
047         if (address == null || startPosition == null || endPosition == null) {
048             throw new NullPointerException();
049         }
050         this.startPosition = startPosition;
051         this.endPosition = endPosition;
052     }
053 
054     /**
055      * Constructs file position object with dummy location at begin of file.
056      *
057      @param   address         For identifying source. Must not be <code>null</code>.
058      */
059     public SourceArea(final String address) {
060         this(address, SourcePosition.BEGIN, SourcePosition.BEGIN);
061     }
062 
063     /**
064      * Get address (or something to identify it) of input source.
065      *
066      @return  address of input source
067      */
068     public final String getAddress() {
069         return this.address;
070     }
071 
072     /**
073      * Get start position.
074      *
075      @return  Start position.
076      */
077     public final SourcePosition getStartPosition() {
078         return startPosition;
079     }
080 
081     /**
082      * Get end position.
083      *
084      @return  End position.
085      */
086     public final SourcePosition getEndPosition() {
087         return endPosition;
088     }
089 
090     public final int hashCode() {
091         return getAddress().hashCode() ^ getStartPosition().hashCode() ^ getEndPosition().hashCode();
092     }
093 
094     public final boolean equals(final Object obj) {
095         if (!(obj instanceof SourceArea)) {
096             return false;
097         }
098         final SourceArea other = (SourceAreaobj;
099         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 }