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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import net.sourceforge.phpdt.core.IJavaElement;
16 * Holds cached structure and properties for a Java element. Subclassed to carry
17 * properties for specific kinds of elements.
19 /* package */class JavaElementInfo {
22 * Collection of handles of immediate children of this object. This is an
23 * empty array if this element has no children.
25 protected IJavaElement[] fChildren;
28 * Shared empty collection used for efficiency.
30 protected static IJavaElement[] fgEmptyChildren = new IJavaElement[] {};
33 * Is the structure of this element known
35 * @see IJavaElement#isStructureKnown()
37 protected boolean fIsStructureKnown = false;
40 * Shared empty collection used for efficiency.
42 static Object[] NO_NON_JAVA_RESOURCES = new Object[] {};
44 protected JavaElementInfo() {
45 fChildren = fgEmptyChildren;
48 public void addChild(IJavaElement child) {
49 if (fChildren == fgEmptyChildren) {
50 setChildren(new IJavaElement[] { child });
52 if (!includesChild(child)) {
53 setChildren(growAndAddToArray(fChildren, child));
58 public Object clone() {
61 } catch (CloneNotSupportedException e) {
66 public IJavaElement[] getChildren() {
71 * Adds the new element to a new array that contains all of the elements of
72 * the old array. Returns the new array.
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;
84 * Returns <code>true</code> if this child is in my children collection
86 protected boolean includesChild(IJavaElement child) {
88 for (int i = 0; i < fChildren.length; i++) {
89 if (fChildren[i].equals(child)) {
97 * @see IJavaElement#isStructureKnown()
99 public boolean isStructureKnown() {
100 return fIsStructureKnown;
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
108 protected IJavaElement[] removeAndShrinkArray(IJavaElement[] array,
109 IJavaElement deletion) {
110 IJavaElement[] old = array;
111 array = new IJavaElement[old.length - 1];
113 for (int i = 0; i < old.length; i++) {
114 if (!old[i].equals(deletion)) {
117 System.arraycopy(old, i + 1, array, j, old.length - (i + 1));
125 public void removeChild(IJavaElement child) {
126 if (includesChild(child)) {
127 setChildren(removeAndShrinkArray(fChildren, child));
131 public void setChildren(IJavaElement[] children) {
132 fChildren = children;
136 * Sets whether the structure of this element known
138 * @see IJavaElement#isStructureKnown()
140 public void setIsStructureKnown(boolean newIsStructureKnown) {
141 fIsStructureKnown = newIsStructureKnown;