Fix some problems in the output of List, MList & TList
[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 wrapping on the Scilab TList datatype
20  */
21 public class ScilabTList extends ArrayList<ScilabType> implements ScilabType {
22
23         /**
24          * Construct an empty tlist.
25          * 
26          * Note that the first element of this collection is the header used by
27          * Scilab to find each field type.
28          */
29         public ScilabTList() {
30                 super();
31         }
32         
33         /**
34          * Construct a tlist with a specified header.
35          *  
36          * @param types type names of the fields.
37          */
38         public ScilabTList(String []types) {
39                 super();
40                 String [][] typesData = new String[1][types.length];
41                 typesData[0] = types;
42                 add(new ScilabString(typesData));
43         }
44
45         /**
46          * Construct a tlist containing the elements of the specified collection, in
47          * the order that they are returned by the specified collection's iterator.
48          * 
49          * @param types
50          *                      type names of the fields.
51          * @param c
52          *                      the collection whose elements are to be placed into this
53          *                      tlist.
54          */
55         public ScilabTList(String[] types, Collection< ? extends ScilabType> c) {
56                 super(c.size() + 1);
57                 
58                 String[][] typesData = new String[1][types.length];
59                 typesData[0] = types;
60                 add(new ScilabString(typesData));
61                 
62                 addAll(c);
63         }
64         
65         /**
66          * @return 1 when there is data on the list, 0 otherwise.
67          * @see org.scilab.modules.types.ScilabType#getHeight()
68          */
69         public int getHeight() {
70                 if (isEmpty()) {
71                         return 0;
72                 }
73                 return 1;
74         }
75
76         /**
77          * @return 1 when there is data on the list, 0 otherwise.
78          * @see org.scilab.modules.types.ScilabType#getWidth()
79          */
80         public int getWidth() {
81                 if (isEmpty()) {
82                         return 0;
83                 }
84                 return size();
85         }
86         
87         /**
88          * Display the representation in the Scilab language of the type
89          * @return the pretty-printed data
90          * @see java.util.AbstractCollection#toString()
91          */
92         public String toString() {
93                 
94                 StringBuffer result = new StringBuffer();
95                 if (isEmpty()) {
96                         result.append("tlist()");
97                         return result.toString();
98                 }
99
100                 result.append("tlist(");
101                 for (int i = 0; i < size(); i++) {
102                         result.append(get(i).toString());
103                         if (i != size() - 1) {
104                                 result.append(", ");
105                         }
106                         
107                 }
108                 result.append(")");
109         
110                 return result.toString();
111         }
112 }