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