KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > common > routing > impl > RegexpSourceSpec


1 package com.ubermq.jms.common.routing.impl;
2
3 import com.ubermq.jms.common.routing.*;
4 import java.io.*;
5 import java.util.regex.*;
6
7 /**
8  * A source specification that uses regular expressions to
9  * determine if two specifications match.
10  */

11 public class RegexpSourceSpec
12     implements Externalizable, SourceSpec
13 {
14     private transient Pattern regexp;
15     private String JavaDoc expr;
16     private String JavaDoc nice;
17     private static final long serialVersionUID = 5;
18     
19     public RegexpSourceSpec()
20     {
21     }
22     
23     public RegexpSourceSpec(String JavaDoc expr)
24     {
25         this(expr, expr);
26     }
27     
28     public RegexpSourceSpec(String JavaDoc expr, String JavaDoc nice)
29         throws IllegalArgumentException JavaDoc
30     {
31         this.expr = expr;
32         this.nice = nice;
33         this.regexp = Pattern.compile(this.expr, Pattern.CASE_INSENSITIVE);
34     }
35     
36     public void readExternal(ObjectInput in)
37         throws IOException
38     {
39         nice = in.readUTF();
40         expr = in.readUTF();
41         regexp = Pattern.compile(expr, Pattern.CASE_INSENSITIVE);
42     }
43     
44     public void writeExternal(ObjectOutput out)
45         throws IOException
46     {
47         out.writeUTF(nice);
48         out.writeUTF(expr);
49     }
50     
51     /**
52      * @return a human readable name for this destination.
53      */

54     public String JavaDoc getDisplayName()
55     {
56         return nice;
57     }
58     
59     /**
60      * An implementation can return true to indicate that
61      * the results of this routing should be cached.
62      * Mostly this should return true except if the created
63      * source spec is a throwaway and will only ever match once.
64      */

65     public boolean shouldCacheResults()
66     {
67         return true;
68     }
69     
70     public boolean matches(SourceDescriptor s)
71     {
72         return regexp.matcher(s.getMatchValue()).matches();
73     }
74     
75     public boolean isMoreSpecificThan(SourceSpec s)
76     {
77         try {
78             RegexpSourceSpec rss = (RegexpSourceSpec)s;
79             return (specificity() > rss.specificity());
80         }
81         catch(ClassCastException JavaDoc cce) {return false;}
82     }
83     
84     private int specificity()
85     {
86         int score = 0;
87         
88         java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(nice, ".");
89         while(st.hasMoreTokens())
90         {
91             String JavaDoc token = st.nextToken();
92             char ch = token.charAt(0);
93             if (Character.isLetterOrDigit(ch))
94                 score += 3;
95             else if (ch == '*')
96                 score += 2;
97             else if (ch == '#')
98                 score += 1;
99         }
100         
101         return score;
102     }
103     
104     public String JavaDoc toString()
105     {
106         return nice;
107     }
108     
109     public boolean equals(Object JavaDoc obj)
110     {
111         if(obj instanceof RegexpSourceSpec)
112         {
113             return nice.equals( ((RegexpSourceSpec)obj).nice );
114         }
115         else
116             return false;
117     }
118     
119     public int hashCode()
120     {
121         return nice.hashCode();
122     }
123
124     public boolean isIdempotentForEqualDescriptors()
125     {
126         return true;
127     }
128 }
129
130
Popular Tags