1. package java.awt.font;
  2. import java.text.CharacterIterator;
  3. class CharArrayIterator implements CharacterIterator {
  4. private char[] chars;
  5. private int pos;
  6. private int begin;
  7. CharArrayIterator(char[] chars) {
  8. reset(chars, 0);
  9. }
  10. CharArrayIterator(char[] chars, int begin) {
  11. reset(chars, begin);
  12. }
  13. /**
  14. * Sets the position to getBeginIndex() and returns the character at that
  15. * position.
  16. * @return the first character in the text, or DONE if the text is empty
  17. * @see getBeginIndex
  18. */
  19. public char first() {
  20. pos = 0;
  21. return current();
  22. }
  23. /**
  24. * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
  25. * and returns the character at that position.
  26. * @return the last character in the text, or DONE if the text is empty
  27. * @see getEndIndex
  28. */
  29. public char last() {
  30. if (chars.length > 0) {
  31. pos = chars.length-1;
  32. }
  33. else {
  34. pos = 0;
  35. }
  36. return current();
  37. }
  38. /**
  39. * Gets the character at the current position (as returned by getIndex()).
  40. * @return the character at the current position or DONE if the current
  41. * position is off the end of the text.
  42. * @see getIndex
  43. */
  44. public char current() {
  45. if (pos >= 0 && pos < chars.length) {
  46. return chars[pos];
  47. }
  48. else {
  49. return DONE;
  50. }
  51. }
  52. /**
  53. * Increments the iterator's index by one and returns the character
  54. * at the new index. If the resulting index is greater or equal
  55. * to getEndIndex(), the current index is reset to getEndIndex() and
  56. * a value of DONE is returned.
  57. * @return the character at the new position or DONE if the new
  58. * position is off the end of the text range.
  59. */
  60. public char next() {
  61. if (pos < chars.length-1) {
  62. pos++;
  63. return chars[pos];
  64. }
  65. else {
  66. pos = chars.length;
  67. return DONE;
  68. }
  69. }
  70. /**
  71. * Decrements the iterator's index by one and returns the character
  72. * at the new index. If the current index is getBeginIndex(), the index
  73. * remains at getBeginIndex() and a value of DONE is returned.
  74. * @return the character at the new position or DONE if the current
  75. * position is equal to getBeginIndex().
  76. */
  77. public char previous() {
  78. if (pos > 0) {
  79. pos--;
  80. return chars[pos];
  81. }
  82. else {
  83. pos = 0;
  84. return DONE;
  85. }
  86. }
  87. /**
  88. * Sets the position to the specified position in the text and returns that
  89. * character.
  90. * @param position the position within the text. Valid values range from
  91. * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown
  92. * if an invalid value is supplied.
  93. * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
  94. */
  95. public char setIndex(int position) {
  96. position -= begin;
  97. if (position < 0 || position > chars.length) {
  98. throw new IllegalArgumentException("Invalid index");
  99. }
  100. pos = position;
  101. return current();
  102. }
  103. /**
  104. * Returns the start index of the text.
  105. * @return the index at which the text begins.
  106. */
  107. public int getBeginIndex() {
  108. return begin;
  109. }
  110. /**
  111. * Returns the end index of the text. This index is the index of the first
  112. * character following the end of the text.
  113. * @return the index after the last character in the text
  114. */
  115. public int getEndIndex() {
  116. return begin+chars.length;
  117. }
  118. /**
  119. * Returns the current index.
  120. * @return the current index.
  121. */
  122. public int getIndex() {
  123. return begin+pos;
  124. }
  125. /**
  126. * Create a copy of this iterator
  127. * @return A copy of this
  128. */
  129. public Object clone() {
  130. CharArrayIterator c = new CharArrayIterator(chars, begin);
  131. c.pos = this.pos;
  132. return c;
  133. }
  134. void reset(char[] chars) {
  135. reset(chars, 0);
  136. }
  137. void reset(char[] chars, int begin) {
  138. this.chars = chars;
  139. this.begin = begin;
  140. pos = 0;
  141. }
  142. }