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