forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatchMode.cs
182 lines (162 loc) · 5.23 KB
/
MatchMode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections;
namespace NHibernate.Criterion
{
/// <summary>
/// Represents an strategy for matching strings using "like".
/// </summary>
public abstract class MatchMode
{
private int _intCode;
private string _name;
private static Hashtable Instances = new Hashtable();
static MatchMode()
{
Instances.Add(Exact._intCode, Exact);
Instances.Add(Start._intCode, Start);
Instances.Add(End._intCode, End);
Instances.Add(Anywhere._intCode, Anywhere);
}
/// <summary>
/// Initialize a new instance of the <see cref="MatchMode" /> class.
/// </summary>
/// <param name="intCode">The code that identifies the match mode.</param>
/// <param name="name">The friendly name of the match mode.</param>
/// <remarks>
/// The parameter <c>intCode</c> is used as the key of <see cref="IDictionary"/>
/// to store instances and to ensure only instance of a particular <see cref="MatchMode"/>
/// is created.
/// </remarks>
protected MatchMode(int intCode, string name)
{
_intCode = intCode;
_name = name;
}
#region System.Object Members
/// <summary>
/// The string representation of the <see cref="MatchMode"/>.
/// </summary>
/// <returns>The friendly name used to describe the <see cref="MatchMode"/>.</returns>
public override string ToString()
{
return _name;
}
#endregion
/// <summary>
/// Convert the pattern, by appending/prepending "%"
/// </summary>
/// <param name="pattern">The string to convert to the appropriate match pattern.</param>
/// <returns>
/// A <see cref="String"/> that contains a "%" in the appropriate place
/// for the Match Strategy.
/// </returns>
public abstract string ToMatchString(string pattern);
// TODO: need to fix up so serialization/deserialization
// preserves the singleton
// private Object ReadResolve()
// {
// return INSTANCES[intCode];
// }
/// <summary>
/// Match the entire string to the pattern
/// </summary>
public static readonly MatchMode Exact = new ExactMatchMode();
/// <summary>
/// Match the start of the string to the pattern
/// </summary>
public static readonly MatchMode Start = new StartMatchMode();
/// <summary>
/// Match the end of the string to the pattern
/// </summary>
public static readonly MatchMode End = new EndMatchMode();
/// <summary>
/// Match the pattern anywhere in the string
/// </summary>
public static readonly MatchMode Anywhere = new AnywhereMatchMode();
/// <summary>
/// The <see cref="MatchMode"/> that matches the entire string to the pattern.
/// </summary>
private class ExactMatchMode : MatchMode
{
/// <summary>
/// Initialize a new instance of the <see cref="ExactMatchMode" /> class.
/// </summary>
public ExactMatchMode() : base(0, "EXACT")
{
}
/// <summary>
/// Converts the string to the Exact MatchMode.
/// </summary>
/// <param name="pattern">The string to convert to the appropriate match pattern.</param>
/// <returns>The <c>pattern</c> exactly the same as it was passed in.</returns>
public override string ToMatchString(string pattern)
{
return pattern;
}
}
/// <summary>
/// The <see cref="MatchMode"/> that matches the start of the string to the pattern.
/// </summary>
private class StartMatchMode : MatchMode
{
/// <summary>
/// Initialize a new instance of the <see cref="StartMatchMode" /> class.
/// </summary>
public StartMatchMode() : base(1, "START")
{
}
/// <summary>
/// Converts the string to the Start MatchMode.
/// </summary>
/// <param name="pattern">The string to convert to the appropriate match pattern.</param>
/// <returns>The <c>pattern</c> with a "<c>%</c>" appended at the end.</returns>
public override string ToMatchString(string pattern)
{
return pattern + '%';
}
}
/// <summary>
/// The <see cref="MatchMode"/> that matches the end of the string to the pattern.
/// </summary>
private class EndMatchMode : MatchMode
{
/// <summary>
/// Initialize a new instance of the <see cref="EndMatchMode" /> class.
/// </summary>
public EndMatchMode() : base(2, "END")
{
}
/// <summary>
/// Converts the string to the End MatchMode.
/// </summary>
/// <param name="pattern">The string to convert to the appropriate match pattern.</param>
/// <returns>The <c>pattern</c> with a "<c>%</c>" appended at the beginning.</returns>
public override string ToMatchString(string pattern)
{
return '%' + pattern;
}
}
/// <summary>
/// The <see cref="MatchMode"/> that exactly matches the string
/// by appending "<c>%</c>" to the beginning and end.
/// </summary>
private class AnywhereMatchMode : MatchMode
{
/// <summary>
/// Initialize a new instance of the <see cref="AnywhereMatchMode" /> class.
/// </summary>
public AnywhereMatchMode() : base(3, "ANYWHERE")
{
}
/// <summary>
/// Converts the string to the Exact MatchMode.
/// </summary>
/// <param name="pattern">The string to convert to the appropriate match pattern.</param>
/// <returns>The <c>pattern</c> with a "<c>%</c>" appended at the beginning and the end.</returns>
public override string ToMatchString(string pattern)
{
return '%' + pattern + '%';
}
}
}
}