KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > expression > regexp > NamePattern


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

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

20 public class NamePattern extends Pattern {
21     /**
22      * The name pattern.
23      */

24     protected transient com.karneim.util.collection.regex.Pattern m_namePattern;
25
26     /**
27      * The name pattern as a string.
28      */

29     protected String JavaDoc m_pattern;
30
31     /**
32      * Private constructor.
33      *
34      * @param pattern the pattern
35      */

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

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

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

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

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