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 org.jpox.store.mapping.JavaTypeMapping;
021:
022: /**
023: * Representation of a Character literal in a Query.
024: *
025: * @version $Revision: 1.11 $
026: **/
027: public class CharacterLiteral extends CharacterExpression implements
028: Literal {
029: private final String value;
030:
031: /** Raw value that this literal represents. */
032: Object rawValue;
033:
034: /**
035: * Creates a char literal
036: * @param qs the QueryExpression
037: * @param mapping the mapping
038: * @param value the char value
039: */
040: public CharacterLiteral(QueryExpression qs,
041: JavaTypeMapping mapping, char value) {
042: super (qs);
043:
044: this .mapping = mapping;
045: this .value = String.valueOf(value);
046: st.append('\'').append(value).append('\'');
047: }
048:
049: /**
050: * Creates a char literal
051: * @param qs the QueryExpression
052: * @param mapping the mapping
053: * @param value the char value
054: */
055: public CharacterLiteral(QueryExpression qs,
056: JavaTypeMapping mapping, String value) {
057: super (qs);
058:
059: this .mapping = mapping;
060: this .value = value;
061: st.appendParameter(mapping, value);
062: }
063:
064: public Object getValue() {
065: if (value == null) {
066: return null;
067: }
068: return new Character(value.charAt(0));
069: }
070:
071: public BooleanExpression eq(ScalarExpression expr) {
072: if (expr instanceof CharacterLiteral) {
073: return new BooleanLiteral(qs, mapping, value
074: .equals(((CharacterLiteral) expr).value));
075: } else {
076: return super .eq(expr);
077: }
078: }
079:
080: public BooleanExpression noteq(ScalarExpression expr) {
081: if (expr instanceof CharacterLiteral) {
082: return new BooleanLiteral(qs, mapping, !value
083: .equals(((CharacterLiteral) expr).value));
084: } else {
085: return super .noteq(expr);
086: }
087: }
088:
089: public BooleanExpression lt(ScalarExpression expr) {
090: if (expr instanceof CharacterLiteral) {
091: return new BooleanLiteral(qs, mapping, value
092: .compareTo(((CharacterLiteral) expr).value) < 0);
093: } else {
094: return super .lt(expr);
095: }
096: }
097:
098: public BooleanExpression lteq(ScalarExpression expr) {
099: if (expr instanceof CharacterLiteral) {
100: return new BooleanLiteral(qs, mapping, value
101: .compareTo(((CharacterLiteral) expr).value) <= 0);
102: } else {
103: return super .lteq(expr);
104: }
105: }
106:
107: public BooleanExpression gt(ScalarExpression expr) {
108: if (expr instanceof CharacterLiteral) {
109: return new BooleanLiteral(qs, mapping, value
110: .compareTo(((CharacterLiteral) expr).value) > 0);
111: } else {
112: return super .gt(expr);
113: }
114: }
115:
116: public BooleanExpression gteq(ScalarExpression expr) {
117: if (expr instanceof CharacterLiteral) {
118: return new BooleanLiteral(qs, mapping, value
119: .compareTo(((CharacterLiteral) expr).value) >= 0);
120: } else {
121: return super .gteq(expr);
122: }
123: }
124:
125: public ScalarExpression add(ScalarExpression expr) {
126: if (expr instanceof CharacterLiteral) {
127: int v = value.charAt(0)
128: + ((CharacterLiteral) expr).value.charAt(0);
129: return new IntegerLiteral(qs, mapping, new Integer(v));
130: } else if (expr instanceof IntegerLiteral) {
131: int v = value.charAt(0)
132: + ((Number) ((IntegerLiteral) expr).getValue())
133: .intValue();
134: return new IntegerLiteral(qs, mapping, new Integer(v));
135: } else {
136: return super .add(expr);
137: }
138: }
139:
140: public ScalarExpression sub(ScalarExpression expr) {
141: if (expr instanceof CharacterLiteral) {
142: int v = value.charAt(0)
143: - ((CharacterLiteral) expr).value.charAt(0);
144: return new IntegerLiteral(qs, mapping, new Integer(v));
145: } else if (expr instanceof IntegerLiteral) {
146: int v = value.charAt(0)
147: - ((Number) ((IntegerLiteral) expr).getValue())
148: .intValue();
149: return new IntegerLiteral(qs, mapping, new Integer(v));
150: } else {
151: return super .add(expr);
152: }
153: }
154:
155: public ScalarExpression mod(ScalarExpression expr) {
156: if (expr instanceof CharacterLiteral) {
157: int v = value.charAt(0)
158: % ((CharacterLiteral) expr).value.charAt(0);
159: return new IntegerLiteral(qs, mapping, new Integer(v));
160: } else if (expr instanceof IntegerLiteral) {
161: int v = value.charAt(0)
162: % ((Number) ((IntegerLiteral) expr).getValue())
163: .intValue();
164: return new IntegerLiteral(qs, mapping, new Integer(v));
165: } else {
166: return super .mod(expr);
167: }
168: }
169:
170: /**
171: * Converts the Character Literal to lower case
172: * @return the lowercased Character Literal
173: */
174: public CharacterExpression toLowerCaseMethod() {
175: return new CharacterLiteral(qs, mapping, value.toLowerCase());
176: }
177:
178: /**
179: * Converts the Character Literal to upper case
180: * @return the uppercased Character Literal
181: */
182: public CharacterExpression toUpperCaseMethod() {
183: return new CharacterLiteral(qs, mapping, value.toUpperCase());
184: }
185:
186: public ScalarExpression neg() {
187: int v = -(value.charAt(0));
188: return new IntegerLiteral(qs, mapping, new Integer(v));
189: }
190:
191: public ScalarExpression com() {
192: int v = ~(value.charAt(0));
193: return new IntegerLiteral(qs, mapping, new Integer(v));
194: }
195:
196: /**
197: * Method to save a "raw" value that this literal represents.
198: * This value differs from the literal value since that is of the same type as this literal.
199: * @param val The raw value
200: */
201: public void setRawValue(Object val) {
202: this .rawValue = val;
203: }
204:
205: /**
206: * Accessor for the "raw" value that this literal represents.
207: * This value differs from the literal value since that is of the same type as this literal.
208: * @return The raw value
209: */
210: public Object getRawValue() {
211: return rawValue;
212: }
213: }
|