KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > RubyBoolean


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
15  * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
16  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
17  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
18  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
19  *
20  * Alternatively, the contents of this file may be used under the terms of
21  * either of the GNU General Public License Version 2 or later (the "GPL"),
22  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23  * in which case the provisions of the GPL or the LGPL are applicable instead
24  * of those above. If you wish to allow use of your version of this file only
25  * under the terms of either the GPL or the LGPL, and not to allow others to
26  * use your version of this file under the terms of the CPL, indicate your
27  * decision by deleting the provisions above and replace them with the notice
28  * and other provisions required by the GPL or the LGPL. If you do not delete
29  * the provisions above, a recipient may use your version of this file under
30  * the terms of any one of the CPL, the GPL or the LGPL.
31  ***** END LICENSE BLOCK *****/

32 package org.jruby;
33
34 import org.jruby.runtime.CallbackFactory;
35 import org.jruby.runtime.ClassIndex;
36 import org.jruby.runtime.ObjectAllocator;
37 import org.jruby.runtime.builtin.IRubyObject;
38 import org.jruby.runtime.marshal.MarshalStream;
39
40 /**
41  *
42  * @author jpetersen
43  */

44 public class RubyBoolean extends RubyObject {
45     private final Ruby runtime;
46     
47     private final boolean value;
48
49     public RubyBoolean(Ruby runtime, boolean value) {
50         super(runtime, null, // Don't initialize with class
51
false); // Don't put in object space
52
this.value = value;
53         this.runtime = runtime;
54     }
55     
56     public int getNativeTypeIndex() {
57         return value ? ClassIndex.TRUE : ClassIndex.FALSE;
58     }
59     
60     public Ruby getRuntime() {
61         return runtime;
62     }
63     
64     public boolean isImmediate() {
65         return true;
66     }
67
68     public Class JavaDoc getJavaClass() {
69         return Boolean.TYPE;
70     }
71
72     public RubyClass getMetaClass() {
73         return value
74             ? getRuntime().getClass("TrueClass")
75             : getRuntime().getClass("FalseClass");
76     }
77
78     public boolean isTrue() {
79         return value;
80     }
81
82     public boolean isFalse() {
83         return !value;
84     }
85
86     public RubyFixnum id() {
87         return getRuntime().newFixnum(value ? 2 : 0);
88     }
89
90     public static RubyClass createFalseClass(Ruby runtime) {
91         RubyClass falseClass = runtime.defineClass("FalseClass", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
92         falseClass.index = ClassIndex.FALSE;
93
94         CallbackFactory fact = runtime.callbackFactory(RubyBoolean.class);
95
96         falseClass.defineFastMethod("type", fact.getFastMethod("type"));
97         falseClass.defineFastMethod("&", fact.getFastMethod("false_and", RubyKernel.IRUBY_OBJECT));
98         falseClass.defineFastMethod("|", fact.getFastMethod("false_or", RubyKernel.IRUBY_OBJECT));
99         falseClass.defineFastMethod("^", fact.getFastMethod("false_xor", RubyKernel.IRUBY_OBJECT));
100         falseClass.defineFastMethod("id", fact.getFastMethod("false_id"));
101         falseClass.defineFastMethod("to_s", fact.getFastMethod("false_to_s"));
102
103         runtime.defineGlobalConstant("FALSE", runtime.getFalse());
104
105         return falseClass;
106     }
107
108     public static RubyClass createTrueClass(Ruby runtime) {
109         RubyClass trueClass = runtime.defineClass("TrueClass", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
110                 trueClass.index = ClassIndex.TRUE;
111
112         CallbackFactory fact = runtime.callbackFactory(RubyBoolean.class);
113
114         trueClass.defineFastMethod("type", fact.getFastMethod("type"));
115         trueClass.defineFastMethod("&", fact.getFastMethod("true_and", RubyKernel.IRUBY_OBJECT));
116         trueClass.defineFastMethod("|", fact.getFastMethod("true_or", RubyKernel.IRUBY_OBJECT));
117         trueClass.defineFastMethod("^", fact.getFastMethod("true_xor", RubyKernel.IRUBY_OBJECT));
118         trueClass.defineFastMethod("id", fact.getFastMethod("true_id"));
119         trueClass.defineFastMethod("to_s", fact.getFastMethod("true_to_s"));
120
121         runtime.defineGlobalConstant("TRUE", runtime.getTrue());
122
123         return trueClass;
124     }
125
126     public static RubyBoolean newBoolean(Ruby runtime, boolean value) {
127         return value ? runtime.getTrue() : runtime.getFalse();
128     }
129
130     /** false_type
131      * true_type
132      *
133      */

134     public RubyClass type() {
135         return getMetaClass();
136     }
137
138     public IRubyObject false_and(IRubyObject oth) {
139         return this;
140     }
141
142     public IRubyObject false_or(IRubyObject oth) {
143         return oth.isTrue() ? getRuntime().getTrue() : this;
144     }
145
146     public IRubyObject false_xor(IRubyObject oth) {
147         return oth.isTrue() ? getRuntime().getTrue() : this;
148     }
149
150     public IRubyObject false_id() {
151         return RubyFixnum.zero(getRuntime());
152     }
153
154     public IRubyObject false_to_s() {
155         return getRuntime().newString("false");
156     }
157
158     public IRubyObject true_and(IRubyObject oth) {
159         return oth.isTrue() ? this : getRuntime().getFalse();
160     }
161
162     public IRubyObject true_or(IRubyObject oth) {
163         return this;
164     }
165
166     public IRubyObject true_xor(IRubyObject oth) {
167         return oth.isTrue() ? getRuntime().getFalse() : this;
168     }
169
170     public IRubyObject true_id() {
171         return getRuntime().newFixnum(2);
172     }
173
174     public IRubyObject true_to_s() {
175         return getRuntime().newString("true");
176     }
177
178     public void marshalTo(MarshalStream output) throws java.io.IOException JavaDoc {
179         output.write(isTrue() ? 'T' : 'F');
180     }
181 }
182
183
Popular Tags