KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > BinaryDoubleExpr


1 /*
2  * Copyright (c) 1998-2006 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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.sql;
31
32 import com.caucho.log.Log;
33
34 import java.sql.SQLException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 class BinaryDoubleExpr extends Expr {
39   private static final Logger JavaDoc log = Log.open(BinaryDoubleExpr.class);
40
41   private Expr _left;
42   private Expr _right;
43   private int _op;
44
45   BinaryDoubleExpr(Expr left, Expr right, int op)
46   {
47     _left = left;
48     _right = right;
49     _op = op;
50   }
51
52   protected Expr bind(FromItem []fromItems)
53     throws SQLException JavaDoc
54   {
55     throw new UnsupportedOperationException JavaDoc();
56   }
57
58   /**
59    * Returns the type of the expression.
60    */

61   public Class JavaDoc getType()
62   {
63     return double.class;
64   }
65
66   /**
67    * Returns the cost based on the given FromList.
68    */

69   public long subCost(ArrayList JavaDoc<FromItem> fromList)
70   {
71     return _left.subCost(fromList) + _right.subCost(fromList);
72   }
73
74   /**
75    * Evaluates the expression as a double.
76    *
77    * @param rows the current tuple being evaluated
78    *
79    * @return the double value
80    */

81   public double evalDouble(QueryContext context)
82     throws SQLException JavaDoc
83   {
84     switch (_op) {
85     case '+':
86       return _left.evalDouble(context) + _right.evalDouble(context);
87
88     case '-':
89       return _left.evalDouble(context) - _right.evalDouble(context);
90
91     case '*':
92       return _left.evalDouble(context) * _right.evalDouble(context);
93
94     case '/':
95       return _left.evalDouble(context) / _right.evalDouble(context);
96
97     case '%':
98       return _left.evalDouble(context) % _right.evalDouble(context);
99
100     default:
101       throw new IllegalStateException JavaDoc();
102     }
103   }
104
105   /**
106    * Evaluates the expression as a double.
107    *
108    * @param rows the current tuple being evaluated
109    *
110    * @return the double value
111    */

112   public long evalLong(QueryContext context)
113     throws SQLException JavaDoc
114   {
115     return (long) evalDouble(context);
116   }
117
118   /**
119    * Evaluates the expression as a string.
120    *
121    * @param rows the current tuple being evaluated
122    *
123    * @return the string value
124    */

125   public String JavaDoc evalString(QueryContext context)
126     throws SQLException JavaDoc
127   {
128     return String.valueOf(evalDouble(context));
129   }
130
131   /**
132    * Evaluates aggregate functions during the group phase.
133    *
134    * @param state the current database tuple
135    */

136   public void evalGroup(QueryContext context)
137     throws SQLException JavaDoc
138   {
139     _left.evalGroup(context);
140     _right.evalGroup(context);
141   }
142
143   public boolean equals(Object JavaDoc o)
144   {
145     if (o == null || ! BinaryDoubleExpr.class.equals(o.getClass()))
146       return false;
147
148     BinaryDoubleExpr expr = (BinaryDoubleExpr) o;
149
150     return (_op == expr._op &&
151         _left.equals(expr._left) &&
152         _right.equals(expr._right));
153   }
154
155   public String JavaDoc toString()
156   {
157     switch (_op) {
158     case '+':
159       return "(" + _left + " + " + _right + ")";
160
161     case '-':
162       return "(" + _left + " - " + _right + ")";
163
164     case '*':
165       return "(" + _left + " * " + _right + ")";
166
167     case '/':
168       return "(" + _left + " / " + _right + ")";
169
170     case '%':
171       return "(" + _left + " % " + _right + ")";
172
173     default:
174       throw new IllegalStateException JavaDoc();
175     }
176   }
177 }
178
Popular Tags