1 package org.rosuda.REngine ;
4 * Utility class to wrap an Object into a REXP object.
6 * This facilitates wrapping native java objects and arrays
7 * into REXP objects that can be pushed to R
9 * @author Romain Francois <francoisromain@free.fr>
11 public class REXPWrapper {
14 private static Class byte_ARRAY ;
15 private static Class short_ARRAY ;
16 private static Class int_ARRAY ;
17 private static Class long_ARRAY ;
18 private static Class float_ARRAY ;
19 private static Class double_ARRAY ;
20 private static Class boolean_ARRAY ;
22 private static Class String_ARRAY ;
24 private static Class Byte_ARRAY ;
25 private static Class Short_ARRAY;
26 private static Class Integer_ARRAY ;
27 private static Class Long_ARRAY ;
28 private static Class Float_ARRAY ;
29 private static Class Double_ARRAY ;
30 private static Class Boolean_ARRAY ;
34 byte_ARRAY = Class.forName("[B") ;
35 short_ARRAY = Class.forName("[S" );
36 int_ARRAY = Class.forName("[I" );
37 long_ARRAY = (new long[1]).getClass() ; /* FIXME */
38 float_ARRAY = Class.forName("[F" ) ;
39 double_ARRAY = Class.forName("[D" );
40 boolean_ARRAY = Class.forName("[Z" ) ;
42 String_ARRAY = Class.forName( "[Ljava.lang.String;") ;
44 Byte_ARRAY = Class.forName( "[Ljava.lang.Byte;" ) ;
45 Short_ARRAY = Class.forName( "[Ljava.lang.Short;" ) ;
46 Integer_ARRAY = Class.forName( "[Ljava.lang.Integer;" ) ;
47 Long_ARRAY = Class.forName( "[Ljava.lang.Long;" ) ;
48 Float_ARRAY = Class.forName( "[Ljava.lang.Float;" ) ;
49 Double_ARRAY = Class.forName( "[Ljava.lang.Double;" ) ;
50 Boolean_ARRAY = Class.forName( "[Ljava.lang.Boolean;" ) ;
53 } catch( Exception e){
54 // should never happen
56 System.err.println( "problem while initiating the classes" ) ;
61 * Wraps an Object into a REXP
66 * <li>Byte (byte) : REXPRaw </li>
67 * <li>Short (short) : REXPInteger </li>
68 * <li>Integer (int) : REXPInteger </li>
69 * <li>Long (long) : REXPInteger</li>
70 * <li>Float (float) : REXPDouble</li>
71 * <li>Double (double) : REXPDouble </li>
72 * <li>Boolean (boolean) : REXPLogical</li>
74 * <li>String : REXPString </li>
75 * <li>String[] : REXPString </li>
77 * <li>byte[] or Byte[] : REXPRaw</li>
78 * <li>short[] or Short[] : REXPInteger</li>
79 * <li>int[] or Integer[] : REXPInteger</li>
80 * <li>long[] or Long[] : REXPInteger</li>
81 * <li>float[] or Float[] : REXPDouble</li>
82 * <li>double[] or Double[] : REXPDouble </li>
83 * <li>boolean[] or Boolean[]: REXPLogical</li>
85 * <li>null for anything else</li>
88 * @param o object to wrap
89 * @return REXP object that represents o or null if the conversion is not possible
91 public static REXP wrap( Object o ) {
93 /* nothing to do in that case */
94 if( o instanceof REXP){
98 Class clazz = o.getClass() ;
102 if( clazz == Byte.class ){
103 byte[] load = new byte[1];
104 load[0] = ((Byte)o).byteValue() ;
105 return new REXPRaw( load );
108 if( clazz == Short.class ){
109 return new REXPInteger( ((Short)o).intValue() ) ;
112 if( clazz == Integer.class ){
113 return new REXPInteger( ((Integer)o).intValue() ) ;
116 if( clazz == Long.class ){
117 return new REXPInteger( ((Long)o).intValue() ) ;
120 if( clazz == Float.class ){
121 return new REXPDouble( ((Float)o).doubleValue() ) ;
124 if( clazz == Double.class ){
125 return new REXPDouble( ((Double)o).doubleValue() ) ;
128 if( clazz == Boolean.class ){
129 return new REXPLogical( ((Boolean)o).booleanValue() ) ;
133 /* Strings -> REXPString */
135 if( clazz == String.class ){
136 return new REXPString( (String)o ) ;
139 if( clazz == String_ARRAY ){ /* String[] */
140 return new REXPString( (String[])o );
143 /* array of byte or Bytes -> REXPRaw */
145 if( clazz == byte_ARRAY ){ /* byte[] */
146 return new REXPRaw( (byte[])o ) ;
149 if( clazz == Byte_ARRAY ){ /* Byte[] */
150 Byte[] b = (Byte[])o;
152 byte[] bytes = new byte[b.length];
153 for( int i=0; i<n; i++){
154 bytes[i] = b[i].byteValue() ;
156 return new REXPRaw( bytes );
159 /* arrays of short or Short -> REXPInteger */
161 if( clazz == short_ARRAY ){ /* short[] */
162 short[] shorts = (short[])o ;
163 int[] ints = new int[ shorts.length ];
165 for( int i=0; i<n; i++ ){
168 return new REXPInteger( ints ) ;
171 if( clazz == Short_ARRAY ){ /* Short[] */
172 Short[] shorts = (Short[])o;
173 int n = shorts.length ;
174 int[] ints = new int[shorts.length];
175 for( int i=0; i<n; i++){
176 ints[i] = shorts[i].intValue() ;
178 return new REXPInteger( ints );
182 /* arrays of int or Integer -> REXPInteger */
184 if( clazz == int_ARRAY ){ /* int[] */
185 return new REXPInteger( (int[])o ) ;
188 if( clazz == Integer_ARRAY ){ /* Integer[] */
189 Integer[] integers = (Integer[])o;
190 int n = integers.length ;
191 int[] ints = new int[integers.length];
192 for( int i=0; i<n; i++){
193 ints[i] = integers[i].intValue() ;
195 return new REXPInteger( ints );
198 /* arrays of long or Long -> REXPInteger */
200 if( clazz == long_ARRAY ){ /* long[] */
201 long[] longs = (long[])o;
202 int n = longs.length ;
203 int[] ints = new int[longs.length];
204 for( int i=0; i<n; i++){
205 ints[i] = (int)longs[i] ;
207 return new REXPInteger( ints );
210 if( clazz == Long_ARRAY ){ /* Long[] */
211 Long[] longs = (Long[])o;
212 int n = longs.length ;
213 int[] ints = new int[longs.length];
214 for( int i=0; i<n; i++){
215 ints[i] = longs[i].intValue() ;
217 return new REXPInteger( ints );
220 /* float or Float arrays -> REXPDouble */
222 if( clazz == float_ARRAY ){ /* float[] */
223 float[] floats = (float[])o;
224 int n = floats.length ;
225 double[] doubles = new double[floats.length];
226 for( int i=0; i<n; i++){
227 doubles[i] = (double)floats[i] ;
229 return new REXPDouble( doubles );
232 if( clazz == Float_ARRAY ){ /* Float[] */
233 Float[] floats = (Float[])o;
234 int n = floats.length ;
235 double[] doubles = new double[floats.length];
236 for( int i=0; i<n; i++){
237 doubles[i] = floats[i].doubleValue() ;
239 return new REXPDouble( doubles );
243 /* double or Double arrays -> REXPDouble */
245 if(clazz == double_ARRAY ) { /* double[] */
246 return new REXPDouble( (double[])o ) ;
249 if( clazz == Double_ARRAY ){ /* Double[] */
250 Double[] doubles = (Double[])o;
251 double n = doubles.length ;
252 double[] d = new double[doubles.length];
253 for( int i=0; i<n; i++){
254 d[i] = doubles[i].doubleValue() ;
256 return new REXPDouble( d );
260 /* boolean arrays -> REXPLogical */
262 if( clazz == boolean_ARRAY ){ /* boolean[] */
263 return new REXPLogical( (boolean[])o ) ;
266 if( clazz == Boolean_ARRAY ){ /* Boolean[] */
267 Boolean[] booleans = (Boolean[])o;
268 int n = booleans.length ;
269 boolean[] b = new boolean[booleans.length];
270 for( int i=0; i<n; i++){
271 b[i] = booleans[i].booleanValue() ;
273 return new REXPLogical( b );
276 /* give up and return null */