1 /*
2  * Scilab (http://www.scilab.org/) - This file is part of Scilab
3  * Copyright (C) 2011 - DIGITEO - Calixte DENIZET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 
16 package org.scilab.modules.javasci;
17 
18 import java.util.HashMap;
19 import java.util.Map;
20 
21 import org.scilab.modules.types.GetScilabVariable;
22 import org.scilab.modules.types.ScilabVariables;
23 import org.scilab.modules.types.ScilabVariablesHandler;
24 import org.scilab.modules.types.ScilabType;
25 
26 /**
27  * Class to get Scilab data from a Java call
28  */
29 public final class ScilabVariablesJavasci implements ScilabVariablesHandler {
30 
31     private static final Map<Thread, ScilabType> map = new HashMap<Thread, ScilabType>();
32 
33     private static int id = -1;
34 
35     /**
36      * Constructor
37      */
ScilabVariablesJavasci()38     private ScilabVariablesJavasci() { }
39 
40     /**
41      * {@inheritDoc}
42      */
handle(ScilabType var)43     public void handle(ScilabType var) {
44         map.put(Thread.currentThread(), var);
45     }
46 
47     /**
48      * Get a Scilab variable with a given name
49      * @param name the variable name
50      * @param swapRowCol if true the returned data will be stored row by row
51      * @param byref if true the variable is passed by reference if it is possible
52      * @return the corresponding ScilabType object
53      */
getScilabVariable(String name, boolean swapRowCol, boolean byref)54     public static final ScilabType getScilabVariable(String name, boolean swapRowCol, boolean byref) {
55         if (id == -1) {
56             id = ScilabVariables.addScilabVariablesHandler(new ScilabVariablesJavasci());
57         }
58 
59         if (name != null && !name.isEmpty()) {
60             if (byref) {
61                 GetScilabVariable.getScilabVariableAsReference(name, id);
62             } else {
63                 GetScilabVariable.getScilabVariable(name, swapRowCol ? 1 : 0, id);
64             }
65             Thread t = Thread.currentThread();
66             ScilabType var = map.get(t);
67             map.remove(t);
68 
69             return var;
70         }
71 
72         return null;
73     }
74 
75     /**
76      * Get a Scilab variable with a given name
77      * @param name the variable name
78      * @param swapRowCol if true the returned data will be stored row by row
79      * @return the corresponding ScilabType object
80      */
getScilabVariable(String name, boolean swapRowCol)81     public static final ScilabType getScilabVariable(String name, boolean swapRowCol) {
82         return getScilabVariable(name, swapRowCol, false);
83     }
84 
85     /**
86      * Get a Scilab variable with a given name
87      * @param name the variable name
88      * @return the corresponding ScilabType object where the data are stored row by row
89      */
getScilabVariable(String name)90     public static final ScilabType getScilabVariable(String name) {
91         return getScilabVariable(name, true);
92     }
93 }
94