001: /**********************************************************************
002: Copyright (c) 2002 Mike Martin (TJDO) and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
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:
016: Contributors:
017: 2003 Andy Jefferson - coding standards
018: ...
019: **********************************************************************/package org.jpox.store.expression;
020:
021: import java.math.BigInteger;
022:
023: import org.jpox.store.mapping.JavaTypeMapping;
024: import org.jpox.store.query.StatementText;
025:
026: /**
027: * Representation of an Integer literal.
028: *
029: * @version $Revision: 1.17 $
030: **/
031: public class IntegerLiteral extends NumericExpression implements
032: Literal {
033: private final Number value;
034:
035: /** Raw value that this literal represents. */
036: Object rawValue;
037:
038: private StatementText stUsingParameter = new StatementText();
039:
040: private final boolean useParameter;
041:
042: /**
043: * Creates a integer literal
044: * @param qs the QueryExpression
045: * @param mapping the mapping
046: * @param value the integer value
047: */
048: public IntegerLiteral(QueryExpression qs, JavaTypeMapping mapping,
049: Number value) {
050: super (qs);
051: this .mapping = mapping;
052:
053: this .value = value;
054: st.append(String.valueOf(value));
055: stUsingParameter.appendParameter(mapping, value);
056: this .useParameter = true;
057: }
058:
059: /**
060: * Creates a integer literal. This constructor should only be used when the <code>value</code>
061: * will not change if the Query is run several times.
062: * @param qs the QueryExpression
063: * @param mapping the mapping
064: * @param value the integer value
065: * @param useParameter whether to use parameter or a literal in the expression when preparing the statement
066: */
067: public IntegerLiteral(QueryExpression qs, JavaTypeMapping mapping,
068: Number value, boolean useParameter) {
069: super (qs);
070: this .mapping = mapping;
071: this .value = value;
072: st.append(String.valueOf(value));
073: stUsingParameter.appendParameter(mapping, value);
074: this .useParameter = useParameter;
075: }
076:
077: public Object getValue() {
078: return value;
079: }
080:
081: public BooleanExpression eq(ScalarExpression expr) {
082: assertValidTypeForParameterComparison(expr,
083: NumericExpression.class);
084:
085: if (expr instanceof IntegerLiteral) {
086: return new BooleanLiteral(qs, mapping, new BigInteger(value
087: .toString()).compareTo(new BigInteger(
088: ((IntegerLiteral) expr).value.toString())) == 0);
089: } else if (expr instanceof CharacterExpression) {
090: CharacterLiteral literal = new CharacterLiteral(qs,
091: mapping, String.valueOf((char) value.intValue()));
092: return new BooleanExpression(expr, OP_EQ, literal);
093: } else {
094: return super .eq(expr);
095: }
096: }
097:
098: public BooleanExpression noteq(ScalarExpression expr) {
099: assertValidTypeForParameterComparison(expr,
100: NumericExpression.class);
101:
102: if (expr instanceof IntegerLiteral) {
103: return new BooleanLiteral(qs, mapping, new BigInteger(value
104: .toString()).compareTo(new BigInteger(
105: ((IntegerLiteral) expr).value.toString())) != 0);
106: } else if (expr instanceof CharacterExpression) {
107: CharacterLiteral literal = new CharacterLiteral(qs,
108: mapping, String.valueOf((char) value.intValue()));
109: return new BooleanExpression(expr, OP_NOTEQ, literal);
110: } else {
111: return super .noteq(expr);
112: }
113: }
114:
115: public BooleanExpression lt(ScalarExpression expr) {
116: if (expr instanceof IntegerLiteral) {
117: return new BooleanLiteral(qs, mapping, new BigInteger(value
118: .toString()).compareTo(new BigInteger(
119: ((IntegerLiteral) expr).value.toString())) < 0);
120: } else if (expr instanceof CharacterExpression) {
121: CharacterLiteral literal = new CharacterLiteral(qs,
122: mapping, String.valueOf((char) value.intValue()));
123: return new BooleanExpression(literal, OP_LT, expr);
124: } else {
125: return super .lt(expr);
126: }
127: }
128:
129: public BooleanExpression lteq(ScalarExpression expr) {
130: if (expr instanceof IntegerLiteral) {
131: return new BooleanLiteral(qs, mapping, new BigInteger(value
132: .toString()).compareTo(new BigInteger(
133: ((IntegerLiteral) expr).value.toString())) <= 0);
134: } else if (expr instanceof CharacterExpression) {
135: CharacterLiteral literal = new CharacterLiteral(qs,
136: mapping, String.valueOf((char) value.intValue()));
137: return new BooleanExpression(literal, OP_LTEQ, expr);
138: } else {
139: return super .lteq(expr);
140: }
141: }
142:
143: public BooleanExpression gt(ScalarExpression expr) {
144: if (expr instanceof IntegerLiteral) {
145: return new BooleanLiteral(qs, mapping, new BigInteger(value
146: .toString()).compareTo(new BigInteger(
147: ((IntegerLiteral) expr).value.toString())) > 0);
148: } else if (expr instanceof CharacterExpression) {
149: CharacterLiteral literal = new CharacterLiteral(qs,
150: mapping, String.valueOf((char) value.intValue()));
151: return new BooleanExpression(literal, OP_GT, expr);
152: } else {
153: return super .gt(expr);
154: }
155: }
156:
157: public BooleanExpression gteq(ScalarExpression expr) {
158: if (expr instanceof IntegerLiteral) {
159: return new BooleanLiteral(qs, mapping, new BigInteger(value
160: .toString()).compareTo(new BigInteger(
161: ((IntegerLiteral) expr).value.toString())) >= 0);
162: } else if (expr instanceof CharacterExpression) {
163: CharacterLiteral literal = new CharacterLiteral(qs,
164: mapping, String.valueOf((char) value.intValue()));
165: return new BooleanExpression(literal, OP_GTEQ, expr);
166: } else {
167: return super .gteq(expr);
168: }
169: }
170:
171: /**
172: * If both operands are instances of IntegerLiteral, the operation results in BigInteger type. TODO fix this to follow JVM type conversions
173: */
174: public ScalarExpression add(ScalarExpression expr) {
175: if (expr instanceof IntegerLiteral) {
176: return new IntegerLiteral(qs, mapping, new BigInteger(value
177: .toString()).add(new BigInteger(
178: ((IntegerLiteral) expr).value.toString())));
179: } else if (expr instanceof CharacterLiteral) {
180: int v = ((CharacterLiteral) expr).getValue().toString()
181: .charAt(0);
182: return new IntegerLiteral(qs, mapping, new BigInteger(value
183: .toString()).add(new BigInteger("" + v)));
184: } else {
185: return super .add(expr);
186: }
187: }
188:
189: /**
190: * If both operands are instances of IntegerLiteral, the operation results in BigInteger type. TODO fix this to follow JVM type conversions
191: */
192: public ScalarExpression sub(ScalarExpression expr) {
193: if (expr instanceof IntegerLiteral) {
194: return new IntegerLiteral(qs, mapping, new BigInteger(value
195: .toString()).subtract(new BigInteger(
196: ((IntegerLiteral) expr).value.toString())));
197: } else if (expr instanceof CharacterLiteral) {
198: int v = ((CharacterLiteral) expr).getValue().toString()
199: .charAt(0);
200: return new IntegerLiteral(qs, mapping, new BigInteger(value
201: .toString()).subtract(new BigInteger("" + v)));
202: } else {
203: return super .sub(expr);
204: }
205: }
206:
207: /**
208: * If both operands are instances of IntegerLiteral, the operation results in BigInteger type. TODO fix this to follow JVM type conversions
209: */
210: public ScalarExpression mul(ScalarExpression expr) {
211: if (expr instanceof IntegerLiteral) {
212: return new IntegerLiteral(qs, mapping, new BigInteger(value
213: .toString()).multiply(new BigInteger(
214: ((IntegerLiteral) expr).value.toString())));
215: } else if (expr instanceof CharacterLiteral) {
216: int v = ((CharacterLiteral) expr).getValue().toString()
217: .charAt(0);
218: return new IntegerLiteral(qs, mapping, new BigInteger(value
219: .toString()).multiply(new BigInteger("" + v)));
220: } else {
221: return super .mul(expr);
222: }
223: }
224:
225: /**
226: * If both operands are instances of IntegerLiteral, the operation results in BigInteger type. TODO fix this to follow JVM type conversions
227: */
228: public ScalarExpression div(ScalarExpression expr) {
229: if (expr instanceof IntegerLiteral) {
230: return new IntegerLiteral(qs, mapping, new BigInteger(value
231: .toString()).divide(new BigInteger(
232: ((IntegerLiteral) expr).value.toString())));
233: } else if (expr instanceof CharacterLiteral) {
234: int v = ((CharacterLiteral) expr).getValue().toString()
235: .charAt(0);
236: return new IntegerLiteral(qs, mapping, new BigInteger(value
237: .toString()).divide(new BigInteger("" + v)));
238: } else {
239: return super .div(expr);
240: }
241: }
242:
243: /**
244: * If both operands are instances of IntegerLiteral, the operation results in BigInteger type. TODO fix this to follow JVM type conversions
245: */
246: public ScalarExpression mod(ScalarExpression expr) {
247: if (expr instanceof IntegerLiteral) {
248: return new IntegerLiteral(qs, mapping, new BigInteger(value
249: .toString()).mod(new BigInteger(
250: ((IntegerLiteral) expr).value.toString())));
251: } else if (expr instanceof CharacterLiteral) {
252: int v = ((CharacterLiteral) expr).getValue().toString()
253: .charAt(0);
254: return new IntegerLiteral(qs, mapping, new BigInteger(value
255: .toString()).mod(new BigInteger("" + v)));
256: } else {
257: return super .mod(expr);
258: }
259: }
260:
261: /**
262: * Operation results in BigInteger type. TODO fix this to follow JVM type conversions
263: */
264: public ScalarExpression neg() {
265: return new IntegerLiteral(qs, mapping, new BigInteger(value
266: .toString()).negate());
267: }
268:
269: public StatementText toStatementText(int mode) {
270: if (mode == ScalarExpression.FILTER && useParameter) {
271: return stUsingParameter;
272: }
273: return super .toStatementText(mode);
274: }
275:
276: /**
277: * Method to save a "raw" value that this literal represents.
278: * This value differs from the literal value since that is of the same type as this literal.
279: * @param val The raw value
280: */
281: public void setRawValue(Object val) {
282: this .rawValue = val;
283: }
284:
285: /**
286: * Accessor for the "raw" value that this literal represents.
287: * This value differs from the literal value since that is of the same type as this literal.
288: * @return The raw value
289: */
290: public Object getRawValue() {
291: return rawValue;
292: }
293: }
|