KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > query > BetweenExpr


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.query;
30
31 import com.caucho.util.CharBuffer;
32
33 /**
34  * Bound identifier expression.
35  */

36 class BetweenExpr extends AbstractAmberExpr {
37   private AmberExpr _expr;
38   
39   private AmberExpr _min;
40   private AmberExpr _max;
41
42   private boolean _isNot;
43
44   /**
45    * Creates a new cmp expression
46    */

47   BetweenExpr(AmberExpr expr, AmberExpr min, AmberExpr max, boolean isNot)
48   {
49     _expr = expr;
50     _min = min;
51     _max = max;
52
53     _isNot = isNot;
54   }
55
56   /**
57    * Binds the expression as a select item.
58    */

59   public AmberExpr bindSelect(QueryParser parser)
60   {
61     _expr = _expr.bindSelect(parser);
62     _min = _min.bindSelect(parser);
63     _max = _max.bindSelect(parser);
64
65     return this;
66   }
67
68   /**
69    * Returns true if the expression uses the from item.
70    */

71   public boolean usesFrom(FromItem from, int type, boolean isNot)
72   {
73     return (_expr.usesFrom(from, type) ||
74         _min.usesFrom(from, type) ||
75         _max.usesFrom(from, type));
76   }
77   
78   /**
79    * Generates the where expression.
80    */

81   public void generateWhere(CharBuffer cb)
82   {
83     cb.append('(');
84     _expr.generateWhere(cb);
85
86     if (_isNot)
87       cb.append(" NOT");
88
89     cb.append(" BETWEEN ");
90
91     _min.generateWhere(cb);
92
93     cb.append(" AND ");
94
95     _max.generateWhere(cb);
96     
97     cb.append(')');
98   }
99
100   public String toString()
101   {
102     CharBuffer cb = CharBuffer.allocate();
103     
104     cb.append('(');
105     cb.append(_expr);
106
107     if (_isNot)
108       cb.append(" NOT");
109
110     cb.append(" BETWEEN ");
111
112     cb.append(_min);
113
114     cb.append(" AND ");
115
116     cb.append(_max);
117     
118     cb.append(')');
119
120     return cb.close();
121   }
122 }
123
Popular Tags