01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.core.search.matching;
11:
12: import java.io.IOException;
13:
14: import org.eclipse.core.runtime.IProgressMonitor;
15: import org.eclipse.core.runtime.OperationCanceledException;
16: import org.eclipse.jdt.core.search.*;
17: import org.eclipse.jdt.internal.compiler.util.SimpleSet;
18: import org.eclipse.jdt.internal.core.index.*;
19: import org.eclipse.jdt.internal.core.search.IndexQueryRequestor;
20:
21: /**
22: * Query the index multiple times and do an 'and' on the results.
23: */
24: public abstract class IntersectingPattern extends JavaSearchPattern {
25:
26: public IntersectingPattern(int patternKind, int matchRule) {
27: super (patternKind, matchRule);
28: }
29:
30: public void findIndexMatches(Index index,
31: IndexQueryRequestor requestor,
32: SearchParticipant participant, IJavaSearchScope scope,
33: IProgressMonitor progressMonitor) throws IOException {
34: if (progressMonitor != null && progressMonitor.isCanceled())
35: throw new OperationCanceledException();
36:
37: this .resetQuery();
38: SimpleSet intersectedNames = null;
39: try {
40: index.startQuery();
41: do {
42: SearchPattern pattern = ((InternalSearchPattern) this )
43: .currentPattern();
44: EntryResult[] entries = ((InternalSearchPattern) pattern)
45: .queryIn(index);
46: if (entries == null)
47: return;
48:
49: SearchPattern decodedResult = pattern.getBlankPattern();
50: SimpleSet newIntersectedNames = new SimpleSet(3);
51: for (int i = 0, l = entries.length; i < l; i++) {
52: if (progressMonitor != null
53: && progressMonitor.isCanceled())
54: throw new OperationCanceledException();
55:
56: EntryResult entry = entries[i];
57: decodedResult.decodeIndexKey(entry.getWord());
58: if (pattern.matchesDecodedKey(decodedResult)) {
59: String[] names = entry.getDocumentNames(index);
60: if (intersectedNames != null) {
61: for (int j = 0, n = names.length; j < n; j++)
62: if (intersectedNames.includes(names[j]))
63: newIntersectedNames.add(names[j]);
64: } else {
65: for (int j = 0, n = names.length; j < n; j++)
66: newIntersectedNames.add(names[j]);
67: }
68: }
69: }
70:
71: if (newIntersectedNames.elementSize == 0)
72: return;
73: intersectedNames = newIntersectedNames;
74: } while (this .hasNextQuery());
75: } finally {
76: index.stopQuery();
77: }
78:
79: String containerPath = index.containerPath;
80: char separator = index.separator;
81: Object[] names = intersectedNames.values;
82: for (int i = 0, l = names.length; i < l; i++)
83: if (names[i] != null)
84: ((InternalSearchPattern) this ).acceptMatch(
85: (String) names[i], containerPath, separator,
86: null/*no pattern*/, requestor, participant,
87: scope); // AndPatterns cannot provide the decoded result
88: }
89:
90: /**
91: * Returns whether another query must be done.
92: */
93: protected abstract boolean hasNextQuery();
94:
95: /**
96: * Resets the query and prepares this pattern to be queried.
97: */
98: protected abstract void resetQuery();
99: }
|