KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > sqls > impl > BooleanConstraintImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.sqls.impl;
18
19 import org.apache.ws.jaxme.sqls.BooleanConstraint;
20 import org.apache.ws.jaxme.sqls.CombinedConstraint;
21 import org.apache.ws.jaxme.sqls.ConstrainedStatement;
22
23
24 /**
25  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
26  */

27 public class BooleanConstraintImpl extends PartsImpl implements BooleanConstraint {
28     /** Default implementation of {@link BooleanConstraint.Type}.
29      */

30     public static class TypeImpl extends SQLFactoryImpl.IdentImpl implements BooleanConstraint.Type {
31         private static final long serialVersionUID = 3762254145096135991L;
32         /** Creates a new instance with the given name.
33          */

34         public TypeImpl(String JavaDoc pName) {
35             super(pName);
36         }
37     }
38     
39     private BooleanConstraint.Type type;
40     
41     protected BooleanConstraintImpl(CombinedConstraint pCombinedConstraint,
42             BooleanConstraint.Type pType) {
43         super(pCombinedConstraint.getConstrainedStatement());
44         if (pType == null) {
45             throw new NullPointerException JavaDoc("The type must not be null.");
46         }
47         type = pType;
48     }
49     
50     public BooleanConstraint.Type getType() {
51         return type;
52     }
53     
54     protected void add(Object JavaDoc pPart) {
55         Type type = getType();
56         if (BooleanConstraint.Type.IN.equals(type)) {
57             // Arbitrary number of parts
58
} else if (BooleanConstraint.Type.ISNULL.equals(type)) {
59             // Exactly one part
60
if (getNumParts() == 1) {
61                 throw new IllegalStateException JavaDoc("An IS NULL clause cannot have more than one part.");
62             }
63         } else if (BooleanConstraint.Type.BETWEEN.equals(type)) {
64             // Exactly three parts
65
if (getNumParts() == 3) {
66                 throw new IllegalStateException JavaDoc("A BETWEEN clause cannot have more than three parts.");
67             }
68         } else {
69             // Exactly two parts
70
if (getNumParts() == 2) {
71                 throw new IllegalStateException JavaDoc("An " + getType() + " clause cannot have more than two parts.");
72             }
73         }
74         super.add(pPart);
75     }
76     
77     public ConstrainedStatement getConstrainedStatement() {
78         return (ConstrainedStatement) getStatement();
79     }
80
81     public int getMinimumParts() { return 1; }
82
83     public int getMaximumParts() {
84         if (BooleanConstraint.Type.IN.equals(type)) {
85             return 0;
86         } else if (BooleanConstraint.Type.EXISTS.equals(type)
87                    || BooleanConstraint.Type.ISNULL.equals(type)) {
88             return 1;
89         } else if (BooleanConstraint.Type.BETWEEN.equals(type)) {
90             return 3;
91         } else if (BooleanConstraint.Type.EQ.equals(type)
92                    || BooleanConstraint.Type.NE.equals(type)
93                    || BooleanConstraint.Type.GT.equals(type)
94                    || BooleanConstraint.Type.LT.equals(type)
95                    || BooleanConstraint.Type.GE.equals(type)
96                    || BooleanConstraint.Type.LE.equals(type)
97                    || BooleanConstraint.Type.LIKE.equals(type)) {
98             return 2;
99         } else {
100             throw new IllegalStateException JavaDoc("Invalid type: " + type);
101         }
102     }
103 }
104
Popular Tags