Types: protect the ScilabList.toString() method to any null data
[scilab.git] / scilab / modules / types / src / java / org / scilab / modules / types / ScilabTList.java
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET
4  * 
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  * 
11  */
12
13 package org.scilab.modules.types;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17
18 /**
19  * This class provides a representation on the Scilab TList datatype
20  * <br>
21  * Example:<br />
22  * <code>
23  * ScilabTList data = new ScilabTList();<br />
24  * data.add(new ScilabString("hello"));<br />
25  * data.add(new ScilabDouble(2));<br />
26  * </code>
27  * @see org.scilab.modules.javasci.Scilab
28  */
29 public class ScilabTList extends ArrayList<ScilabType> implements ScilabType {
30
31         /**
32          * Construct an empty tlist.
33          * 
34          * Note that the first element of this collection is the header used by
35          * Scilab to find each field type.
36          */
37         public ScilabTList() {
38                 super();
39         }
40         
41         /**
42          * Construct a tlist with a specified header.
43          *  
44          * @param types type names of the fields.
45          */
46         public ScilabTList(String []types) {
47                 super();
48                 String [][] typesData = new String[1][types.length];
49                 typesData[0] = types;
50                 add(new ScilabString(typesData));
51         }
52
53         /**
54          * Construct a tlist containing the elements of the specified collection, in
55          * the order that they are returned by the specified collection's iterator.
56          * 
57          * @param types
58          *                      type names of the fields.
59          * @param c
60          *                      the collection whose elements are to be placed into this
61          *                      tlist.
62          */
63         public ScilabTList(String[] types, Collection< ? extends ScilabType> c) {
64                 super(c.size() + 1);
65                 
66                 String[][] typesData = new String[1][types.length];
67                 typesData[0] = types;
68                 add(new ScilabString(typesData));
69                 
70                 addAll(c);
71         }
72         
73         /**
74          * @return 1 when there is data on the list, 0 otherwise.
75          * @see org.scilab.modules.types.ScilabType#getHeight()
76          */
77         @Override
78         public int getHeight() {
79                 if (isEmpty()) {
80                         return 0;
81                 }
82                 return 1;
83         }
84
85         /**
86          * @return 1 when there is data on the list, 0 otherwise.
87          * @see org.scilab.modules.types.ScilabType#getWidth()
88          */
89         @Override
90         public int getWidth() {
91                 if (isEmpty()) {
92                         return 0;
93                 }
94                 return size();
95         }
96         
97         /**
98          * Display the representation in the Scilab language of the type<br />
99          * Note that the representation can be copied/pasted straight into Scilab 
100      * 
101          * @return the pretty-printed data
102          * @see java.util.AbstractCollection#toString()
103          */
104         @Override
105         public String toString() {
106                 
107                 StringBuffer result = new StringBuffer();
108                 if (isEmpty()) {
109                         result.append("tlist()");
110                         return result.toString();
111                 }
112
113                 result.append("tlist(");
114                 for (int i = 0; i < size(); i++) {
115                         result.append(get(i));
116                         if (i != size() - 1) {
117                                 result.append(", ");
118                         }
119                         
120                 }
121                 result.append(")");
122         
123                 return result.toString();
124         }
125 }