Пример #1
0
  /**
   * Returns true if <code>aTreePath</code> is a descendant of this {@code TreePath}. A {@code
   * TreePath} {@code P1} is a descendant of a {@code TreePath} {@code P2} if {@code P1} contains
   * all of the elements that make up {@code P2's} path. For example, if this object has the path
   * {@code [a, b]}, and <code>aTreePath</code> has the path {@code [a, b, c]}, then <code>aTreePath
   * </code> is a descendant of this object. However, if <code>aTreePath</code> has the path {@code
   * [a]}, then it is not a descendant of this object. By this definition a {@code TreePath} is
   * always considered a descendant of itself. That is, <code>aTreePath.isDescendant(aTreePath)
   * </code> returns {@code true}.
   *
   * @param aTreePath the {@code TreePath} to check
   * @return true if <code>aTreePath</code> is a descendant of this path
   */
  public boolean isDescendant(TreePath aTreePath) {
    if (aTreePath == this) return true;

    if (aTreePath != null) {
      int pathLength = getPathCount();
      int oPathLength = aTreePath.getPathCount();

      if (oPathLength < pathLength)
        // Can't be a descendant, has fewer components in the path.
        return false;
      while (oPathLength-- > pathLength) aTreePath = aTreePath.getParentPath();
      return equals(aTreePath);
    }
    return false;
  }
Пример #2
0
  /**
   * Compares this {@code TreePath} to the specified object. This returns {@code true} if {@code o}
   * is a {@code TreePath} with the exact same elements (as determined by using {@code equals} on
   * each element of the path).
   *
   * @param o the object to compare
   */
  public boolean equals(Object o) {
    if (o == this) return true;
    if (o instanceof TreePath) {
      TreePath oTreePath = (TreePath) o;

      if (getPathCount() != oTreePath.getPathCount()) return false;
      for (TreePath path = this; path != null; path = path.getParentPath()) {
        if (!(path.getLastPathComponent().equals(oTreePath.getLastPathComponent()))) {
          return false;
        }
        oTreePath = oTreePath.getParentPath();
      }
      return true;
    }
    return false;
  }