KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > criterion > MatchMode


1 //$Id: MatchMode.java,v 1.4 2005/02/12 07:19:13 steveebersole Exp $
2
package org.hibernate.criterion;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 /**
9  * Represents an strategy for matching strings using "like".
10  *
11  * @see Example#enableLike(MatchMode)
12  * @author Gavin King
13  */

14 public abstract class MatchMode implements Serializable JavaDoc {
15     private final String JavaDoc name;
16     private static final Map JavaDoc INSTANCES = new HashMap JavaDoc();
17
18     protected MatchMode(String JavaDoc name) {
19         this.name=name;
20     }
21     public String JavaDoc toString() {
22         return name;
23     }
24
25     /**
26      * Match the entire string to the pattern
27      */

28     public static final MatchMode EXACT = new MatchMode("EXACT") {
29         public String JavaDoc toMatchString(String JavaDoc pattern) {
30             return pattern;
31         }
32     };
33
34     /**
35      * Match the start of the string to the pattern
36      */

37     public static final MatchMode START = new MatchMode("START") {
38         public String JavaDoc toMatchString(String JavaDoc pattern) {
39             return pattern + '%';
40         }
41     };
42
43     /**
44      * Match the end of the string to the pattern
45      */

46     public static final MatchMode END = new MatchMode("END") {
47         public String JavaDoc toMatchString(String JavaDoc pattern) {
48             return '%' + pattern;
49         }
50     };
51
52     /**
53      * Match the pattern anywhere in the string
54      */

55     public static final MatchMode ANYWHERE = new MatchMode("ANYWHERE") {
56         public String JavaDoc toMatchString(String JavaDoc pattern) {
57             return '%' + pattern + '%';
58         }
59     };
60
61     static {
62         INSTANCES.put( EXACT.name, EXACT );
63         INSTANCES.put( END.name, END );
64         INSTANCES.put( START.name, START );
65         INSTANCES.put( ANYWHERE.name, ANYWHERE );
66     }
67
68     private Object JavaDoc readResolve() {
69         return INSTANCES.get(name);
70     }
71
72     /**
73      * convert the pattern, by appending/prepending "%"
74      */

75     public abstract String JavaDoc toMatchString(String JavaDoc pattern);
76
77 }
78
79
80
81
82
83
Popular Tags