60d767a268d7d3b659e972474bd9bda01a000db1
[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         private static final long serialVersionUID = 8080160982092586620L;
32
33         /**
34          * Construct an empty tlist.
35          * 
36          * Note that the first element of this collection is the header used by
37          * Scilab to find each field type.
38          */
39         public ScilabTList() {
40                 super();
41         }
42         
43         /**
44          * Construct a tlist with a specified header.
45          *  
46          * @param types type names of the fields.
47          */
48         public ScilabTList(String []types) {
49                 super();
50                 String [][] typesData = new String[1][types.length];
51                 typesData[0] = types;
52                 add(new ScilabString(typesData));
53         }
54
55         /**
56          * Construct a tlist containing the elements of the specified collection, in
57          * the order that they are returned by the specified collection's iterator.
58          * 
59          * @param types
60          *                      type names of the fields.
61          * @param c
62          *                      the collection whose elements are to be placed into this
63          *                      tlist.
64          */
65         public ScilabTList(String[] types, Collection< ? extends ScilabType> c) {
66                 super(c.size() + 1);
67                 
68                 String[][] typesData = new String[1][types.length];
69                 typesData[0] = types;
70                 add(new ScilabString(typesData));
71                 
72                 addAll(c);
73         }
74         
75         /**
76          * @return 1 when there is data on the list, 0 otherwise.
77          * @see org.scilab.modules.types.ScilabType#getHeight()
78          */
79         @Override
80         public int getHeight() {
81                 if (isEmpty()) {
82                         return 0;
83                 }
84                 return 1;
85         }
86
87         /**
88          * @return 1 when there is data on the list, 0 otherwise.
89          * @see org.scilab.modules.types.ScilabType#getWidth()
90          */
91         @Override
92         public int getWidth() {
93                 if (isEmpty()) {
94                         return 0;
95                 }
96                 return size();
97         }
98         
99         /**
100          * Display the representation in the Scilab language of the type<br />
101          * Note that the representation can be copied/pasted straight into Scilab 
102      * 
103          * @return the pretty-printed data
104          * @see java.util.AbstractCollection#toString()
105          */
106         @Override
107         public String toString() {
108                 
109                 StringBuffer result = new StringBuffer();
110                 if (isEmpty()) {
111                         result.append("tlist()");
112                         return result.toString();
113                 }
114
115                 result.append("tlist(");
116                 for (int i = 0; i < size(); i++) {
117                         result.append(get(i));
118                         if (i != size() - 1) {
119                                 result.append(", ");
120                         }
121                         
122                 }
123                 result.append(")");
124         
125                 return result.toString();
126         }
127 }