001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
015: * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
016: * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
017: * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
018: * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
019: *
020: * Alternatively, the contents of this file may be used under the terms of
021: * either of the GNU General Public License Version 2 or later (the "GPL"),
022: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
023: * in which case the provisions of the GPL or the LGPL are applicable instead
024: * of those above. If you wish to allow use of your version of this file only
025: * under the terms of either the GPL or the LGPL, and not to allow others to
026: * use your version of this file under the terms of the CPL, indicate your
027: * decision by deleting the provisions above and replace them with the notice
028: * and other provisions required by the GPL or the LGPL. If you do not delete
029: * the provisions above, a recipient may use your version of this file under
030: * the terms of any one of the CPL, the GPL or the LGPL.
031: ***** END LICENSE BLOCK *****/package org.jruby;
032:
033: import org.jruby.runtime.CallbackFactory;
034: import org.jruby.runtime.ClassIndex;
035: import org.jruby.runtime.ObjectAllocator;
036: import org.jruby.runtime.builtin.IRubyObject;
037: import org.jruby.runtime.marshal.MarshalStream;
038:
039: /**
040: *
041: * @author jpetersen
042: */
043: public class RubyBoolean extends RubyObject {
044: private final Ruby runtime;
045:
046: public RubyBoolean(Ruby runtime, boolean value) {
047: super (runtime, (value ? runtime.getClass("TrueClass") : runtime
048: .getClass("FalseClass")), // Don't initialize with class
049: false); // Don't put in object space
050: this .isTrue = value;
051: this .runtime = runtime;
052: }
053:
054: public int getNativeTypeIndex() {
055: return isTrue ? ClassIndex.TRUE : ClassIndex.FALSE;
056: }
057:
058: public Ruby getRuntime() {
059: return runtime;
060: }
061:
062: public boolean isImmediate() {
063: return true;
064: }
065:
066: public Class getJavaClass() {
067: return Boolean.TYPE;
068: }
069:
070: // public RubyClass getMetaClass() {
071: // return isTrue
072: // ? getRuntime().getClass("TrueClass")
073: // : getRuntime().getClass("FalseClass");
074: // }
075:
076: public RubyFixnum id() {
077: return getRuntime().newFixnum(isTrue ? 2 : 0);
078: }
079:
080: public static RubyClass createFalseClass(Ruby runtime) {
081: RubyClass falseClass = runtime.defineClass("FalseClass",
082: runtime.getObject(),
083: ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
084: falseClass.index = ClassIndex.FALSE;
085:
086: CallbackFactory fact = runtime
087: .callbackFactory(RubyBoolean.class);
088:
089: falseClass.defineFastMethod("type", fact.getFastMethod("type"));
090: falseClass.defineFastMethod("&", fact.getFastMethod(
091: "false_and", RubyKernel.IRUBY_OBJECT));
092: falseClass.defineFastMethod("|", fact.getFastMethod("false_or",
093: RubyKernel.IRUBY_OBJECT));
094: falseClass.defineFastMethod("^", fact.getFastMethod(
095: "false_xor", RubyKernel.IRUBY_OBJECT));
096: falseClass.defineFastMethod("id", fact
097: .getFastMethod("false_id"));
098: falseClass.defineFastMethod("to_s", fact
099: .getFastMethod("false_to_s"));
100: falseClass.getMetaClass().undefineMethod("new");
101:
102: return falseClass;
103: }
104:
105: public static RubyClass createTrueClass(Ruby runtime) {
106: RubyClass trueClass = runtime
107: .defineClass("TrueClass", runtime.getObject(),
108: ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
109: trueClass.index = ClassIndex.TRUE;
110:
111: CallbackFactory fact = runtime
112: .callbackFactory(RubyBoolean.class);
113:
114: trueClass.defineFastMethod("type", fact.getFastMethod("type"));
115: trueClass.defineFastMethod("&", fact.getFastMethod("true_and",
116: RubyKernel.IRUBY_OBJECT));
117: trueClass.defineFastMethod("|", fact.getFastMethod("true_or",
118: RubyKernel.IRUBY_OBJECT));
119: trueClass.defineFastMethod("^", fact.getFastMethod("true_xor",
120: RubyKernel.IRUBY_OBJECT));
121: trueClass.defineFastMethod("id", fact.getFastMethod("true_id"));
122: trueClass.defineFastMethod("to_s", fact
123: .getFastMethod("true_to_s"));
124: trueClass.getMetaClass().undefineMethod("new");
125:
126: return trueClass;
127: }
128:
129: public static RubyBoolean newBoolean(Ruby runtime, boolean value) {
130: return value ? runtime.getTrue() : runtime.getFalse();
131: }
132:
133: /** false_type
134: * true_type
135: *
136: */
137: public RubyClass type() {
138: return getMetaClass();
139: }
140:
141: public IRubyObject false_and(IRubyObject oth) {
142: return this ;
143: }
144:
145: public IRubyObject false_or(IRubyObject oth) {
146: return oth.isTrue() ? getRuntime().getTrue() : this ;
147: }
148:
149: public IRubyObject false_xor(IRubyObject oth) {
150: return oth.isTrue() ? getRuntime().getTrue() : this ;
151: }
152:
153: public IRubyObject false_id() {
154: return RubyFixnum.zero(getRuntime());
155: }
156:
157: public IRubyObject false_to_s() {
158: return getRuntime().newString("false");
159: }
160:
161: public IRubyObject true_and(IRubyObject oth) {
162: return oth.isTrue() ? this : getRuntime().getFalse();
163: }
164:
165: public IRubyObject true_or(IRubyObject oth) {
166: return this ;
167: }
168:
169: public IRubyObject true_xor(IRubyObject oth) {
170: return oth.isTrue() ? getRuntime().getFalse() : this ;
171: }
172:
173: public IRubyObject true_id() {
174: return getRuntime().newFixnum(2);
175: }
176:
177: public IRubyObject true_to_s() {
178: return getRuntime().newString("true");
179: }
180:
181: public void marshalTo(MarshalStream output)
182: throws java.io.IOException {
183: output.write(isTrue() ? 'T' : 'F');
184: }
185: }
|