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