Code example for SparseArray

Methods: clone, put

0
    static final int SELECTOR_CONTAINER = 20;
    static final int SELECTOR_PATTERN = 21;
    static final int SELECTOR_PARENT = 22;
    static final int SELECTOR_COUNT = 23;
 
    private SparseArray<Object> mSelectorAttributes = new SparseArray<Object>();
    public static final String LOG_TAG = "ByClass";
 
    public UiSelector() { 
    } 
 
    protected UiSelector(UiSelector by) {
        mSelectorAttributes = by.cloneSelectors().mSelectorAttributes;
    } 
 
    protected UiSelector cloneSelectors() { 
        UiSelector ret = new UiSelector();
        ret.mSelectorAttributes = mSelectorAttributes.clone();
        return ret;
    } 
 
    static UiSelector patternBuilder(UiSelector selector) {
        if(!selector.hasPatternSelector()) {
            return new UiSelector().patternSelector(selector);
        } 
        return selector;
    } 
 
    static UiSelector patternBuilder(UiSelector container, UiSelector pattern) {
        return new UiSelector( 
                new UiSelector().containerSelector(container).patternSelector(pattern));
    } 
 
    public UiSelector text(String text) {
        return buildSelector(SELECTOR_TEXT, text);
    } 
 
    public UiSelector textStartsWith(String text) {
        return buildSelector(SELECTOR_START_TEXT, text);
    } 
 
    public UiSelector textContains(String text) {
        return buildSelector(SELECTOR_CONTAINS_TEXT, text);
    } 
 
    public UiSelector className(String className) {
        return buildSelector(SELECTOR_CLASS, className);
    } 
 
    public UiSelector description(String desc) {
        return buildSelector(SELECTOR_DESCRIPTION, desc);
    } 
 
    public UiSelector descriptionStartsWith(String desc) {
        return buildSelector(SELECTOR_START_DESCRIPTION, desc);
    } 
 
    public UiSelector descriptionContains(String desc) {
        return buildSelector(SELECTOR_CONTAINS_DESCRIPTION, desc);
    } 
 
    public UiSelector index(final int index) {
        return buildSelector(SELECTOR_INDEX, index);
    } 
 
    public UiSelector instance(final int instance) {
        return buildSelector(SELECTOR_INSTANCE, instance);
    } 
 
    public UiSelector enabled(boolean val) {
        return buildSelector(SELECTOR_ENABLED, val);
    } 
 
    public UiSelector focused(boolean val) {
        return buildSelector(SELECTOR_FOCUSED, val);
    } 
 
    public UiSelector focusable(boolean val) {
        return buildSelector(SELECTOR_FOCUSABLE, val);
    } 
 
    public UiSelector scrollable(boolean val) {
        return buildSelector(SELECTOR_SCROLLABLE, val);
    } 
 
    public UiSelector selected(boolean val) {
        return buildSelector(SELECTOR_SELECTED, val);
    } 
 
    public UiSelector checked(boolean val) {
        return buildSelector(SELECTOR_CHECKED, val);
    } 
 
    public UiSelector clickable(boolean val) {
        return buildSelector(SELECTOR_CLICKABLE, val);
    } 
 
    public UiSelector childSelector(UiSelector by) {
        return buildSelector(SELECTOR_CHILD, by);
    } 
 
    private UiSelector patternSelector(UiSelector by) {
        return buildSelector(SELECTOR_PATTERN, by);
    } 
 
    private UiSelector containerSelector(UiSelector by) {
        return buildSelector(SELECTOR_CONTAINER, by);
    } 
 
    public UiSelector fromParent(UiSelector by) {
        return buildSelector(SELECTOR_PARENT, by);
    } 
 
    public UiSelector packageName(String name) {
        return buildSelector(SELECTOR_PACKAGE_NAME, name);
    } 
 
    /** 
     * Building a By selector always returns a new By selector and never modifies the 
     * existing By selector being used. For example a test library have predefined 
     * By selectors SA, SB, SC etc. The test may decide that it needs a By selector SX in the 
     * context of SB. It can use SX = SB.critereon(x) to generate SX without modifying the 
     * state of SB which is expected to be something else. For this we will return a new 
     * By selector every time. 
     */ 
    private UiSelector buildSelector(int selectorId, Object selectorValue) {
        UiSelector by = new UiSelector(this);
        if(selectorId == SELECTOR_CHILD || selectorId == SELECTOR_PARENT)
            by.getLastSubSelector().mSelectorAttributes.put(selectorId, selectorValue);
        else 
            by.mSelectorAttributes.put(selectorId, selectorValue);
        return by;
    } 
 
    /** 
     * Selectors may have a hierarchy defined by specifying child nodes to be matched.