KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > javasupport > JavaArray


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

31 package org.jruby.javasupport;
32
33 import java.lang.reflect.Array JavaDoc;
34
35 import org.jruby.Ruby;
36 import org.jruby.RubyClass;
37 import org.jruby.RubyFixnum;
38 import org.jruby.RubyInteger;
39 import org.jruby.RubyModule;
40 import org.jruby.runtime.ObjectAllocator;
41 import org.jruby.runtime.builtin.IRubyObject;
42
43 public class JavaArray extends JavaObject {
44
45     public JavaArray(Ruby runtime, Object JavaDoc array) {
46         super(runtime, runtime.getModule("Java").getClass("JavaArray"), array);
47         assert array.getClass().isArray();
48     }
49
50     public static RubyClass createJavaArrayClass(Ruby runtime, RubyModule javaModule) {
51         // FIXME: NOT_ALLOCATABLE_ALLOCATOR is probably not right here, since we might
52
// eventually want JavaArray to be marshallable. JRUBY-414
53
return javaModule.defineClassUnder("JavaArray", javaModule.getClass("JavaObject"), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
54     }
55
56     public RubyFixnum length() {
57         return getRuntime().newFixnum(getLength());
58     }
59
60     private int getLength() {
61         return Array.getLength(getValue());
62     }
63
64     public IRubyObject aref(IRubyObject index) {
65         if (! (index instanceof RubyInteger)) {
66             throw getRuntime().newTypeError(index, getRuntime().getClass("Integer"));
67         }
68         int intIndex = (int) ((RubyInteger) index).getLongValue();
69         if (intIndex < 0 || intIndex >= getLength()) {
70             throw getRuntime().newArgumentError(
71                                     "index out of bounds for java array (" + intIndex +
72                                     " for length " + getLength() + ")");
73         }
74         Object JavaDoc result = Array.get(getValue(), intIndex);
75         if (result == null) {
76             return getRuntime().getNil();
77         }
78         return JavaObject.wrap(getRuntime(), result);
79     }
80
81     public IRubyObject aset(IRubyObject index, IRubyObject value) {
82          if (! (index instanceof RubyInteger)) {
83             throw getRuntime().newTypeError(index, getRuntime().getClass("Integer"));
84         }
85         int intIndex = (int) ((RubyInteger) index).getLongValue();
86         if (! (value instanceof JavaObject)) {
87             throw getRuntime().newTypeError("not a java object:" + value);
88         }
89         Object JavaDoc javaObject = ((JavaObject) value).getValue();
90         try {
91             Array.set(getValue(), intIndex, javaObject);
92         } catch (IndexOutOfBoundsException JavaDoc e) {
93             throw getRuntime().newArgumentError(
94                                     "index out of bounds for java array (" + intIndex +
95                                     " for length " + getLength() + ")");
96         } catch (ArrayStoreException JavaDoc e) {
97             throw getRuntime().newArgumentError(
98                                     "wrong element type " + javaObject.getClass() + "(array is " +
99                                     getValue().getClass() + ")");
100         }
101         return value;
102     }
103
104     public IRubyObject afill(IRubyObject beginIndex, IRubyObject endIndex, IRubyObject value) {
105         if (! (beginIndex instanceof RubyInteger)) {
106             throw getRuntime().newTypeError(beginIndex, getRuntime().getClass("Integer"));
107         }
108         int intIndex = (int) ((RubyInteger) beginIndex).getLongValue();
109         if (! (endIndex instanceof RubyInteger)) {
110             throw getRuntime().newTypeError(endIndex, getRuntime().getClass("Integer"));
111         }
112         int intEndIndex = (int) ((RubyInteger) endIndex).getLongValue();
113         if (! (value instanceof JavaObject)) {
114             throw getRuntime().newTypeError("not a java object:" + value);
115         }
116         Object JavaDoc javaObject = ((JavaObject) value).getValue();
117         Object JavaDoc self = getValue();
118         try {
119           for ( ; intIndex < intEndIndex; intIndex++) {
120             Array.set(self, intIndex, javaObject);
121           }
122         } catch (IndexOutOfBoundsException JavaDoc e) {
123             throw getRuntime().newArgumentError(
124                                     "index out of bounds for java array (" + intIndex +
125                                     " for length " + getLength() + ")");
126         } catch (ArrayStoreException JavaDoc e) {
127             throw getRuntime().newArgumentError(
128                                     "wrong element type " + javaObject.getClass() + "(array is " +
129                                     getValue().getClass() + ")");
130         }
131         return value;
132     }
133 }
134
Popular Tags