1 /*
2 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
30 class CharArrayIterator implements CharacterIterator {
31
32 private char[] chars;
33 private int pos;
34 private int begin;
35
36 CharArrayIterator(char[] chars) {
37
38 reset(chars, 0);
39 }
40
41 CharArrayIterator(char[] chars, int begin) {
42
43 reset(chars, begin);
44 }
45
46 /**
47 * Sets the position to getBeginIndex() and returns the character at that
48 * position.
49 * @return the first character in the text, or DONE if the text is empty
50 * @see getBeginIndex
51 */
52 public char first() {
53
54 pos = 0;
55 return current();
56 }
57
58 /**
59 * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
60 * and returns the character at that position.
61 * @return the last character in the text, or DONE if the text is empty
62 * @see getEndIndex
63 */
64 public char last() {
65
66 if (chars.length > 0) {
67 pos = chars.length-1;
68 }
69 else {
70 pos = 0;
71 }
72 return current();
73 }
74
75 /**
76 * Gets the character at the current position (as returned by getIndex()).
77 * @return the character at the current position or DONE if the current
78 * position is off the end of the text.
79 * @see getIndex
80 */
81 public char current() {
82
83 if (pos >= 0 && pos < chars.length) {
84 return chars[pos];
85 }
86 else {
87 return DONE;
88 }
89 }
90
91 /**
92 * Increments the iterator's index by one and returns the character
93 * at the new index. If the resulting index is greater or equal
94 * to getEndIndex(), the current index is reset to getEndIndex() and
95 * a value of DONE is returned.
96 * @return the character at the new position or DONE if the new
97 * position is off the end of the text range.
98 */
99 public char next() {
|
1 /*
2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
30 class CharArrayIterator implements CharacterIterator {
31
32 private char[] chars;
33 private int pos;
34 private int begin;
35
36 CharArrayIterator(char[] chars) {
37
38 reset(chars, 0);
39 }
40
41 CharArrayIterator(char[] chars, int begin) {
42
43 reset(chars, begin);
44 }
45
46 /**
47 * Sets the position to getBeginIndex() and returns the character at that
48 * position.
49 * @return the first character in the text, or DONE if the text is empty
50 * @see #getBeginIndex
51 */
52 public char first() {
53
54 pos = 0;
55 return current();
56 }
57
58 /**
59 * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
60 * and returns the character at that position.
61 * @return the last character in the text, or DONE if the text is empty
62 * @see #getEndIndex
63 */
64 public char last() {
65
66 if (chars.length > 0) {
67 pos = chars.length-1;
68 }
69 else {
70 pos = 0;
71 }
72 return current();
73 }
74
75 /**
76 * Gets the character at the current position (as returned by getIndex()).
77 * @return the character at the current position or DONE if the current
78 * position is off the end of the text.
79 * @see #getIndex
80 */
81 public char current() {
82
83 if (pos >= 0 && pos < chars.length) {
84 return chars[pos];
85 }
86 else {
87 return DONE;
88 }
89 }
90
91 /**
92 * Increments the iterator's index by one and returns the character
93 * at the new index. If the resulting index is greater or equal
94 * to getEndIndex(), the current index is reset to getEndIndex() and
95 * a value of DONE is returned.
96 * @return the character at the new position or DONE if the new
97 * position is off the end of the text range.
98 */
99 public char next() {
|