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