]> gerrit.simantics Code Review - simantics/r.git/blob - org.simantics.r.scl/src/org/simantics/r/scl/variable/RNamedItemNode.java
617f2d484be87248b2726c029cc90159aeaff243
[simantics/r.git] / org.simantics.r.scl / src / org / simantics / r / scl / variable / RNamedItemNode.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 RNamedItemNode implements RVariableNode {\r
19 \r
20         public RNamedItemNode(RVariableNode parent, String name) {\r
21                 this.parent = parent;\r
22                 this.name = name;\r
23         }\r
24 \r
25         private RVariableNode parent;\r
26         private String name;\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(name);\r
43         }\r
44 \r
45         @Override\r
46         public String getName() {\r
47                 return RNodeManager.NAMED_ITEM_NAME_PREFIX + name;\r
48         }\r
49 \r
50         @Override\r
51         public RVariableNode getParent() {\r
52                 return parent;\r
53         }\r
54 }\r