rt/emul/compact/src/main/java/java/text/CharacterIteratorFieldDelegate.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) 2000, 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
package java.text;
jtulach@1334
    26
jtulach@1334
    27
import java.util.ArrayList;
jtulach@1334
    28
jtulach@1334
    29
/**
jtulach@1334
    30
 * CharacterIteratorFieldDelegate combines the notifications from a Format
jtulach@1334
    31
 * into a resulting <code>AttributedCharacterIterator</code>. The resulting
jtulach@1334
    32
 * <code>AttributedCharacterIterator</code> can be retrieved by way of
jtulach@1334
    33
 * the <code>getIterator</code> method.
jtulach@1334
    34
 *
jtulach@1334
    35
 */
jtulach@1334
    36
class CharacterIteratorFieldDelegate implements Format.FieldDelegate {
jtulach@1334
    37
    /**
jtulach@1334
    38
     * Array of AttributeStrings. Whenever <code>formatted</code> is invoked
jtulach@1334
    39
     * for a region > size, a new instance of AttributedString is added to
jtulach@1334
    40
     * attributedStrings. Subsequent invocations of <code>formatted</code>
jtulach@1334
    41
     * for existing regions result in invoking addAttribute on the existing
jtulach@1334
    42
     * AttributedStrings.
jtulach@1334
    43
     */
jtulach@1334
    44
    private ArrayList attributedStrings;
jtulach@1334
    45
    /**
jtulach@1334
    46
     * Running count of the number of characters that have
jtulach@1334
    47
     * been encountered.
jtulach@1334
    48
     */
jtulach@1334
    49
    private int size;
jtulach@1334
    50
jtulach@1334
    51
jtulach@1334
    52
    CharacterIteratorFieldDelegate() {
jtulach@1334
    53
        attributedStrings = new ArrayList();
jtulach@1334
    54
    }
jtulach@1334
    55
jtulach@1334
    56
    public void formatted(Format.Field attr, Object value, int start, int end,
jtulach@1334
    57
                          StringBuffer buffer) {
jtulach@1334
    58
        if (start != end) {
jtulach@1334
    59
            if (start < size) {
jtulach@1334
    60
                // Adjust attributes of existing runs
jtulach@1334
    61
                int index = size;
jtulach@1334
    62
                int asIndex = attributedStrings.size() - 1;
jtulach@1334
    63
jtulach@1334
    64
                while (start < index) {
jtulach@1334
    65
                    AttributedString as = (AttributedString)attributedStrings.
jtulach@1334
    66
                                           get(asIndex--);
jtulach@1334
    67
                    int newIndex = index - as.length();
jtulach@1334
    68
                    int aStart = Math.max(0, start - newIndex);
jtulach@1334
    69
jtulach@1334
    70
                    as.addAttribute(attr, value, aStart, Math.min(
jtulach@1334
    71
                                    end - start, as.length() - aStart) +
jtulach@1334
    72
                                    aStart);
jtulach@1334
    73
                    index = newIndex;
jtulach@1334
    74
                }
jtulach@1334
    75
            }
jtulach@1334
    76
            if (size < start) {
jtulach@1334
    77
                // Pad attributes
jtulach@1334
    78
                attributedStrings.add(new AttributedString(
jtulach@1334
    79
                                          buffer.substring(size, start)));
jtulach@1334
    80
                size = start;
jtulach@1334
    81
            }
jtulach@1334
    82
            if (size < end) {
jtulach@1334
    83
                // Add new string
jtulach@1334
    84
                int aStart = Math.max(start, size);
jtulach@1334
    85
                AttributedString string = new AttributedString(
jtulach@1334
    86
                                   buffer.substring(aStart, end));
jtulach@1334
    87
jtulach@1334
    88
                string.addAttribute(attr, value);
jtulach@1334
    89
                attributedStrings.add(string);
jtulach@1334
    90
                size = end;
jtulach@1334
    91
            }
jtulach@1334
    92
        }
jtulach@1334
    93
    }
jtulach@1334
    94
jtulach@1334
    95
    public void formatted(int fieldID, Format.Field attr, Object value,
jtulach@1334
    96
                          int start, int end, StringBuffer buffer) {
jtulach@1334
    97
        formatted(attr, value, start, end, buffer);
jtulach@1334
    98
    }
jtulach@1334
    99
jtulach@1334
   100
    /**
jtulach@1334
   101
     * Returns an <code>AttributedCharacterIterator</code> that can be used
jtulach@1334
   102
     * to iterate over the resulting formatted String.
jtulach@1334
   103
     *
jtulach@1334
   104
     * @pararm string Result of formatting.
jtulach@1334
   105
     */
jtulach@1334
   106
    public AttributedCharacterIterator getIterator(String string) {
jtulach@1334
   107
        // Add the last AttributedCharacterIterator if necessary
jtulach@1334
   108
        // assert(size <= string.length());
jtulach@1334
   109
        if (string.length() > size) {
jtulach@1334
   110
            attributedStrings.add(new AttributedString(
jtulach@1334
   111
                                  string.substring(size)));
jtulach@1334
   112
            size = string.length();
jtulach@1334
   113
        }
jtulach@1334
   114
        int iCount = attributedStrings.size();
jtulach@1334
   115
        AttributedCharacterIterator iterators[] = new
jtulach@1334
   116
                                    AttributedCharacterIterator[iCount];
jtulach@1334
   117
jtulach@1334
   118
        for (int counter = 0; counter < iCount; counter++) {
jtulach@1334
   119
            iterators[counter] = ((AttributedString)attributedStrings.
jtulach@1334
   120
                                  get(counter)).getIterator();
jtulach@1334
   121
        }
jtulach@1334
   122
        return new AttributedString(iterators).getIterator();
jtulach@1334
   123
    }
jtulach@1334
   124
}