KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > env > BooleanValue


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.quercus.env;
31
32 import com.caucho.vfs.WriteStream;
33
34 import java.io.IOException JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.io.Serializable JavaDoc;
37 import java.util.IdentityHashMap JavaDoc;
38
39 /**
40  * Represents a PHP boolean value.
41  */

42 public class BooleanValue extends Value
43   implements Serializable JavaDoc
44 {
45   public static final BooleanValue TRUE = new BooleanValue(true);
46   public static final BooleanValue FALSE = new BooleanValue(false);
47
48   private final boolean _value;
49
50   protected BooleanValue(boolean value)
51   {
52     _value = value;
53   }
54
55   public static BooleanValue create(boolean value)
56   {
57     return value ? TRUE : FALSE;
58   }
59
60   public static BooleanValue create(Boolean JavaDoc value)
61   {
62     if (value == null)
63       return FALSE;
64     else if (Boolean.TRUE.equals(value))
65       return TRUE;
66     else
67       return FALSE;
68   }
69
70   /**
71    * Returns the type.
72    */

73   public String JavaDoc getType()
74   {
75     return "boolean";
76   }
77
78   /**
79    * Returns true for a scalar
80    */

81   public boolean isScalar()
82   {
83     return true;
84   }
85
86   /**
87    * Converts to a boolean.
88    */

89   public boolean toBoolean()
90   {
91     return _value;
92   }
93
94   /**
95    * Converts to a long.
96    */

97   public long toLong()
98   {
99     return _value ? 1 : 0;
100   }
101
102   /**
103    * Converts to a double.
104    */

105   public double toDouble()
106   {
107     return _value ? 1 : 0;
108   }
109
110   /**
111    * Converts to a string.
112    */

113   public String JavaDoc toString()
114   {
115     return _value ? "1" : "";
116   }
117
118   /**
119    * Converts to an object.
120    */

121   public Value toObject(Env env)
122   {
123     Value obj = env.createObject();
124
125     obj.putField(env, "scalar", this);
126
127     return obj;
128   }
129
130   /**
131    * Converts to an object.
132    */

133   public Object JavaDoc toObject()
134   {
135     return _value ? Boolean.TRUE : Boolean.FALSE;
136   }
137
138   /**
139    * Converts to a java object.
140    */

141   public Object JavaDoc toJavaObject()
142   {
143     return _value ? Boolean.TRUE : Boolean.FALSE;
144   }
145
146   /**
147    * Converts to an array if null.
148    */

149   public Value toAutoArray()
150   {
151     if (! _value)
152       return new ArrayValueImpl();
153     else
154       return this;
155   }
156
157   /**
158    * Converts to an object if null.
159    */

160   public Value toAutoObject(Env env)
161   {
162     if (! _value)
163       return env.createObject();
164     else
165       return this;
166   }
167
168   /**
169    * Converts to a key.
170    */

171   public Value toKey()
172   {
173     return _value ? LongValue.ONE : LongValue.ZERO;
174   }
175
176   /**
177    * Returns true for equality
178    */

179   public boolean eq(Value rValue)
180   {
181     return _value == rValue.toBoolean();
182
183 /*
184     if (rValue instanceof StringValue) {
185       String v = rValue.toString();
186
187       if (_value)
188     return ! v.equals("") && ! v.equals("0");
189       else
190     return v.equals("") || v.equals("0");
191     }
192     else if (rValue.isNumberConvertible())
193       return toDouble() == rValue.toDouble();
194     else
195       return toString().equals(rValue.toString());
196 */

197   }
198   
199   /**
200    * Returns true for equality
201    */

202   public int cmp(Value rValue)
203   {
204     double l = _value ? 1 : 0;
205     double r = rValue.toDouble();
206
207     if (l == r)
208       return 0;
209     else if (l < r)
210       return -1;
211     else
212       return 1;
213   }
214
215   /**
216    * Prints the value.
217    * @param env
218    */

219   public void print(Env env)
220   {
221     env.print(_value ? "1" : "");
222   }
223
224   //
225
// Java generator code
226
//
227

228   /**
229    * Generates code to recreate the expression.
230    *
231    * @param out the writer to the Java source code.
232    */

233   public void generate(PrintWriter JavaDoc out)
234     throws IOException JavaDoc
235   {
236     if (_value)
237       out.print("com.caucho.quercus.env.BooleanValue.TRUE");
238     else
239       out.print("com.caucho.quercus.env.BooleanValue.FALSE");
240   }
241
242   /**
243    * Generates code to recreate the expression.
244    *
245    * @param out the writer to the Java source code.
246    */

247   public void generateBoolean(PrintWriter JavaDoc out)
248     throws IOException JavaDoc
249   {
250     if (_value)
251       out.print("true");
252     else
253       out.print("false");
254   }
255
256   /**
257    * Serializes the value.
258    */

259   public void serialize(StringBuilder JavaDoc sb)
260   {
261     sb.append("b:");
262     sb.append(_value ? 1 : 0);
263     sb.append(';');
264   }
265
266   /**
267    * Exports the value.
268    */

269   public void varExport(StringBuilder JavaDoc sb)
270   {
271     sb.append(_value ? "true" : "false");
272   }
273
274   /**
275    * Returns the hash code
276    */

277   public int hashCode()
278   {
279     return _value ? 17 : 37;
280   }
281
282   /**
283    * Compare for equality.
284    */

285   public boolean equals(Object JavaDoc o)
286   {
287     if (this == o)
288       return true;
289     else if (o.getClass() != getClass())
290       return false;
291
292     BooleanValue value = (BooleanValue) o;
293
294     return _value == value._value;
295   }
296
297
298   public void varDumpImpl(Env env,
299                           WriteStream out,
300                           int depth,
301                           IdentityHashMap JavaDoc<Value,String JavaDoc> valueSet)
302     throws IOException JavaDoc
303   {
304     if (toBoolean())
305       out.print("bool(true)");
306     else
307       out.print("bool(false)");
308   }
309   
310   //
311
// Java Serialization
312
//
313

314   private Object JavaDoc readResolve()
315   {
316     if (_value == true)
317       return TRUE;
318     else
319       return FALSE;
320   }
321 }
322
323
Popular Tags