All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.deque.axe.android.utils.AxeTree Maven / Gradle / Ivy

The newest version!
package com.deque.axe.android.utils;

import androidx.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public interface AxeTree> {

  Iterable getTreeChildren();

  T getTreeNode();

  String getNodeId();

  @Retention(RetentionPolicy.SOURCE)
  @IntDef({
    CallBackResponse.CONTINUE,
    CallBackResponse.STOP,
    CallBackResponse.SKIP_BRANCH
  })
  @interface CallBackResponse {
    int CONTINUE = 0;
    int STOP = 1;
    int SKIP_BRANCH = 2;
  }

  interface Callback {
    @CallBackResponse
    int run(final T instance);
  }

  /**
   * Run the callback on each view in the hierarchy, unless asked to stop.
   * @param callback The function to run.
   * @return Whether or not to keep going deeper into the hierarchy.
   */
  @CallBackResponse
  default int forEachRecursive(final Callback callback) {

    int callbackResponse = callback.run(this.getTreeNode());

    if (callbackResponse == CallBackResponse.STOP) {
      return CallBackResponse.STOP;
    }

    if (callbackResponse == CallBackResponse.SKIP_BRANCH) {
      return CallBackResponse.CONTINUE;
    }

    Iterable children = getTreeChildren();

    if (children != null) {
      for (T child : children) {
        if (child.forEachRecursive(callback) == CallBackResponse.STOP) {
          return CallBackResponse.STOP;
        }
      }
    }

    return CallBackResponse.CONTINUE;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy