1. /*
  2. * @(#)Segment.java 1.18 01/04/21
  3. *
  4. * Copyright 1997-2001 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.text;
  11. import java.text.CharacterIterator;
  12. /**
  13. * A segment of a character array representing a fragment
  14. * of text. It should be treated as immutable even though
  15. * the array is directly accessable. This gives fast access
  16. * to fragments of text without the overhead of copying
  17. * around characters. This is effectively an unprotected
  18. * String.
  19. * <p>
  20. * The Segment implements the java.text.CharacterIterator
  21. * interface to support use with the i18n support without
  22. * copying text into a string.
  23. *
  24. * @author Timothy Prinzing
  25. * @version 1.18 04/21/01
  26. */
  27. public class Segment implements Cloneable, CharacterIterator {
  28. /**
  29. * This is the array containing the text of
  30. * interest. This array should never be modified;
  31. * it is available only for efficiency.
  32. */
  33. public char[] array;
  34. /**
  35. * This is the offset into the array that
  36. * the desired text begins.
  37. */
  38. public int offset;
  39. /**
  40. * This is the number of array elements that
  41. * make up the text of interest.
  42. */
  43. public int count;
  44. /**
  45. * Creates a new segment.
  46. */
  47. public Segment() {
  48. array = null;
  49. offset = 0;
  50. count = 0;
  51. }
  52. /**
  53. * Creates a new segment referring to an existing array.
  54. *
  55. * @param array the array to refer to
  56. * @param offset the offset into the array
  57. * @param count the number of characters
  58. */
  59. public Segment(char[] array, int offset, int count) {
  60. this.array = array;
  61. this.offset = offset;
  62. this.count = count;
  63. }
  64. /**
  65. * Converts a segment into a String.
  66. *
  67. * @return the string
  68. */
  69. public String toString() {
  70. if (array != null) {
  71. return new String(array, offset, count);
  72. }
  73. return new String();
  74. }
  75. // --- CharacterIterator methods -------------------------------------
  76. /**
  77. * Sets the position to getBeginIndex() and returns the character at that
  78. * position.
  79. * @return the first character in the text, or DONE if the text is empty
  80. * @see #getBeginIndex
  81. */
  82. public char first() {
  83. pos = offset;
  84. if (count != 0) {
  85. return array[pos];
  86. }
  87. return DONE;
  88. }
  89. /**
  90. * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
  91. * and returns the character at that position.
  92. * @return the last character in the text, or DONE if the text is empty
  93. * @see #getEndIndex
  94. */
  95. public char last() {
  96. pos = offset + count;
  97. if (count != 0) {
  98. pos -= 1;
  99. return array[pos];
  100. }
  101. return DONE;
  102. }
  103. /**
  104. * Gets the character at the current position (as returned by getIndex()).
  105. * @return the character at the current position or DONE if the current
  106. * position is off the end of the text.
  107. * @see #getIndex
  108. */
  109. public char current() {
  110. if (count != 0 && pos < offset + count) {
  111. return array[pos];
  112. }
  113. return DONE;
  114. }
  115. /**
  116. * Increments the iterator's index by one and returns the character
  117. * at the new index. If the resulting index is greater or equal
  118. * to getEndIndex(), the current index is reset to getEndIndex() and
  119. * a value of DONE is returned.
  120. * @return the character at the new position or DONE if the new
  121. * position is off the end of the text range.
  122. */
  123. public char next() {
  124. pos += 1;
  125. int end = offset + count;
  126. if (pos >= end) {
  127. pos = end;
  128. return DONE;
  129. }
  130. return current();
  131. }
  132. /**
  133. * Decrements the iterator's index by one and returns the character
  134. * at the new index. If the current index is getBeginIndex(), the index
  135. * remains at getBeginIndex() and a value of DONE is returned.
  136. * @return the character at the new position or DONE if the current
  137. * position is equal to getBeginIndex().
  138. */
  139. public char previous() {
  140. if (pos == offset) {
  141. return DONE;
  142. }
  143. pos -= 1;
  144. return current();
  145. }
  146. /**
  147. * Sets the position to the specified position in the text and returns that
  148. * character.
  149. * @param position the position within the text. Valid values range from
  150. * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown
  151. * if an invalid value is supplied.
  152. * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
  153. */
  154. public char setIndex(int position) {
  155. int end = offset + count;
  156. if ((position < offset) || (position > end)) {
  157. throw new IllegalArgumentException("bad position: " + position);
  158. }
  159. pos = position;
  160. if ((pos != end) && (count != 0)) {
  161. return array[pos];
  162. }
  163. return DONE;
  164. }
  165. /**
  166. * Returns the start index of the text.
  167. * @return the index at which the text begins.
  168. */
  169. public int getBeginIndex() {
  170. return offset;
  171. }
  172. /**
  173. * Returns the end index of the text. This index is the index of the first
  174. * character following the end of the text.
  175. * @return the index after the last character in the text
  176. */
  177. public int getEndIndex() {
  178. return offset + count;
  179. }
  180. /**
  181. * Returns the current index.
  182. * @return the current index.
  183. */
  184. public int getIndex() {
  185. return pos;
  186. }
  187. /**
  188. * Creates a shallow copy.
  189. *
  190. * @return the copy
  191. */
  192. public Object clone() {
  193. Object o;
  194. try {
  195. o = super.clone();
  196. } catch (CloneNotSupportedException cnse) {
  197. o = null;
  198. }
  199. return o;
  200. }
  201. private int pos;
  202. }