jdk/src/share/classes/java/awt/font/CharArrayIterator.java
author mchung
Tue, 08 Dec 2009 09:02:09 -0800
changeset 4374 f672d9cf521e
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6907568: java/awt/KeyboardFocusManager.java inproperly merged and lost a changeset Summary: Reapply fix for 6879044 in java.awt.KeyboardFocusManager Reviewed-by: dcherepanov, asaha
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.text.CharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
class CharArrayIterator implements CharacterIterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    private char[] chars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private int begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    CharArrayIterator(char[] chars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        reset(chars, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    CharArrayIterator(char[] chars, int begin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        reset(chars, begin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Sets the position to getBeginIndex() and returns the character at that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @return the first character in the text, or DONE if the text is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @see getBeginIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public char first() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * and returns the character at that position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @return the last character in the text, or DONE if the text is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @see getEndIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public char last() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (chars.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            pos = chars.length-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Gets the character at the current position (as returned by getIndex()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return the character at the current position or DONE if the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * position is off the end of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @see getIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public char current() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (pos >= 0 && pos < chars.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return chars[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Increments the iterator's index by one and returns the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * at the new index.  If the resulting index is greater or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * to getEndIndex(), the current index is reset to getEndIndex() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * a value of DONE is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @return the character at the new position or DONE if the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * position is off the end of the text range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public char next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (pos < chars.length-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return chars[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            pos = chars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Decrements the iterator's index by one and returns the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * at the new index. If the current index is getBeginIndex(), the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * remains at getBeginIndex() and a value of DONE is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the character at the new position or DONE if the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * position is equal to getBeginIndex().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public char previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (pos > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            pos--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return chars[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Sets the position to the specified position in the text and returns that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param position the position within the text.  Valid values range from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * getBeginIndex() to getEndIndex().  An IllegalArgumentException is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * if an invalid value is supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public char setIndex(int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        position -= begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (position < 0 || position > chars.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new IllegalArgumentException("Invalid index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        pos = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Returns the start index of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return the index at which the text begins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public int getBeginIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Returns the end index of the text.  This index is the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * character following the end of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return the index after the last character in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public int getEndIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return begin+chars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Returns the current index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the current index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public int getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return begin+pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Create a copy of this iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return A copy of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        CharArrayIterator c = new CharArrayIterator(chars, begin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        c.pos = this.pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    void reset(char[] chars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        reset(chars, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    void reset(char[] chars, int begin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        this.chars = chars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this.begin = begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}