2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2011-2011 - DIGITEO - Calixte DENIZET
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
17 package org.scilab.modules.types;
19 import java.io.IOException;
20 import java.io.ObjectInput;
21 import java.io.ObjectOutput;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
28 * This class provides a representation on the Scilab MList datatype<br>
30 * This class is {@link java.io.Serializable} and any modification could impact
31 * load and store of data (Xcos files, Javasci saved data, etc...).<br>
35 * ScilabMList data = new ScilabMList();<BR>
36 * data.add(new ScilabString("hello"));<BR>
37 * data.add(new ScilabDouble(2));<BR>
40 * @see org.scilab.modules.javasci.Scilab
42 public class ScilabMList extends ArrayList<ScilabType> implements ScilabType {
44 private static final long serialVersionUID = 3224510024213901841L;
46 private static final int VERSION = 0;
48 private String varName = "";
51 * Construct an empty mlist.
53 * Note that the first element of this collection is the header used by
54 * Scilab to find each field name.
56 public ScilabMList() {
61 * Construct an empty mlist.
63 * Note that the first element of this collection is the header used by
64 * Scilab to find each field name.
65 * @param varName the variable name
67 public ScilabMList(String varName) {
69 this.varName = varName;
73 * Construct an mlist with a specified header.
76 * field names used by the accessors.
78 public ScilabMList(String[] types) {
80 String[][] typesData = new String[1][types.length];
82 add(new ScilabString(typesData));
86 * Construct a tlist containing the elements of the specified collection, in
87 * the order that they are returned by the specified collection's iterator.
90 * field names, used by the accessors.
92 * the collection whose elements are to be placed into this
95 public ScilabMList(String[] names, Collection <? extends ScilabType > c) {
98 String[][] namesData = new String[1][names.length];
100 add(new ScilabString(namesData));
106 * @param varName the variable name
107 * @param size the initial list size
109 public ScilabMList(String varName, int size) {
111 this.varName = varName;
118 public boolean isReference() {
126 public String getVarName() {
134 public boolean isSwaped() {
139 * Get a map between the fields name and the associated ScilabType objects
143 public Map<String, ScilabType> getMListFields() {
144 Map<String, ScilabType> map = new HashMap<String, ScilabType>();
149 ScilabString mat = (ScilabString) get(0);
154 String[] fields = mat.getData()[0];
155 for (int i = 1; i < fields.length; i++) {
157 map.put(fields[i], get(i));
159 map.put(fields[i], null);
167 * Get the mlist type or null if none
169 * @return the mlist type
171 public String getMListType() {
176 ScilabString mat = (ScilabString) get(0);
181 return mat.getData()[0][0];
185 * @return 1 when there is data on the list, 0 otherwise.
186 * @see org.scilab.modules.types.ScilabType#getHeight()
189 public int getHeight() {
197 * Return the type of Scilab
199 * @return the type of Scilab
203 public ScilabTypeEnum getType() {
204 return ScilabTypeEnum.sci_mlist;
208 * @return 1 when there is data on the list, 0 otherwise.
209 * @see org.scilab.modules.types.ScilabType#getWidth()
212 public int getWidth() {
220 * Get a serialized list; The format is the following: i) returned[0] is an
221 * array of integers containing the Scilab type (ScilabTypeEunm) of the
222 * different elements of the list. ii) returned[i] for i>=1 contains the
223 * serialized form of each items.
225 * @return a serialized ScilabList/
228 public Object[] getSerializedObject() {
229 int[] types = new int[size()];
230 Object[] items = new Object[types.length + 1];
232 for (int i = 0; i < types.length; i++) {
233 ScilabType var = get(i);
234 types[i] = var.getType().swigValue();
235 items[i + 1] = var.getSerializedObject();
243 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
244 int version = in.readInt();
247 int size = in.readInt();
248 ensureCapacity(size + 1);
249 ArrayList list = (ArrayList) this;
250 for (int i = 0; i < size; i++) {
251 list.add(in.readObject());
253 varName = (String) in.readObject();
256 throw new ClassNotFoundException("A class ScilabMList with a version " + version + " does not exists");
261 public void writeExternal(ObjectOutput out) throws IOException {
262 out.writeInt(VERSION);
263 out.writeInt(size());
264 for (Object var : (ArrayList) this) {
265 out.writeObject(var);
267 out.writeObject(varName);
271 * Display the representation in the Scilab language of the type<BR>
272 * Note that the representation can be copied/pasted straight into Scilab
274 * @return the pretty-printed data
275 * @see java.util.AbstractCollection#toString()
278 public String toString() {
280 StringBuffer result = new StringBuffer();
285 result.append("mlist(");
286 for (int i = 0; i < size(); i++) {
287 result.append(get(i));
288 if (i != size() - 1) {
295 return result.toString();