KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > el > DoubleLiteral


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.el;
31
32 import com.caucho.vfs.WriteStream;
33
34 import javax.el.ELContext;
35 import javax.el.ELException;
36 import javax.servlet.jsp.JspWriter JavaDoc;
37 import java.io.IOException JavaDoc;
38
39 /**
40  * Represents a double literal expression.
41  */

42 public class DoubleLiteral extends Expr {
43   private double _value;
44   private Double JavaDoc _objValue;
45
46   /**
47    * Create a new double literal.
48    */

49   public DoubleLiteral(double value)
50   {
51     _value = value;
52     _objValue = new Double JavaDoc(value);
53   }
54
55   /**
56    * Returns true if the expression is constant.
57    */

58   public boolean isConstant()
59   {
60     return true;
61   }
62
63   /**
64    * Evaluate the expression as an object.
65    *
66    * @param env the variable environment
67    *
68    * @return the Double constant
69    */

70   @Override JavaDoc
71   public Object JavaDoc getValue(ELContext env)
72     throws ELException
73   {
74     return _objValue;
75   }
76
77   /**
78    * Evaluate the expression as an object as a long.
79    *
80    * @param env the variable environment
81    *
82    * @return the double value casted to a long
83    */

84   @Override JavaDoc
85   public long evalLong(ELContext env)
86     throws ELException
87   {
88     return (long) _value;
89   }
90
91   /**
92    * Evaluate the expression as an object as a double
93    *
94    * @param env the variable environment
95    *
96    * @return the double value
97    */

98   @Override JavaDoc
99   public double evalDouble(ELContext env)
100     throws ELException
101   {
102     return _value;
103   }
104
105   /**
106    * Evaluates directly to the output. The method returns true
107    * if the default value should be printed instead.
108    *
109    * @param out the output writer
110    * @param env the variable environment
111    * @param escapeXml if true, escape reserved XML
112    *
113    * @return true if the object is null, otherwise false
114    */

115   @Override JavaDoc
116   public boolean print(JspWriter JavaDoc out,
117                        ELContext env,
118                        boolean escapeXml)
119     throws IOException JavaDoc, ELException
120   {
121     out.print(_value);
122
123     return false;
124   }
125
126   /**
127    * Prints the *.java code to create an DoubleLiteral.
128    *
129    * @param os the stream to the generated *.java
130    */

131   @Override JavaDoc
132   public void printCreate(WriteStream os)
133     throws IOException JavaDoc
134   {
135     os.print("new com.caucho.el.DoubleLiteral(");
136     os.print(_value);
137     os.print(")");
138   }
139
140   /**
141    * Returns true for equal longs.
142    */

143   public boolean equals(Object JavaDoc o)
144   {
145     if (! (o instanceof DoubleLiteral))
146       return false;
147
148     DoubleLiteral literal = (DoubleLiteral) o;
149
150     return _value == literal._value;
151   }
152   
153   /**
154    * Returns a readable representation of the expr.
155    */

156   public String JavaDoc toString()
157   {
158     return String.valueOf(_value);
159   }
160 }
161
Popular Tags