KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > TreePath


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.packageview;
12
13 import org.eclipse.jface.util.Assert;
14 import org.eclipse.jface.viewers.IElementComparer;
15
16 /**
17  * A tree path denotes a model element in a tree viewer. Tree path
18  * objects do have value semantics.
19  *
20  * @since 3.1
21  */

22 public final class TreePath {
23     private Object JavaDoc[] fSegments;
24     private int fHash;
25     
26     public TreePath(Object JavaDoc[] segments) {
27         Assert.isNotNull(segments);
28         for (int i= 0; i < segments.length; i++) {
29             Assert.isNotNull(segments[i]);
30         }
31         fSegments= segments;
32     }
33     
34     public Object JavaDoc getSegment(int index) {
35         return fSegments[index];
36     }
37     
38     public int getSegmentCount() {
39         return fSegments.length;
40     }
41     
42     public Object JavaDoc getFirstSegment() {
43         if (fSegments.length == 0)
44             return null;
45         return fSegments[0];
46     }
47     
48     public Object JavaDoc getLastSegment() {
49         if (fSegments.length == 0)
50             return null;
51         return fSegments[fSegments.length - 1];
52     }
53     
54     public boolean equals(Object JavaDoc other) {
55         if (!(other instanceof TreePath))
56             return false;
57         TreePath otherPath= (TreePath)other;
58         if (fSegments.length != otherPath.fSegments.length)
59             return false;
60         for (int i= 0; i < fSegments.length; i++) {
61             if (!fSegments[i].equals(otherPath.fSegments[i]))
62                 return false;
63         }
64         return true;
65     }
66     
67     public int hashCode() {
68         if (fHash != 0)
69             return fHash;
70         for (int i= 0; i < fSegments.length; i++) {
71             fHash= fHash + fSegments[i].hashCode();
72         }
73         return fHash;
74     }
75     
76     public boolean equals(TreePath otherPath, IElementComparer comparer) {
77         if (comparer == null)
78             comparer= DefaultElementComparer.INSTANCE;
79         if (otherPath == null)
80             return false;
81         if (fSegments.length != otherPath.fSegments.length)
82             return false;
83         for (int i= 0; i < fSegments.length; i++) {
84             if (!comparer.equals(fSegments[i], otherPath.fSegments[i]))
85                 return false;
86         }
87         return true;
88     }
89 }
90
Popular Tags