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 javax.servlet.jsp.JspWriter;
037: import java.io.IOException;
038:
039: /**
040: * Represents a double literal expression.
041: */
042: public class DoubleLiteral extends Expr {
043: private double _value;
044: private Double _objValue;
045:
046: /**
047: * Create a new double literal.
048: */
049: public DoubleLiteral(double value) {
050: _value = value;
051: _objValue = new Double(value);
052: }
053:
054: /**
055: * Returns true if the expression is constant.
056: */
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 Double constant
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 double value casted to a long
079: */
080: @Override
081: public long evalLong(ELContext env) throws ELException {
082: return (long) _value;
083: }
084:
085: /**
086: * Evaluate the expression as an object 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 _value;
095: }
096:
097: /**
098: * Evaluates directly to the output. The method returns true
099: * if the default value should be printed instead.
100: *
101: * @param out the output writer
102: * @param env the variable environment
103: * @param escapeXml if true, escape reserved XML
104: *
105: * @return true if the object is null, otherwise false
106: */
107: @Override
108: public boolean print(JspWriter out, ELContext env, boolean escapeXml)
109: throws IOException, ELException {
110: out.print(_value);
111:
112: return false;
113: }
114:
115: /**
116: * Prints the *.java code to create an DoubleLiteral.
117: *
118: * @param os the stream to the generated *.java
119: */
120: @Override
121: public void printCreate(WriteStream os) throws IOException {
122: os.print("new com.caucho.el.DoubleLiteral(");
123: os.print(_value);
124: os.print(")");
125: }
126:
127: /**
128: * Returns true for equal longs.
129: */
130: public boolean equals(Object o) {
131: if (!(o instanceof DoubleLiteral))
132: return false;
133:
134: DoubleLiteral literal = (DoubleLiteral) o;
135:
136: return _value == literal._value;
137: }
138:
139: /**
140: * Returns a readable representation of the expr.
141: */
142: public String toString() {
143: return String.valueOf(_value);
144: }
145: }
|