KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > ast > util > ArgsUtil


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-2002 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.ast.util;
33
34 import org.jruby.Ruby;
35 import org.jruby.RubyArray;
36 import org.jruby.runtime.builtin.IRubyObject;
37
38 /**
39  *
40  * @author jpetersen
41  */

42 public final class ArgsUtil {
43     
44     public static IRubyObject[] convertToJavaArray(IRubyObject value) {
45         if (value == null) {
46             return IRubyObject.NULL_ARRAY;
47         }
48         
49         if (value instanceof RubyArray) {
50             return ((RubyArray)value).toJavaArrayMaybeUnsafe();
51         }
52         
53         return new IRubyObject[] { value };
54     }
55
56     /**
57      * This name may be a bit misleading, since this also attempts to coerce
58      * array behavior using to_ary.
59      *
60      * @param runtime The JRuby runtime
61      * @param value The value to convert
62      * @param coerce Whether to coerce using to_ary or just wrap with an array
63      */

64     public static RubyArray convertToRubyArray(Ruby runtime, IRubyObject value, boolean coerce) {
65         if (value == null) {
66             return runtime.newArray(0);
67         }
68         
69         if (!coerce) {
70             // don't attempt to coerce to array, just wrap and return
71
return runtime.newArray(value);
72         }
73         
74         // FIXME: I don't like this, but all consumers of this method do the same cast.
75
IRubyObject newValue = value.convertToType("Array", "to_ary", false);
76
77         if (newValue.isNil()) {
78             return runtime.newArray(value);
79         }
80         
81         // empirically it appears that to_ary coersions always return array or nil, so this
82
// should always be an array by now.
83
return (RubyArray)newValue;
84     }
85     
86     /**
87      * Remove first element from array
88      *
89      * @param array to have first element "popped" off
90      * @return all but first element of the supplied array
91      */

92     public static IRubyObject[] popArray(IRubyObject[] array) {
93         if (array == null || array.length == 0) {
94             return IRubyObject.NULL_ARRAY;
95         }
96         
97         IRubyObject[] newArray = new IRubyObject[array.length - 1];
98         System.arraycopy(array, 1, newArray, 0, array.length - 1);
99         
100         return newArray;
101     }
102 }
103
Popular Tags