FileDocCategorySizeDatePackage
FixedSizeList.javaAPI DocAndroid 5.1 API7989Thu Mar 12 22:18:30 GMT 2015com.android.dx.util

FixedSizeList

public class FixedSizeList extends MutabilityControl implements ToHuman
Simple (mostly) fixed-size list of objects, which may be made immutable.

Fields Summary
private Object[]
arr
{@code non-null;} array of elements
Constructors Summary
public FixedSizeList(int size)
Constructs an instance. All indices initially contain {@code null}.

param
size the size of the list

        super(size != 0);

        try {
            arr = new Object[size];
        } catch (NegativeArraySizeException ex) {
            // Translate the exception.
            throw new IllegalArgumentException("size < 0");
        }
    
Methods Summary
public booleanequals(java.lang.Object other)
{@inheritDoc}

        if (this == other) {
            // Easy out.
            return true;
        }

        if ((other == null) || (getClass() != other.getClass())) {
            // Another easy out.
            return false;
        }

        FixedSizeList list = (FixedSizeList) other;
        return Arrays.equals(arr, list.arr);
    
protected final java.lang.Objectget0(int n)
Gets the indicated element. It is an error to call this with the index for an element which was never set; if you do that, this will throw {@code NullPointerException}. This method is protected so that subclasses may offer a safe type-checked public interface to their clients.

param
n {@code >= 0, < size();} which element
return
{@code non-null;} the indicated element

        try {
            Object result = arr[n];

            if (result == null) {
                throw new NullPointerException("unset: " + n);
            }

            return result;
        } catch (ArrayIndexOutOfBoundsException ex) {
            // Translate the exception.
            return throwIndex(n);
        }
    
protected final java.lang.ObjectgetOrNull0(int n)
Gets the indicated element, allowing {@code null}s to be returned. This method is protected so that subclasses may (optionally) offer a safe type-checked public interface to their clients.

param
n {@code >= 0, < size();} which element
return
{@code null-ok;} the indicated element

        return arr[n];
    
public inthashCode()
{@inheritDoc}

        return Arrays.hashCode(arr);
    
protected final voidset0(int n, java.lang.Object obj)
Sets the element at the given index, but without doing any type checks on the element. This method is protected so that subclasses may offer a safe type-checked public interface to their clients.

param
n {@code >= 0, < size();} which element
param
obj {@code null-ok;} the value to store

        throwIfImmutable();

        try {
            arr[n] = obj;
        } catch (ArrayIndexOutOfBoundsException ex) {
            // Translate the exception.
            throwIndex(n);
        }
    
public voidshrinkToFit()
Shrinks this instance to fit, by removing any unset ({@code null}) elements, leaving the remaining elements in their original order.

        int sz = arr.length;
        int newSz = 0;

        for (int i = 0; i < sz; i++) {
            if (arr[i] != null) {
                newSz++;
            }
        }

        if (sz == newSz) {
            return;
        }

        throwIfImmutable();

        Object[] newa = new Object[newSz];
        int at = 0;

        for (int i = 0; i < sz; i++) {
            Object one = arr[i];
            if (one != null) {
                newa[at] = one;
                at++;
            }
        }

        arr = newa;
        if (newSz == 0) {
            setImmutable();
        }
    
public final intsize()
Gets the number of elements in this list.

        return arr.length;
    
private java.lang.ObjectthrowIndex(int n)
Throws the appropriate exception for the given index value.

param
n the index value
return
never
throws
IndexOutOfBoundsException always thrown

        if (n < 0) {
            throw new IndexOutOfBoundsException("n < 0");
        }

        throw new IndexOutOfBoundsException("n >= size()");
    
public java.lang.StringtoHuman()
{@inheritDoc} This method will only work if every element of the list implements {@link ToHuman}.

        String name = getClass().getName();

        return toString0(name.substring(name.lastIndexOf('.") + 1) + '{",
                         ", ",
                         "}",
                         true);
    
public java.lang.StringtoHuman(java.lang.String prefix, java.lang.String separator, java.lang.String suffix)
Gets a customized human string for this instance. This method will only work if every element of the list implements {@link ToHuman}.

param
prefix {@code null-ok;} prefix for the start of the result
param
separator {@code null-ok;} separator to insert between each item
param
suffix {@code null-ok;} suffix for the end of the result
return
{@code non-null;} the custom string

        return toString0(prefix, separator, suffix, true);
    
public java.lang.StringtoString()
{@inheritDoc}

        String name = getClass().getName();

        return toString0(name.substring(name.lastIndexOf('.") + 1) + '{",
                         ", ",
                         "}",
                         false);
    
public java.lang.StringtoString(java.lang.String prefix, java.lang.String separator, java.lang.String suffix)
Gets a customized string form for this instance.

param
prefix {@code null-ok;} prefix for the start of the result
param
separator {@code null-ok;} separator to insert between each item
param
suffix {@code null-ok;} suffix for the end of the result
return
{@code non-null;} the custom string

        return toString0(prefix, separator, suffix, false);
    
private java.lang.StringtoString0(java.lang.String prefix, java.lang.String separator, java.lang.String suffix, boolean human)
Helper for {@link #toString} and {@link #toHuman}, which both of those call to pretty much do everything.

param
prefix {@code null-ok;} prefix for the start of the result
param
separator {@code null-ok;} separator to insert between each item
param
suffix {@code null-ok;} suffix for the end of the result
param
human whether the output is to be human
return
{@code non-null;} the custom string

        int len = arr.length;
        StringBuffer sb = new StringBuffer(len * 10 + 10);

        if (prefix != null) {
            sb.append(prefix);
        }

        for (int i = 0; i < len; i++) {
            if ((i != 0) && (separator != null)) {
                sb.append(separator);
            }

            if (human) {
                sb.append(((ToHuman) arr[i]).toHuman());
            } else {
                sb.append(arr[i]);
            }
        }

        if (suffix != null) {
            sb.append(suffix);
        }

        return sb.toString();