Source for javax.swing.text.Segment

   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, the 
 169:    * current index is set to {@link #getEndIndex()} and this method returns 
 170:    * {@link #DONE}.
 171:    * 
 172:    * @return The last character in the segment, or {@link #DONE} if the 
 173:    *         segment contains zero characters.
 174:    */
 175:   public char last()
 176:   {
 177:     if (count == 0)
 178:       {
 179:         current = getEndIndex();
 180:         return DONE;
 181:       }
 182:     
 183:     current = getEndIndex() - 1;
 184:     return array[current];
 185:   }
 186: 
 187:   /**
 188:    * Sets the current index to point to the next character in the segment and 
 189:    * returns that character.  If the next character position is past the end of
 190:    * the segment, the index is set to {@link #getEndIndex()} and the method
 191:    * returns {@link #DONE}.  If the segment contains zero characters, this 
 192:    * method returns {@link #DONE}.
 193:    * 
 194:    * @return The next character in the segment or {@link #DONE} (if the next
 195:    *         character position is past the end of the segment or if the 
 196:    *         segment contains zero characters).
 197:    */
 198:   public char next()
 199:   {
 200:     if (count == 0)
 201:       return DONE;
 202: 
 203:     if ((current + 1) >= getEndIndex())
 204:       {
 205:     current = getEndIndex();
 206:     return DONE;
 207:       }
 208:     
 209:     current++;
 210:     return array[current];
 211:   }
 212: 
 213:   /**
 214:    * Sets the current index to point to the previous character in the segment 
 215:    * and returns that character.  If the current index is equal to 
 216:    * {@link #getBeginIndex()}, or if the segment contains zero characters, this 
 217:    * method returns {@link #DONE}.
 218:    * 
 219:    * @return The previous character in the segment or {@link #DONE} (if the 
 220:    *         current character position is at the beginning of the segment or 
 221:    *         if the segment contains zero characters).
 222:    */
 223:   public char previous()
 224:   {
 225:     if (count == 0
 226:     || current == getBeginIndex())
 227:       return DONE;
 228:     
 229:     current--;
 230:     return array[current];
 231:   }
 232: 
 233:   /**
 234:    * Sets the current index and returns the character at that position (or
 235:    * {@link #DONE} if the index is equal to {@link #getEndIndex()}.
 236:    * 
 237:    * @param position  the current position.
 238:    * 
 239:    * @return The character at the specified <code>position</code>, or
 240:    *         {@link #DONE} if <code>position</code> is equal to 
 241:    *         {@link #getEndIndex()}.
 242:    *         
 243:    * @throws IllegalArgumentException if <code>position</code> is not in the
 244:    *         range {@link #getBeginIndex()} to {@link #getEndIndex()}.
 245:    */
 246:   public char setIndex(int position)
 247:   {
 248:     if (position < getBeginIndex()
 249:     || position > getEndIndex())
 250:       throw new IllegalArgumentException("position: " + position
 251:                                          + ", beginIndex: " + getBeginIndex()
 252:                                          + ", endIndex: " + getEndIndex()
 253:                                          + ", text: " + toString());
 254: 
 255:     current = position;
 256: 
 257:     if (position == getEndIndex())
 258:       return DONE;
 259:     
 260:     return array[current];
 261:   }
 262: 
 263:   /**
 264:    * Returns a <code>String</code> containing the same characters as this 
 265:    * <code>Segment</code>.
 266:    * 
 267:    * @return A <code>String</code> containing the same characters as this 
 268:    *         <code>Segment</code>.
 269:    */
 270:   public String toString()
 271:   {
 272:     return (array != null) ? new String(array, offset, count) : "";
 273:   }
 274: 
 275:   /**
 276:    * Sets the partial return flag.
 277:    * 
 278:    * @param p  the new value of the flag.
 279:    * 
 280:    * @since 1.4
 281:    */
 282:   public void setPartialReturn(boolean p)
 283:   {
 284:     partialReturn = p;
 285:   }
 286:   
 287:   /**
 288:    * Returns the partial return flag.
 289:    * 
 290:    * @return The partial return flag.
 291:    * @since 1.4
 292:    */
 293:   public boolean isPartialReturn()
 294:   {
 295:     return partialReturn;
 296:   }
 297: }