]> gerrit.simantics Code Review - simantics/r.git/blob - bundles/org.simantics.r.scl/src/org/rosuda/REngine/REXPInteger.java
(refs #6833) Test RExp inheritance in SCL
[simantics/r.git] / bundles / org.simantics.r.scl / src / org / rosuda / REngine / REXPInteger.java
1 package org.rosuda.REngine;
2
3 /** REXPDouble represents a vector of integer values. */
4 public class REXPInteger extends REXPVector {
5         public int[] payload;
6         
7         /** NA integer value as defined in R. Unlike its real equivalent this one can be used in comparisons, although {@link #isNA(int) } is provided for consistency. */
8         public static final int NA = -2147483648;
9
10         public static boolean isNA(int value) {
11                 return (value == NA);
12         }
13         
14         /** create integer vector of the length 1 with the given value as its first (and only) element */
15         public REXPInteger(int load) {
16                 super();
17                 payload=new int[] { load };
18         }
19         
20         /** create integer vector with the payload specified by <code>load</code> */
21         public REXPInteger(int[] load) {
22                 super();
23                 payload=(load==null)?new int[0]:load;
24         }
25
26         /** create integer vector with the payload specified by <code>load</code> and attributes <code>attr</code> */
27         public REXPInteger(int[] load, REXPList attr) {
28                 super(attr);
29                 payload=(load==null)?new int[0]:load;
30         }
31
32         public Object asNativeJavaObject() {
33                 return payload;
34         }
35         
36         public int length() { return payload.length; }
37
38         public boolean isInteger() { return true; }
39         public boolean isNumeric() { return true; }
40
41         public int[] asIntegers() { return payload; }
42
43         /** returns the contents of this vector as doubles */
44         public double[] asDoubles() {
45                 double[] d = new double[payload.length];
46                 int i = 0;
47                 while (i < payload.length) { d[i] = (double) payload[i]; i++; }
48                 return d;
49         }
50
51         /** returns the contents of this vector as strings */
52         public String[] asStrings() {
53                 String[] s = new String[payload.length];
54                 int i = 0;
55                 while (i < payload.length) { s[i] = ""+payload[i]; i++; }
56                 return s;
57         }
58         
59         public boolean[] isNA() {
60                 boolean a[] = new boolean[payload.length];
61                 int i = 0;
62                 while (i < a.length) { a[i] = (payload[i]==NA); i++; }
63                 return a;
64         }
65         
66         public String toDebugString() {
67                 StringBuffer sb = new StringBuffer(super.toDebugString()+"{");
68                 int i = 0;
69                 while (i < payload.length && i < maxDebugItems) {
70                         if (i>0) sb.append(",");
71                         sb.append(payload[i]);
72                         i++;
73                 }
74                 if (i < payload.length) sb.append(",..");
75                 return sb.toString()+"}";
76         }       
77 }