001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.expression.regexp;
005:
006: import com.tc.aspectwerkz.util.Strings;
007: import com.tc.aspectwerkz.expression.ExpressionException;
008:
009: import java.io.ObjectInputStream;
010:
011: /**
012: * Implements the regular expression pattern matcher for names.
013: *
014: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
015: */
016: public class NamePattern extends Pattern {
017: /**
018: * The name pattern.
019: */
020: protected transient com.tc.jrexx.regex.Pattern m_namePattern;
021:
022: /**
023: * The name pattern as a string.
024: */
025: protected String m_pattern;
026:
027: /**
028: * Private constructor.
029: *
030: * @param pattern the pattern
031: */
032: NamePattern(final String pattern) {
033: m_pattern = pattern;
034: escape(m_pattern);
035: }
036:
037: /**
038: * Matches a name.
039: *
040: * @param name the name
041: * @return true if we have a matche
042: */
043: public boolean matches(final String name) {
044: if (name == null) {
045: throw new IllegalArgumentException("name can not be null");
046: }
047: if (name.equals("")) {
048: return false;
049: }
050: return m_namePattern.contains(name);
051: }
052:
053: /**
054: * Returns the pattern as a string.
055: *
056: * @return the pattern
057: */
058: public String getPattern() {
059: return m_pattern;
060: }
061:
062: /**
063: * Escapes the name pattern.
064: *
065: * @param namePattern the name pattern
066: */
067: protected void escape(String namePattern) {
068: try {
069: if (namePattern.equals(REGULAR_WILDCARD)) {
070: namePattern = "[a-zA-Z0-9_$.]+";
071: } else {
072: namePattern = Strings.replaceSubString(namePattern,
073: "*", "[a-zA-Z0-9_$]*");
074: }
075: m_namePattern = new com.tc.jrexx.regex.Pattern(namePattern);
076: } catch (Throwable e) {
077: throw new ExpressionException(
078: "type pattern is not well formed: " + namePattern,
079: e);
080: }
081: }
082:
083: /**
084: * Provides custom deserialization.
085: *
086: * @param stream the object input stream containing the serialized object
087: * @throws Exception in case of failure
088: */
089: private void readObject(final ObjectInputStream stream)
090: throws Exception {
091: ObjectInputStream.GetField fields = stream.readFields();
092: m_pattern = (String) fields.get("m_pattern", null);
093: escape(m_pattern);
094: }
095:
096: public int hashCode() {
097: int result = 17;
098: result = (37 * result) + hashCodeOrZeroIfNull(m_pattern);
099: result = (37 * result) + hashCodeOrZeroIfNull(m_namePattern);
100: return result;
101: }
102:
103: protected static int hashCodeOrZeroIfNull(final Object o) {
104: if (null == o) {
105: return 19;
106: }
107: return o.hashCode();
108: }
109:
110: public boolean equals(final Object 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,
120: this .m_namePattern);
121: }
122:
123: protected static boolean areEqualsOrBothNull(final Object o1,
124: final Object o2) {
125: if (null == o1) {
126: return (null == o2);
127: }
128: return o1.equals(o2);
129: }
130: }
|