1 package java.awt.font; 2 3 import java.text.CharacterIterator ; 4 5 class CharArrayIterator implements CharacterIterator { 6 7 private char[] chars; 8 private int pos; 9 private int begin; 10 11 CharArrayIterator(char[] chars) { 12 13 reset(chars, 0); 14 } 15 16 CharArrayIterator(char[] chars, int begin) { 17 18 reset(chars, begin); 19 } 20 21 27 public char first() { 28 29 pos = 0; 30 return current(); 31 } 32 33 39 public char last() { 40 41 if (chars.length > 0) { 42 pos = chars.length-1; 43 } 44 else { 45 pos = 0; 46 } 47 return current(); 48 } 49 50 56 public char current() { 57 58 if (pos >= 0 && pos < chars.length) { 59 return chars[pos]; 60 } 61 else { 62 return DONE; 63 } 64 } 65 66 74 public char next() { 75 76 if (pos < chars.length-1) { 77 pos++; 78 return chars[pos]; 79 } 80 else { 81 pos = chars.length; 82 return DONE; 83 } 84 } 85 86 93 public char previous() { 94 95 if (pos > 0) { 96 pos--; 97 return chars[pos]; 98 } 99 else { 100 pos = 0; 101 return DONE; 102 } 103 } 104 105 113 public char setIndex(int position) { 114 115 position -= begin; 116 if (position < 0 || position > chars.length) { 117 throw new IllegalArgumentException ("Invalid index"); 118 } 119 pos = position; 120 return current(); 121 } 122 123 127 public int getBeginIndex() { 128 return begin; 129 } 130 131 136 public int getEndIndex() { 137 return begin+chars.length; 138 } 139 140 144 public int getIndex() { 145 return begin+pos; 146 } 147 148 152 public Object clone() { 153 CharArrayIterator c = new CharArrayIterator (chars, begin); 154 c.pos = this.pos; 155 return c; 156 } 157 158 void reset(char[] chars) { 159 reset(chars, 0); 160 } 161 162 void reset(char[] chars, int begin) { 163 164 this.chars = chars; 165 this.begin = begin; 166 pos = 0; 167 } 168 } 169 | Popular Tags |