1. /*
  2. * @(#)CharacterIterator.java 1.15 00/01/19
  3. *
  4. * Copyright 1996-2000 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. /*
  11. * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  12. * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  13. *
  14. * The original version of this source code and documentation
  15. * is copyrighted and owned by Taligent, Inc., a wholly-owned
  16. * subsidiary of IBM. These materials are provided under terms
  17. * of a License Agreement between Taligent and Sun. This technology
  18. * is protected by multiple US and International patents.
  19. *
  20. * This notice and attribution to Taligent may not be removed.
  21. * Taligent is a registered trademark of Taligent, Inc.
  22. *
  23. */
  24. package java.text;
  25. /**
  26. * This interface defines a protocol for bidirectional iteration over text.
  27. * The iterator iterates over a bounded sequence of characters. Characters
  28. * are indexed with values beginning with the value returned by getBeginIndex() and
  29. * continuing through the value returned by getEndIndex()-1.
  30. * <p>
  31. * Iterators maintain a current character index, whose valid range is from
  32. * getBeginIndex() to getEndIndex(); the value getEndIndex() is included to allow
  33. * handling of zero-length text ranges and for historical reasons.
  34. * The current index can be retrieved by calling getIndex() and set directly
  35. * by calling setIndex(), first(), and last().
  36. * <p>
  37. * The methods previous() and next() are used for iteration. They return DONE if
  38. * they would move outside the range from getBeginIndex() to getEndIndex() -1,
  39. * signaling that the iterator has reached the end of the sequence. DONE is
  40. * also returned by other methods to indicate that the current index is
  41. * outside this range.
  42. *
  43. * <P>Examples:<P>
  44. *
  45. * Traverse the text from start to finish
  46. * <pre>
  47. * public void traverseForward(CharacterIterator iter) {
  48. * for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
  49. * processChar(c);
  50. * }
  51. * }
  52. * </pre>
  53. *
  54. * Traverse the text backwards, from end to start
  55. * <pre>
  56. * public void traverseBackward(CharacterIterator iter) {
  57. * for(char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) {
  58. * processChar(c);
  59. * }
  60. * }
  61. * </pre>
  62. *
  63. * Traverse both forward and backward from a given position in the text.
  64. * Calls to notBoundary() in this example represents some
  65. * additional stopping criteria.
  66. * <pre>
  67. * public void traverseOut(CharacterIterator iter, int pos) {
  68. * for (char c = iter.setIndex(pos);
  69. * c != CharacterIterator.DONE && notBoundary(c);
  70. * c = iter.next()) {
  71. * }
  72. * int end = iter.getIndex();
  73. * for (char c = iter.setIndex(pos);
  74. * c != CharacterIterator.DONE && notBoundary(c);
  75. * c = iter.previous()) {
  76. * }
  77. * int start = iter.getIndex();
  78. * processSection(start, end);
  79. * }
  80. * </pre>
  81. *
  82. * @see StringCharacterIterator
  83. * @see AttributedCharacterIterator
  84. */
  85. public interface CharacterIterator extends Cloneable
  86. {
  87. /**
  88. * Constant that is returned when the iterator has reached either the end
  89. * or the beginning of the text. The unicode 2.0 standard states that
  90. * '\\uFFFF' is an invalid unicode value and should not occur in any valid
  91. * unicode string.
  92. */
  93. public static final char DONE = '\uFFFF';
  94. /**
  95. * Sets the position to getBeginIndex() and returns the character at that
  96. * position.
  97. * @return the first character in the text, or DONE if the text is empty
  98. * @see #getBeginIndex()
  99. */
  100. public char first();
  101. /**
  102. * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
  103. * and returns the character at that position.
  104. * @return the last character in the text, or DONE if the text is empty
  105. * @see #getEndIndex()
  106. */
  107. public char last();
  108. /**
  109. * Gets the character at the current position (as returned by getIndex()).
  110. * @return the character at the current position or DONE if the current
  111. * position is off the end of the text.
  112. * @see #getIndex()
  113. */
  114. public char current();
  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. /**
  125. * Decrements the iterator's index by one and returns the character
  126. * at the new index. If the current index is getBeginIndex(), the index
  127. * remains at getBeginIndex() and a value of DONE is returned.
  128. * @return the character at the new position or DONE if the current
  129. * position is equal to getBeginIndex().
  130. */
  131. public char previous();
  132. /**
  133. * Sets the position to the specified position in the text and returns that
  134. * character.
  135. * @param position the position within the text. Valid values range from
  136. * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown
  137. * if an invalid value is supplied.
  138. * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
  139. */
  140. public char setIndex(int position);
  141. /**
  142. * Returns the start index of the text.
  143. * @return the index at which the text begins.
  144. */
  145. public int getBeginIndex();
  146. /**
  147. * Returns the end index of the text. This index is the index of the first
  148. * character following the end of the text.
  149. * @return the index after the last character in the text
  150. */
  151. public int getEndIndex();
  152. /**
  153. * Returns the current index.
  154. * @return the current index.
  155. */
  156. public int getIndex();
  157. /**
  158. * Create a copy of this iterator
  159. * @return A copy of this
  160. */
  161. public Object clone();
  162. }