A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / JavaElementInfo.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.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. Subclassed to carry
17  * properties for specific kinds of elements.
18  */
19 /* package */class JavaElementInfo {
20
21         /**
22          * Collection of handles of immediate children of this object. This is an
23          * empty array if this element has no children.
24          */
25         protected IJavaElement[] fChildren;
26
27         /**
28          * Shared empty collection used for efficiency.
29          */
30         protected static IJavaElement[] fgEmptyChildren = new IJavaElement[] {};
31
32         /**
33          * Is the structure of this element known
34          * 
35          * @see IJavaElement#isStructureKnown()
36          */
37         protected boolean fIsStructureKnown = false;
38
39         /**
40          * Shared empty collection used for efficiency.
41          */
42         static Object[] NO_NON_JAVA_RESOURCES = new Object[] {};
43
44         protected JavaElementInfo() {
45                 fChildren = fgEmptyChildren;
46         }
47
48         public void addChild(IJavaElement child) {
49                 if (fChildren == fgEmptyChildren) {
50                         setChildren(new IJavaElement[] { child });
51                 } else {
52                         if (!includesChild(child)) {
53                                 setChildren(growAndAddToArray(fChildren, child));
54                         }
55                 }
56         }
57
58         public Object clone() {
59                 try {
60                         return super.clone();
61                 } catch (CloneNotSupportedException e) {
62                         throw new Error();
63                 }
64         }
65
66         public IJavaElement[] getChildren() {
67                 return fChildren;
68         }
69
70         /**
71          * Adds the new element to a new array that contains all of the elements of
72          * the old array. Returns the new array.
73          */
74         protected IJavaElement[] growAndAddToArray(IJavaElement[] array,
75                         IJavaElement addition) {
76                 IJavaElement[] old = array;
77                 array = new IJavaElement[old.length + 1];
78                 System.arraycopy(old, 0, array, 0, old.length);
79                 array[old.length] = addition;
80                 return array;
81         }
82
83         /**
84          * Returns <code>true</code> if this child is in my children collection
85          */
86         protected boolean includesChild(IJavaElement child) {
87
88                 for (int i = 0; i < fChildren.length; i++) {
89                         if (fChildren[i].equals(child)) {
90                                 return true;
91                         }
92                 }
93                 return false;
94         }
95
96         /**
97          * @see IJavaElement#isStructureKnown()
98          */
99         public boolean isStructureKnown() {
100                 return fIsStructureKnown;
101         }
102
103         /**
104          * Returns an array with all the same elements as the specified array except
105          * for the element to remove. Assumes that the deletion is contained in the
106          * array.
107          */
108         protected IJavaElement[] removeAndShrinkArray(IJavaElement[] array,
109                         IJavaElement deletion) {
110                 IJavaElement[] old = array;
111                 array = new IJavaElement[old.length - 1];
112                 int j = 0;
113                 for (int i = 0; i < old.length; i++) {
114                         if (!old[i].equals(deletion)) {
115                                 array[j] = old[i];
116                         } else {
117                                 System.arraycopy(old, i + 1, array, j, old.length - (i + 1));
118                                 return array;
119                         }
120                         j++;
121                 }
122                 return array;
123         }
124
125         public void removeChild(IJavaElement child) {
126                 if (includesChild(child)) {
127                         setChildren(removeAndShrinkArray(fChildren, child));
128                 }
129         }
130
131         public void setChildren(IJavaElement[] children) {
132                 fChildren = children;
133         }
134
135         /**
136          * Sets whether the structure of this element known
137          * 
138          * @see IJavaElement#isStructureKnown()
139          */
140         public void setIsStructureKnown(boolean newIsStructureKnown) {
141                 fIsStructureKnown = newIsStructureKnown;
142         }
143 }