ef69dd200d7fdcbd6442d94314c897cc152ca201
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / JavaElementInfo.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v0.5 
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v05.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import net.sourceforge.phpdt.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[] fChildren;
27
28         /**
29          * Shared empty collection used for efficiency.
30          */
31         protected static IJavaElement[] fgEmptyChildren = new IJavaElement[]{};
32         /**
33          * Is the structure of this element known
34          * @see IJavaElement#isStructureKnown()
35          */
36         protected boolean fIsStructureKnown = false;
37
38         /**
39          * Shared empty collection used for efficiency.
40          */
41         static Object[] NO_NON_JAVA_RESOURCES = new Object[] {};        
42         protected JavaElementInfo() {
43                 fChildren = fgEmptyChildren;
44         }
45         public void addChild(IJavaElement child) {
46                 if (fChildren == fgEmptyChildren) {
47                         setChildren(new IJavaElement[] {child});
48                 } else {
49                         if (!includesChild(child)) {
50                                 setChildren(growAndAddToArray(fChildren, child));
51                         }
52                 }
53         }
54         public Object clone() {
55                 try {
56                         return super.clone();
57                 }
58                 catch (CloneNotSupportedException e) {
59                         throw new Error();
60                 }
61         }
62         public IJavaElement[] getChildren() {
63                 return fChildren;
64         }
65         /**
66          * Adds the new element to a new array that contains all of the elements of the old array.
67          * Returns the new array.
68          */
69         protected IJavaElement[] growAndAddToArray(IJavaElement[] array, IJavaElement addition) {
70                 IJavaElement[] old = array;
71                 array = new IJavaElement[old.length + 1];
72                 System.arraycopy(old, 0, array, 0, old.length);
73                 array[old.length] = addition;
74                 return array;
75         }
76         /**
77          * Returns <code>true</code> if this child is in my children collection
78          */
79         protected boolean includesChild(IJavaElement child) {
80                 
81                 for (int i= 0; i < fChildren.length; i++) {
82                         if (fChildren[i].equals(child)) {
83                                 return true;
84                         }
85                 }
86                 return false;
87         }
88         /**
89          * @see IJavaElement#isStructureKnown()
90          */
91         public boolean isStructureKnown() {
92                 return fIsStructureKnown;
93         }
94         /**
95          * Returns an array with all the same elements as the specified array except for
96          * the element to remove. Assumes that the deletion is contained in the array.
97          */
98         protected IJavaElement[] removeAndShrinkArray(IJavaElement[] array, IJavaElement deletion) {
99                 IJavaElement[] old = array;
100                 array = new IJavaElement[old.length - 1];
101                 int j = 0;
102                 for (int i = 0; i < old.length; i++) {
103                         if (!old[i].equals(deletion)) {
104                                 array[j] = old[i];
105                         } else {
106                                 System.arraycopy(old, i + 1, array, j, old.length - (i + 1));
107                                 return array;
108                         }
109                         j++;
110                 }
111                 return array;
112         }
113         public void removeChild(IJavaElement child) {
114                 if (includesChild(child)) {
115                         setChildren(removeAndShrinkArray(fChildren, child));
116                 }
117         }
118         public void setChildren(IJavaElement[] children) {
119                 fChildren = children;
120         }
121         /**
122          * Sets whether the structure of this element known
123          * @see IJavaElement#isStructureKnown()
124          */
125         public void setIsStructureKnown(boolean newIsStructureKnown) {
126                 fIsStructureKnown = newIsStructureKnown;
127         }
128 }