001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.jdt.core.IField;
015: import org.eclipse.jdt.core.IInitializer;
016: import org.eclipse.jdt.core.IMethod;
017: import org.eclipse.jdt.core.IPackageFragment;
018: import org.eclipse.jdt.core.IType;
019:
020: /**
021: * @see IJavaElementRequestor
022: */
023:
024: public class JavaElementRequestor implements IJavaElementRequestor {
025: /**
026: * True if this requestor no longer wants to receive
027: * results from its <code>IRequestorNameLookup</code>.
028: */
029: protected boolean fCanceled = false;
030:
031: /**
032: * A collection of the resulting fields, or <code>null</code>
033: * if no field results have been received.
034: */
035: protected ArrayList fFields = null;
036:
037: /**
038: * A collection of the resulting initializers, or <code>null</code>
039: * if no initializer results have been received.
040: */
041: protected ArrayList fInitializers = null;
042:
043: /**
044: * A collection of the resulting member types, or <code>null</code>
045: * if no member type results have been received.
046: */
047: protected ArrayList fMemberTypes = null;
048:
049: /**
050: * A collection of the resulting methods, or <code>null</code>
051: * if no method results have been received.
052: */
053: protected ArrayList fMethods = null;
054:
055: /**
056: * A collection of the resulting package fragments, or <code>null</code>
057: * if no package fragment results have been received.
058: */
059: protected ArrayList fPackageFragments = null;
060:
061: /**
062: * A collection of the resulting types, or <code>null</code>
063: * if no type results have been received.
064: */
065: protected ArrayList fTypes = null;
066:
067: /**
068: * Empty arrays used for efficiency
069: */
070: protected static IField[] fgEmptyFieldArray = new IField[0];
071: protected static IInitializer[] fgEmptyInitializerArray = new IInitializer[0];
072: protected static IType[] fgEmptyTypeArray = new IType[0];
073: protected static IPackageFragment[] fgEmptyPackageFragmentArray = new IPackageFragment[0];
074: protected static IMethod[] fgEmptyMethodArray = new IMethod[0];
075:
076: /**
077: * @see IJavaElementRequestor
078: */
079: public void acceptField(IField field) {
080: if (fFields == null) {
081: fFields = new ArrayList();
082: }
083: fFields.add(field);
084: }
085:
086: /**
087: * @see IJavaElementRequestor
088: */
089: public void acceptInitializer(IInitializer initializer) {
090: if (fInitializers == null) {
091: fInitializers = new ArrayList();
092: }
093: fInitializers.add(initializer);
094: }
095:
096: /**
097: * @see IJavaElementRequestor
098: */
099: public void acceptMemberType(IType type) {
100: if (fMemberTypes == null) {
101: fMemberTypes = new ArrayList();
102: }
103: fMemberTypes.add(type);
104: }
105:
106: /**
107: * @see IJavaElementRequestor
108: */
109: public void acceptMethod(IMethod method) {
110: if (fMethods == null) {
111: fMethods = new ArrayList();
112: }
113: fMethods.add(method);
114: }
115:
116: /**
117: * @see IJavaElementRequestor
118: */
119: public void acceptPackageFragment(IPackageFragment packageFragment) {
120: if (fPackageFragments == null) {
121: fPackageFragments = new ArrayList();
122: }
123: fPackageFragments.add(packageFragment);
124: }
125:
126: /**
127: * @see IJavaElementRequestor
128: */
129: public void acceptType(IType type) {
130: if (fTypes == null) {
131: fTypes = new ArrayList();
132: }
133: fTypes.add(type);
134: }
135:
136: /**
137: * @see IJavaElementRequestor
138: */
139: public IField[] getFields() {
140: if (fFields == null) {
141: return fgEmptyFieldArray;
142: }
143: int size = fFields.size();
144: IField[] results = new IField[size];
145: fFields.toArray(results);
146: return results;
147: }
148:
149: /**
150: * @see IJavaElementRequestor
151: */
152: public IInitializer[] getInitializers() {
153: if (fInitializers == null) {
154: return fgEmptyInitializerArray;
155: }
156: int size = fInitializers.size();
157: IInitializer[] results = new IInitializer[size];
158: fInitializers.toArray(results);
159: return results;
160: }
161:
162: /**
163: * @see IJavaElementRequestor
164: */
165: public IType[] getMemberTypes() {
166: if (fMemberTypes == null) {
167: return fgEmptyTypeArray;
168: }
169: int size = fMemberTypes.size();
170: IType[] results = new IType[size];
171: fMemberTypes.toArray(results);
172: return results;
173: }
174:
175: /**
176: * @see IJavaElementRequestor
177: */
178: public IMethod[] getMethods() {
179: if (fMethods == null) {
180: return fgEmptyMethodArray;
181: }
182: int size = fMethods.size();
183: IMethod[] results = new IMethod[size];
184: fMethods.toArray(results);
185: return results;
186: }
187:
188: /**
189: * @see IJavaElementRequestor
190: */
191: public IPackageFragment[] getPackageFragments() {
192: if (fPackageFragments == null) {
193: return fgEmptyPackageFragmentArray;
194: }
195: int size = fPackageFragments.size();
196: IPackageFragment[] results = new IPackageFragment[size];
197: fPackageFragments.toArray(results);
198: return results;
199: }
200:
201: /**
202: * @see IJavaElementRequestor
203: */
204: public IType[] getTypes() {
205: if (fTypes == null) {
206: return fgEmptyTypeArray;
207: }
208: int size = fTypes.size();
209: IType[] results = new IType[size];
210: fTypes.toArray(results);
211: return results;
212: }
213:
214: /**
215: * @see IJavaElementRequestor
216: */
217: public boolean isCanceled() {
218: return fCanceled;
219: }
220:
221: /**
222: * Reset the state of this requestor.
223: */
224: public void reset() {
225: fCanceled = false;
226: fFields = null;
227: fInitializers = null;
228: fMemberTypes = null;
229: fMethods = null;
230: fPackageFragments = null;
231: fTypes = null;
232: }
233:
234: /**
235: * Sets the #isCanceled state of this requestor to true or false.
236: */
237: public void setCanceled(boolean b) {
238: fCanceled = b;
239: }
240: }
|