]> gerrit.simantics Code Review - simantics/r.git/blob - bundles/org.simantics.r.scl/src/org/simantics/r/scl/variable/RListItemNode.java
(refs #6833) Test RExp inheritance in SCL
[simantics/r.git] / bundles / org.simantics.r.scl / src / org / simantics / r / scl / variable / RListItemNode.java
1 /*******************************************************************************\r
2  * Copyright (c) 2014, 2016 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.r.scl.variable;\r
13 \r
14 import org.rosuda.REngine.REXP;\r
15 import org.rosuda.REngine.REXPMismatchException;\r
16 import org.rosuda.REngine.RList;\r
17 \r
18 public class RListItemNode implements RVariableNode {\r
19 \r
20         private RVariableNode parent;\r
21         private int index;\r
22         \r
23         public RListItemNode(RVariableNode parent, int index) {\r
24                 this.index = index;\r
25                 this.parent = parent;\r
26         }\r
27 \r
28         @Override\r
29         public REXP getValue() {\r
30                 REXP parentValue = parent.getValue();\r
31                 if (parentValue == null || ! parentValue.isList())\r
32                         return null;\r
33                 \r
34                 RList list;\r
35                 try {\r
36                         list = parentValue.asList();\r
37                 } catch (REXPMismatchException e) {\r
38                         // Should never happen\r
39                         return null;\r
40                 }\r
41                 \r
42                 return list.at(index);\r
43         }\r
44 \r
45         @Override\r
46         public String getName() {\r
47                 return RNodeManager.INDEXED_ITEM_NAME_PREFIX + Integer.toString(index);\r
48         }\r
49 \r
50         @Override\r
51         public RVariableNode getParent() {\r
52                 return parent;\r
53         }\r
54 }\r