001: /*
002: * Copyright 2004-2007 Gary Bentley
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may
005: * not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.josql.expressions;
016:
017: import java.util.List;
018: import java.util.ArrayList;
019:
020: import org.josql.Query;
021: import org.josql.QueryExecutionException;
022: import org.josql.QueryParseException;
023:
024: /**
025: * This class represents a list of expressions used within a SQL statement.
026: * <p>
027: * Expressions lists are created using pairs of "[]" brackets, i.e.
028: * <pre>
029: * [toString, 10, true]
030: * </pre>
031: * Would create a list of expressions that should be treated as a unit.
032: * <p>
033: * Since expression lists are also expressions it is possible to nest them to the nth degree, if desired.
034: * i.e.
035: * <pre>
036: * [toString, [1, 2, 3, 4], [true, true]]
037: * </pre>
038: */
039: public class ExpressionList extends ValueExpression {
040:
041: private List expressions = null;
042:
043: /**
044: * Get the expected return type, which is {@link List}.
045: *
046: * @param q The Query object.
047: * @return {@link List}.
048: */
049: public Class getExpectedReturnType(Query q) {
050:
051: return List.class;
052:
053: }
054:
055: /**
056: * Initialises this expression list, each expression in the list is inited.
057: *
058: * @param q The Query object.
059: * @throws QueryParseException If one of the expressions cannot be inited.
060: */
061: public void init(Query q) throws QueryParseException {
062:
063: for (int i = 0; i < this .expressions.size(); i++) {
064:
065: Expression exp = (Expression) this .expressions.get(i);
066:
067: exp.init(q);
068:
069: }
070:
071: }
072:
073: /**
074: * Returns the expressions, a list of {@link Expression} objects.
075: *
076: * @return The expressions.
077: */
078: public List getExpressions() {
079:
080: return this .expressions;
081:
082: }
083:
084: /**
085: * Add an expression to the list.
086: *
087: * @param expr The expression.
088: */
089: public void addExpression(Expression expr) {
090:
091: if (this .expressions == null) {
092:
093: this .expressions = new ArrayList();
094:
095: }
096:
097: this .expressions.add(expr);
098:
099: }
100:
101: /**
102: * Set the expressions.
103: *
104: * @param exprs The expressions.
105: */
106: public void setExpressions(List exprs) {
107:
108: this .expressions = exprs;
109:
110: }
111:
112: /**
113: * Gets the value of the expressions, this will return a list of the values for
114: * each of the expressions in the list. In essence {@link Expression#getValue(Object,Query)}
115: * is called on each of the expressions.
116: *
117: * @param o The current object.
118: * @param q The Query object.
119: * @return A list of the values, if there are no expressions in the list then an empty list is returned.
120: * @throws QueryExecutionException If something goes wrong the acquisition of the values.
121: */
122: public Object getValue(Object o, Query q)
123: throws QueryExecutionException {
124:
125: List ret = new ArrayList();
126:
127: for (int i = 0; i < this .expressions.size(); i++) {
128:
129: Expression exp = (Expression) this .expressions.get(i);
130:
131: ret.add(exp.getValue(o, q));
132:
133: }
134:
135: return ret;
136:
137: }
138:
139: /**
140: * Returns <code>true</code> if one of the expression values is non-null.
141: * Note: for efficiency this method calls {@link Expression#getValue(Object,Query)}
142: * on each expression directly and returns <code>true</code> for the first non-null
143: * value found, as such if any of your expressions triggers side-effects then this
144: * method should not be used.
145: *
146: * @param o The current object.
147: * @param q The Query object.
148: * @return <code>true</code> if one of the expression values is non-null.
149: * @throws QueryExecutionException If a problem occurs during evaluation.
150: */
151: public boolean isTrue(Object o, Query q)
152: throws QueryExecutionException {
153:
154: for (int i = 0; i < this .expressions.size(); i++) {
155:
156: Expression exp = (Expression) this .expressions.get(i);
157:
158: if (exp.getValue(o, q) != null) {
159:
160: return true;
161:
162: }
163:
164: }
165:
166: return false;
167:
168: }
169:
170: /**
171: * Evaluates the value of this expression list. This is just a thin-wrapper around:
172: * {@link #getValue(Object,Query)}.
173: *
174: * @param o The current object.
175: * @param q The Query object.
176: * @return The value of this expression list.
177: * @throws QueryExecutionException If there is a problem getting the values.
178: */
179: public Object evaluate(Object o, Query q)
180: throws QueryExecutionException {
181:
182: return this .getValue(o, q);
183:
184: }
185:
186: /**
187: * Returns a string version of this expression list.
188: * Returns in the form: "[" Expression [ , Expression ]* "]".
189: *
190: * @return A string version of the expression list.
191: */
192: public String toString() {
193:
194: StringBuffer buf = new StringBuffer("[");
195:
196: for (int i = 0; i < this .expressions.size(); i++) {
197:
198: buf.append(this .expressions.get(i));
199:
200: if (i < this .expressions.size() - 1) {
201:
202: buf.append(", ");
203:
204: }
205:
206: }
207:
208: buf.append("]");
209:
210: return buf.toString();
211:
212: }
213:
214: /**
215: * Returns <code>true</code> if this expression list is empty (no expressions) or
216: * if ALL of the expressions have a fixed result.
217: *
218: * @param q The Query object.
219: * @return See description.
220: */
221: public boolean hasFixedResult(Query q) {
222:
223: if (this .expressions.size() == 0) {
224:
225: return true;
226:
227: }
228:
229: for (int i = 0; i < this .expressions.size(); i++) {
230:
231: Expression exp = (Expression) this .expressions.get(i);
232:
233: if (!exp.hasFixedResult(q)) {
234:
235: return false;
236:
237: }
238:
239: }
240:
241: return true;
242:
243: }
244:
245: }
|