jdk/src/share/classes/java/text/AttributedString.java
author dbhole
Thu, 24 May 2012 19:00:16 -0700
changeset 12848 da701d422d2c
parent 10419 12c063b39232
child 14342 8435a30053c1
permissions -rw-r--r--
7117230: clean up warnings in java.text Reviewed-by: jrose, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.text.AttributedCharacterIterator.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An AttributedString holds text and related attribute information. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * may be used as the actual data storage in some cases where a text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * reader wants to access attributed text through the AttributedCharacterIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * An attribute is a key/value pair, identified by the key.  No two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * attributes on a given character can have the same key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>The values for an attribute are immutable, or must not be mutated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * by clients or storage.  They are always passed by reference, and not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see AttributedCharacterIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see Annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class AttributedString {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // since there are no vectors of int, we have to use arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // We allocate them in chunks of 10 elements so we don't have to allocate all the time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int ARRAY_SIZE_INCREMENT = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // field holding the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    String text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // fields holding run attribute information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // run attributes are organized by run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int runArraySize;               // current size of the arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int runCount;                   // actual number of runs, <= runArraySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int runStarts[];                // start index for each run
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
    64
    Vector<Attribute> runAttributes[];         // vector of attribute keys for each run
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
    65
    Vector<Object> runAttributeValues[];    // parallel vector of attribute values for each run
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Constructs an AttributedString instance with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * AttributedCharacterIterators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param iterators AttributedCharacterIterators to construct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * AttributedString from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @throws NullPointerException if iterators is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    AttributedString(AttributedCharacterIterator[] iterators) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (iterators == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new NullPointerException("Iterators must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (iterators.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            text = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // Build the String contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            StringBuffer buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            for (int counter = 0; counter < iterators.length; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                appendContents(buffer, iterators[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            text = buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (text.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                // Determine the runs, creating a new run when the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                // differ.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                int offset = 0;
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
    95
                Map<Attribute,Object> last = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                for (int counter = 0; counter < iterators.length; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    AttributedCharacterIterator iterator = iterators[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    int start = iterator.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    int end = iterator.getEndIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    int index = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    while (index < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        iterator.setIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   106
                        Map<Attribute,Object> attrs = iterator.getAttributes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        if (mapsDiffer(last, attrs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            setAttributes(attrs, index - start + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        last = attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        index = iterator.getRunLimit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    offset += (end - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Constructs an AttributedString instance with the given text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param text The text for this attributed string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @exception NullPointerException if <code>text</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public AttributedString(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (text == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Constructs an AttributedString instance with the given text and attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param text The text for this attributed string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param attributes The attributes that apply to the entire string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @exception NullPointerException if <code>text</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *            <code>attributes</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception IllegalArgumentException if the text has length 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * and the attributes parameter is not an empty Map (attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * cannot be applied to a 0-length range).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public AttributedString(String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                            Map<? extends Attribute, ?> attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (text == null || attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (text.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (attributes.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new IllegalArgumentException("Can't add attribute to 0-length text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        int attributeCount = attributes.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (attributeCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            createRunAttributeDataVectors();
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   159
            Vector<Attribute> newRunAttributes = new Vector<>(attributeCount);
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   160
            Vector<Object> newRunAttributeValues = new Vector<>(attributeCount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            runAttributes[0] = newRunAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            runAttributeValues[0] = newRunAttributeValues;
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   163
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   164
            Iterator<? extends Map.Entry<? extends Attribute, ?>> iterator = attributes.entrySet().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            while (iterator.hasNext()) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   166
                Map.Entry<? extends Attribute, ?> entry = iterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                newRunAttributes.addElement(entry.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                newRunAttributeValues.addElement(entry.getValue());
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Constructs an AttributedString instance with the given attributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * text represented by AttributedCharacterIterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param text The text for this attributed string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @exception NullPointerException if <code>text</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public AttributedString(AttributedCharacterIterator text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // If performance is critical, this constructor should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // implemented here rather than invoking the constructor for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // subrange. We can avoid some range checking in the loops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        this(text, text.getBeginIndex(), text.getEndIndex(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Constructs an AttributedString instance with the subrange of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * the given attributed text represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * AttributedCharacterIterator. If the given range produces an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * empty text, all attributes will be discarded.  Note that any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * attributes wrapped by an Annotation object are discarded for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * subrange of the original attribute range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param text The text for this attributed string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param beginIndex Index of the first character of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param endIndex Index of the character following the last character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @exception NullPointerException if <code>text</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @exception IllegalArgumentException if the subrange given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * beginIndex and endIndex is out of the text range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @see java.text.Annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public AttributedString(AttributedCharacterIterator text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            int beginIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                            int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        this(text, beginIndex, endIndex, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Constructs an AttributedString instance with the subrange of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * the given attributed text represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * AttributedCharacterIterator.  Only attributes that match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * given attributes will be incorporated into the instance. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * given range produces an empty text, all attributes will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * discarded. Note that any attributes wrapped by an Annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * object are discarded for a subrange of the original attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param text The text for this attributed string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param beginIndex Index of the first character of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param endIndex Index of the character following the last character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param attributes Specifies attributes to be extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * from the text. If null is specified, all available attributes will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @exception NullPointerException if <code>text</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @exception IllegalArgumentException if the subrange given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * beginIndex and endIndex is out of the text range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @see java.text.Annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public AttributedString(AttributedCharacterIterator text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            int beginIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            int endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                            Attribute[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (text == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Validate the given subrange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int textBeginIndex = text.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int textEndIndex = text.getEndIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (beginIndex < textBeginIndex || endIndex > textEndIndex || beginIndex > endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throw new IllegalArgumentException("Invalid substring range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // Copy the given string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        StringBuffer textBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        text.setIndex(beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        for (char c = text.current(); text.getIndex() < endIndex; c = text.next())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            textBuffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        this.text = textBuffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (beginIndex == endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        // Select attribute keys to be taken care of
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   256
        HashSet<Attribute> keys = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            keys.addAll(text.getAllAttributeKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            for (int i = 0; i < attributes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                keys.add(attributes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            keys.retainAll(text.getAllAttributeKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (keys.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // Get and set attribute runs for each attribute name. Need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // scan from the top of the text so that we can discard any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // Annotation that is no longer applied to a subset text segment.
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   270
        Iterator<Attribute> itr = keys.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        while (itr.hasNext()) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   272
            Attribute attributeKey = itr.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            text.setIndex(textBeginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            while (text.getIndex() < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                int start = text.getRunStart(attributeKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                int limit = text.getRunLimit(attributeKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                Object value = text.getAttribute(attributeKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    if (value instanceof Annotation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        if (start >= beginIndex && limit <= endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                            addAttribute(attributeKey, value, start - beginIndex, limit - beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                            if (limit > endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        // if the run is beyond the given (subset) range, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        // don't need to process further.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        if (start >= endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        if (limit > beginIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            // attribute is applied to any subrange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            if (start < beginIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                start = beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            if (limit > endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                limit = endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            if (start != limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                addAttribute(attributeKey, value, start - beginIndex, limit - beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                text.setIndex(limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Adds an attribute to the entire string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param attribute the attribute key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param value the value of the attribute; may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @exception NullPointerException if <code>attribute</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @exception IllegalArgumentException if the AttributedString has length 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * (attributes cannot be applied to a 0-length range).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void addAttribute(Attribute attribute, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (attribute == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        int len = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new IllegalArgumentException("Can't add attribute to 0-length text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        addAttributeImpl(attribute, value, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Adds an attribute to a subrange of the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @param attribute the attribute key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param value The value of the attribute. May be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param beginIndex Index of the first character of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param endIndex Index of the character following the last character of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @exception NullPointerException if <code>attribute</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @exception IllegalArgumentException if beginIndex is less then 0, endIndex is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * greater than the length of the string, or beginIndex and endIndex together don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * define a non-empty subrange of the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public void addAttribute(Attribute attribute, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (attribute == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (beginIndex < 0 || endIndex > length() || beginIndex >= endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            throw new IllegalArgumentException("Invalid substring range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        addAttributeImpl(attribute, value, beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Adds a set of attributes to a subrange of the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param attributes The attributes to be added to the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param beginIndex Index of the first character of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param endIndex Index of the character following the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * character of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @exception NullPointerException if <code>attributes</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @exception IllegalArgumentException if beginIndex is less then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * 0, endIndex is greater than the length of the string, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * beginIndex and endIndex together don't define a non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * subrange of the string and the attributes parameter is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * empty Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public void addAttributes(Map<? extends Attribute, ?> attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                              int beginIndex, int endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (beginIndex < 0 || endIndex > length() || beginIndex > endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            throw new IllegalArgumentException("Invalid substring range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (beginIndex == endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (attributes.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            throw new IllegalArgumentException("Can't add attribute to 0-length text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        // make sure we have run attribute data vectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (runCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            createRunAttributeDataVectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // break up runs if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        int beginRunIndex = ensureRunBreak(beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        int endRunIndex = ensureRunBreak(endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   394
        Iterator<? extends Map.Entry<? extends Attribute, ?>> iterator =
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   395
            attributes.entrySet().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        while (iterator.hasNext()) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   397
            Map.Entry<? extends Attribute, ?> entry = iterator.next();
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   398
            addAttributeRunData(entry.getKey(), entry.getValue(), beginRunIndex, endRunIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private synchronized void addAttributeImpl(Attribute attribute, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // make sure we have run attribute data vectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (runCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            createRunAttributeDataVectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // break up runs if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        int beginRunIndex = ensureRunBreak(beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        int endRunIndex = ensureRunBreak(endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        addAttributeRunData(attribute, value, beginRunIndex, endRunIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    private final void createRunAttributeDataVectors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // use temporary variables so things remain consistent in case of an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        int newRunStarts[] = new int[ARRAY_SIZE_INCREMENT];
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   420
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   421
        @SuppressWarnings("unchecked")
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   422
        Vector<Attribute> newRunAttributes[] = (Vector<Attribute>[]) new Vector<?>[ARRAY_SIZE_INCREMENT];
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   423
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   424
        @SuppressWarnings("unchecked")
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   425
        Vector<Object> newRunAttributeValues[] = (Vector<Object>[]) new Vector<?>[ARRAY_SIZE_INCREMENT];
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   426
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        runStarts = newRunStarts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        runAttributes = newRunAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        runAttributeValues = newRunAttributeValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        runArraySize = ARRAY_SIZE_INCREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        runCount = 1; // assume initial run starting at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    // ensure there's a run break at offset, return the index of the run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private final int ensureRunBreak(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return ensureRunBreak(offset, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Ensures there is a run break at offset, returning the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * the run. If this results in splitting a run, two things can happen:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <li>If copyAttrs is true, the attributes from the existing run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *     will be placed in both of the newly created runs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <li>If copyAttrs is false, the attributes from the existing run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * will NOT be copied to the run to the right (>= offset) of the break,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * but will exist on the run to the left (< offset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    private final int ensureRunBreak(int offset, boolean copyAttrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (offset == length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            return runCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // search for the run index where this offset should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        int runIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        while (runIndex < runCount && runStarts[runIndex] < offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            runIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // if the offset is at a run start already, we're done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (runIndex < runCount && runStarts[runIndex] == offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            return runIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // we'll have to break up a run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // first, make sure we have enough space in our arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (runCount == runArraySize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            int newArraySize = runArraySize + ARRAY_SIZE_INCREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            int newRunStarts[] = new int[newArraySize];
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   471
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   472
            @SuppressWarnings("unchecked")
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   473
            Vector<Attribute> newRunAttributes[] = (Vector<Attribute>[]) new Vector<?>[newArraySize];
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   474
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   475
            @SuppressWarnings("unchecked")
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   476
            Vector<Object> newRunAttributeValues[] = (Vector<Object>[]) new Vector<?>[newArraySize];
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   477
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            for (int i = 0; i < runArraySize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                newRunStarts[i] = runStarts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                newRunAttributes[i] = runAttributes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                newRunAttributeValues[i] = runAttributeValues[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            runStarts = newRunStarts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            runAttributes = newRunAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            runAttributeValues = newRunAttributeValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            runArraySize = newArraySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // make copies of the attribute information of the old run that the new one used to be part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        // use temporary variables so things remain consistent in case of an exception
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   491
        Vector<Attribute> newRunAttributes = null;
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   492
        Vector<Object> newRunAttributeValues = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (copyAttrs) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   495
            Vector<Attribute> oldRunAttributes = runAttributes[runIndex - 1];
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   496
            Vector<Object> oldRunAttributeValues = runAttributeValues[runIndex - 1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            if (oldRunAttributes != null) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   498
                newRunAttributes = new Vector<>(oldRunAttributes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (oldRunAttributeValues != null) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   501
                newRunAttributeValues =  new Vector<>(oldRunAttributeValues);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        // now actually break up the run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        runCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        for (int i = runCount - 1; i > runIndex; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            runStarts[i] = runStarts[i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            runAttributes[i] = runAttributes[i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            runAttributeValues[i] = runAttributeValues[i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        runStarts[runIndex] = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        runAttributes[runIndex] = newRunAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        runAttributeValues[runIndex] = newRunAttributeValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        return runIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    // add the attribute attribute/value to all runs where beginRunIndex <= runIndex < endRunIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private void addAttributeRunData(Attribute attribute, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            int beginRunIndex, int endRunIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        for (int i = beginRunIndex; i < endRunIndex; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            int keyValueIndex = -1; // index of key and value in our vectors; assume we don't have an entry yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            if (runAttributes[i] == null) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   526
                Vector<Attribute> newRunAttributes = new Vector<>();
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   527
                Vector<Object> newRunAttributeValues = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                runAttributes[i] = newRunAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                runAttributeValues[i] = newRunAttributeValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                // check whether we have an entry already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                keyValueIndex = runAttributes[i].indexOf(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            if (keyValueIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                // create new entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                int oldSize = runAttributes[i].size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                runAttributes[i].addElement(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    runAttributeValues[i].addElement(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    runAttributes[i].setSize(oldSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    runAttributeValues[i].setSize(oldSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                // update existing entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                runAttributeValues[i].set(keyValueIndex, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Creates an AttributedCharacterIterator instance that provides access to the entire contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @return An iterator providing access to the text and its attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public AttributedCharacterIterator getIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        return getIterator(null, 0, length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * Creates an AttributedCharacterIterator instance that provides access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * selected contents of this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Information about attributes not listed in attributes that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * implementor may have need not be made accessible through the iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * If the list is null, all available attribute information should be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @param attributes a list of attributes that the client is interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @return an iterator providing access to the entire text and its selected attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public AttributedCharacterIterator getIterator(Attribute[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return getIterator(attributes, 0, length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Creates an AttributedCharacterIterator instance that provides access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * selected contents of this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Information about attributes not listed in attributes that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * implementor may have need not be made accessible through the iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * If the list is null, all available attribute information should be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param attributes a list of attributes that the client is interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @param beginIndex the index of the first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @param endIndex the index of the character following the last character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @return an iterator providing access to the text and its attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @exception IllegalArgumentException if beginIndex is less then 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * endIndex is greater than the length of the string, or beginIndex is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * greater than endIndex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public AttributedCharacterIterator getIterator(Attribute[] attributes, int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        return new AttributedStringIterator(attributes, beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    // all (with the exception of length) reading operations are private,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    // since AttributedString instances are accessed through iterators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    // length is package private so that CharacterIteratorFieldDelegate can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    // access it without creating an AttributedCharacterIterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    private char charAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return text.charAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    private synchronized Object getAttribute(Attribute attribute, int runIndex) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   612
        Vector<Attribute> currentRunAttributes = runAttributes[runIndex];
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   613
        Vector<Object> currentRunAttributeValues = runAttributeValues[runIndex];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (currentRunAttributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        int attributeIndex = currentRunAttributes.indexOf(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (attributeIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            return currentRunAttributeValues.elementAt(attributeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    // gets an attribute value, but returns an annotation only if it's range does not extend outside the range beginIndex..endIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private Object getAttributeCheckRange(Attribute attribute, int runIndex, int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        Object value = getAttribute(attribute, runIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if (value instanceof Annotation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            // need to check whether the annotation's range extends outside the iterator's range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            if (beginIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                int currIndex = runIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                int runStart = runStarts[currIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                while (runStart >= beginIndex &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                        valuesMatch(value, getAttribute(attribute, currIndex - 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    currIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    runStart = runStarts[currIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                if (runStart < beginIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    // annotation's range starts before iterator's range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            int textLength = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            if (endIndex < textLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                int currIndex = runIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                int runLimit = (currIndex < runCount - 1) ? runStarts[currIndex + 1] : textLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                while (runLimit <= endIndex &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        valuesMatch(value, getAttribute(attribute, currIndex + 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    currIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    runLimit = (currIndex < runCount - 1) ? runStarts[currIndex + 1] : textLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                if (runLimit > endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    // annotation's range ends after iterator's range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            // annotation's range is subrange of iterator's range,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            // so we can return the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    // returns whether all specified attributes have equal values in the runs with the given indices
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   665
    private boolean attributeValuesMatch(Set<? extends Attribute> attributes, int runIndex1, int runIndex2) {
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   666
        Iterator<? extends Attribute> iterator = attributes.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        while (iterator.hasNext()) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   668
            Attribute key = iterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
           if (!valuesMatch(getAttribute(key, runIndex1), getAttribute(key, runIndex2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    // returns whether the two objects are either both null or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    private final static boolean valuesMatch(Object value1, Object value2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (value1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            return value2 == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            return value1.equals(value2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Appends the contents of the CharacterIterator iterator into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * StringBuffer buf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    private final void appendContents(StringBuffer buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                                      CharacterIterator iterator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        int index = iterator.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        int end = iterator.getEndIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        while (index < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            iterator.setIndex(index++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            buf.append(iterator.current());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * Sets the attributes for the range from offset to the next run break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * (typically the end of the text) to the ones specified in attrs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * This is only meant to be called from the constructor!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   705
    private void setAttributes(Map<Attribute, Object> attrs, int offset) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (runCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            createRunAttributeDataVectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        int index = ensureRunBreak(offset, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (attrs != null && (size = attrs.size()) > 0) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   714
            Vector<Attribute> runAttrs = new Vector<>(size);
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   715
            Vector<Object> runValues = new Vector<>(size);
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   716
            Iterator<Map.Entry<Attribute, Object>> iterator = attrs.entrySet().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            while (iterator.hasNext()) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   719
                Map.Entry<Attribute, Object> entry = iterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                runAttrs.add(entry.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                runValues.add(entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            runAttributes[index] = runAttrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            runAttributeValues[index] = runValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Returns true if the attributes specified in last and attrs differ.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   732
    private static <K,V> boolean mapsDiffer(Map<K, V> last, Map<K, V> attrs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        if (last == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            return (attrs != null && attrs.size() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        return (!last.equals(attrs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    // the iterator class associated with this string class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    final private class AttributedStringIterator implements AttributedCharacterIterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        // note on synchronization:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        // we don't synchronize on the iterator, assuming that an iterator is only used in one thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        // we do synchronize access to the AttributedString however, since it's more likely to be shared between threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        // start and end index for our iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        private int beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        private int endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        // attributes that our client is interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        private Attribute[] relevantAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // the current index for our iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        // invariant: beginIndex <= currentIndex <= endIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        private int currentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        // information about the run that includes currentIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        private int currentRunIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        private int currentRunStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        private int currentRunLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        // constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        AttributedStringIterator(Attribute[] attributes, int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            if (beginIndex < 0 || beginIndex > endIndex || endIndex > length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                throw new IllegalArgumentException("Invalid substring range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            this.beginIndex = beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            this.endIndex = endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            this.currentIndex = beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            updateRunInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            if (attributes != null) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   776
                relevantAttributes = attributes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // Object methods. See documentation in that class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            if (!(obj instanceof AttributedStringIterator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            AttributedStringIterator that = (AttributedStringIterator) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            if (AttributedString.this != that.getString())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            if (currentIndex != that.currentIndex || beginIndex != that.beginIndex || endIndex != that.endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            return text.hashCode() ^ currentIndex ^ beginIndex ^ endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                AttributedStringIterator other = (AttributedStringIterator) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                return other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   809
                throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        // CharacterIterator methods. See documentation in that interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        public char first() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            return internalSetIndex(beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        public char last() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            if (endIndex == beginIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                return internalSetIndex(endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                return internalSetIndex(endIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        public char current() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            if (currentIndex == endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                return charAt(currentIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        public char next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            if (currentIndex < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                return internalSetIndex(currentIndex + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        public char previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            if (currentIndex > beginIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                return internalSetIndex(currentIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        public char setIndex(int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            if (position < beginIndex || position > endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                throw new IllegalArgumentException("Invalid index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            return internalSetIndex(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        public int getBeginIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            return beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        public int getEndIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            return endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        public int getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            return currentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        // AttributedCharacterIterator methods. See documentation in that interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        public int getRunStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            return currentRunStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        public int getRunStart(Attribute attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            if (currentRunStart == beginIndex || currentRunIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                return currentRunStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                Object value = getAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                int runStart = currentRunStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                int runIndex = currentRunIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                while (runStart > beginIndex &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                        valuesMatch(value, AttributedString.this.getAttribute(attribute, runIndex - 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                    runIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    runStart = runStarts[runIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                if (runStart < beginIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                    runStart = beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                return runStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        public int getRunStart(Set<? extends Attribute> attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            if (currentRunStart == beginIndex || currentRunIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                return currentRunStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                int runStart = currentRunStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                int runIndex = currentRunIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                while (runStart > beginIndex &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        AttributedString.this.attributeValuesMatch(attributes, currentRunIndex, runIndex - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    runIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                    runStart = runStarts[runIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                if (runStart < beginIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                    runStart = beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                return runStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        public int getRunLimit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return currentRunLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        public int getRunLimit(Attribute attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            if (currentRunLimit == endIndex || currentRunIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                return currentRunLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                Object value = getAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                int runLimit = currentRunLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                int runIndex = currentRunIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                while (runLimit < endIndex &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        valuesMatch(value, AttributedString.this.getAttribute(attribute, runIndex + 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    runIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    runLimit = runIndex < runCount - 1 ? runStarts[runIndex + 1] : endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                if (runLimit > endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    runLimit = endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                return runLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        public int getRunLimit(Set<? extends Attribute> attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            if (currentRunLimit == endIndex || currentRunIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                return currentRunLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                int runLimit = currentRunLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                int runIndex = currentRunIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                while (runLimit < endIndex &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                        AttributedString.this.attributeValuesMatch(attributes, currentRunIndex, runIndex + 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                    runIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    runLimit = runIndex < runCount - 1 ? runStarts[runIndex + 1] : endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                if (runLimit > endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    runLimit = endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                return runLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        public Map<Attribute,Object> getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            if (runAttributes == null || currentRunIndex == -1 || runAttributes[currentRunIndex] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                // ??? would be nice to return null, but current spec doesn't allow it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                // returning Hashtable saves AttributeMap from dealing with emptiness
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   959
                return new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            return new AttributeMap(currentRunIndex, beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        public Set<Attribute> getAllAttributeKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            // ??? This should screen out attribute keys that aren't relevant to the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            if (runAttributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                // ??? would be nice to return null, but current spec doesn't allow it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                // returning HashSet saves us from dealing with emptiness
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   969
                return new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            synchronized (AttributedString.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                // ??? should try to create this only once, then update if necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                // and give callers read-only view
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   974
                Set<Attribute> keys = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                while (i < runCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    if (runStarts[i] < endIndex && (i == runCount - 1 || runStarts[i + 1] > beginIndex)) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   978
                        Vector<Attribute> currentRunAttributes = runAttributes[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                        if (currentRunAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                            int j = currentRunAttributes.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                            while (j-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                                keys.add(currentRunAttributes.get(j));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        public Object getAttribute(Attribute attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            int runIndex = currentRunIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if (runIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            return AttributedString.this.getAttributeCheckRange(attribute, runIndex, beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        // internally used methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        private AttributedString getString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            return AttributedString.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        // set the current index, update information about the current run if necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        // return the character at the current index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        private char internalSetIndex(int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            currentIndex = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            if (position < currentRunStart || position >= currentRunLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                updateRunInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            if (currentIndex == endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                return charAt(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        // update the information about the current run
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        private void updateRunInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            if (currentIndex == endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                currentRunStart = currentRunLimit = endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                currentRunIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                synchronized (AttributedString.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                    int runIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    while (runIndex < runCount - 1 && runStarts[runIndex + 1] <= currentIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                        runIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                    currentRunIndex = runIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    if (runIndex >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                        currentRunStart = runStarts[runIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                        if (currentRunStart < beginIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                            currentRunStart = beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                        currentRunStart = beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                    if (runIndex < runCount - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                        currentRunLimit = runStarts[runIndex + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                        if (currentRunLimit > endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                            currentRunLimit = endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                        currentRunLimit = endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    // the map class associated with this string class, giving access to the attributes of one run
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    final private class AttributeMap extends AbstractMap<Attribute,Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        int runIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        int beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        int endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        AttributeMap(int runIndex, int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            this.runIndex = runIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            this.beginIndex = beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            this.endIndex = endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
  1067
        public Set<Map.Entry<Attribute, Object>> entrySet() {
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
  1068
            HashSet<Map.Entry<Attribute, Object>> set = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            synchronized (AttributedString.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                int size = runAttributes[runIndex].size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                for (int i = 0; i < size; i++) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
  1072
                    Attribute key = runAttributes[runIndex].get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    Object value = runAttributeValues[runIndex].get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    if (value instanceof Annotation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                        value = AttributedString.this.getAttributeCheckRange(key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                                                             runIndex, beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    }
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
  1081
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
  1082
                    Map.Entry<Attribute, Object> entry = new AttributeEntry(key, value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                    set.add(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        public Object get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            return AttributedString.this.getAttributeCheckRange((Attribute) key, runIndex, beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
  1095
class AttributeEntry implements Map.Entry<Attribute,Object> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    private Attribute key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    private Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    AttributeEntry(Attribute key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if (!(o instanceof AttributeEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        AttributeEntry other = (AttributeEntry) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        return other.key.equals(key) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            (value == null ? other.value == null : other.value.equals(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
  1114
    public Attribute getKey() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    public Object setValue(Object newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        return key.hashCode() ^ (value==null ? 0 : value.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        return key.toString()+"="+value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
}