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;
45 private static final ScilabTypeEnum type = ScilabTypeEnum.sci_mlist;
47 private static final int VERSION = 0;
49 private String varName = "";
52 * Construct an empty mlist.
54 * Note that the first element of this collection is the header used by
55 * Scilab to find each field name.
57 public ScilabMList() {
62 * Construct an empty mlist.
64 * Note that the first element of this collection is the header used by
65 * Scilab to find each field 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;
117 public boolean isReference() {
124 public String getVarName() {
131 public boolean isSwaped() {
136 * Get a map between the fields name and the associated ScilabType objects
140 public Map<String, ScilabType> getMListFields() {
141 Map<String, ScilabType> map = new HashMap<String, ScilabType>();
146 ScilabString mat = (ScilabString) get(0);
151 String[] fields = mat.getData()[0];
152 for (int i = 1; i < fields.length; i++) {
154 map.put(fields[i], get(i));
156 map.put(fields[i], null);
164 * Get the mlist type or null if none
166 * @return the mlist type
168 public String getMListType() {
173 ScilabString mat = (ScilabString) get(0);
178 return mat.getData()[0][0];
182 * @return 1 when there is data on the list, 0 otherwise.
183 * @see org.scilab.modules.types.ScilabType#getHeight()
186 public int getHeight() {
194 * Return the type of Scilab
196 * @return the type of Scilab
200 public ScilabTypeEnum getType() {
205 * @return 1 when there is data on the list, 0 otherwise.
206 * @see org.scilab.modules.types.ScilabType#getWidth()
209 public int getWidth() {
217 * Get a serialized list; The format is the following: i) returned[0] is an
218 * array of integers containing the Scilab type (ScilabTypeEunm) of the
219 * different elements of the list. ii) returned[i] for i>=1 contains the
220 * serialized form of each items.
222 * @return a serialized ScilabList/
224 public Object[] getSerializedObject() {
225 int[] types = new int[size()];
226 Object[] items = new Object[types.length + 1];
228 for (int i = 0; i < types.length; i++) {
229 ScilabType var = get(i);
230 types[i] = var.getType().swigValue();
231 items[i + 1] = var.getSerializedObject();
239 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
240 int version = in.readInt();
243 int size = in.readInt();
244 ensureCapacity(size + 1);
245 ArrayList list = (ArrayList) this;
246 for (int i = 0; i < size; i++) {
247 list.add(in.readObject());
249 varName = (String) in.readObject();
252 throw new ClassNotFoundException("A class ScilabMList with a version " + version + " does not exists");
257 public void writeExternal(ObjectOutput out) throws IOException {
258 out.writeInt(VERSION);
259 out.writeInt(size());
260 for (Object var : (ArrayList) this) {
261 out.writeObject(var);
263 out.writeObject(varName);
267 * Display the representation in the Scilab language of the type<BR>
268 * Note that the representation can be copied/pasted straight into Scilab
270 * @return the pretty-printed data
271 * @see java.util.AbstractCollection#toString()
274 public String toString() {
276 StringBuffer result = new StringBuffer();
281 result.append("mlist(");
282 for (int i = 0; i < size(); i++) {
283 result.append(get(i));
284 if (i != size() - 1) {
291 return result.toString();