KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > matching > AndPattern


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.search.matching;
12
13 import java.io.IOException JavaDoc;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.OperationCanceledException;
17 import org.eclipse.jdt.core.search.*;
18 import org.eclipse.jdt.internal.compiler.util.SimpleSet;
19 import org.eclipse.jdt.internal.core.index.*;
20 import org.eclipse.jdt.internal.core.search.IndexQueryRequestor;
21
22 /**
23  * Query the index multiple times and do an 'and' on the results.
24  */

25 public abstract class AndPattern extends JavaSearchPattern { // TODO should rename IntersectingPattern, and make AndPattern a true subclass
26

27 public AndPattern(int patternKind, int matchRule) {
28     super(patternKind, matchRule);
29 }
30 public void findIndexMatches(Index index, IndexQueryRequestor requestor, SearchParticipant participant, IJavaSearchScope scope, IProgressMonitor progressMonitor) throws IOException JavaDoc {
31     if (progressMonitor != null && progressMonitor.isCanceled()) throw new OperationCanceledException();
32
33     this.resetQuery();
34     SimpleSet intersectedNames = null;
35     try {
36         index.startQuery();
37         do {
38             SearchPattern pattern = ((InternalSearchPattern) this).currentPattern();
39             EntryResult[] entries = ((InternalSearchPattern)pattern).queryIn(index);
40             if (entries == null) return;
41
42             SearchPattern decodedResult = pattern.getBlankPattern();
43             SimpleSet newIntersectedNames = new SimpleSet(3);
44             for (int i = 0, l = entries.length; i < l; i++) {
45                 if (progressMonitor != null && progressMonitor.isCanceled()) throw new OperationCanceledException();
46
47                 EntryResult entry = entries[i];
48                 decodedResult.decodeIndexKey(entry.getWord());
49                 if (pattern.matchesDecodedKey(decodedResult)) {
50                     String JavaDoc[] names = entry.getDocumentNames(index);
51                     if (intersectedNames != null) {
52                         for (int j = 0, n = names.length; j < n; j++)
53                             if (intersectedNames.includes(names[j]))
54                                 newIntersectedNames.add(names[j]);
55                     } else {
56                         for (int j = 0, n = names.length; j < n; j++)
57                             newIntersectedNames.add(names[j]);
58                     }
59                 }
60             }
61
62             if (newIntersectedNames.elementSize == 0) return;
63             intersectedNames = newIntersectedNames;
64         } while (this.hasNextQuery());
65     } finally {
66         index.stopQuery();
67     }
68
69     String JavaDoc containerPath = index.containerPath;
70     Object JavaDoc[] names = intersectedNames.values;
71     for (int i = 0, l = names.length; i < l; i++)
72         if (names[i] != null)
73             ((InternalSearchPattern) this).acceptMatch((String JavaDoc) names[i], containerPath, null/*no pattern*/, requestor, participant, scope); // AndPatterns cannot provide the decoded result
74
}
75 /**
76  * Returns whether another query must be done.
77  */

78 protected abstract boolean hasNextQuery();
79 /**
80  * Resets the query and prepares this pattern to be queried.
81  */

82 protected abstract void resetQuery();
83 }
84
Popular Tags