Path: blob/master/src/java.base/share/classes/java/text/CharacterIteratorFieldDelegate.java
41152 views
/*1* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package java.text;2526import java.util.ArrayList;2728/**29* CharacterIteratorFieldDelegate combines the notifications from a Format30* into a resulting {@code AttributedCharacterIterator}. The resulting31* {@code AttributedCharacterIterator} can be retrieved by way of32* the {@code getIterator} method.33*34*/35class CharacterIteratorFieldDelegate implements Format.FieldDelegate {36/**37* Array of AttributeStrings. Whenever {@code formatted} is invoked38* for a region > size, a new instance of AttributedString is added to39* attributedStrings. Subsequent invocations of {@code formatted}40* for existing regions result in invoking addAttribute on the existing41* AttributedStrings.42*/43private ArrayList<AttributedString> attributedStrings;44/**45* Running count of the number of characters that have46* been encountered.47*/48private int size;495051CharacterIteratorFieldDelegate() {52attributedStrings = new ArrayList<>();53}5455public void formatted(Format.Field attr, Object value, int start, int end,56StringBuffer buffer) {57if (start != end) {58if (start < size) {59// Adjust attributes of existing runs60int index = size;61int asIndex = attributedStrings.size() - 1;6263while (start < index) {64AttributedString as = attributedStrings.65get(asIndex--);66int newIndex = index - as.length();67int aStart = Math.max(0, start - newIndex);6869as.addAttribute(attr, value, aStart, Math.min(70end - start, as.length() - aStart) +71aStart);72index = newIndex;73}74}75if (size < start) {76// Pad attributes77attributedStrings.add(new AttributedString(78buffer.substring(size, start)));79size = start;80}81if (size < end) {82// Add new string83int aStart = Math.max(start, size);84AttributedString string = new AttributedString(85buffer.substring(aStart, end));8687string.addAttribute(attr, value);88attributedStrings.add(string);89size = end;90}91}92}9394public void formatted(int fieldID, Format.Field attr, Object value,95int start, int end, StringBuffer buffer) {96formatted(attr, value, start, end, buffer);97}9899/**100* Returns an {@code AttributedCharacterIterator} that can be used101* to iterate over the resulting formatted String.102*103* @pararm string Result of formatting.104*/105public AttributedCharacterIterator getIterator(String string) {106// Add the last AttributedCharacterIterator if necessary107// assert(size <= string.length());108if (string.length() > size) {109attributedStrings.add(new AttributedString(110string.substring(size)));111size = string.length();112}113int iCount = attributedStrings.size();114AttributedCharacterIterator iterators[] = new115AttributedCharacterIterator[iCount];116117for (int counter = 0; counter < iCount; counter++) {118iterators[counter] = attributedStrings.119get(counter).getIterator();120}121return new AttributedString(iterators).getIterator();122}123}124125126