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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
package org.rosuda.REngine;
import java.util.Vector;
import java.util.HashMap;
/** REXPGenericVector represents a generic vector in R. Its elements can be typically of any {@link REXP} type. */
public class REXPGenericVector extends REXPVector {
/** payload */
private RList payload;
/** creates a new generic vector from a list. If the list is named, the <code>"names"</code> attribute is created automatically from it.
* @param list list to create the vector from */
public REXPGenericVector(RList list) {
super();
payload=(list==null)?new RList():list;
// automatically generate 'names' attribute
if (payload.isNamed())
attr = new REXPList(
new RList(new REXP[] { new REXPString(payload.keys()) },
new String[] { "names" }));
}
/** creates a new generic vector from a list. Note that the names in the list are ignored as they are expected to be defined by the attributes parameter.
* @param list list to create the vector from (names are ignored - use {@link #REXPGenericVector(RList)} or the <code>"names"</code> attribute for named lists
* @param attr attributes */
public REXPGenericVector(RList list, REXPList attr) {
super(attr);
payload=(list==null)?new RList():list;
}
/* generic vectors are converted either to a <code>Map</code> (if it is a named vector and there are no duplicate names) or a <code>Vector</code>. The contained elements are converted using <code>asNativeJavaObject</code> recursively. */
public Object asNativeJavaObject() throws REXPMismatchException {
// we need to convert the inside as well
int n = payload.size();
// named list -> map but only if
// a) all names are present
// b) there are no duplicates in the names
if (payload.isNamed()) {
String[] names = payload.keys();
if (names.length == n) {
HashMap map = new HashMap();
boolean valid = true;
for (int i = 0; i < n; i++) {
if (map.containsKey(names[i])) {
valid = false;
break;
}
Object value = payload.elementAt(i);
if (value != null)
value = ((REXP) value).asNativeJavaObject();
map.put(names[i], value);
}
if (valid)
return map;
}
}
// otherwise drop names and use just a vector
Vector v = new Vector();
for (int i = 0; i < n; i++) {
Object value = payload.elementAt(i);
if (value != null)
value = ((REXP) value).asNativeJavaObject();
v.addElement(value);
}
return v;
}
public int length() { return payload.size(); }
public boolean isList() { return true; }
public boolean isRecursive() { return true; }
public RList asList() { return payload; }
public String toString() {
return super.toString()+(asList().isNamed()?"named":"");
}
public String toDebugString() {
StringBuffer sb = new StringBuffer(super.toDebugString()+"{");
int i = 0;
while (i < payload.size() && i < maxDebugItems) {
if (i>0) sb.append(",\n");
sb.append(payload.at(i).toDebugString());
i++;
}
if (i < payload.size()) sb.append(",..");
return sb.toString()+"}";
}
}
|