FileDocCategorySizeDatePackage
SpanSet.javaAPI DocAndroid 5.1 API3717Thu Mar 12 22:22:10 GMT 2015android.text

SpanSet

public class SpanSet extends Object
A cached set of spans. Caches the result of {@link Spanned#getSpans(int, int, Class)} and then provides faster access to {@link Spanned#nextSpanTransition(int, int, Class)}. Fields are left public for a convenient direct access. Note that empty spans are ignored by this class.
hide

Fields Summary
private final Class
classType
int
numberOfSpans
E[]
spans
int[]
spanStarts
int[]
spanEnds
int[]
spanFlags
Constructors Summary
SpanSet(Class type)

        classType = type;
        numberOfSpans = 0;
    
Methods Summary
intgetNextTransition(int start, int limit)
Similar to {@link Spanned#nextSpanTransition(int, int, Class)}

        for (int i = 0; i < numberOfSpans; i++) {
            final int spanStart = spanStarts[i];
            final int spanEnd = spanEnds[i];
            if (spanStart > start && spanStart < limit) limit = spanStart;
            if (spanEnd > start && spanEnd < limit) limit = spanEnd;
        }
        return limit;
    
public booleanhasSpansIntersecting(int start, int end)
Returns true if there are spans intersecting the given interval.

param
end must be strictly greater than start

        for (int i = 0; i < numberOfSpans; i++) {
            // equal test is valid since both intervals are not empty by construction
            if (spanStarts[i] >= end || spanEnds[i] <= start) continue;
            return true;
        }
        return false;
    
public voidinit(Spanned spanned, int start, int limit)

        final E[] allSpans = spanned.getSpans(start, limit, classType);
        final int length = allSpans.length;

        if (length > 0 && (spans == null || spans.length < length)) {
            // These arrays may end up being too large because of the discarded empty spans
            spans = (E[]) Array.newInstance(classType, length);
            spanStarts = new int[length];
            spanEnds = new int[length];
            spanFlags = new int[length];
        }

        numberOfSpans = 0;
        for (int i = 0; i < length; i++) {
            final E span = allSpans[i];

            final int spanStart = spanned.getSpanStart(span);
            final int spanEnd = spanned.getSpanEnd(span);
            if (spanStart == spanEnd) continue;

            final int spanFlag = spanned.getSpanFlags(span);

            spans[numberOfSpans] = span;
            spanStarts[numberOfSpans] = spanStart;
            spanEnds[numberOfSpans] = spanEnd;
            spanFlags[numberOfSpans] = spanFlag;

            numberOfSpans++;
        }
    
public voidrecycle()
Removes all internal references to the spans to avoid memory leaks.

        // The spans array is guaranteed to be not null when numberOfSpans is > 0
        for (int i = 0; i < numberOfSpans; i++) {
            spans[i] = null; // prevent a leak: no reference kept when TextLine is recycled
        }