jdk/src/share/classes/javax/swing/text/Segment.java
author dav
Mon, 07 Apr 2008 14:53:51 +0400
changeset 438 2ae294e4518c
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6613529: Avoid duplicate object creation within JDK packages Summary: avoid using constructors when unique values are not necessary Reviewed-by: volk, igor, peterz
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 1997-2006 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
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.text.CharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * A segment of a character array representing a fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * of text.  It should be treated as immutable even though
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * the array is directly accessible.  This gives fast access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * to fragments of text without the overhead of copying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * around characters.  This is effectively an unprotected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The Segment implements the java.text.CharacterIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * interface to support use with the i18n support without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * copying text into a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class Segment implements Cloneable, CharacterIterator, CharSequence {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * This is the array containing the text of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * interest.  This array should never be modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * it is available only for efficiency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public char[] array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * This is the offset into the array that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * the desired text begins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * This is the number of array elements that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * make up the text of interest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean partialReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates a new segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public Segment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this(null, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Creates a new segment referring to an existing array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param array the array to refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param offset the offset into the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param count the number of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public Segment(char[] array, int offset, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.array = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.count = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        partialReturn = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Flag to indicate that partial returns are valid.  If the flag is true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * an implementation of the interface method Document.getText(position,length,Segment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * should return as much text as possible without making a copy.  The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * state of the flag is false which will cause Document.getText(position,length,Segment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * to provide the same return behavior it always had, which may or may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * make a copy of the text depending upon the request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param p whether or not partial returns are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public void setPartialReturn(boolean p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        partialReturn = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Flag to indicate that partial returns are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @return whether or not partial returns are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public boolean isPartialReturn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return partialReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Converts a segment into a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (array != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return new String(array, offset, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   121
        return "";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // --- CharacterIterator methods -------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Sets the position to getBeginIndex() and returns the character at that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @return the first character in the text, or DONE if the text is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @see #getBeginIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public char first() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        pos = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return array[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * and returns the character at that position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return the last character in the text, or DONE if the text is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see #getEndIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public char last() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        pos = offset + count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            pos -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return array[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Gets the character at the current position (as returned by getIndex()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return the character at the current position or DONE if the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * position is off the end of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @see #getIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public char current() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (count != 0 && pos < offset + count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return array[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Increments the iterator's index by one and returns the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * at the new index.  If the resulting index is greater or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * to getEndIndex(), the current index is reset to getEndIndex() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * a value of DONE is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return the character at the new position or DONE if the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * position is off the end of the text range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public char next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        pos += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        int end = offset + count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (pos >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            pos = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Decrements the iterator's index by one and returns the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * at the new index. If the current index is getBeginIndex(), the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * remains at getBeginIndex() and a value of DONE is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return the character at the new position or DONE if the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * position is equal to getBeginIndex().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public char previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (pos == offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        pos -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Sets the position to the specified position in the text and returns that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param position the position within the text.  Valid values range from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * getBeginIndex() to getEndIndex().  An IllegalArgumentException is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * if an invalid value is supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public char setIndex(int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int end = offset + count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if ((position < offset) || (position > end)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throw new IllegalArgumentException("bad position: " + position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        pos = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if ((pos != end) && (count != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return array[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Returns the start index of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return the index at which the text begins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public int getBeginIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Returns the end index of the text.  This index is the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * character following the end of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return the index after the last character in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public int getEndIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return offset + count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Returns the current index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return the current index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public int getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    // --- CharSequence methods -------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public char charAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (index < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            || index >= count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new StringIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return array[offset + index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public CharSequence subSequence(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (start < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            throw new StringIndexOutOfBoundsException(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (end > count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            throw new StringIndexOutOfBoundsException(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (start > end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throw new StringIndexOutOfBoundsException(end - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        Segment segment = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        segment.array = this.array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        segment.offset = this.offset + start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        segment.count = end - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return segment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Creates a shallow copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        Object o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            o = super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } catch (CloneNotSupportedException cnse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            o = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
}