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