001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.ejb.query;
012:
013: /**
014: * A literal.
015: */
016: public class LiteralNode extends Node {
017:
018: public static final int LONG = 1;
019: public static final int DOUBLE = 2;
020: public static final int BOOLEAN = 3;
021: public static final int STRING = 4;
022:
023: private int type;
024: private long longValue;
025: private double doubleValue;
026: private String stringValue;
027: private boolean booleanValue;
028:
029: public LiteralNode(int type, String s) {
030: this .type = type;
031: int len, c;
032: switch (type) {
033: case LONG:
034: len = s.length();
035: c = s.charAt(len - 1);
036: if (c == 'l' || c == 'L') {
037: s = s.substring(0, len - 1);
038: }
039: if (s.startsWith("0x")) {
040: longValue = Long.parseLong(s.substring(2), 16);
041: } else {
042: longValue = Long.parseLong(s);
043: }
044: break;
045: case DOUBLE:
046: len = s.length();
047: c = s.charAt(len - 1);
048: if (c == 'f' || c == 'F' || c == 'd' || c == 'D') {
049: s = s.substring(0, len - 1);
050: }
051: doubleValue = Double.parseDouble(s);
052: break;
053: case BOOLEAN:
054: s = s.toUpperCase();
055: booleanValue = "TRUE".equals(s);
056: break;
057: case STRING:
058: stringValue = s.substring(1, s.length() - 1);
059: break;
060: default:
061: throw new IllegalArgumentException("Invalid type " + type
062: + " for " + s);
063: }
064: }
065:
066: public int getType() {
067: return type;
068: }
069:
070: public long getLongValue() {
071: return longValue;
072: }
073:
074: public double getDoubleValue() {
075: return doubleValue;
076: }
077:
078: public String getStringValue() {
079: return stringValue;
080: }
081:
082: public boolean getBooleanValue() {
083: return booleanValue;
084: }
085:
086: public Object arrive(NodeVisitor v, Object msg) {
087: return v.arriveLiteralNode(this , msg);
088: }
089:
090: public String toStringImp() {
091: switch (type) {
092: case LONG:
093: return Long.toString(longValue) + "L";
094: case DOUBLE:
095: return Double.toString(doubleValue);
096: case BOOLEAN:
097: return booleanValue ? "TRUE" : "FALSE";
098: case STRING:
099: return "'" + stringValue + "'";
100: }
101: return "<? type " + type + "?>";
102: }
103:
104: }
|