⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treepath.java

📁 gcc的组建
💻 JAVA
字号:
/* TreePath.java --   Copyright (C) 2002, 2005  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.tree;import java.io.Serializable;import java.util.Arrays;/** * A <code>TreePath</code> represents a sequence of tree elements that form * a path starting from the root of a tree.  A tree element can be represented * by any {@link Object}. *  * @author Andrew Selkirk */public class TreePath implements Serializable{  static final long serialVersionUID = 4380036194768077479L;  /**   * path   */  private Object[] path = null;  /**   * Creates a path from the list of objects representing tree elements.  The   * incoming array is copied so that subsequent changes do not affect this   * tree path.   *    * @param path  the elements in the path (<code>null</code> not permitted).   *    * @throws IllegalArgumentException if <code>path</code> is <code>null</code>.   */  public TreePath(Object[] path)  {    if (path == null)      throw new IllegalArgumentException("Null 'path' not permitted.");    this.path = new Object[path.length];    System.arraycopy(path, 0, this.path, 0, path.length);  }  /**   * Creates a new path from a single element.   *    * @param element the element (<code>null</code> not permitted).   *    * @throws IllegalArgumentException if <code>element</code> is    *         <code>null</code>.   */  public TreePath(Object element)  {    path = new Object[1];    path[0] = element;  }  /**   * Creates a new tree path by adding the specified <code>element</code> to   * the <code>path</code>.   *    * @param path  a tree path.   * @param element  a path element.   */  protected TreePath(TreePath path, Object element)  {    if (element == null)      throw new NullPointerException("Null 'element' argument.");    Object[] treepath = path.getPath();    // Create Tree Path    this.path = new Object[treepath.length + 1];    System.arraycopy(treepath, 0, this.path, 0, treepath.length);    this.path[treepath.length] = element;  }  /**   * Creates a new tree path using the first <code>length</code> elements   * from the given array.   *    * @param path  the path elements.   * @param length  the path length.   */  protected TreePath(Object[] path, int length)  {    // Create Path    this.path = new Object[length];    System.arraycopy(path, 0, this.path, 0, length);  }  /**   * Default constructor.   */  protected TreePath()  {    path = new Object[0];  }  /**   * Returns a hashcode for the path.   *    * @return A hashcode.   */  public int hashCode()  {    return getLastPathComponent().hashCode();  }  /**   * Tests this path for equality with an arbitrary object.  An object is    * considered equal to this path if and only if:   * <ul>   * <li>the object is not <code>null</code>;</li>   * <li>the object is an instanceof {@link TreePath};</li>   * <li>the object contains the same elements in the same order as this   * {@link TreePath};</li>   * </ul>   *    * @param object  the object (<code>null</code> permitted).   *    * @returns <code>true</code> if <code>obj</code> is equal to this tree path,   *          and <code>false</code> otherwise.   */  public boolean equals(Object object)  {    Object[] treepath;    int index;    if (object instanceof TreePath)      {        treepath = ((TreePath) object).getPath();        if (treepath.length != path.length)          return false;        for (index = 0; index < path.length; index++)          {            if (!treepath[index].equals(path[index]))              return false;          }        // Tree Path's are equals        return true;      }    // Unequal    return false;  }  /**   * Returns a string representation of this path.   *    * @return A string representation of this path.   */  public String toString()  {    if (path.length == 1)      return String.valueOf(path[0]);    else      return Arrays.asList(path).toString();  }  /**   * Returns an array containing the path elements.   *    * @returns An array containing the path elements.   */  public Object[] getPath()  {    return (Object[]) path.clone();  }  /**   * Returns the last object in the path.   *    * @return The last object in the path.   */  public Object getLastPathComponent()  {    return path[path.length - 1];  }  /**   * Returns the number of elements in the path.   *    * @returns The number of elements in the path.   */  public int getPathCount()  {    return path.length;  }  /**   * Returns the element at the specified position in the path.   *    * @param position the element position (<code>0 &lt N - 1</code>, where    *                 <code>N</code> is the number of elements in the path).   *    * @return The element at the specified position.   *    * @throws IllegalArgumentException if <code>position</code> is outside the   *         valid range.   */  public Object getPathComponent(int position)  {    if (position < 0 || position >= getPathCount())       throw new IllegalArgumentException("Invalid position: " + position);    return path[position];  }  /**   * Returns <code>true</code> if <code>path</code> is a descendant of this   * path, and <code>false</code> otherwise.  If <code>path</code> is    * <code>null</code>, this method returns <code>false</code>.   *    * @param path  the path to check (<code>null</code> permitted).   *    * @returns <code>true</code> if <code>path</code> is a descendant of this   *          path, and <code>false</code> otherwise   */  public boolean isDescendant(TreePath path)  {    if (path == null)      return false;    int count = getPathCount();    if (path.getPathCount() < count)      return false;    for (int i = 0; i < count; i++)    {      if (!this.path[i].equals(path.getPathComponent(i)))        return false;    }    return true;  }  /**   * Creates a new path that is equivalent to this path plus the specified   * element.   *    * @param element  the element.   *    * @returns A tree path.   */  public TreePath pathByAddingChild(Object element)  {    return new TreePath(this, element);  }  /**   * Returns the parent path, which is a path containing all the same elements   * as this path, except for the last one.  If this path contains only one   * element, the method returns <code>null</code>.   *    * @returns The parent path, or <code>null</code> if this path has only one   *          element.   */  public TreePath getParentPath()  {    // If this path has only one element, then we return null. That    // is what the JDK does.    if (path.length <= 1)      return null;    return new TreePath(this.getPath(), path.length - 1);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -