KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > RubyInteger


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

34 package org.jruby;
35
36 import org.jruby.runtime.Block;
37 import org.jruby.runtime.CallbackFactory;
38 import org.jruby.runtime.ObjectAllocator;
39 import org.jruby.runtime.ThreadContext;
40 import org.jruby.runtime.builtin.IRubyObject;
41
42 /** Implementation of the Integer class.
43  *
44  * @author jpetersen
45  */

46 public abstract class RubyInteger extends RubyNumeric {
47
48     public static RubyClass createIntegerClass(Ruby runtime) {
49         RubyClass integer = runtime.defineClass("Integer", runtime.getClass("Numeric"),
50                 ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
51         CallbackFactory callbackFactory = runtime.callbackFactory(RubyInteger.class);
52         integer.getSingletonClass().undefineMethod("allocate");
53         integer.getSingletonClass().undefineMethod("new");
54
55         integer.defineFastMethod("integer?", callbackFactory.getFastMethod("int_p"));
56         integer.defineMethod("upto", callbackFactory.getMethod("upto", RubyKernel.IRUBY_OBJECT));
57         integer.defineMethod("downto", callbackFactory.getMethod("downto", RubyKernel.IRUBY_OBJECT));
58         integer.defineMethod("times", callbackFactory.getMethod("times"));
59
60         integer.includeModule(runtime.getModule("Precision"));
61
62         integer.defineFastMethod("succ", callbackFactory.getFastMethod("succ"));
63         integer.defineFastMethod("next", callbackFactory.getFastMethod("succ"));
64         integer.defineFastMethod("chr", callbackFactory.getFastMethod("chr"));
65         integer.defineFastMethod("to_i", callbackFactory.getFastMethod("to_i"));
66         integer.defineFastMethod("to_int", callbackFactory.getFastMethod("to_i"));
67         integer.defineFastMethod("floor", callbackFactory.getFastMethod("to_i"));
68         integer.defineFastMethod("ceil", callbackFactory.getFastMethod("to_i"));
69         integer.defineFastMethod("round", callbackFactory.getFastMethod("to_i"));
70         integer.defineFastMethod("truncate", callbackFactory.getFastMethod("to_i"));
71
72         integer.getMetaClass().defineFastMethod("induced_from", callbackFactory.getFastSingletonMethod("induced_from",
73                 RubyKernel.IRUBY_OBJECT));
74         return integer;
75     }
76
77     public RubyInteger(Ruby runtime, RubyClass rubyClass) {
78         super(runtime, rubyClass);
79     }
80     
81     public RubyInteger convertToInteger() {
82         return this;
83     }
84
85     // conversion
86
protected RubyFloat toFloat() {
87         return RubyFloat.newFloat(getRuntime(), getDoubleValue());
88     }
89
90     /* ================
91      * Instance Methods
92      * ================
93      */

94
95     /** int_int_p
96      *
97      */

98     public IRubyObject int_p() {
99         return getRuntime().getTrue();
100     }
101
102     /** int_upto
103      *
104      */

105     public IRubyObject upto(IRubyObject to, Block block) {
106         ThreadContext context = getRuntime().getCurrentContext();
107
108         if (this instanceof RubyFixnum && to instanceof RubyFixnum) {
109
110             RubyFixnum toFixnum = (RubyFixnum) to;
111             long toValue = toFixnum.getLongValue();
112             for (long i = getLongValue(); i <= toValue; i++) {
113                 context.yield(RubyFixnum.newFixnum(getRuntime(), i), block);
114             }
115         } else {
116             RubyNumeric i = this;
117
118             while (true) {
119                 if (i.callMethod(context, ">", to).isTrue()) {
120                     break;
121                 }
122                 context.yield(i, block);
123                 i = (RubyNumeric) i.callMethod(context, "+", RubyFixnum.one(getRuntime()));
124             }
125         }
126         return this;
127     }
128
129     /** int_downto
130      *
131      */

132     // TODO: Make callCoerced work in block context...then fix downto, step, and upto.
133
public IRubyObject downto(IRubyObject to, Block block) {
134         ThreadContext context = getRuntime().getCurrentContext();
135
136         if (this instanceof RubyFixnum && to instanceof RubyFixnum) {
137             RubyFixnum toFixnum = (RubyFixnum) to;
138             long toValue = toFixnum.getLongValue();
139             for (long i = getLongValue(); i >= toValue; i--) {
140                 context.yield(RubyFixnum.newFixnum(getRuntime(), i), block);
141             }
142         } else {
143             RubyNumeric i = this;
144
145             while (true) {
146                 if (i.callMethod(context, "<", to).isTrue()) {
147                     break;
148                 }
149                 context.yield(i, block);
150                 i = (RubyNumeric) i.callMethod(context, "-", RubyFixnum.one(getRuntime()));
151             }
152         }
153         return this;
154     }
155
156     public IRubyObject times(Block block) {
157         ThreadContext context = getRuntime().getCurrentContext();
158
159         if (this instanceof RubyFixnum) {
160
161             long value = getLongValue();
162             for (long i = 0; i < value; i++) {
163                 context.yield(RubyFixnum.newFixnum(getRuntime(), i), block);
164             }
165         } else {
166             RubyNumeric i = RubyFixnum.zero(getRuntime());
167             while (true) {
168                 if (!i.callMethod(context, "<", this).isTrue()) {
169                     break;
170                 }
171                 context.yield(i, block);
172                 i = (RubyNumeric) i.callMethod(context, "+", RubyFixnum.one(getRuntime()));
173             }
174         }
175
176         return this;
177     }
178
179     /** int_succ
180      *
181      */

182     public IRubyObject succ() {
183         if (this instanceof RubyFixnum) {
184             return RubyFixnum.newFixnum(getRuntime(), getLongValue() + 1L);
185         } else {
186             return callMethod(getRuntime().getCurrentContext(), "+", RubyFixnum.one(getRuntime()));
187         }
188     }
189
190     /** int_chr
191      *
192      */

193     public RubyString chr() {
194         if (getLongValue() < 0 || getLongValue() > 0xff) {
195             throw getRuntime().newRangeError(this.toString() + " out of char range");
196         }
197         return getRuntime().newString(new String JavaDoc(new char[] { (char) getLongValue() }));
198     }
199
200     /** int_to_i
201      *
202      */

203     public RubyInteger to_i() {
204         return this;
205     }
206
207     /* ================
208      * Singleton Methods
209      * ================
210      */

211
212     /** rb_int_induced_from
213      *
214      */

215     public static IRubyObject induced_from(IRubyObject recv, IRubyObject other) {
216         if (other instanceof RubyFixnum || other instanceof RubyBignum) {
217             return other;
218         } else if (other instanceof RubyFloat) {
219             return other.callMethod(recv.getRuntime().getCurrentContext(), "to_i");
220         } else {
221             throw recv.getRuntime().newTypeError(
222                     "failed to convert " + other.getMetaClass().getName() + " into Integer");
223     }
224 }
225 }
226
Popular Tags