1 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 ; 14 15 20 public class NamePattern extends Pattern { 21 24 protected transient com.karneim.util.collection.regex.Pattern m_namePattern; 25 26 29 protected String m_pattern; 30 31 36 NamePattern(final String pattern) { 37 m_pattern = pattern; 38 escape(m_pattern); 39 } 40 41 47 public boolean matches(final String name) { 48 if (name == null) { 49 throw new IllegalArgumentException ("name can not be null"); 50 } 51 if (name.equals("")) { 52 return false; 53 } 54 return m_namePattern.contains(name); 55 } 56 57 62 public String getPattern() { 63 return m_pattern; 64 } 65 66 71 protected void escape(String 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 e) { 80 throw new ExpressionException("type pattern is not well formed: " + namePattern, e); 81 } 82 } 83 84 90 private void readObject(final ObjectInputStream stream) throws Exception { 91 ObjectInputStream.GetField fields = stream.readFields(); 92 m_pattern = (String ) 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 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, this.m_namePattern); 120 } 121 122 protected static boolean areEqualsOrBothNull(final Object o1, final Object o2) { 123 if (null == o1) { 124 return (null == o2); 125 } 126 return o1.equals(o2); 127 } 128 } | Popular Tags |