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.quercus.env;
031:
032: import com.caucho.vfs.WriteStream;
033:
034: import java.io.IOException;
035: import java.io.PrintWriter;
036: import java.io.Serializable;
037: import java.util.IdentityHashMap;
038:
039: /**
040: * Represents a PHP boolean value.
041: */
042: public class BooleanValue extends Value implements Serializable {
043: public static final BooleanValue TRUE = new BooleanValue(true);
044: public static final BooleanValue FALSE = new BooleanValue(false);
045:
046: private final boolean _value;
047:
048: protected BooleanValue(boolean value) {
049: _value = value;
050: }
051:
052: public static BooleanValue create(boolean value) {
053: return value ? TRUE : FALSE;
054: }
055:
056: public static Value create(Boolean value) {
057: if (value == null) {
058: // php/3c23
059: return NullValue.NULL;
060: } else if (Boolean.TRUE.equals(value))
061: return TRUE;
062: else
063: return FALSE;
064: }
065:
066: /**
067: * Returns the type.
068: */
069: public String getType() {
070: return "boolean";
071: }
072:
073: /**
074: * Returns the ValueType.
075: */
076: @Override
077: public ValueType getValueType() {
078: return ValueType.BOOLEAN;
079: }
080:
081: /**
082: * Returns true for a BooleanValue
083: */
084: @Override
085: public boolean isBoolean() {
086: return true;
087: }
088:
089: /**
090: * Returns true for a scalar
091: */
092: public boolean isScalar() {
093: return true;
094: }
095:
096: /**
097: * Converts to a boolean.
098: */
099: public boolean toBoolean() {
100: return _value;
101: }
102:
103: /**
104: * Returns true if the value is empty
105: */
106: @Override
107: public boolean isEmpty() {
108: return !_value;
109: }
110:
111: /**
112: * Converts to a long.
113: */
114: public long toLong() {
115: return _value ? 1 : 0;
116: }
117:
118: /**
119: * Converts to a double.
120: */
121: public double toDouble() {
122: return _value ? 1 : 0;
123: }
124:
125: /**
126: * Converts to a string.
127: */
128: public String toString() {
129: return _value ? "1" : "";
130: }
131:
132: /**
133: * Converts to a string builder
134: */
135: @Override
136: public StringValue toStringBuilder(Env env) {
137: StringValue sb = env.createUnicodeBuilder();
138:
139: if (_value)
140: sb.append("1");
141:
142: return sb;
143: }
144:
145: /**
146: * Converts to an object.
147: */
148: public Object toObject() {
149: return _value ? Boolean.TRUE : Boolean.FALSE;
150: }
151:
152: /**
153: * Converts to a java object.
154: */
155: public Object toJavaObject() {
156: return _value ? Boolean.TRUE : Boolean.FALSE;
157: }
158:
159: /**
160: * Converts to an array if null.
161: */
162: public Value toAutoArray() {
163: if (!_value)
164: return new ArrayValueImpl();
165: else
166: return this ;
167: }
168:
169: /**
170: * Converts to an object if null.
171: */
172: public Value toAutoObject(Env env) {
173: if (!_value)
174: return env.createObject();
175: else
176: return this ;
177: }
178:
179: /**
180: * Converts to a key.
181: */
182: public Value toKey() {
183: return _value ? LongValue.ONE : LongValue.ZERO;
184: }
185:
186: /**
187: * Returns true for equality
188: */
189: public boolean eq(Value rValue) {
190: return _value == rValue.toBoolean();
191: }
192:
193: /**
194: * Returns true for equality
195: */
196: public int cmp(Value rValue) {
197: double l = _value ? 1 : 0;
198: double r = rValue.toDouble();
199:
200: if (l == r)
201: return 0;
202: else if (l < r)
203: return -1;
204: else
205: return 1;
206: }
207:
208: /**
209: * Return the length as a string.
210: */
211: @Override
212: public int length() {
213: return _value ? 1 : 0;
214: }
215:
216: /**
217: * Prints the value.
218: * @param env
219: */
220: public void print(Env env) {
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 out) throws IOException {
234: if (_value)
235: out.print("com.caucho.quercus.env.BooleanValue.TRUE");
236: else
237: out.print("com.caucho.quercus.env.BooleanValue.FALSE");
238: }
239:
240: /**
241: * Generates code to recreate the expression.
242: *
243: * @param out the writer to the Java source code.
244: */
245: public void generateBoolean(PrintWriter out) throws IOException {
246: if (_value)
247: out.print("true");
248: else
249: out.print("false");
250: }
251:
252: /**
253: * Serializes the value.
254: */
255: public void serialize(StringBuilder sb) {
256: sb.append("b:");
257: sb.append(_value ? 1 : 0);
258: sb.append(';');
259: }
260:
261: /**
262: * Exports the value.
263: */
264: public void varExport(StringBuilder sb) {
265: sb.append(_value ? "true" : "false");
266: }
267:
268: /**
269: * Returns the hash code
270: */
271: public int hashCode() {
272: return _value ? 17 : 37;
273: }
274:
275: /**
276: * Compare for equality.
277: */
278: public boolean equals(Object o) {
279: if (this == o)
280: return true;
281: else if (o.getClass() != getClass())
282: return false;
283:
284: BooleanValue value = (BooleanValue) o;
285:
286: return _value == value._value;
287: }
288:
289: @Override
290: public String toDebugString() {
291: if (toBoolean())
292: return "true";
293: else
294: return "false";
295: }
296:
297: public void varDumpImpl(Env env, WriteStream out, int depth,
298: IdentityHashMap<Value, String> valueSet) throws IOException {
299: if (toBoolean())
300: out.print("bool(true)");
301: else
302: out.print("bool(false)");
303: }
304:
305: //
306: // Java Serialization
307: //
308:
309: private Object readResolve() {
310: if (_value == true)
311: return TRUE;
312: else
313: return FALSE;
314: }
315: }
|