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
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.
17 * Subclassed to carry properties for specific kinds of elements.
19 /* package */ class JavaElementInfo {
22 * Collection of handles of immediate children of this
23 * object. This is an empty array if this element has
26 protected IJavaElement[] fChildren;
29 * Shared empty collection used for efficiency.
31 protected static IJavaElement[] fgEmptyChildren = new IJavaElement[]{};
33 * Is the structure of this element known
34 * @see IJavaElement#isStructureKnown()
36 protected boolean fIsStructureKnown = false;
39 * Shared empty collection used for efficiency.
41 static Object[] NO_NON_JAVA_RESOURCES = new Object[] {};
42 protected JavaElementInfo() {
43 fChildren = fgEmptyChildren;
45 public void addChild(IJavaElement child) {
46 if (fChildren == fgEmptyChildren) {
47 setChildren(new IJavaElement[] {child});
49 if (!includesChild(child)) {
50 setChildren(growAndAddToArray(fChildren, child));
54 public Object clone() {
58 catch (CloneNotSupportedException e) {
62 public IJavaElement[] getChildren() {
66 * Adds the new element to a new array that contains all of the elements of the old array.
67 * Returns the new array.
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;
77 * Returns <code>true</code> if this child is in my children collection
79 protected boolean includesChild(IJavaElement child) {
81 for (int i= 0; i < fChildren.length; i++) {
82 if (fChildren[i].equals(child)) {
89 * @see IJavaElement#isStructureKnown()
91 public boolean isStructureKnown() {
92 return fIsStructureKnown;
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.
98 protected IJavaElement[] removeAndShrinkArray(IJavaElement[] array, IJavaElement deletion) {
99 IJavaElement[] old = array;
100 array = new IJavaElement[old.length - 1];
102 for (int i = 0; i < old.length; i++) {
103 if (!old[i].equals(deletion)) {
106 System.arraycopy(old, i + 1, array, j, old.length - (i + 1));
113 public void removeChild(IJavaElement child) {
114 if (includesChild(child)) {
115 setChildren(removeAndShrinkArray(fChildren, child));
118 public void setChildren(IJavaElement[] children) {
119 fChildren = children;
122 * Sets whether the structure of this element known
123 * @see IJavaElement#isStructureKnown()
125 public void setIsStructureKnown(boolean newIsStructureKnown) {
126 fIsStructureKnown = newIsStructureKnown;