Frames | No Frames |
1: /* Segment.java -- 2: Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: package javax.swing.text; 39: 40: import java.text.CharacterIterator; 41: 42: /** 43: * A text fragment represented by a sequence of characters stored in an array. 44: */ 45: public class Segment implements Cloneable, CharacterIterator 46: { 47: private boolean partialReturn; 48: 49: /** The current index. */ 50: private int current; 51: 52: /** Storage for the characters (may contain additional characters). */ 53: public char[] array; 54: 55: /** The number of characters in the segment. */ 56: public int count; 57: 58: /** The offset of the first character in the segment. */ 59: public int offset; 60: 61: /** 62: * Creates a new <code>Segment</code>. 63: */ 64: public Segment() 65: { 66: // Nothing to do here. 67: } 68: 69: /** 70: * Creates a new <code>Segment</code>. 71: * 72: * @param array the underlying character data. 73: * @param offset the offset of the first character in the segment. 74: * @param count the number of characters in the segment. 75: */ 76: public Segment(char[] array, int offset, int count) 77: { 78: this.array = array; 79: this.offset = offset; 80: this.count = count; 81: } 82: 83: /** 84: * Clones the segment (note that the underlying character array is not cloned, 85: * just the reference to it). 86: * 87: * @return A clone of the segment. 88: */ 89: public Object clone() 90: { 91: try 92: { 93: return super.clone(); 94: } 95: catch (CloneNotSupportedException e) 96: { 97: return null; 98: } 99: } 100: 101: /** 102: * Returns the character at the current index. If the segment consists of 103: * zero characters, or the current index has passed the end of the 104: * characters in the segment, this method returns {@link #DONE}. 105: * 106: * @return The character at the current index. 107: */ 108: public char current() 109: { 110: if (count == 0 111: || current >= getEndIndex()) 112: return DONE; 113: 114: return array[current]; 115: } 116: 117: /** 118: * Sets the current index to the first character in the segment and returns 119: * that character. If the segment contains zero characters, this method 120: * returns {@link #DONE}. 121: * 122: * @return The first character in the segment, or {@link #DONE} if the 123: * segment contains zero characters. 124: */ 125: public char first() 126: { 127: if (count == 0) 128: return DONE; 129: 130: current = getBeginIndex(); 131: return array[current]; 132: } 133: 134: /** 135: * Returns the index of the first character in the segment. 136: * 137: * @return The index of the first character. 138: */ 139: public int getBeginIndex() 140: { 141: return offset; 142: } 143: 144: /** 145: * Returns the end index for the segment (one position beyond the last 146: * character in the segment - note that this can be outside the range of the 147: * underlying character array). 148: * 149: * @return The end index for the segment. 150: */ 151: public int getEndIndex() 152: { 153: return offset + count; 154: } 155: 156: /** 157: * Returns the index of the current character in the segment. 158: * 159: * @return The index of the current character. 160: */ 161: public int getIndex() 162: { 163: return current; 164: } 165: 166: /** 167: * Sets the current index to point to the last character in the segment and 168: * returns that character. If the segment contains zero characters, this 169: * method returns {@link #DONE}. 170: * 171: * @return The last character in the segment, or {@link #DONE} if the 172: * segment contains zero characters. 173: */ 174: public char last() 175: { 176: if (count == 0) 177: return DONE; 178: 179: current = getEndIndex() - 1; 180: return array[current]; 181: } 182: 183: /** 184: * Sets the current index to point to the next character in the segment and 185: * returns that character. If the next character position is past the end of 186: * the segment, the index is set to {@link #getEndIndex()} and the method 187: * returns {@link #DONE}. If the segment contains zero characters, this 188: * method returns {@link #DONE}. 189: * 190: * @return The next character in the segment or {@link #DONE} (if the next 191: * character position is past the end of the segment or if the 192: * segment contains zero characters). 193: */ 194: public char next() 195: { 196: if (count == 0) 197: return DONE; 198: 199: if ((current + 1) >= getEndIndex()) 200: { 201: current = getEndIndex(); 202: return DONE; 203: } 204: 205: current++; 206: return array[current]; 207: } 208: 209: /** 210: * Sets the current index to point to the previous character in the segment 211: * and returns that character. If the current index is equal to 212: * {@link #getBeginIndex()}, or if the segment contains zero characters, this 213: * method returns {@link #DONE}. 214: * 215: * @return The previous character in the segment or {@link #DONE} (if the 216: * current character position is at the beginning of the segment or 217: * if the segment contains zero characters). 218: */ 219: public char previous() 220: { 221: if (count == 0 222: || current == getBeginIndex()) 223: return DONE; 224: 225: current--; 226: return array[current]; 227: } 228: 229: /** 230: * Sets the current index and returns the character at that position (or 231: * {@link #DONE} if the index is equal to {@link #getEndIndex()}. 232: * 233: * @param position the current position. 234: * 235: * @return The character at the specified <code>position</code>, or 236: * {@link #DONE} if <code>position</code> is equal to 237: * {@link #getEndIndex()}. 238: * 239: * @throws IllegalArgumentException if <code>position</code> is not in the 240: * range {@link #getBeginIndex()} to {@link #getEndIndex()}. 241: */ 242: public char setIndex(int position) 243: { 244: if (position < getBeginIndex() 245: || position > getEndIndex()) 246: throw new IllegalArgumentException("position: " + position 247: + ", beginIndex: " + getBeginIndex() 248: + ", endIndex: " + getEndIndex() 249: + ", text: " + toString()); 250: 251: current = position; 252: 253: if (position == getEndIndex()) 254: return DONE; 255: 256: return array[current]; 257: } 258: 259: /** 260: * Returns a <code>String</code> containing the same characters as this 261: * <code>Segment</code>. 262: * 263: * @return A <code>String</code> containing the same characters as this 264: * <code>Segment</code>. 265: */ 266: public String toString() 267: { 268: return (array != null) ? new String(array, offset, count) : ""; 269: } 270: 271: /** 272: * Sets the partial return flag. 273: * 274: * @param p the new value of the flag. 275: * 276: * @since 1.4 277: */ 278: public void setPartialReturn(boolean p) 279: { 280: partialReturn = p; 281: } 282: 283: /** 284: * Returns the partial return flag. 285: * 286: * @return The partial return flag. 287: * @since 1.4 288: */ 289: public boolean isPartialReturn() 290: { 291: return partialReturn; 292: } 293: }