KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: Restrictions.java,v 1.8 2005/05/25 01:22:05 oneovthafew Exp $
2
package org.hibernate.criterion;
3
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.hibernate.type.Type;
9 import org.hibernate.util.ArrayHelper;
10
11 /**
12  * The <tt>criterion</tt> package may be used by applications as a framework for building
13  * new kinds of <tt>Criterion</tt>. However, it is intended that most applications will
14  * simply use the built-in criterion types via the static factory methods of this class.
15  *
16  * @see org.hibernate.Criteria
17  * @see Projections factory methods for <tt>Projection</tt> instances
18  * @author Gavin King
19  */

20 public class Restrictions {
21
22     Restrictions() {
23         //cannot be instantiated
24
}
25
26     /**
27      * Apply an "equal" constraint to the identifier property
28      * @param propertyName
29      * @param value
30      * @return Criterion
31      */

32     public static Criterion idEq(Object JavaDoc value) {
33         return new IdentifierEqExpression(value);
34     }
35     /**
36      * Apply an "equal" constraint to the named property
37      * @param propertyName
38      * @param value
39      * @return Criterion
40      */

41     public static SimpleExpression eq(String JavaDoc propertyName, Object JavaDoc value) {
42         return new SimpleExpression(propertyName, value, "=");
43     }
44     /**
45      * Apply a "not equal" constraint to the named property
46      * @param propertyName
47      * @param value
48      * @return Criterion
49      */

50     public static SimpleExpression ne(String JavaDoc propertyName, Object JavaDoc value) {
51         return new SimpleExpression(propertyName, value, "<>");
52     }
53     /**
54      * Apply a "like" constraint to the named property
55      * @param propertyName
56      * @param value
57      * @return Criterion
58      */

59     public static SimpleExpression like(String JavaDoc propertyName, Object JavaDoc value) {
60         return new SimpleExpression(propertyName, value, " like ");
61     }
62     /**
63      * Apply a "like" constraint to the named property
64      * @param propertyName
65      * @param value
66      * @return Criterion
67      */

68     public static SimpleExpression like(String JavaDoc propertyName, String JavaDoc value, MatchMode matchMode) {
69         return new SimpleExpression(propertyName, matchMode.toMatchString(value), " like " );
70     }
71     /**
72      * A case-insensitive "like", similar to Postgres <tt>ilike</tt>
73      * operator
74      *
75      * @param propertyName
76      * @param value
77      * @return Criterion
78      */

79     public static Criterion ilike(String JavaDoc propertyName, String JavaDoc value, MatchMode matchMode) {
80         return new IlikeExpression(propertyName, value, matchMode);
81     }
82     /**
83      * A case-insensitive "like", similar to Postgres <tt>ilike</tt>
84      * operator
85      *
86      * @param propertyName
87      * @param value
88      * @return Criterion
89      */

90     public static Criterion ilike(String JavaDoc propertyName, Object JavaDoc value) {
91         return new IlikeExpression(propertyName, value);
92     }
93     /**
94      * Apply a "greater than" constraint to the named property
95      * @param propertyName
96      * @param value
97      * @return Criterion
98      */

99     public static SimpleExpression gt(String JavaDoc propertyName, Object JavaDoc value) {
100         return new SimpleExpression(propertyName, value, ">");
101     }
102     /**
103      * Apply a "less than" constraint to the named property
104      * @param propertyName
105      * @param value
106      * @return Criterion
107      */

108     public static SimpleExpression lt(String JavaDoc propertyName, Object JavaDoc value) {
109         return new SimpleExpression(propertyName, value, "<");
110     }
111     /**
112      * Apply a "less than or equal" constraint to the named property
113      * @param propertyName
114      * @param value
115      * @return Criterion
116      */

117     public static SimpleExpression le(String JavaDoc propertyName, Object JavaDoc value) {
118         return new SimpleExpression(propertyName, value, "<=");
119     }
120     /**
121      * Apply a "greater than or equal" constraint to the named property
122      * @param propertyName
123      * @param value
124      * @return Criterion
125      */

126     public static SimpleExpression ge(String JavaDoc propertyName, Object JavaDoc value) {
127         return new SimpleExpression(propertyName, value, ">=");
128     }
129     /**
130      * Apply a "between" constraint to the named property
131      * @param propertyName
132      * @param lo value
133      * @param hi value
134      * @return Criterion
135      */

136     public static Criterion between(String JavaDoc propertyName, Object JavaDoc lo, Object JavaDoc hi) {
137         return new BetweenExpression(propertyName, lo, hi);
138     }
139     /**
140      * Apply an "in" constraint to the named property
141      * @param propertyName
142      * @param values
143      * @return Criterion
144      */

145     public static Criterion in(String JavaDoc propertyName, Object JavaDoc[] values) {
146         return new InExpression(propertyName, values);
147     }
148     /**
149      * Apply an "in" constraint to the named property
150      * @param propertyName
151      * @param values
152      * @return Criterion
153      */

154     public static Criterion in(String JavaDoc propertyName, Collection JavaDoc values) {
155         return new InExpression( propertyName, values.toArray() );
156     }
157     /**
158      * Apply an "is null" constraint to the named property
159      * @return Criterion
160      */

161     public static Criterion isNull(String JavaDoc propertyName) {
162         return new NullExpression(propertyName);
163     }
164     /**
165      * Apply an "equal" constraint to two properties
166      */

167     public static PropertyExpression eqProperty(String JavaDoc propertyName, String JavaDoc otherPropertyName) {
168         return new PropertyExpression(propertyName, otherPropertyName, "=");
169     }
170     /**
171      * Apply a "not equal" constraint to two properties
172      */

173     public static PropertyExpression neProperty(String JavaDoc propertyName, String JavaDoc otherPropertyName) {
174         return new PropertyExpression(propertyName, otherPropertyName, "<>");
175     }
176     /**
177      * Apply a "less than" constraint to two properties
178      */

179     public static PropertyExpression ltProperty(String JavaDoc propertyName, String JavaDoc otherPropertyName) {
180         return new PropertyExpression(propertyName, otherPropertyName, "<");
181     }
182     /**
183      * Apply a "less than or equal" constraint to two properties
184      */

185     public static PropertyExpression leProperty(String JavaDoc propertyName, String JavaDoc otherPropertyName) {
186         return new PropertyExpression(propertyName, otherPropertyName, "<=");
187     }
188     /**
189      * Apply a "greater than" constraint to two properties
190      */

191     public static PropertyExpression gtProperty(String JavaDoc propertyName, String JavaDoc otherPropertyName) {
192         return new PropertyExpression(propertyName, otherPropertyName, ">");
193     }
194     /**
195      * Apply a "greater than or equal" constraint to two properties
196      */

197     public static PropertyExpression geProperty(String JavaDoc propertyName, String JavaDoc otherPropertyName) {
198         return new PropertyExpression(propertyName, otherPropertyName, ">=");
199     }
200     /**
201      * Apply an "is not null" constraint to the named property
202      * @return Criterion
203      */

204     public static Criterion isNotNull(String JavaDoc propertyName) {
205         return new NotNullExpression(propertyName);
206     }
207     /**
208      * Return the conjuction of two expressions
209      *
210      * @param lhs
211      * @param rhs
212      * @return Criterion
213      */

214     public static LogicalExpression and(Criterion lhs, Criterion rhs) {
215         return new LogicalExpression(lhs, rhs, "and");
216     }
217     /**
218      * Return the disjuction of two expressions
219      *
220      * @param lhs
221      * @param rhs
222      * @return Criterion
223      */

224     public static LogicalExpression or(Criterion lhs, Criterion rhs) {
225         return new LogicalExpression(lhs, rhs, "or");
226     }
227     /**
228      * Return the negation of an expression
229      *
230      * @param expression
231      * @return Criterion
232      */

233     public static Criterion not(Criterion expression) {
234         return new NotExpression(expression);
235     }
236     /**
237      * Apply a constraint expressed in SQL, with the given JDBC
238      * parameters. Any occurrences of <tt>{alias}</tt> will be
239      * replaced by the table alias.
240      *
241      * @param sql
242      * @param values
243      * @param types
244      * @return Criterion
245      */

246     public static Criterion sqlRestriction(String JavaDoc sql, Object JavaDoc[] values, Type[] types) {
247         return new SQLCriterion(sql, values, types);
248     }
249     /**
250      * Apply a constraint expressed in SQL, with the given JDBC
251      * parameter. Any occurrences of <tt>{alias}</tt> will be replaced
252      * by the table alias.
253      *
254      * @param sql
255      * @param value
256      * @param type
257      * @return Criterion
258      */

259     public static Criterion sqlRestriction(String JavaDoc sql, Object JavaDoc value, Type type) {
260         return new SQLCriterion(sql, new Object JavaDoc[] { value }, new Type[] { type } );
261     }
262     /**
263      * Apply a constraint expressed in SQL. Any occurrences of <tt>{alias}</tt>
264      * will be replaced by the table alias.
265      *
266      * @param sql
267      * @return Criterion
268      */

269     public static Criterion sqlRestriction(String JavaDoc sql) {
270         return new SQLCriterion(sql, ArrayHelper.EMPTY_OBJECT_ARRAY, ArrayHelper.EMPTY_TYPE_ARRAY);
271     }
272
273     /**
274      * Group expressions together in a single conjunction (A and B and C...)
275      *
276      * @return Conjunction
277      */

278     public static Conjunction conjunction() {
279         return new Conjunction();
280     }
281
282     /**
283      * Group expressions together in a single disjunction (A or B or C...)
284      *
285      * @return Conjunction
286      */

287     public static Disjunction disjunction() {
288         return new Disjunction();
289     }
290
291     /**
292      * Apply an "equals" constraint to each property in the
293      * key set of a <tt>Map</tt>
294      *
295      * @param propertyNameValues a map from property names to values
296      * @return Criterion
297      */

298     public static Criterion allEq(Map JavaDoc propertyNameValues) {
299         Conjunction conj = conjunction();
300         Iterator JavaDoc iter = propertyNameValues.entrySet().iterator();
301         while ( iter.hasNext() ) {
302             Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
303             conj.add( eq( (String JavaDoc) me.getKey(), me.getValue() ) );
304         }
305         return conj;
306     }
307     
308     /**
309      * Constrain a collection valued property to be empty
310      */

311     public static Criterion isEmpty(String JavaDoc propertyName) {
312         return new EmptyExpression(propertyName);
313     }
314
315     /**
316      * Constrain a collection valued property to be non-empty
317      */

318     public static Criterion isNotEmpty(String JavaDoc propertyName) {
319         return new NotEmptyExpression(propertyName);
320     }
321     
322     /**
323      * Constrain a collection valued property by size
324      */

325     public static Criterion sizeEq(String JavaDoc propertyName, int size) {
326         return new SizeExpression(propertyName, size);
327     }
328     
329     public static NaturalIdentifier naturalId() {
330         return new NaturalIdentifier();
331     }
332         
333 }
334
Popular Tags