1 package org.rosuda.REngine;
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;
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;
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;
15 public static boolean isNA(byte value) {
19 /** create logical vector of the length 1 with the given value as its first (and only) element */
20 public REXPLogical(boolean load) {
22 payload = new byte[] { load ? TRUE : FALSE };
25 /** create logical vector of the length 1 with the given value as its first (and only) element */
26 public REXPLogical(byte load) {
28 payload = new byte[] { load };
31 /** create logical vector with the payload specified by <code>load</code> */
32 public REXPLogical(byte[] load) {
34 payload = (load==null) ? new byte[0]:load;
37 /** create logical vector with the payload specified by <code>load</code> */
38 public REXPLogical(boolean[] load) {
40 payload = new byte[(load == null) ? 0 : load.length];
42 for (int i = 0; i < load.length; i++)
43 payload[i] = load[i] ? TRUE : FALSE;
46 /** create integer vector with the payload specified by <code>load</code> and attributes <code>attr</code> */
47 public REXPLogical(byte[] load, REXPList attr) {
49 payload = (load==null) ? new byte[0] : load;
52 /** create integer vector with the payload specified by <code>load</code> and attributes <code>attr</code> */
53 public REXPLogical(boolean[] load, REXPList attr) {
55 payload = new byte[(load == null) ? 0 : load.length];
57 for (int i = 0; i < load.length; i++)
58 payload[i] = load[i] ? TRUE : FALSE;
61 public int length() { return payload.length; }
63 public boolean isLogical() { return true; }
65 public Object asNativeJavaObject() {
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);
76 public byte[] asBytes() { return payload; }
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);
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");
94 public boolean[] isNA() {
95 boolean a[] = new boolean[payload.length];
97 while (i < a.length) { a[i] = (payload[i] == NA); i++; }
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];
106 while (i < a.length) { a[i] = (payload[i] != NA && payload[i] != FALSE); i++; }
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];
115 while (i < a.length) { a[i] = (payload[i] == FALSE); i++; }
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(); }
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(); }
129 public String toDebugString() {
130 StringBuffer sb = new StringBuffer(super.toDebugString()+"{");
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"));
137 if (i < payload.length) sb.append(",..");
138 return sb.toString()+"}";