KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.core.search.*;
17 import org.eclipse.jdt.internal.core.index.Index;
18 import org.eclipse.jdt.internal.core.search.IndexQueryRequestor;
19 import org.eclipse.jdt.internal.core.search.indexing.IIndexConstants;
20
21 public class OrPattern extends SearchPattern implements IIndexConstants {
22
23     protected SearchPattern[] patterns;
24
25     /*
26      * Whether this pattern is erasure match.
27      */

28 // boolean isErasureMatch;
29

30     /**
31      * One of {@link #R_ERASURE_MATCH}, {@link #R_EQUIVALENT_MATCH}, {@link #R_FULL_MATCH}.
32      */

33     int matchCompatibility;
34
35     public OrPattern(SearchPattern leftPattern, SearchPattern rightPattern) {
36         super(Math.max(leftPattern.getMatchRule(), rightPattern.getMatchRule()));
37         ((InternalSearchPattern)this).kind = OR_PATTERN;
38         ((InternalSearchPattern)this).mustResolve = ((InternalSearchPattern) leftPattern).mustResolve || ((InternalSearchPattern) rightPattern).mustResolve;
39     
40         SearchPattern[] leftPatterns = leftPattern instanceof OrPattern ? ((OrPattern) leftPattern).patterns : null;
41         SearchPattern[] rightPatterns = rightPattern instanceof OrPattern ? ((OrPattern) rightPattern).patterns : null;
42         int leftSize = leftPatterns == null ? 1 : leftPatterns.length;
43         int rightSize = rightPatterns == null ? 1 : rightPatterns.length;
44         this.patterns = new SearchPattern[leftSize + rightSize];
45     
46         if (leftPatterns == null)
47             this.patterns[0] = leftPattern;
48         else
49             System.arraycopy(leftPatterns, 0, this.patterns, 0, leftSize);
50         if (rightPatterns == null)
51             this.patterns[leftSize] = rightPattern;
52         else
53             System.arraycopy(rightPatterns, 0, this.patterns, leftSize, rightSize);
54
55         // Store erasure match
56
matchCompatibility = 0;
57         for (int i = 0, length = this.patterns.length; i < length; i++) {
58             matchCompatibility |= ((JavaSearchPattern) this.patterns[i]).matchCompatibility;
59         }
60     }
61     void findIndexMatches(Index index, IndexQueryRequestor requestor, SearchParticipant participant, IJavaSearchScope scope, IProgressMonitor progressMonitor) throws IOException JavaDoc {
62         // per construction, OR pattern can only be used with a PathCollector (which already gather results using a set)
63
try {
64             index.startQuery();
65             for (int i = 0, length = this.patterns.length; i < length; i++)
66                 ((InternalSearchPattern)this.patterns[i]).findIndexMatches(index, requestor, participant, scope, progressMonitor);
67         } finally {
68             index.stopQuery();
69         }
70     }
71
72     public SearchPattern getBlankPattern() {
73         return null;
74     }
75
76     boolean isErasureMatch() {
77         return (this.matchCompatibility & R_ERASURE_MATCH) != 0;
78     }
79
80     boolean isPolymorphicSearch() {
81         for (int i = 0, length = this.patterns.length; i < length; i++)
82             if (((InternalSearchPattern) this.patterns[i]).isPolymorphicSearch()) return true;
83         return false;
84     }
85
86     /**
87      * Returns whether the pattern has signatures or not.
88      * @return true if one at least of the stored pattern has signatures.
89      */

90     public final boolean hasSignatures() {
91         boolean isErasureMatch = isErasureMatch();
92         for (int i = 0, length = this.patterns.length; i < length && !isErasureMatch; i++) {
93             if (((JavaSearchPattern) this.patterns[i]).hasSignatures()) return true;
94         }
95         return false;
96     }
97
98     public String JavaDoc toString() {
99         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
100         buffer.append(this.patterns[0].toString());
101         for (int i = 1, length = this.patterns.length; i < length; i++) {
102             buffer.append("\n| "); //$NON-NLS-1$
103
buffer.append(this.patterns[i].toString());
104         }
105         return buffer.toString();
106     }
107 }
108
Popular Tags