]> gerrit.simantics Code Review - simantics/r.git/blob - bundles/org.simantics.r.scl/src/org/rosuda/REngine/REXPLogical.java
Restructured R repository for Tycho POMless builds.
[simantics/r.git] / bundles / org.simantics.r.scl / src / org / rosuda / REngine / REXPLogical.java
1 package org.rosuda.REngine;
2
3 /** REXPLogical represents a vector of logical values (TRUE, FALSE or NA). Unlike Java's boolean type R's logicals support NA values therefore either of {@link #isTRUE()}, {@link #isFALSE()} or {@link #isNA()} must be used to convert logicals to boolean values. */
4 public class REXPLogical extends REXPVector {
5         protected byte[] 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         static final int NA_internal = -2147483648;
9
10         /** NA boolean value as used in REXPLogical implementation. This differs from the value used in R since R uses int data type and we use byte. Unlike its real equivalent this one can be used in comparisons, although {@link #isNA(byte) } is provided for consistency. */
11         public static final byte NA = -128;
12         public static final byte TRUE = 1;
13         public static final byte FALSE = 0;
14         
15         public static boolean isNA(byte value) {
16                 return (value == NA);
17         }
18         
19         /** create logical vector of the length 1 with the given value as its first (and only) element */
20         public REXPLogical(boolean load) {
21                 super();
22                 payload = new byte[] { load ? TRUE : FALSE };
23         }
24
25         /** create logical vector of the length 1 with the given value as its first (and only) element */
26         public REXPLogical(byte load) {
27                 super();
28                 payload = new byte[] { load };
29         }
30         
31         /** create logical vector with the payload specified by <code>load</code> */
32         public REXPLogical(byte[] load) {
33                 super();
34                 payload = (load==null) ? new byte[0]:load;
35         }
36
37         /** create logical vector with the payload specified by <code>load</code> */
38         public REXPLogical(boolean[] load) {
39                 super();
40                 payload = new byte[(load == null) ? 0 : load.length];
41                 if (load != null)
42                         for (int i = 0; i < load.length; i++)
43                                 payload[i] = load[i] ? TRUE : FALSE;
44         }
45         
46         /** create integer vector with the payload specified by <code>load</code> and attributes <code>attr</code> */
47         public REXPLogical(byte[] load, REXPList attr) {
48                 super(attr);
49                 payload = (load==null) ? new byte[0] : load;
50         }
51         
52         /** create integer vector with the payload specified by <code>load</code> and attributes <code>attr</code> */
53         public REXPLogical(boolean[] load, REXPList attr) {
54                 super(attr);
55                 payload = new byte[(load == null) ? 0 : load.length];
56                 if (load != null)
57                         for (int i = 0; i < load.length; i++)
58                                 payload[i] = load[i] ? TRUE : FALSE;
59         }
60
61         public int length() { return payload.length; }
62
63         public boolean isLogical() { return true; }
64
65         public Object asNativeJavaObject() {
66                 return payload;
67         }
68
69         public int[] asIntegers() {
70                 int p[] = new int[payload.length];
71                 for (int i = 0; i < payload.length; i++) // map bytes to integers including NA representation
72                         p[i] = (payload[i] == NA) ? REXPInteger.NA : ((payload[i] == FALSE) ? 0 : 1);
73                 return p;
74         }
75
76         public byte[] asBytes() { return payload; }
77
78         /** returns the contents of this vector as doubles */
79         public double[] asDoubles() {
80                 double[] d = new double[payload.length];
81                 for (int i = 0; i < payload.length; i++)
82                         d[i] = (payload[i] == NA) ? REXPDouble.NA : ((payload[i] == FALSE) ? 0.0 : 1.0);
83                 return d;
84         }
85
86         /** returns the contents of this vector as strings */
87         public String[] asStrings() {
88                 String[] s = new String[payload.length];
89                 for (int i = 0; i < payload.length; i++)
90                         s[i] = (payload[i] == NA) ? "NA" : ((payload[i] == FALSE) ? "FALSE" : "TRUE");
91                 return s;
92         }
93         
94         public boolean[] isNA() {
95                 boolean a[] = new boolean[payload.length];
96                 int i = 0;
97                 while (i < a.length) { a[i] = (payload[i] == NA); i++; }
98                 return a;
99         }
100         
101         /** returns a boolean array of the same langth as the receiver with <code>true</code> for <code>TRUE</code> values and <code>false</code> for <code>FALSE</code> and <code>NA</code> values.
102          @return boolean array */
103         public boolean[] isTRUE() {
104                 boolean a[] = new boolean[payload.length];
105                 int i = 0;
106                 while (i < a.length) { a[i] = (payload[i] != NA && payload[i] != FALSE); i++; }
107                 return a;
108         }
109         
110         /** returns a boolean array of the same langth as the receiver with <code>true</code> for <code>FALSE</code> values and <code>false</code> for <code>TRUE</code> and <code>NA</code> values.
111          @return boolean array */
112         public boolean[] isFALSE() {
113                 boolean a[] = new boolean[payload.length];
114                 int i = 0;
115                 while (i < a.length) { a[i] = (payload[i] == FALSE); i++; }
116                 return a;
117         }
118         
119         /** returns a boolean array of the same langth as the receiver with <code>true</code> for <code>TRUE</code> values and <code>false</code> for <code>FALSE</code> and <code>NA</code> values.
120          @return boolean array
121          @deprecated replaced by {@link #isTRUE()} for consistency with R nomenclature. */
122         public boolean[] isTrue() { return isTRUE(); }
123
124         /** returns a boolean array of the same langth as the receiver with <code>true</code> for <code>FALSE</code> values and <code>false</code> for <code>TRUE</code> and <code>NA</code> values.
125          @return boolean array
126          @deprecated replaced by {@link #isTRUE()} for consistency with R nomenclature. */
127         public boolean[] isFalse() { return isFALSE(); }
128
129         public String toDebugString() {
130                 StringBuffer sb = new StringBuffer(super.toDebugString()+"{");
131                 int i = 0;
132                 while (i < payload.length && i < maxDebugItems) {
133                         if (i>0) sb.append(",");
134                         sb.append((payload[i] == NA) ? "NA" : ((payload[i] == FALSE) ? "FALSE" : "TRUE"));
135                         i++;
136                 }
137                 if (i < payload.length) sb.append(",..");
138                 return sb.toString()+"}";
139         }
140 }