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;
18 * Holds cached structure and properties for a Java element.
19 * Subclassed to carry properties for specific kinds of elements.
21 /* package */ class JavaElementInfo {
24 * Collection of handles of immediate children of this
25 * object. This is an empty array if this element has
28 protected IJavaElement[] fChildren;
31 * Shared empty collection used for efficiency.
33 protected static IJavaElement[] fgEmptyChildren = new IJavaElement[]{};
35 * Is the structure of this element known
36 * @see IJavaElement#isStructureKnown()
38 protected boolean fIsStructureKnown = false;
41 * Shared empty collection used for efficiency.
43 static Object[] NO_NON_JAVA_RESOURCES = new Object[] {};
44 protected JavaElementInfo() {
45 fChildren = fgEmptyChildren;
47 public void addChild(IJavaElement child) {
48 if (fChildren == fgEmptyChildren) {
49 setChildren(new IJavaElement[] {child});
51 if (!includesChild(child)) {
52 setChildren(growAndAddToArray(fChildren, child));
56 public Object clone() {
60 catch (CloneNotSupportedException e) {
64 public IJavaElement[] getChildren() {
68 * Adds the new element to a new array that contains all of the elements of the old array.
69 * Returns the new array.
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;
79 * Returns <code>true</code> if this child is in my children collection
81 protected boolean includesChild(IJavaElement child) {
83 for (int i= 0; i < fChildren.length; i++) {
84 if (fChildren[i].equals(child)) {
91 * @see IJavaElement#isStructureKnown()
93 public boolean isStructureKnown() {
94 return fIsStructureKnown;
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.
100 protected IJavaElement[] removeAndShrinkArray(IJavaElement[] array, IJavaElement deletion) {
101 IJavaElement[] old = array;
102 array = new IJavaElement[old.length - 1];
104 for (int i = 0; i < old.length; i++) {
105 if (!old[i].equals(deletion)) {
108 System.arraycopy(old, i + 1, array, j, old.length - (i + 1));
115 public void removeChild(IJavaElement child) {
116 if (includesChild(child)) {
117 setChildren(removeAndShrinkArray(fChildren, child));
120 public void setChildren(IJavaElement[] children) {
121 fChildren = children;
124 * Sets whether the structure of this element known
125 * @see IJavaElement#isStructureKnown()
127 public void setIsStructureKnown(boolean newIsStructureKnown) {
128 fIsStructureKnown = newIsStructureKnown;