FileDocCategorySizeDatePackage
SwitchList.javaAPI DocAndroid 1.5 API5129Wed May 06 22:41:02 BST 2009com.android.dx.cf.code

SwitchList

public final class SwitchList extends com.android.dx.util.MutabilityControl
List of (value, target) mappings representing the choices of a tableswitch or lookupswitch instruction. It also holds the default target for the switch.

Fields Summary
private final com.android.dx.util.IntList
values
non-null; list of test values
private final com.android.dx.util.IntList
targets
non-null; list of targets corresponding to the test values; there is always one extra element in the target list, to hold the default target
private int
size
ultimate size of the list
Constructors Summary
public SwitchList(int size)
Constructs an instance.

param
size >= 0; the number of elements to be in the table

        super(true);
        this.values = new IntList(size);
        this.targets = new IntList(size + 1);
        this.size = size;
    
Methods Summary
public voidadd(int value, int target)
Adds the given item.

param
value the test value
param
target >= 0; the absolute (not relative) target address

        throwIfImmutable();

        if (target < 0) {
            throw new IllegalArgumentException("target < 0");
        }

        values.add(value);
        targets.add(target);
    
public intgetDefaultTarget()
Gets the default target. This is just a shorthand for getTarget(size()).

return
>= 0; the default target

        return targets.get(size);
    
public intgetTarget(int n)
Gets the indicated target. Asking for the target at size() returns the default target.

param
n >= 0, <= size(); which index
return
>= 0; the target

        return targets.get(n);
    
public com.android.dx.util.IntListgetTargets()
Gets the list of all targets. This includes one extra element at the end of the list, which holds the default target.

return
non-null; the target list

        return targets;
    
public intgetValue(int n)
Gets the indicated test value.

param
n >= 0;, < size(); which index
return
the test value

        return values.get(n);
    
public com.android.dx.util.IntListgetValues()
Gets the list of all case values.

return
non-null; the case value list

        return values;
    
public voidremoveSuperfluousDefaults()
Shrinks this instance if possible, removing test elements that refer to the default target. This is only valid after the instance is fully populated, including the default target (naturally).

        throwIfImmutable();

        int sz = size;

        if (sz != (targets.size() - 1)) {
            throw new IllegalArgumentException("incomplete instance");
        }

        int defaultTarget = targets.get(sz);
        int at = 0;

        for (int i = 0; i < sz; i++) {
            int target = targets.get(i);
            if (target != defaultTarget) {
                if (i != at) {
                    targets.set(at, target);
                    values.set(at, values.get(i));
                }
                at++;
            }
        }

        if (at != sz) {
            values.shrink(at);
            targets.set(at, defaultTarget);
            targets.shrink(at + 1);
            size = at;
        }
    
public voidsetDefaultTarget(int target)
Sets the default target. It is only valid to call this method when all the non-default elements have been set.

param
target >= 0; the absolute (not relative) default target address

        throwIfImmutable();

        if (target < 0) {
            throw new IllegalArgumentException("target < 0");
        }

        if (targets.size() != size) {
            throw new RuntimeException("non-default elements not all set");
        }

        targets.add(target);
    
public voidsetImmutable()
{@inheritDoc}

        values.setImmutable();
        targets.setImmutable();
        super.setImmutable();
    
public intsize()
Gets the size of the list.

return
>= 0; the list size

        return size;