KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > expression > regexp > NamePattern


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.expression.regexp;
5
6
7 import com.tc.aspectwerkz.util.Strings;
8 import com.tc.aspectwerkz.expression.ExpressionException;
9
10 import java.io.ObjectInputStream JavaDoc;
11
12 /**
13  * Implements the regular expression pattern matcher for names.
14  *
15  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
16  */

17 public class NamePattern extends Pattern {
18   /**
19    * The name pattern.
20    */

21   protected transient com.tc.jrexx.regex.Pattern m_namePattern;
22
23   /**
24    * The name pattern as a string.
25    */

26   protected String JavaDoc m_pattern;
27
28   /**
29    * Private constructor.
30    *
31    * @param pattern the pattern
32    */

33   NamePattern(final String JavaDoc pattern) {
34     m_pattern = pattern;
35     escape(m_pattern);
36   }
37
38   /**
39    * Matches a name.
40    *
41    * @param name the name
42    * @return true if we have a matche
43    */

44   public boolean matches(final String JavaDoc name) {
45     if (name == null) {
46       throw new IllegalArgumentException JavaDoc("name can not be null");
47     }
48     if (name.equals("")) {
49       return false;
50     }
51     return m_namePattern.contains(name);
52   }
53
54   /**
55    * Returns the pattern as a string.
56    *
57    * @return the pattern
58    */

59   public String JavaDoc getPattern() {
60     return m_pattern;
61   }
62
63   /**
64    * Escapes the name pattern.
65    *
66    * @param namePattern the name pattern
67    */

68   protected void escape(String JavaDoc namePattern) {
69     try {
70       if (namePattern.equals(REGULAR_WILDCARD)) {
71         namePattern = "[a-zA-Z0-9_$.]+";
72       } else {
73         namePattern = Strings.replaceSubString(namePattern, "*", "[a-zA-Z0-9_$]*");
74       }
75       m_namePattern = new com.tc.jrexx.regex.Pattern(namePattern);
76     } catch (Throwable JavaDoc e) {
77       throw new ExpressionException("type pattern is not well formed: " + namePattern, e);
78     }
79   }
80
81   /**
82    * Provides custom deserialization.
83    *
84    * @param stream the object input stream containing the serialized object
85    * @throws Exception in case of failure
86    */

87   private void readObject(final ObjectInputStream JavaDoc stream) throws Exception JavaDoc {
88     ObjectInputStream.GetField JavaDoc fields = stream.readFields();
89     m_pattern = (String JavaDoc) fields.get("m_pattern", null);
90     escape(m_pattern);
91   }
92
93   public int hashCode() {
94     int result = 17;
95     result = (37 * result) + hashCodeOrZeroIfNull(m_pattern);
96     result = (37 * result) + hashCodeOrZeroIfNull(m_namePattern);
97     return result;
98   }
99
100   protected static int hashCodeOrZeroIfNull(final Object JavaDoc o) {
101     if (null == o) {
102       return 19;
103     }
104     return o.hashCode();
105   }
106
107   public boolean equals(final Object JavaDoc o) {
108     if (this == o) {
109       return true;
110     }
111     if (!(o instanceof NamePattern)) {
112       return false;
113     }
114     final NamePattern obj = (NamePattern) o;
115     return areEqualsOrBothNull(obj.m_pattern, this.m_pattern)
116             && areEqualsOrBothNull(obj.m_namePattern, this.m_namePattern);
117   }
118
119   protected static boolean areEqualsOrBothNull(final Object JavaDoc o1, final Object JavaDoc o2) {
120     if (null == o1) {
121       return (null == o2);
122     }
123     return o1.equals(o2);
124   }
125 }
Popular Tags