FileDocCategorySizeDatePackage
AttributedString.javaAPI DocAndroid 1.5 API29284Wed May 06 22:41:06 BST 2009java.text

AttributedString

public class AttributedString extends Object
Holds a string with attributes describing the characters of this string.
since
Android 1.0

Fields Summary
String
text
Map
attributeMap
Constructors Summary
public AttributedString(AttributedCharacterIterator iterator)
Constructs an {@code AttributedString} from an {@code AttributedCharacterIterator}, which represents attributed text.

param
iterator the {@code AttributedCharacterIterator} that contains the text for this attributed string.
since
Android 1.0

        if (iterator.getBeginIndex() > iterator.getEndIndex()) {
            // text.0A=Invalid substring range
            throw new IllegalArgumentException(Messages.getString("text.0A")); //$NON-NLS-1$
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = iterator.getBeginIndex(); i < iterator.getEndIndex(); i++) {
            buffer.append(iterator.current());
            iterator.next();
        }
        text = buffer.toString();
        Set<AttributedCharacterIterator.Attribute> attributes = iterator
                .getAllAttributeKeys();
        if (attributes == null) {
            return;
        }
        attributeMap = new HashMap<Attribute, List<Range>>(
                (attributes.size() * 4 / 3) + 1);

        Iterator<Attribute> it = attributes.iterator();
        while (it.hasNext()) {
            AttributedCharacterIterator.Attribute attribute = it.next();
            iterator.setIndex(0);
            while (iterator.current() != CharacterIterator.DONE) {
                int start = iterator.getRunStart(attribute);
                int limit = iterator.getRunLimit(attribute);
                Object value = iterator.getAttribute(attribute);
                if (value != null) {
                    addAttribute(attribute, value, start, limit);
                }
                iterator.setIndex(limit);
            }
        }
    
private AttributedString(AttributedCharacterIterator iterator, int start, int end, Set attributes)

        if (start < iterator.getBeginIndex() || end > iterator.getEndIndex()
                || start > end) {
            throw new IllegalArgumentException();
        }

        if (attributes == null) {
            return;
        }

        StringBuffer buffer = new StringBuffer();
        iterator.setIndex(start);
        while (iterator.getIndex() < end) {
            buffer.append(iterator.current());
            iterator.next();
        }
        text = buffer.toString();
        attributeMap = new HashMap<Attribute, List<Range>>(
                (attributes.size() * 4 / 3) + 1);

        Iterator<Attribute> it = attributes.iterator();
        while (it.hasNext()) {
            AttributedCharacterIterator.Attribute attribute = it.next();
            iterator.setIndex(start);
            while (iterator.getIndex() < end) {
                Object value = iterator.getAttribute(attribute);
                int runStart = iterator.getRunStart(attribute);
                int limit = iterator.getRunLimit(attribute);
                if ((value instanceof Annotation && runStart >= start && limit <= end)
                        || (value != null && !(value instanceof Annotation))) {
                    addAttribute(attribute, value, (runStart < start ? start
                            : runStart)
                            - start, (limit > end ? end : limit) - start);
                }
                iterator.setIndex(limit);
            }
        }
    
public AttributedString(AttributedCharacterIterator iterator, int start, int end)
Constructs an {@code AttributedString} from a range of the text contained in the specified {@code AttributedCharacterIterator}, starting at {@code start} and ending at {@code end}. All attributes will be copied to this attributed string.

param
iterator the {@code AttributedCharacterIterator} that contains the text for this attributed string.
param
start the start index of the range of the copied text.
param
end the end index of the range of the copied text.
throws
IllegalArgumentException if {@code start} is less than first index of {@code iterator}, {@code end} is greater than the last index + 1 in {@code iterator} or if {@code start > end}.
since
Android 1.0

        this(iterator, start, end, iterator.getAllAttributeKeys());
    
public AttributedString(AttributedCharacterIterator iterator, int start, int end, java.text.AttributedCharacterIterator.Attribute[] attributes)
Constructs an {@code AttributedString} from a range of the text contained in the specified {@code AttributedCharacterIterator}, starting at {@code start}, ending at {@code end} and it will copy the attributes defined in the specified set. If the set is {@code null} then all attributes are copied.

param
iterator the {@code AttributedCharacterIterator} that contains the text for this attributed string.
param
start the start index of the range of the copied text.
param
end the end index of the range of the copied text.
param
attributes the set of attributes that will be copied, or all if it is {@code null}.
throws
IllegalArgumentException if {@code start} is less than first index of {@code iterator}, {@code end} is greater than the last index + 1 in {@code iterator} or if {@code start > end}.
since
Android 1.0

        // BEGIN android-removed
        // this(iterator, start, end, new HashSet<Attribute>(Arrays
        //         .asList(attributes)));
        // END android-removed
        // BEGIN android-added
        this(iterator, start, end, (attributes == null
                ? new HashSet<Attribute>()
                : new HashSet<Attribute>(Arrays.asList(attributes))));
        // END android-added
    
public AttributedString(String value)
Creates an {@code AttributedString} from the given text.

param
value the text to take as base for this attributed string.
since
Android 1.0

        if (value == null) {
            throw new NullPointerException();
        }
        text = value;
        attributeMap = new HashMap<Attribute, List<Range>>(11);
    
public AttributedString(String value, Map attributes)
Creates an {@code AttributedString} from the given text and the attributes. The whole text has the given attributes applied.

param
value the text to take as base for this attributed string.
param
attributes the attributes that the text is associated with.
throws
IllegalArgumentException if the length of {@code value} is 0 but the size of {@code attributes} is greater than 0.
throws
NullPointerException if {@code value} is {@code null}.
since
Android 1.0

        if (value == null) {
            throw new NullPointerException();
        }
        if (value.length() == 0 && !attributes.isEmpty()) {
            // text.0B=Cannot add attributes to empty string
            throw new IllegalArgumentException(Messages.getString("text.0B")); //$NON-NLS-1$
        }
        text = value;
        attributeMap = new HashMap<Attribute, List<Range>>(
                (attributes.size() * 4 / 3) + 1);
        Iterator<?> it = attributes.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next();
            ArrayList<Range> ranges = new ArrayList<Range>(1);
            ranges.add(new Range(0, text.length(), entry.getValue()));
            attributeMap.put((AttributedCharacterIterator.Attribute) entry
                    .getKey(), ranges);
        }
    
Methods Summary
public voidaddAttribute(java.text.AttributedCharacterIterator.Attribute attribute, java.lang.Object value)
Applies a given attribute to this string.

param
attribute the attribute that will be applied to this string.
param
value the value of the attribute that will be applied to this string.
throws
IllegalArgumentException if the length of this attributed string is 0.
throws
NullPointerException if {@code attribute} is {@code null}.
since
Android 1.0

        if (null == attribute) {
            throw new NullPointerException();
        }
        if (text.length() == 0) {
            throw new IllegalArgumentException();
        }

        List<Range> ranges = attributeMap.get(attribute);
        if (ranges == null) {
            ranges = new ArrayList<Range>(1);
            attributeMap.put(attribute, ranges);
        } else {
            ranges.clear();
        }
        ranges.add(new Range(0, text.length(), value));
    
public voidaddAttribute(java.text.AttributedCharacterIterator.Attribute attribute, java.lang.Object value, int start, int end)
Applies a given attribute to the given range of this string.

param
attribute the attribute that will be applied to this string.
param
value the value of the attribute that will be applied to this string.
param
start the start of the range where the attribute will be applied.
param
end the end of the range where the attribute will be applied.
throws
IllegalArgumentException if {@code start < 0}, {@code end} is greater than the length of this string, or if {@code start >= end}.
throws
NullPointerException if {@code attribute} is {@code null}.
since
Android 1.0

        if (null == attribute) {
            throw new NullPointerException();
        }
        if (start < 0 || end > text.length() || start >= end) {
            throw new IllegalArgumentException();
        }

        if (value == null) {
            return;
        }

        List<Range> ranges = attributeMap.get(attribute);
        if (ranges == null) {
            ranges = new ArrayList<Range>(1);
            ranges.add(new Range(start, end, value));
            attributeMap.put(attribute, ranges);
            return;
        }
        ListIterator<Range> it = ranges.listIterator();
        // BEGIN android-changed
        // copied from a newer version of harmony
        // value can't be null
        while (it.hasNext()) {
            Range range = it.next();
            if (end <= range.start) {
                it.previous();
                break;
            } else if (start < range.end
                    || (start == range.end && value.equals(range.value))) {
                Range r1 = null, r3;
                it.remove();
                r1 = new Range(range.start, start, range.value);
                r3 = new Range(end, range.end, range.value);

                while (end > range.end && it.hasNext()) {
                    range = it.next();
                    if (end <= range.end) {
                        if (end > range.start
                                || (end == range.start && value.equals(range.value))) {
                            it.remove();
                            r3 = new Range(end, range.end, range.value);
                            break;
                        }
                    } else {
                        it.remove();
                    }
                }

                if (value.equals(r1.value)) {
                    if (value.equals(r3.value)) {
                        it.add(new Range(r1.start < start ? r1.start : start,
                                r3.end > end ? r3.end : end, r1.value));
                    } else {
                        it.add(new Range(r1.start < start ? r1.start : start,
                                end, r1.value));
                        if (r3.start < r3.end) {
                            it.add(r3);
                        }
                    }
                } else {
                    if (value.equals(r3.value)) {
                        if (r1.start < r1.end) {
                            it.add(r1);
                        }
                        it.add(new Range(start, r3.end > end ? r3.end : end,
                                r3.value));
                    } else {
                        if (r1.start < r1.end) {
                            it.add(r1);
                        }
                        it.add(new Range(start, end, value));
                        if (r3.start < r3.end) {
                            it.add(r3);
                        }
                    }
                }
                return;
            }
        }
        // END android-changed
        it.add(new Range(start, end, value));
    
public voidaddAttributes(java.util.Map attributes, int start, int end)
Applies a given set of attributes to the given range of the string.

param
attributes the set of attributes that will be applied to this string.
param
start the start of the range where the attribute will be applied.
param
end the end of the range where the attribute will be applied.
throws
IllegalArgumentException if {@code start < 0}, {@code end} is greater than the length of this string, or if {@code start >= end}.
since
Android 1.0

        Iterator<?> it = attributes.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next();
            addAttribute(
                    (AttributedCharacterIterator.Attribute) entry.getKey(),
                    entry.getValue(), start, end);
        }
    
public java.text.AttributedCharacterIteratorgetIterator()
Returns an {@code AttributedCharacterIterator} that gives access to the complete content of this attributed string.

return
the newly created {@code AttributedCharacterIterator}.
since
Android 1.0

        return new AttributedIterator(this);
    
public java.text.AttributedCharacterIteratorgetIterator(java.text.AttributedCharacterIterator.Attribute[] attributes)
Returns an {@code AttributedCharacterIterator} that gives access to the complete content of this attributed string. Only attributes contained in {@code attributes} are available from this iterator if they are defined for this text.

param
attributes the array containing attributes that will be in the new iterator if they are defined for this text.
return
the newly created {@code AttributedCharacterIterator}.
since
Android 1.0

        return new AttributedIterator(this, attributes, 0, text.length());
    
public java.text.AttributedCharacterIteratorgetIterator(java.text.AttributedCharacterIterator.Attribute[] attributes, int start, int end)
Returns an {@code AttributedCharacterIterator} that gives access to the contents of this attributed string starting at index {@code start} up to index {@code end}. Only attributes contained in {@code attributes} are available from this iterator if they are defined for this text.

param
attributes the array containing attributes that will be in the new iterator if they are defined for this text.
param
start the start index of the iterator on the underlying text.
param
end the end index of the iterator on the underlying text.
return
the newly created {@code AttributedCharacterIterator}.
since
Android 1.0

        return new AttributedIterator(this, attributes, start, end);