KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > RubyNil


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 Charles O Nutter <headius@headius.com>
19  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * either of the GNU General Public License Version 2 or later (the "GPL"),
23  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24  * in which case the provisions of the GPL or the LGPL are applicable instead
25  * of those above. If you wish to allow use of your version of this file only
26  * under the terms of either the GPL or the LGPL, and not to allow others to
27  * use your version of this file under the terms of the CPL, indicate your
28  * decision by deleting the provisions above and replace them with the notice
29  * and other provisions required by the GPL or the LGPL. If you do not delete
30  * the provisions above, a recipient may use your version of this file under
31  * the terms of any one of the CPL, the GPL or the LGPL.
32  ***** END LICENSE BLOCK *****/

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

44 public class RubyNil extends RubyObject {
45     private final Ruby runtime;
46
47     public RubyNil(Ruby runtime) {
48             super(runtime, null);
49             this.runtime = runtime;
50     }
51
52     public Ruby getRuntime() {
53             return runtime;
54     }
55     
56     public static ObjectAllocator NIL_ALLOCATOR = new ObjectAllocator() {
57         public IRubyObject allocate(Ruby runtime, RubyClass klass) {
58             return runtime.getNil();
59         }
60     };
61     
62     public static RubyClass createNilClass(Ruby runtime) {
63         RubyClass nilClass = runtime.defineClass("NilClass", runtime.getObject(), NIL_ALLOCATOR);
64         nilClass.index = ClassIndex.NIL;
65         
66         CallbackFactory callbackFactory = runtime.callbackFactory(RubyNil.class);
67         nilClass.defineFastMethod("type", callbackFactory.getFastSingletonMethod("type"));
68         nilClass.defineFastMethod("to_i", callbackFactory.getFastSingletonMethod("to_i"));
69         nilClass.defineFastMethod("to_s", callbackFactory.getFastSingletonMethod("to_s"));
70         nilClass.defineFastMethod("to_a", callbackFactory.getFastSingletonMethod("to_a"));
71         nilClass.defineFastMethod("to_f", callbackFactory.getFastSingletonMethod("to_f"));
72         nilClass.defineFastMethod("inspect", callbackFactory.getFastSingletonMethod("inspect"));
73         
74         nilClass.defineFastMethod("&", callbackFactory.getFastSingletonMethod("op_and", RubyKernel.IRUBY_OBJECT));
75         nilClass.defineFastMethod("|", callbackFactory.getFastSingletonMethod("op_or", RubyKernel.IRUBY_OBJECT));
76         nilClass.defineFastMethod("^", callbackFactory.getFastSingletonMethod("op_xor", RubyKernel.IRUBY_OBJECT));
77         nilClass.defineFastMethod("nil?", callbackFactory.getFastMethod("nil_p"));
78         nilClass.defineFastMethod("id", callbackFactory.getFastSingletonMethod("id"));
79         nilClass.defineFastMethod("taint", callbackFactory.getFastMethod("taint"));
80         nilClass.defineFastMethod("freeze", callbackFactory.getFastMethod("freeze"));
81
82         nilClass.getMetaClass().undefineMethod("new");
83         
84         runtime.defineGlobalConstant("NIL", runtime.getNil());
85         
86         return nilClass;
87     }
88     
89     public int getNativeTypeIndex() {
90         return ClassIndex.NIL;
91     }
92     
93     public RubyClass getMetaClass() {
94         return runtime.getNilClass();
95     }
96
97     public boolean isImmediate() {
98         return true;
99     }
100     
101     public boolean safeHasInstanceVariables() {
102         return false;
103     }
104     
105     // Methods of the Nil Class (nil_*):
106

107     /** nil_to_i
108     *
109     */

110    public static RubyFixnum to_i(IRubyObject recv) {
111        return RubyFixnum.zero(recv.getRuntime());
112    }
113
114    /**
115     * nil_to_f
116     *
117     */

118     public static RubyFloat to_f(IRubyObject recv) {
119         return RubyFloat.newFloat(recv.getRuntime(), 0.0D);
120     }
121
122     /** nil_to_s
123      *
124      */

125     public static RubyString to_s(IRubyObject recv) {
126         return recv.getRuntime().newString("");
127     }
128     
129     /** nil_to_a
130      *
131      */

132     public static RubyArray to_a(IRubyObject recv) {
133         return recv.getRuntime().newArray(0);
134     }
135     
136     /** nil_inspect
137      *
138      */

139     public static RubyString inspect(IRubyObject recv) {
140         return recv.getRuntime().newString("nil");
141     }
142     
143     /** nil_type
144      *
145      */

146     public static RubyClass type(IRubyObject recv) {
147         return recv.getRuntime().getClass("NilClass");
148     }
149     
150     /** nil_and
151      *
152      */

153     public static RubyBoolean op_and(IRubyObject recv, IRubyObject obj) {
154         return recv.getRuntime().getFalse();
155     }
156     
157     /** nil_or
158      *
159      */

160     public static RubyBoolean op_or(IRubyObject recv, IRubyObject obj) {
161         return recv.getRuntime().newBoolean(obj.isTrue());
162     }
163
164     /** nil_xor
165      *
166      */

167     public static RubyBoolean op_xor(IRubyObject recv, IRubyObject obj) {
168         return recv.getRuntime().newBoolean(obj.isTrue());
169     }
170
171     public static RubyFixnum id(IRubyObject recv) {
172         return recv.getRuntime().newFixnum(4);
173     }
174
175     public boolean isNil() {
176         return true;
177     }
178     
179     public boolean isFalse() {
180         return true;
181     }
182
183     public boolean isTrue() {
184         return false;
185     }
186     
187     public IRubyObject freeze() {
188         return this;
189     }
190     
191     public IRubyObject nil_p() {
192         return getRuntime().getTrue();
193     }
194     
195     public IRubyObject taint() {
196         return this;
197     }
198
199     public RubyFixnum id() {
200         return getRuntime().newFixnum(4);
201     }
202 }
203
Popular Tags