001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.search.matching;
011:
012: import java.io.IOException;
013:
014: import org.eclipse.core.runtime.IProgressMonitor;
015: import org.eclipse.jdt.core.search.*;
016: import org.eclipse.jdt.internal.core.index.Index;
017: import org.eclipse.jdt.internal.core.search.IndexQueryRequestor;
018: import org.eclipse.jdt.internal.core.search.indexing.IIndexConstants;
019:
020: public class OrPattern extends SearchPattern implements IIndexConstants {
021:
022: protected SearchPattern[] patterns;
023:
024: /*
025: * Whether this pattern is erasure match.
026: */
027: // boolean isErasureMatch;
028: /**
029: * One of {@link #R_ERASURE_MATCH}, {@link #R_EQUIVALENT_MATCH}, {@link #R_FULL_MATCH}.
030: */
031: int matchCompatibility;
032:
033: public OrPattern(SearchPattern leftPattern,
034: SearchPattern rightPattern) {
035: super (Math.max(leftPattern.getMatchRule(), rightPattern
036: .getMatchRule()));
037: ((InternalSearchPattern) this ).kind = OR_PATTERN;
038: ((InternalSearchPattern) this ).mustResolve = ((InternalSearchPattern) leftPattern).mustResolve
039: || ((InternalSearchPattern) rightPattern).mustResolve;
040:
041: SearchPattern[] leftPatterns = leftPattern instanceof OrPattern ? ((OrPattern) leftPattern).patterns
042: : null;
043: SearchPattern[] rightPatterns = rightPattern instanceof OrPattern ? ((OrPattern) rightPattern).patterns
044: : null;
045: int leftSize = leftPatterns == null ? 1 : leftPatterns.length;
046: int rightSize = rightPatterns == null ? 1
047: : rightPatterns.length;
048: this .patterns = new SearchPattern[leftSize + rightSize];
049:
050: if (leftPatterns == null)
051: this .patterns[0] = leftPattern;
052: else
053: System.arraycopy(leftPatterns, 0, this .patterns, 0,
054: leftSize);
055: if (rightPatterns == null)
056: this .patterns[leftSize] = rightPattern;
057: else
058: System.arraycopy(rightPatterns, 0, this .patterns, leftSize,
059: rightSize);
060:
061: // Store erasure match
062: matchCompatibility = 0;
063: for (int i = 0, length = this .patterns.length; i < length; i++) {
064: matchCompatibility |= ((JavaSearchPattern) this .patterns[i]).matchCompatibility;
065: }
066: }
067:
068: void findIndexMatches(Index index, IndexQueryRequestor requestor,
069: SearchParticipant participant, IJavaSearchScope scope,
070: IProgressMonitor progressMonitor) throws IOException {
071: // per construction, OR pattern can only be used with a PathCollector (which already gather results using a set)
072: try {
073: index.startQuery();
074: for (int i = 0, length = this .patterns.length; i < length; i++)
075: ((InternalSearchPattern) this .patterns[i])
076: .findIndexMatches(index, requestor,
077: participant, scope, progressMonitor);
078: } finally {
079: index.stopQuery();
080: }
081: }
082:
083: public SearchPattern getBlankPattern() {
084: return null;
085: }
086:
087: boolean isErasureMatch() {
088: return (this .matchCompatibility & R_ERASURE_MATCH) != 0;
089: }
090:
091: boolean isPolymorphicSearch() {
092: for (int i = 0, length = this .patterns.length; i < length; i++)
093: if (((InternalSearchPattern) this .patterns[i])
094: .isPolymorphicSearch())
095: return true;
096: return false;
097: }
098:
099: /**
100: * Returns whether the pattern has signatures or not.
101: * @return true if one at least of the stored pattern has signatures.
102: */
103: public final boolean hasSignatures() {
104: boolean isErasureMatch = isErasureMatch();
105: for (int i = 0, length = this .patterns.length; i < length
106: && !isErasureMatch; i++) {
107: if (((JavaSearchPattern) this .patterns[i]).hasSignatures())
108: return true;
109: }
110: return false;
111: }
112:
113: public String toString() {
114: StringBuffer buffer = new StringBuffer();
115: buffer.append(this .patterns[0].toString());
116: for (int i = 1, length = this .patterns.length; i < length; i++) {
117: buffer.append("\n| "); //$NON-NLS-1$
118: buffer.append(this.patterns[i].toString());
119: }
120: return buffer.toString();
121: }
122: }
|