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