01: /*
02: * Sun Public License Notice
03: *
04: * The contents of this file are subject to the Sun Public License
05: * Version 1.0 (the "License"). You may not use this file except in
06: * compliance with the License. A copy of the License is available at
07: * http://www.sun.com/
08: *
09: * The Original Code is NetBeans. The Initial Developer of the Original
10: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
11: * Microsystems, Inc. All Rights Reserved.
12: */
13:
14: package org.netbeans.editor.ext;
15:
16: import java.text.CharacterIterator;
17:
18: /**
19: * Character-iterator that operates on the array of characters.
20: *
21: * @author Miloslav Metelka
22: * @version 1.00
23: */
24:
25: public class CharacterArrayIterator implements CharacterIterator {
26:
27: char[] chars;
28:
29: int beginIndex;
30:
31: int endIndex;
32:
33: int index;
34:
35: public CharacterArrayIterator(char[] chars, int beginIndex,
36: int endIndex) {
37: this .chars = chars;
38: this .beginIndex = beginIndex;
39: this .endIndex = endIndex;
40: index = beginIndex;
41: }
42:
43: private char currentChar() {
44: return (index >= beginIndex && index < endIndex) ? chars[index]
45: : DONE;
46: }
47:
48: public char first() {
49: index = beginIndex;
50: return currentChar();
51: }
52:
53: public char last() {
54: index = endIndex - 1;
55: return currentChar();
56: }
57:
58: public char current() {
59: return currentChar();
60: }
61:
62: public char next() {
63: index = Math.min(index + 1, endIndex);
64: return currentChar();
65: }
66:
67: public char previous() {
68: if (index <= beginIndex) {
69: return DONE;
70: } else {
71: return chars[--index];
72: }
73: }
74:
75: public char setIndex(int position) {
76: if (position < beginIndex || position >= endIndex) {
77: throw new IllegalArgumentException();
78: }
79: index = position;
80: return currentChar();
81: }
82:
83: public int getBeginIndex() {
84: return beginIndex;
85: }
86:
87: public int getEndIndex() {
88: return endIndex;
89: }
90:
91: public int getIndex() {
92: return index;
93: }
94:
95: public Object clone() {
96: return new CharacterArrayIterator(chars, beginIndex, endIndex);
97: }
98:
99: }
|