SubTextInput.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 
019 
020 /**
021  * Wraps a text output stream.
022  *
023  @author  Michael Meyling
024  */
025 public class SubTextInput extends TextInput {
026 
027     /** Absolute start of this input relative to base {@link SubTextInput}. */
028     private final int absoluteStart;
029 
030     /** Absolute end of this input relative to base {@link SubTextInput}. */
031     private final int absoluteEnd;
032 
033     /**
034      * Constructor.
035      *
036      @param   source  Text input.
037      */
038     public SubTextInput(final String source) {
039         super(source);
040         this.absoluteStart = 0;
041         this.absoluteEnd = source.length();
042     }
043 
044     /**
045      * Constructor.
046      *
047      @param   original    Parent input.
048      @param   absoluteStart       Input part starts here.
049      @param   absoluteEnd         Input part ends here.
050      */
051     public SubTextInput(final SubTextInput original, final int absoluteStart, final int absoluteEnd) {
052         super(original.getAbsoluteSubstring(absoluteStart, absoluteEnd));
053         this.absoluteStart = absoluteStart;
054         this.absoluteEnd = absoluteEnd;
055     }
056 
057     /**
058      * Get the absolute start position of the current {@link SubTextInput}.
059      *
060      @return  Absolute start position.
061      */
062     public int getAbsoluteStart() {
063         return absoluteStart;
064     }
065 
066     /**
067      * Get the absolute end position of the current {@link SubTextInput}.
068      *
069      @return  Absolute end position.
070      */
071     public int getAbsoluteEnd() {
072         return absoluteEnd;
073     }
074 
075     /**
076      * Get the absolute position of the current position.
077      *
078      @return  Absolute position of current position.
079      */
080     public int getAbsolutePosition() {
081         return getAbsoluteStart() + getPosition();
082     }
083 
084     /**
085      * Set the current position by calculating the relative position
086      * from the given absolute position.
087      *
088      @param   absolutePosition    This should be the absolute position.
089      */
090     public void setAbsolutePosition(final int absolutePosition) {
091         setPosition(absolutePosition - getAbsoluteStart());
092     }
093 
094     /**
095      * Get sub string of source. The given parameters have values for the underlying original
096      * SubTextInput at the base.
097      *
098      @param absoluteFrom  Absolute start of sub string.
099      @param absoluteTo    Absolute end of sub string.
100      @return  Sub string.
101      */
102     public String getAbsoluteSubstring(final int absoluteFrom, final int absoluteTo) {
103         return getSubstring(absoluteFrom - getAbsoluteStart(), absoluteTo - getAbsoluteStart());
104     }
105 
106     /**
107      * Get sub string of source as a new {@link SubTextInput}. The given parameters have
108      * values for the underlying original SubTextInput at the base.
109      *
110      @param absoluteFrom  Absolute start of sub string.
111      @param absoluteTo    Absolute end of sub string.
112      @return  Sub string.
113      */
114     public SubTextInput getSubTextInput(final int absoluteFrom, final int absoluteTo) {
115         return new SubTextInput(this, absoluteFrom, absoluteTo);
116     }
117 
118 }