KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.hibernate.criterion;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.hibernate.Criteria;
8 import org.hibernate.HibernateException;
9 import org.hibernate.engine.TypedValue;
10 import org.hibernate.util.StringHelper;
11
12 /**
13  * A sequence of a logical expressions combined by some
14  * associative logical operator
15  *
16  * @author Gavin King
17  */

18 public class Junction implements Criterion {
19
20     private final List JavaDoc criteria = new ArrayList JavaDoc();
21     private final String JavaDoc op;
22     
23     protected Junction(String JavaDoc op) {
24         this.op = op;
25     }
26     
27     public Junction add(Criterion criterion) {
28         criteria.add(criterion);
29         return this;
30     }
31
32     public String JavaDoc getOp() {
33         return op;
34     }
35
36     public TypedValue[] getTypedValues(Criteria crit, CriteriaQuery criteriaQuery)
37     throws HibernateException {
38         ArrayList JavaDoc typedValues = new ArrayList JavaDoc();
39         Iterator JavaDoc iter = criteria.iterator();
40         while ( iter.hasNext() ) {
41             TypedValue[] subvalues = ( (Criterion) iter.next() ).getTypedValues(crit, criteriaQuery);
42             for ( int i=0; i<subvalues.length; i++ ) {
43                 typedValues.add( subvalues[i] );
44             }
45         }
46         return (TypedValue[]) typedValues.toArray( new TypedValue[ typedValues.size() ] );
47     }
48
49     public String JavaDoc toSqlString(Criteria crit, CriteriaQuery criteriaQuery)
50     throws HibernateException {
51
52         if ( criteria.size()==0 ) return "1=1";
53
54         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc()
55             .append('(');
56         Iterator JavaDoc iter = criteria.iterator();
57         while ( iter.hasNext() ) {
58             buffer.append( ( (Criterion) iter.next() ).toSqlString(crit, criteriaQuery) );
59             if ( iter.hasNext() ) buffer.append(' ').append(op).append(' ');
60         }
61         return buffer.append(')').toString();
62     }
63
64     /**
65      * @see java.lang.Object#toString()
66      */

67     public String JavaDoc toString() {
68         return '(' + StringHelper.join( ' ' + op + ' ', criteria.iterator() ) + ')';
69     }
70
71 }
72
Popular Tags