001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.el;
031:
032: import com.caucho.vfs.WriteStream;
033:
034: import javax.el.ELContext;
035: import javax.el.ELException;
036: import java.io.IOException;
037:
038: /**
039: * Represents a long literal expression.
040: */
041: public class LongLiteral extends Expr {
042: private long _value;
043: private Long _objValue;
044:
045: /**
046: * Creates the literal with its value.
047: */
048: public LongLiteral(long value) {
049: _value = value;
050: _objValue = new Long(value);
051: }
052:
053: /**
054: * Returns true if the expression is constant.
055: */
056: @Override
057: public boolean isConstant() {
058: return true;
059: }
060:
061: /**
062: * Evaluate the expression as an object.
063: *
064: * @param env the variable environment
065: *
066: * @return the long value as a Long
067: */
068: @Override
069: public Object getValue(ELContext env) throws ELException {
070: return _objValue;
071: }
072:
073: /**
074: * Evaluate the expression as an object as a long.
075: *
076: * @param env the variable environment
077: *
078: * @return the long value
079: */
080: @Override
081: public long evalLong(ELContext env) throws ELException {
082: return _value;
083: }
084:
085: /**
086: * Evaluate the expression as a double.
087: *
088: * @param env the variable environment
089: *
090: * @return the double value
091: */
092: @Override
093: public double evalDouble(ELContext env) throws ELException {
094: return (double) _value;
095: }
096:
097: /**
098: * Evalutes directly to the output.
099: *
100: * @param out the output stream
101: * @param env the variable environment
102: */
103: @Override
104: public boolean print(WriteStream out, ELContext env,
105: boolean isEscaped) throws IOException, ELException {
106: out.print(_value);
107:
108: return false;
109: }
110:
111: /**
112: * Prints the code to create an LongLiteral.
113: *
114: * @param os the stream to the generated *.java file
115: */
116: public void printCreate(WriteStream os) throws IOException {
117: os.print("new com.caucho.el.LongLiteral(");
118: os.print(_value);
119: os.print("L)");
120: }
121:
122: /**
123: * Returns true for equal strings.
124: */
125: public boolean equals(Object o) {
126: if (!(o instanceof LongLiteral))
127: return false;
128:
129: LongLiteral literal = (LongLiteral) o;
130:
131: return _value == literal._value;
132: }
133:
134: /**
135: * Returns a readable representation of the expr.
136: */
137: public String toString() {
138: return String.valueOf(_value);
139: }
140: }
|