KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > registry > MarkerQuery


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.ui.internal.ide.registry;
12
13 import org.eclipse.core.resources.IMarker;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
16
17 /**
18  * Instances of this class hold a marker type id and/or
19  * a series of marker attributes. This information may be used
20  * to determine if a given marker is of the same marker
21  * type and determine its values for the attributes.
22  */

23 public class MarkerQuery {
24     /**
25      * The marker type targetted by this query.
26      * May be <code>null</code>.
27      */

28     private String JavaDoc type;
29
30     /**
31      * A sorted list of the attributes targetted by this query.
32      * The list is sorted from least to greatest according to
33      * <code>Sting.compare</code>
34      */

35     private String JavaDoc[] attributes;
36
37     /**
38      * Cached hash code value
39      */

40     private int hashCode;
41
42     /**
43      * Creates a new marker query with the given type
44      * and attributes.
45      * <p>
46      * The type may be <code>null</code>. The attributes may
47      * be empty, but not <code>null</code>.
48      * </p>
49      *
50      * @param markerType the targetted marker type
51      * @param markerAttributes the targetted marker attributes
52      */

53     public MarkerQuery(String JavaDoc markerType, String JavaDoc[] markerAttributes) {
54         if (markerAttributes == null) {
55             throw new IllegalArgumentException JavaDoc();
56         }
57
58         type = markerType;
59         attributes = markerAttributes;
60         computeHashCode();
61     }
62
63     /**
64      * Performs a query against the given marker.
65      * <p>
66      * Returns a <code>MarkerQueryResult</code> if the marker
67      * is appropriate for this query (correct type and has
68      * all of the query attributes), otherwise <code>null</code>
69      * is returned.
70      *
71      * @param marker the marker to perform the query against
72      * @return a marker query result or <code>null</code>
73      */

74     public MarkerQueryResult performQuery(IMarker marker) {
75         // Check type
76
try {
77             if (type != null && !type.equals(marker.getType())) {
78                 return null;
79             }
80         } catch (CoreException e) {
81             IDEWorkbenchPlugin
82                     .log("Error accessing marker type", e.getStatus()); //$NON-NLS-1$
83
return null;
84         }
85
86         // Check attributes
87
String JavaDoc[] values = new String JavaDoc[attributes.length];
88         for (int i = 0; i < attributes.length; i++) {
89             try {
90                 Object JavaDoc value = marker.getAttribute(attributes[i]);
91                 if (value == null) {
92                     return null;
93                 }
94                 values[i] = value.toString();
95             } catch (CoreException e) {
96                 IDEWorkbenchPlugin.log(
97                         "Error accessing marker attribute", e.getStatus()); //$NON-NLS-1$
98
return null;
99             }
100         }
101
102         // Create and return the result
103
return new MarkerQueryResult(values);
104     }
105
106     /* (non-Javadoc)
107      * Method declared on Object.
108      */

109     public boolean equals(Object JavaDoc o) {
110         if (!(o instanceof MarkerQuery)) {
111             return false;
112         }
113
114         if (o == this) {
115             return true;
116         }
117
118         MarkerQuery mq = (MarkerQuery) o;
119         if (!(type == null ? mq.type == null : type.equals(mq.type))) {
120             return false;
121         }
122
123         if (attributes.length != mq.attributes.length) {
124             return false;
125         }
126
127         for (int i = 0; i < attributes.length; i++) {
128             if (!(attributes[i].equals(mq.attributes[i]))) {
129                 return false;
130             }
131         }
132
133         return true;
134     }
135
136     /* (non-Javadoc)
137      * Method declared on Object.
138      */

139     public int hashCode() {
140         return hashCode;
141     }
142
143     /**
144      * Computes the hash code for this instance.
145      */

146     public void computeHashCode() {
147         hashCode = 19;
148
149         if (type != null) {
150             hashCode = hashCode * 37 + type.hashCode();
151         }
152
153         for (int i = 0; i < attributes.length; i++) {
154             hashCode = hashCode * 37 + attributes[i].hashCode();
155         }
156     }
157
158     /**
159      * Returns the targetted marker type. May be
160      * <code>null</code>
161      *
162      * @return the targetted marker type
163      */

164     public String JavaDoc getType() {
165         return type;
166     }
167
168     /**
169      * Returns the targetted attributes.
170      * The array may be empty.
171      *
172      * @return the targetted attributes
173      */

174     public String JavaDoc[] getAttributes() {
175         return attributes;
176     }
177 }
178
179
Popular Tags