KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > JavaElementInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core;
12
13 import org.eclipse.jdt.core.IJavaElement;
14
15 /**
16  * Holds cached structure and properties for a Java element.
17  * Subclassed to carry properties for specific kinds of elements.
18  */

19 /* package */ class JavaElementInfo {
20
21     /**
22      * Collection of handles of immediate children of this
23      * object. This is an empty array if this element has
24      * no children.
25      */

26     protected IJavaElement[] children;
27
28     /**
29      * Shared empty collection used for efficiency.
30      */

31     static Object JavaDoc[] NO_NON_JAVA_RESOURCES = new Object JavaDoc[] {};
32     
33     protected JavaElementInfo() {
34         this.children = JavaElement.NO_ELEMENTS;
35     }
36     public void addChild(IJavaElement child) {
37         int length = this.children.length;
38         if (length == 0) {
39             this.children = new IJavaElement[] {child};
40         } else {
41             for (int i = 0; i < length; i++) {
42                 if (children[i].equals(child))
43                     return; // already included
44
}
45             System.arraycopy(this.children, 0, this.children = new IJavaElement[length+1], 0, length);
46             this.children[length] = child;
47         }
48     }
49     public Object JavaDoc clone() {
50         try {
51             return super.clone();
52         }
53         catch (CloneNotSupportedException JavaDoc e) {
54             throw new Error JavaDoc();
55         }
56     }
57     public IJavaElement[] getChildren() {
58         return this.children;
59     }
60     public void removeChild(IJavaElement child) {
61         for (int i = 0, length = this.children.length; i < length; i++) {
62             IJavaElement element = this.children[i];
63             if (element.equals(child)) {
64                 if (length == 1) {
65                     this.children = JavaElement.NO_ELEMENTS;
66                 } else {
67                     IJavaElement[] newChildren = new IJavaElement[length-1];
68                     System.arraycopy(this.children, 0, newChildren , 0, i);
69                     if (i < length-1)
70                         System.arraycopy(this.children, i+1, newChildren, i, length-1-i);
71                     this.children = newChildren;
72                 }
73                 break;
74             }
75         }
76     }
77     public void setChildren(IJavaElement[] children) {
78         this.children = children;
79     }
80 }
81
Popular Tags