1 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 ; 11 12 17 public class NamePattern extends Pattern { 18 21 protected transient com.tc.jrexx.regex.Pattern m_namePattern; 22 23 26 protected String m_pattern; 27 28 33 NamePattern(final String pattern) { 34 m_pattern = pattern; 35 escape(m_pattern); 36 } 37 38 44 public boolean matches(final String name) { 45 if (name == null) { 46 throw new IllegalArgumentException ("name can not be null"); 47 } 48 if (name.equals("")) { 49 return false; 50 } 51 return m_namePattern.contains(name); 52 } 53 54 59 public String getPattern() { 60 return m_pattern; 61 } 62 63 68 protected void escape(String 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 e) { 77 throw new ExpressionException("type pattern is not well formed: " + namePattern, e); 78 } 79 } 80 81 87 private void readObject(final ObjectInputStream stream) throws Exception { 88 ObjectInputStream.GetField fields = stream.readFields(); 89 m_pattern = (String ) 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 o) { 101 if (null == o) { 102 return 19; 103 } 104 return o.hashCode(); 105 } 106 107 public boolean equals(final Object 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 o1, final Object o2) { 120 if (null == o1) { 121 return (null == o2); 122 } 123 return o1.equals(o2); 124 } 125 } | Popular Tags |