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.activities;
011:
012: import java.util.regex.Pattern;
013:
014: import org.eclipse.ui.activities.IActivityPatternBinding;
015: import org.eclipse.ui.internal.util.Util;
016:
017: public final class ActivityPatternBinding implements
018: IActivityPatternBinding {
019: private final static int HASH_FACTOR = 89;
020:
021: private final static int HASH_INITIAL = ActivityPatternBinding.class
022: .getName().hashCode();
023:
024: private String activityId;
025:
026: private transient int hashCode = HASH_INITIAL;
027:
028: private Pattern pattern;
029:
030: private transient String string;
031:
032: public ActivityPatternBinding(String activityId, String pattern) {
033: this (activityId, Pattern.compile(pattern));
034: }
035:
036: public ActivityPatternBinding(String activityId, Pattern pattern) {
037: if (pattern == null) {
038: throw new NullPointerException();
039: }
040:
041: this .activityId = activityId;
042: this .pattern = pattern;
043: }
044:
045: public int compareTo(Object object) {
046: ActivityPatternBinding castedObject = (ActivityPatternBinding) object;
047: int compareTo = Util.compare(activityId,
048: castedObject.activityId);
049:
050: if (compareTo == 0) {
051: compareTo = Util.compare(pattern.pattern(),
052: castedObject.pattern.pattern());
053: }
054:
055: return compareTo;
056: }
057:
058: public boolean equals(Object object) {
059: if (!(object instanceof ActivityPatternBinding)) {
060: return false;
061: }
062:
063: final ActivityPatternBinding castedObject = (ActivityPatternBinding) object;
064: if (!Util.equals(activityId, castedObject.activityId)) {
065: return false;
066: }
067:
068: return Util.equals(pattern, castedObject.pattern);
069: }
070:
071: public String getActivityId() {
072: return activityId;
073: }
074:
075: public Pattern getPattern() {
076: return pattern;
077: }
078:
079: public int hashCode() {
080: if (hashCode == HASH_INITIAL) {
081: hashCode = hashCode * HASH_FACTOR
082: + Util.hashCode(activityId);
083: hashCode = hashCode * HASH_FACTOR + Util.hashCode(pattern);
084: if (hashCode == HASH_INITIAL) {
085: hashCode++;
086: }
087: }
088:
089: return hashCode;
090: }
091:
092: public String toString() {
093: if (string == null) {
094: final StringBuffer stringBuffer = new StringBuffer();
095: stringBuffer.append('[');
096: stringBuffer.append(activityId);
097: stringBuffer.append(',');
098: stringBuffer.append(pattern);
099: stringBuffer.append(']');
100: string = stringBuffer.toString();
101: }
102:
103: return string;
104: }
105:
106: /**
107: * Returns whether this binding's pattern matches the given string
108: *
109: * @param toMatch the string to match
110: * @return <code>true</code> if it matches, <code>false</code> if not
111: * @since 3.1
112: */
113: public boolean isMatch(String toMatch) {
114: return pattern.matcher(toMatch).matches();
115: }
116: }
|