Showing the changes to java/pkg/java/awt/font/CharArrayIterator.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,193 @@ | |||
| 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 | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package java.awt.font; | ||
| 27 | |||
| 28 | import java.text.CharacterIterator; | ||
| 29 | |||
| 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() { | ||
| 100 | |||
| 101 | if (pos < chars.length-1) { | ||
| 102 | pos++; | ||
| 103 | return chars[pos]; | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | pos = chars.length; | ||
| 107 | return DONE; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | /** | ||
| 112 | * Decrements the iterator's index by one and returns the character | ||
| 113 | * at the new index. If the current index is getBeginIndex(), the index | ||
| 114 | * remains at getBeginIndex() and a value of DONE is returned. | ||
| 115 | * @return the character at the new position or DONE if the current | ||
| 116 | * position is equal to getBeginIndex(). | ||
| 117 | */ | ||
| 118 | public char previous() { | ||
| 119 | |||
| 120 | if (pos > 0) { | ||
| 121 | pos--; | ||
| 122 | return chars[pos]; | ||
| 123 | } | ||
| 124 | else { | ||
| 125 | pos = 0; | ||
| 126 | return DONE; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | /** | ||
| 131 | * Sets the position to the specified position in the text and returns that | ||
| 132 | * character. | ||
| 133 | * @param position the position within the text. Valid values range from | ||
| 134 | * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown | ||
| 135 | * if an invalid value is supplied. | ||
| 136 | * @return the character at the specified position or DONE if the specified position is equal to getEndIndex() | ||
| 137 | */ | ||
| 138 | public char setIndex(int position) { | ||
| 139 | |||
| 140 | position -= begin; | ||
| 141 | if (position < 0 || position > chars.length) { | ||
| 142 | throw new IllegalArgumentException("Invalid index"); | ||
| 143 | } | ||
| 144 | pos = position; | ||
| 145 | return current(); | ||
| 146 | } | ||
| 147 | |||
| 148 | /** | ||
| 149 | * Returns the start index of the text. | ||
| 150 | * @return the index at which the text begins. | ||
| 151 | */ | ||
| 152 | public int getBeginIndex() { | ||
| 153 | return begin; | ||
| 154 | } | ||
| 155 | |||
| 156 | /** | ||
| 157 | * Returns the end index of the text. This index is the index of the first | ||
| 158 | * character following the end of the text. | ||
| 159 | * @return the index after the last character in the text | ||
| 160 | */ | ||
| 161 | public int getEndIndex() { | ||
| 162 | return begin+chars.length; | ||
| 163 | } | ||
| 164 | |||
| 165 | /** | ||
| 166 | * Returns the current index. | ||
| 167 | * @return the current index. | ||
| 168 | */ | ||
| 169 | public int getIndex() { | ||
| 170 | return begin+pos; | ||
| 171 | } | ||
| 172 | |||
| 173 | /** | ||
| 174 | * Create a copy of this iterator | ||
| 175 | * @return A copy of this | ||
| 176 | */ | ||
| 177 | public Object clone() { | ||
| 178 | CharArrayIterator c = new CharArrayIterator(chars, begin); | ||
| 179 | c.pos = this.pos; | ||
| 180 | return c; | ||
| 181 | } | ||
| 182 | |||
| 183 | void reset(char[] chars) { | ||
| 184 | reset(chars, 0); | ||
| 185 | } | ||
| 186 | |||
| 187 | void reset(char[] chars, int begin) { | ||
| 188 | |||
| 189 | this.chars = chars; | ||
| 190 | this.begin = begin; | ||
| 191 | pos = 0; | ||
| 192 | } | ||
| 193 | } | ||