File: REXPInteger.java

package info (click to toggle)
rjava 1.0-14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,188 kB
  • sloc: java: 13,223; ansic: 5,503; sh: 3,776; xml: 325; makefile: 250; perl: 33
file content (77 lines) | stat: -rw-r--r-- 2,159 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.rosuda.REngine;

/** REXPDouble represents a vector of integer values. */
public class REXPInteger extends REXPVector {
	protected int[] payload;
	
	/** 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. */
	public static final int NA = -2147483648;

	public static boolean isNA(int value) {
		return (value == NA);
	}
	
	/** create integer vector of the length 1 with the given value as its first (and only) element */
	public REXPInteger(int load) {
		super();
		payload=new int[] { load };
	}
	
	/** create integer vector with the payload specified by <code>load</code> */
	public REXPInteger(int[] load) {
		super();
		payload=(load==null)?new int[0]:load;
	}

	/** create integer vector with the payload specified by <code>load</code> and attributes <code>attr</code> */
	public REXPInteger(int[] load, REXPList attr) {
		super(attr);
		payload=(load==null)?new int[0]:load;
	}

	public Object asNativeJavaObject() {
		return payload;
	}
	
	public int length() { return payload.length; }

	public boolean isInteger() { return true; }
	public boolean isNumeric() { return true; }

	public int[] asIntegers() { return payload; }

	/** returns the contents of this vector as doubles */
	public double[] asDoubles() {
		double[] d = new double[payload.length];
		int i = 0;
		while (i < payload.length) { d[i] = (double) payload[i]; i++; }
		return d;
	}

	/** returns the contents of this vector as strings */
	public String[] asStrings() {
		String[] s = new String[payload.length];
		int i = 0;
		while (i < payload.length) { s[i] = ""+payload[i]; i++; }
		return s;
	}
	
	public boolean[] isNA() {
		boolean a[] = new boolean[payload.length];
		int i = 0;
		while (i < a.length) { a[i] = (payload[i]==NA); i++; }
		return a;
	}
	
	public String toDebugString() {
		StringBuffer sb = new StringBuffer(super.toDebugString()+"{");
		int i = 0;
		while (i < payload.length && i < maxDebugItems) {
			if (i>0) sb.append(",");
			sb.append(payload[i]);
			i++;
		}
		if (i < payload.length) sb.append(",..");
		return sb.toString()+"}";
	}	
}