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: Contributors:
016: 2003 Andy Jefferson - coding standards
017: ...
018: **********************************************************************/package org.jpox.store.expression;
019:
020: import java.math.BigDecimal;
021:
022: import org.jpox.store.DatastoreAdapter;
023: import org.jpox.store.mapping.JavaTypeMapping;
024: import org.jpox.util.SQLFormat;
025:
026: /**
027: * Representation of a FloatPoint literal in a query.
028: *
029: * @version $Revision: 1.8 $
030: **/
031: public class FloatingPointLiteral extends NumericExpression implements
032: Literal {
033: private final BigDecimal value;
034:
035: /** Raw value that this literal represents. */
036: Object rawValue;
037:
038: /**
039: * Creates a floating point literal
040: * @param qs the QueryExpression
041: * @param mapping the mapping
042: * @param value the floating point value
043: */
044: public FloatingPointLiteral(QueryExpression qs,
045: JavaTypeMapping mapping, Float value) {
046: super (qs);
047: this .mapping = mapping;
048: this .value = new BigDecimal(value.toString());
049:
050: DatastoreAdapter dba = qs.getStoreManager()
051: .getDatastoreAdapter();
052: st
053: .appendParameter(dba
054: .getMapping(Float.class, qs.getStoreManager(),
055: qs.getClassLoaderResolver()), value);
056: }
057:
058: public Object getValue() {
059: return value;
060: }
061:
062: /**
063: * Creates a floating point literal
064: * @param qs the QueryExpression
065: * @param mapping the mapping
066: * @param value the floating point value
067: */
068: public FloatingPointLiteral(QueryExpression qs,
069: JavaTypeMapping mapping, Double value) {
070: super (qs);
071: this .mapping = mapping;
072: this .value = new BigDecimal(value.toString());
073:
074: DatastoreAdapter dba = qs.getStoreManager()
075: .getDatastoreAdapter();
076: st
077: .appendParameter(dba
078: .getMapping(Double.class, qs.getStoreManager(),
079: qs.getClassLoaderResolver()), value);
080: }
081:
082: /**
083: * Creates a floating point literal
084: * @param qs the QueryExpression
085: * @param value the floating point value
086: */
087: public FloatingPointLiteral(QueryExpression qs, BigDecimal value) {
088: super (qs);
089:
090: this .value = value;
091: st.append(SQLFormat.format(value));
092: }
093:
094: public BooleanExpression eq(ScalarExpression expr) {
095: assertValidTypeForParameterComparison(expr,
096: NumericExpression.class);
097:
098: if (expr instanceof FloatingPointLiteral) {
099: return new BooleanLiteral(
100: qs,
101: mapping,
102: value
103: .compareTo(((FloatingPointLiteral) expr).value) == 0);
104: } else if (expr instanceof CharacterExpression) {
105: CharacterLiteral literal = new CharacterLiteral(qs,
106: mapping, String.valueOf((char) value.intValue()));
107: return new BooleanExpression(expr, OP_EQ, literal);
108: } else {
109: return super .eq(expr);
110: }
111: }
112:
113: public BooleanExpression noteq(ScalarExpression expr) {
114: assertValidTypeForParameterComparison(expr,
115: NumericExpression.class);
116:
117: if (expr instanceof FloatingPointLiteral) {
118: return new BooleanLiteral(
119: qs,
120: mapping,
121: value
122: .compareTo(((FloatingPointLiteral) expr).value) != 0);
123: } else if (expr instanceof CharacterExpression) {
124: CharacterLiteral literal = new CharacterLiteral(qs,
125: mapping, String.valueOf((char) value.intValue()));
126: return new BooleanExpression(expr, OP_NOTEQ, literal);
127: } else {
128: return super .noteq(expr);
129: }
130: }
131:
132: public BooleanExpression lt(ScalarExpression expr) {
133: if (expr instanceof FloatingPointLiteral) {
134: return new BooleanLiteral(qs, mapping, value
135: .compareTo(((FloatingPointLiteral) expr).value) < 0);
136: } else if (expr instanceof CharacterExpression) {
137: CharacterLiteral literal = new CharacterLiteral(qs,
138: mapping, String.valueOf((char) value.intValue()));
139: return new BooleanExpression(literal, OP_LT, expr);
140: } else {
141: return super .lt(expr);
142: }
143: }
144:
145: public BooleanExpression lteq(ScalarExpression expr) {
146: if (expr instanceof FloatingPointLiteral) {
147: return new BooleanLiteral(
148: qs,
149: mapping,
150: value
151: .compareTo(((FloatingPointLiteral) expr).value) <= 0);
152: } else if (expr instanceof CharacterExpression) {
153: CharacterLiteral literal = new CharacterLiteral(qs,
154: mapping, String.valueOf((char) value.intValue()));
155: return new BooleanExpression(literal, OP_LTEQ, expr);
156: } else {
157: return super .lteq(expr);
158: }
159: }
160:
161: public BooleanExpression gt(ScalarExpression expr) {
162: if (expr instanceof FloatingPointLiteral) {
163: return new BooleanLiteral(qs, mapping, value
164: .compareTo(((FloatingPointLiteral) expr).value) > 0);
165: } else if (expr instanceof CharacterExpression) {
166: CharacterLiteral literal = new CharacterLiteral(qs,
167: mapping, String.valueOf((char) value.intValue()));
168: return new BooleanExpression(literal, OP_GT, expr);
169: } else {
170: return super .gt(expr);
171: }
172: }
173:
174: public BooleanExpression gteq(ScalarExpression expr) {
175: if (expr instanceof FloatingPointLiteral) {
176: return new BooleanLiteral(
177: qs,
178: mapping,
179: value
180: .compareTo(((FloatingPointLiteral) expr).value) >= 0);
181: } else if (expr instanceof CharacterExpression) {
182: CharacterLiteral literal = new CharacterLiteral(qs,
183: mapping, String.valueOf((char) value.intValue()));
184: return new BooleanExpression(literal, OP_GTEQ, expr);
185: } else {
186: return super .gteq(expr);
187: }
188: }
189:
190: public ScalarExpression add(ScalarExpression expr) {
191: if (expr instanceof FloatingPointLiteral) {
192: return new FloatingPointLiteral(qs, value
193: .add(((FloatingPointLiteral) expr).value));
194: } else {
195: return super .add(expr);
196: }
197: }
198:
199: public ScalarExpression sub(ScalarExpression expr) {
200: if (expr instanceof FloatingPointLiteral) {
201: return new FloatingPointLiteral(qs, value
202: .subtract(((FloatingPointLiteral) expr).value));
203: } else {
204: return super .sub(expr);
205: }
206: }
207:
208: public ScalarExpression mul(ScalarExpression expr) {
209: if (expr instanceof FloatingPointLiteral) {
210: return new FloatingPointLiteral(qs, value
211: .multiply(((FloatingPointLiteral) expr).value));
212: } else {
213: return super .mul(expr);
214: }
215: }
216:
217: public ScalarExpression div(ScalarExpression expr) {
218: if (expr instanceof FloatingPointLiteral) {
219: return new FloatingPointLiteral(qs, value.divide(
220: ((FloatingPointLiteral) expr).value,
221: BigDecimal.ROUND_DOWN));
222: } else {
223: return super .mul(expr);
224: }
225: }
226:
227: public ScalarExpression neg() {
228: return new FloatingPointLiteral(qs, value.negate());
229: }
230:
231: /**
232: * Method to save a "raw" value that this literal represents.
233: * This value differs from the literal value since that is of the same type as this literal.
234: * @param val The raw value
235: */
236: public void setRawValue(Object val) {
237: this .rawValue = val;
238: }
239:
240: /**
241: * Accessor for the "raw" value that this literal represents.
242: * This value differs from the literal value since that is of the same type as this literal.
243: * @return The raw value
244: */
245: public Object getRawValue() {
246: return rawValue;
247: }
248: }
|