001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.ui.internal.ide.registry;
011:
012: import org.eclipse.core.resources.IMarker;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
015:
016: /**
017: * Instances of this class hold a marker type id and/or
018: * a series of marker attributes. This information may be used
019: * to determine if a given marker is of the same marker
020: * type and determine its values for the attributes.
021: */
022: public class MarkerQuery {
023: /**
024: * The marker type targetted by this query.
025: * May be <code>null</code>.
026: */
027: private String type;
028:
029: /**
030: * A sorted list of the attributes targetted by this query.
031: * The list is sorted from least to greatest according to
032: * <code>Sting.compare</code>
033: */
034: private String[] attributes;
035:
036: /**
037: * Cached hash code value
038: */
039: private int hashCode;
040:
041: /**
042: * Creates a new marker query with the given type
043: * and attributes.
044: * <p>
045: * The type may be <code>null</code>. The attributes may
046: * be empty, but not <code>null</code>.
047: * </p>
048: *
049: * @param markerType the targetted marker type
050: * @param markerAttributes the targetted marker attributes
051: */
052: public MarkerQuery(String markerType, String[] markerAttributes) {
053: if (markerAttributes == null) {
054: throw new IllegalArgumentException();
055: }
056:
057: type = markerType;
058: attributes = markerAttributes;
059: computeHashCode();
060: }
061:
062: /**
063: * Performs a query against the given marker.
064: * <p>
065: * Returns a <code>MarkerQueryResult</code> if the marker
066: * is appropriate for this query (correct type and has
067: * all of the query attributes), otherwise <code>null</code>
068: * is returned.
069: *
070: * @param marker the marker to perform the query against
071: * @return a marker query result or <code>null</code>
072: */
073: public MarkerQueryResult performQuery(IMarker marker) {
074: // Check type
075: try {
076: if (type != null && !type.equals(marker.getType())) {
077: return null;
078: }
079: } catch (CoreException e) {
080: IDEWorkbenchPlugin.log(
081: "Error accessing marker type", e.getStatus()); //$NON-NLS-1$
082: return null;
083: }
084:
085: // Check attributes
086: String[] values = new String[attributes.length];
087: for (int i = 0; i < attributes.length; i++) {
088: try {
089: Object value = marker.getAttribute(attributes[i]);
090: if (value == null) {
091: return null;
092: }
093: values[i] = value.toString();
094: } catch (CoreException e) {
095: IDEWorkbenchPlugin
096: .log(
097: "Error accessing marker attribute", e.getStatus()); //$NON-NLS-1$
098: return null;
099: }
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 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 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[] getAttributes() {
175: return attributes;
176: }
177: }
|