KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > activities > ActivityPatternBinding


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
12 package org.eclipse.ui.internal.activities;
13
14 import java.util.regex.Pattern JavaDoc;
15
16 import org.eclipse.ui.activities.IActivityPatternBinding;
17 import org.eclipse.ui.internal.util.Util;
18
19 public final class ActivityPatternBinding implements IActivityPatternBinding {
20     private final static int HASH_FACTOR = 89;
21
22     private final static int HASH_INITIAL = ActivityPatternBinding.class
23             .getName().hashCode();
24
25     private String JavaDoc activityId;
26
27     private transient int hashCode = HASH_INITIAL;
28
29     private Pattern JavaDoc pattern;
30
31     private transient String JavaDoc string;
32
33     public ActivityPatternBinding(String JavaDoc activityId, String JavaDoc pattern) {
34         this(activityId, Pattern.compile(pattern));
35     }
36
37     public ActivityPatternBinding(String JavaDoc activityId, Pattern JavaDoc pattern) {
38         if (pattern == null) {
39             throw new NullPointerException JavaDoc();
40         }
41
42         this.activityId = activityId;
43         this.pattern = pattern;
44     }
45
46     public int compareTo(Object JavaDoc object) {
47         ActivityPatternBinding castedObject = (ActivityPatternBinding) object;
48         int compareTo = Util.compare(activityId, castedObject.activityId);
49
50         if (compareTo == 0) {
51             compareTo = Util.compare(pattern.pattern(), castedObject.pattern
52                     .pattern());
53         }
54
55         return compareTo;
56     }
57
58     public boolean equals(Object JavaDoc object) {
59         if (!(object instanceof ActivityPatternBinding)) {
60             return false;
61         }
62
63         final ActivityPatternBinding castedObject = (ActivityPatternBinding) object;
64         if (!Util.equals(activityId, castedObject.activityId)) {
65             return false;
66         }
67
68         return Util.equals(pattern, castedObject.pattern);
69     }
70
71     public String JavaDoc getActivityId() {
72         return activityId;
73     }
74
75     public Pattern JavaDoc getPattern() {
76         return pattern;
77     }
78
79     public int hashCode() {
80         if (hashCode == HASH_INITIAL) {
81             hashCode = hashCode * HASH_FACTOR + Util.hashCode(activityId);
82             hashCode = hashCode * HASH_FACTOR + Util.hashCode(pattern);
83             if (hashCode == HASH_INITIAL) {
84                 hashCode++;
85             }
86         }
87
88         return hashCode;
89     }
90
91     public String JavaDoc toString() {
92         if (string == null) {
93             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
94             stringBuffer.append('[');
95             stringBuffer.append(activityId);
96             stringBuffer.append(',');
97             stringBuffer.append(pattern);
98             stringBuffer.append(']');
99             string = stringBuffer.toString();
100         }
101
102         return string;
103     }
104
105     /**
106      * Returns whether this binding's pattern matches the given string
107      *
108      * @param toMatch the string to match
109      * @return <code>true</code> if it matches, <code>false</code> if not
110      * @since 3.1
111      */

112     public boolean isMatch(String JavaDoc toMatch) {
113         return pattern.matcher(toMatch).matches();
114     }
115 }
116
Popular Tags