]> gerrit.simantics Code Review - simantics/r.git/blob - org.simantics.r.scl/src/org/rosuda/REngine/REXPList.java
004a78b1c0706d0a3c603a66aef0bb8b25e2f957
[simantics/r.git] / org.simantics.r.scl / src / org / rosuda / REngine / REXPList.java
1 package org.rosuda.REngine;
2
3 /** Represents a pairlist in R. Unlike the actual internal R implementation this one
4     does not use CAR/CDR/TAG linked representation but a @link{RList} object. */
5 public class REXPList extends REXPVector {
6         public RList payload;
7         
8         /* create a new pairlist with the contents of a named R list and no attributes.
9            @param list named list with the contents */
10         public REXPList(RList list) {
11                 super();
12                 payload=(list==null)?new RList():list;
13         }
14
15         /* create a new pairlist with the contents of a named R list and attributes.
16            @param list named list with the contents
17            @param attr attributes */
18         public REXPList(RList list, REXPList attr) {
19                 super(attr);
20                 payload=(list==null)?new RList():list;
21         }
22
23         /* create a pairlist containing just one pair comprising of one value and one name.
24            This is a convenience constructor most commonly used to create attribute pairlists.
25            @param value of the element in the pairlist (must not be <code>null</code>)
26            @param name of the element in the pairlist (must not be <code>null</code>) */
27         public REXPList(REXP value, String name) {
28                 super();
29                 payload = new RList(new REXP[] { value }, new String[] { name });
30         }
31
32         public Object asNativeJavaObject() throws REXPMismatchException {
33                 // since REXPGenericVector does the hard work, we just cheat and use it in turn
34                 REXPGenericVector v = new REXPGenericVector(payload);
35                 return v.asNativeJavaObject();
36         }
37
38         public int length() { return payload.size(); }
39
40         public boolean isList() { return true; }
41         public boolean isPairList() { return true; }
42
43         public boolean isRecursive() { return true; }
44
45         public RList asList() { return payload; }
46         
47         public String toString() {
48                 return super.toString()+(asList().isNamed()?"named":"");
49         }
50         
51         public String toDebugString() {
52                 StringBuffer sb = new StringBuffer(super.toDebugString()+"{");
53                 int i = 0;
54                 while (i < payload.size() && i < maxDebugItems) {
55                         if (i>0) sb.append(",\n");
56                         String name = payload.keyAt(i);
57                         if (name!=null) sb.append(name+"=");
58                         sb.append(payload.at(i).toDebugString());
59                         i++;
60                 }
61                 if (i < payload.size()) sb.append(",..");
62                 return sb.toString()+"}";
63         }
64 }