added a builder to parse files with eclipse's build mechanisms
[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
17 /**
18  * Holds cached structure and properties for a Java element.
19  * Subclassed to carry properties for specific kinds of elements.
20  */
21 /* package */ class JavaElementInfo {
22
23         /**
24          * Collection of handles of immediate children of this
25          * object. This is an empty array if this element has
26          * no children.
27          */
28         protected IJavaElement[] fChildren;
29
30         /**
31          * Shared empty collection used for efficiency.
32          */
33         protected static IJavaElement[] fgEmptyChildren = new IJavaElement[]{};
34         /**
35          * Is the structure of this element known
36          * @see IJavaElement#isStructureKnown()
37          */
38         protected boolean fIsStructureKnown = false;
39
40         /**
41          * Shared empty collection used for efficiency.
42          */
43         static Object[] NO_NON_JAVA_RESOURCES = new Object[] {};        
44         protected JavaElementInfo() {
45                 fChildren = fgEmptyChildren;
46         }
47         public void addChild(IJavaElement child) {
48                 if (fChildren == fgEmptyChildren) {
49                         setChildren(new IJavaElement[] {child});
50                 } else {
51                         if (!includesChild(child)) {
52                                 setChildren(growAndAddToArray(fChildren, child));
53                         }
54                 }
55         }
56         public Object clone() {
57                 try {
58                         return super.clone();
59                 }
60                 catch (CloneNotSupportedException e) {
61                         throw new Error();
62                 }
63         }
64         public IJavaElement[] getChildren() {
65                 return fChildren;
66         }
67         /**
68          * Adds the new element to a new array that contains all of the elements of the old array.
69          * Returns the new array.
70          */
71         protected IJavaElement[] growAndAddToArray(IJavaElement[] array, IJavaElement addition) {
72                 IJavaElement[] old = array;
73                 array = new IJavaElement[old.length + 1];
74                 System.arraycopy(old, 0, array, 0, old.length);
75                 array[old.length] = addition;
76                 return array;
77         }
78         /**
79          * Returns <code>true</code> if this child is in my children collection
80          */
81         protected boolean includesChild(IJavaElement child) {
82                 
83                 for (int i= 0; i < fChildren.length; i++) {
84                         if (fChildren[i].equals(child)) {
85                                 return true;
86                         }
87                 }
88                 return false;
89         }
90         /**
91          * @see IJavaElement#isStructureKnown()
92          */
93         public boolean isStructureKnown() {
94                 return fIsStructureKnown;
95         }
96         /**
97          * Returns an array with all the same elements as the specified array except for
98          * the element to remove. Assumes that the deletion is contained in the array.
99          */
100         protected IJavaElement[] removeAndShrinkArray(IJavaElement[] array, IJavaElement deletion) {
101                 IJavaElement[] old = array;
102                 array = new IJavaElement[old.length - 1];
103                 int j = 0;
104                 for (int i = 0; i < old.length; i++) {
105                         if (!old[i].equals(deletion)) {
106                                 array[j] = old[i];
107                         } else {
108                                 System.arraycopy(old, i + 1, array, j, old.length - (i + 1));
109                                 return array;
110                         }
111                         j++;
112                 }
113                 return array;
114         }
115         public void removeChild(IJavaElement child) {
116                 if (includesChild(child)) {
117                         setChildren(removeAndShrinkArray(fChildren, child));
118                 }
119         }
120         public void setChildren(IJavaElement[] children) {
121                 fChildren = children;
122         }
123         /**
124          * Sets whether the structure of this element known
125          * @see IJavaElement#isStructureKnown()
126          */
127         public void setIsStructureKnown(boolean newIsStructureKnown) {
128                 fIsStructureKnown = newIsStructureKnown;
129         }
130 }