001: /**********************************************************************
002: Copyright (c) 2002 Kelly Grizzle (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: 2002 Mike Martin (TJDO)
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.DatastoreAdapter;
024: import org.jpox.store.mapping.JavaTypeMapping;
025:
026: /**
027: * Representation of a Byte literal.
028: *
029: * @version $Revision: 1.8 $
030: **/
031: public class ByteLiteral extends NumericExpression implements Literal {
032: private final BigInteger value;
033:
034: /** Raw value that this literal represents. */
035: Object rawValue;
036:
037: /**
038: * Creates a byte literal
039: * @param qs the QueryExpression
040: * @param mapping the mapping
041: * @param value the literal value
042: */
043: public ByteLiteral(QueryExpression qs, JavaTypeMapping mapping,
044: BigInteger value) {
045: super (qs);
046: this .mapping = mapping;
047:
048: this .value = value;
049:
050: DatastoreAdapter dba = qs.getStoreManager()
051: .getDatastoreAdapter();
052: mapping = dba.getMapping(Byte.class, qs.getStoreManager(), qs
053: .getClassLoaderResolver());
054: st.appendParameter(mapping, value);
055:
056: }
057:
058: public Object getValue() {
059: return value;
060: }
061:
062: public BooleanExpression eq(ScalarExpression expr) {
063: if (expr instanceof ByteLiteral) {
064: return new BooleanLiteral(qs, mapping, value
065: .compareTo(((ByteLiteral) expr).value) == 0);
066: } else if (expr instanceof CharacterExpression) {
067: return new BooleanExpression(this , OP_EQ, expr);
068: } else {
069: return super .eq(expr);
070: }
071: }
072:
073: public BooleanExpression noteq(ScalarExpression expr) {
074: if (expr instanceof ByteLiteral) {
075: return new BooleanLiteral(qs, mapping, value
076: .compareTo(((ByteLiteral) expr).value) != 0);
077: } else if (expr instanceof CharacterExpression) {
078: return new BooleanExpression(this , OP_NOTEQ, expr);
079: } else {
080: return super .noteq(expr);
081: }
082: }
083:
084: public BooleanExpression lt(ScalarExpression expr) {
085: if (expr instanceof ByteLiteral) {
086: return new BooleanLiteral(qs, mapping, value
087: .compareTo(((ByteLiteral) expr).value) < 0);
088: } else if (expr instanceof CharacterExpression) {
089: return new BooleanExpression(this , OP_LT, expr);
090: } else {
091: return super .lt(expr);
092: }
093: }
094:
095: public BooleanExpression lteq(ScalarExpression expr) {
096: if (expr instanceof ByteLiteral) {
097: return new BooleanLiteral(qs, mapping, value
098: .compareTo(((ByteLiteral) expr).value) <= 0);
099: } else if (expr instanceof CharacterExpression) {
100: return new BooleanExpression(this , OP_LTEQ, expr);
101: } else {
102: return super .lteq(expr);
103: }
104: }
105:
106: public BooleanExpression gt(ScalarExpression expr) {
107: if (expr instanceof ByteLiteral) {
108: return new BooleanLiteral(qs, mapping, value
109: .compareTo(((ByteLiteral) expr).value) > 0);
110: } else if (expr instanceof CharacterExpression) {
111: return new BooleanExpression(this , OP_GT, expr);
112: } else {
113: return super .gt(expr);
114: }
115: }
116:
117: public BooleanExpression gteq(ScalarExpression expr) {
118: if (expr instanceof ByteLiteral) {
119: return new BooleanLiteral(qs, mapping, value
120: .compareTo(((ByteLiteral) expr).value) >= 0);
121: } else if (expr instanceof CharacterExpression) {
122: return new BooleanExpression(this , OP_GTEQ, expr);
123: } else {
124: return super .gteq(expr);
125: }
126: }
127:
128: public ScalarExpression add(ScalarExpression expr) {
129: if (expr instanceof ByteLiteral) {
130: return new ByteLiteral(qs, mapping, value
131: .add(((ByteLiteral) expr).value));
132: } else {
133: return super .add(expr);
134: }
135: }
136:
137: public ScalarExpression sub(ScalarExpression expr) {
138: if (expr instanceof ByteLiteral) {
139: return new ByteLiteral(qs, mapping, value
140: .subtract(((ByteLiteral) expr).value));
141: } else {
142: return super .sub(expr);
143: }
144: }
145:
146: public ScalarExpression mul(ScalarExpression expr) {
147: if (expr instanceof ByteLiteral) {
148: return new ByteLiteral(qs, mapping, value
149: .multiply(((ByteLiteral) expr).value));
150: } else {
151: return super .mul(expr);
152: }
153: }
154:
155: public ScalarExpression div(ScalarExpression expr) {
156: if (expr instanceof ByteLiteral) {
157: return new ByteLiteral(qs, mapping, value
158: .divide(((ByteLiteral) expr).value));
159: } else {
160: return super .div(expr);
161: }
162: }
163:
164: public ScalarExpression mod(ScalarExpression expr) {
165: if (expr instanceof ByteLiteral) {
166: return new ByteLiteral(qs, mapping, value
167: .mod(((ByteLiteral) expr).value));
168: } else {
169: return super .mod(expr);
170: }
171: }
172:
173: public ScalarExpression neg() {
174: return new ByteLiteral(qs, mapping, value.negate());
175: }
176:
177: /**
178: * Method to save a "raw" value that this literal represents.
179: * This value differs from the literal value since that is of the same type as this literal.
180: * @param val The raw value
181: */
182: public void setRawValue(Object val) {
183: this .rawValue = val;
184: }
185:
186: /**
187: * Accessor for the "raw" value that this literal represents.
188: * This value differs from the literal value since that is of the same type as this literal.
189: * @return The raw value
190: */
191: public Object getRawValue() {
192: return rawValue;
193: }
194: }
|