Fix some minor bugs, Update of the comments, 100 % test coverage, tests update and...
[scilab.git] / scilab / modules / types / src / java / org / scilab / modules / types / ScilabInteger.java
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2009-2009 - DIGITEO - Antoine ELIAS
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.Arrays;
16
17 /**
18  * This class provides a wrapping on the Scilab Integer datatype
19  */
20 public class ScilabInteger implements ScilabType {
21
22         private long[][] longData = null;
23         private short[][] shortData = null;
24         private int[][] intData = null;
25         private byte[][] byteData = null;
26         private ScilabIntegerTypeEnum type;
27         
28         /**
29          * Default constructor
30          */
31         public ScilabInteger() {
32
33         }
34
35         /**
36          * Constructor with values
37          * 
38          * @param data the values
39          * @param bUnsigned true, if the values are unsigned; false if they are signed.
40          */
41         public ScilabInteger(byte[][] data, boolean bUnsigned) {
42                 this.setData(data, bUnsigned);
43         }
44
45         /**
46          * Constructor with values
47          * 
48          * @param data the values
49          * @param bUnsigned true, if the values are unsigned; false if they are signed.
50          */
51         public ScilabInteger(short[][] data, boolean bUnsigned) {
52                 this.setData(data, bUnsigned);
53         }
54
55         /**
56          * Constructor with values
57          * 
58          * @param data the values
59          * @param bUnsigned true, if the values are unsigned; false if they are signed.
60          */
61         public ScilabInteger(int[][] data, boolean bUnsigned) {
62                 this.setData(data, bUnsigned);
63         }
64
65         /**
66          * Constructor with values
67          * 
68          * @param data the values
69          * @param bUnsigned true, if the values are unsigned; false if they are signed.
70          */
71         public ScilabInteger(long[][] data, boolean bUnsigned) {
72                 this.setData(data, bUnsigned);
73         }
74
75         /**
76          * Constructor with single signed value
77          * 
78          * @param value the unique value
79          */
80         public ScilabInteger(byte value) {
81                 this.byteData = new byte[1][1];
82                 this.byteData[0][0] = value;
83                 this.type = ScilabIntegerTypeEnum.sci_int8;
84         }
85
86         /**
87          * Constructor with single signed value
88          * 
89          * @param value the unique value
90          */
91         public ScilabInteger(short value) {
92                 this.shortData = new short[1][1];
93                 this.shortData[0][0] = value;
94                 this.type = ScilabIntegerTypeEnum.sci_int16;
95         }
96
97         /**
98          * Constructor with single signed value
99          * 
100          * @param value the unique value
101          */
102         public ScilabInteger(int value) {
103                 this.intData = new int[1][1];
104                 this.intData[0][0] = value;
105                 this.type = ScilabIntegerTypeEnum.sci_int32;
106         }
107
108         /**
109          * Constructor with single signed value
110          * 
111          * @param value the unique value
112          */
113         public ScilabInteger(long value) {
114                 this.longData = new long[1][1];
115                 this.longData[0][0] = value;
116                 this.type = ScilabIntegerTypeEnum.sci_int64;
117         }
118
119         /**
120          * Set the current values 
121          * 
122          * @param data the values to set
123          * @param bUnsigned true, if these values are unsigned; false otherwise.
124          */
125         public void setData(byte[][] data, boolean bUnsigned) {
126                 this.byteData = data;
127                 if (bUnsigned) {
128                         this.type = ScilabIntegerTypeEnum.sci_uint8;
129                 } else {
130                         this.type = ScilabIntegerTypeEnum.sci_int8;
131                 }
132         }
133
134         /**
135          * Set the current values 
136          * 
137          * @param data the values to set
138          * @param bUnsigned true, if these values are unsigned; false otherwise.
139          */
140         public void setData(short[][] data, boolean bUnsigned) {
141                 this.shortData = data;
142                 if (bUnsigned) {
143                         this.type = ScilabIntegerTypeEnum.sci_uint16;
144                 } else {
145                         this.type = ScilabIntegerTypeEnum.sci_int16;
146                 }
147         }
148
149         /**
150          * Set the current values 
151          * 
152          * @param data the values to set
153          * @param bUnsigned true, if these values are unsigned; false otherwise.
154          */
155         public void setData(int[][] data, boolean bUnsigned) {
156                 this.intData = data;
157                 if (bUnsigned) {
158                         this.type = ScilabIntegerTypeEnum.sci_uint32;
159                 } else {
160                         this.type = ScilabIntegerTypeEnum.sci_int32;
161                 }
162         }
163
164         /**
165          * Set the current values 
166          * 
167          * @param data the values to set
168          * @param bUnsigned true, if these values are unsigned; false otherwise.
169          */
170         public void setData(long[][] data, boolean bUnsigned) {
171                 this.longData = data;
172                 if (bUnsigned) {
173                         this.type = ScilabIntegerTypeEnum.sci_uint64;
174                 } else {
175                         this.type = ScilabIntegerTypeEnum.sci_int64;
176                 }
177         }
178
179         /**
180          * If the precision is not 64, all values will be converted to long 
181          * (attention, the convertion can be long)
182          * if precision is 64, just return the data
183          * @return the values
184          */
185         public long[][] getData() {
186                 long[][] convertedMatrix = new long[this.getHeight()][this.getWidth()];
187                 switch (this.getPrec()) {
188
189                         case sci_int8:
190                         case sci_uint8:
191                                 for (int i = 0; i < this.getHeight(); i++) {
192                                         for (int j = 0; j < this.getWidth(); j++) {
193                                                 convertedMatrix[i][j] = Long.valueOf(byteData[i][j]);
194                                         }
195                                 }
196                                 return convertedMatrix;
197                         case sci_int16:
198                         case sci_uint16:
199                                 for (int i = 0; i < this.getHeight(); i++) {
200                                         for (int j = 0; j < this.getWidth(); j++) {
201                                                 convertedMatrix[i][j] = Long.valueOf(shortData[i][j]);
202                                         }
203                                 }
204                                 return convertedMatrix;
205                         case sci_int32:
206                         case sci_uint32:
207                                 for (int i = 0; i < this.getHeight(); i++) {
208                                         for (int j = 0; j < this.getWidth(); j++) {
209                                                 convertedMatrix[i][j] = Long.valueOf(intData[i][j]);
210                                         }
211                                 }
212                                 return convertedMatrix;
213                         case sci_int64:
214                         case sci_uint64:
215                                 return longData;
216                 }
217                 return null;
218         }
219         
220
221         /**
222          * Returns the value as the form of short
223          * @return the values as short
224          */
225         public short[][] getDataAsShort() {
226                 return shortData;
227         }
228
229         /**
230          * Returns the value as the form of byte
231          * @return the values as byte
232          */
233         public byte[][] getDataAsByte() {
234                 return byteData;
235         }
236
237         /**
238          * Returns the value as the form of int
239          * @return the values as int
240          */
241         public int[][] getDataAsInt() {
242                 return intData;
243         }
244
245         /**
246          * Returns the value as the form of long
247          * Only for Scilab 6.X
248          * @return the values as long
249          */
250         public long[][] getDataAsLong() {
251                 return longData;
252         }
253
254         /**
255          * @return the precision of the values
256          */
257         public ScilabIntegerTypeEnum getPrec() {
258                 return type;
259         }
260
261         /**
262          * @return true, if the values are signed, false otherwise.
263          */
264         public boolean isUnsigned() {
265                 switch (type) {
266                         case sci_int8:
267                         case sci_int16:
268                         case sci_int32:
269                         case sci_int64:
270                                 return false;
271                         case sci_uint8:
272                         case sci_uint16:
273                         case sci_uint32:
274                         case sci_uint64:
275                                 return true;
276                 }
277                 return false;
278         }
279
280         /**
281          * Manage the old representation of IntegerType 
282          * @param typeName the typeName (TYPE8, TYPE16, TYPE32, TYPE64)
283          * @param unsigned unsigned or not
284          * @return the converted type to ScilabIntegerTypeEnum. null is cannot convert
285          */
286         public static ScilabIntegerTypeEnum convertOldType(String typeName, boolean unsigned) {
287                 
288                 if (typeName.equals("TYPE8")) {
289                         if (unsigned) {
290                                 return ScilabIntegerTypeEnum.sci_uint8;
291                         } else {
292                                 return ScilabIntegerTypeEnum.sci_int8;
293                         }
294                 }
295                 if (typeName.equals("TYPE16")) {
296                         if (unsigned) {
297                                 return ScilabIntegerTypeEnum.sci_uint16;
298                         } else {
299                                 return ScilabIntegerTypeEnum.sci_int16;
300                         }
301                 }
302                 if (typeName.equals("TYPE32")) {
303                         if (unsigned) {
304                                 return ScilabIntegerTypeEnum.sci_uint32;
305                         } else {
306                                 return ScilabIntegerTypeEnum.sci_int32;
307                         }
308                 }
309                 if (typeName.equals("TYPE64")) {
310                         if (unsigned) {
311                                 return ScilabIntegerTypeEnum.sci_uint64;
312                         } else {
313                                 return ScilabIntegerTypeEnum.sci_int64;
314                         }
315                 }
316                 return null;
317
318         }
319
320
321         /**
322          * @return the height of the value matrix
323          * @see org.scilab.modules.types.ScilabType#getHeight()
324          */
325         @Override
326         public int getHeight() {
327                 if (this.getPrec() == null) {
328                         return 0;
329                 }
330                 switch (this.getPrec()) {
331                         case sci_int8:
332                         case sci_uint8:
333                                 if (byteData == null) {
334                                         return 0;
335                                 }
336                                 return byteData.length;
337                         case sci_int16:
338                         case sci_uint16:
339                                 if (shortData == null) {
340                                         return 0;
341                                 }
342                                 return shortData.length;
343                         case sci_int32:
344                         case sci_uint32:
345                                 if (intData == null) {
346                                         return 0;
347                                 }
348                                 return intData.length;
349                         case sci_int64:
350                         case sci_uint64:
351                                 if (longData == null) {
352                                         return 0;
353                                 }
354                                 return longData.length;
355                         default:
356                                 return 0;
357                 }
358         }
359
360         /**
361          * @return the width of the value matrix
362          * @see org.scilab.modules.types.ScilabType#getWidth()
363          */
364         @Override
365         public int getWidth() {
366                 if (this.getPrec() == null) {
367                         return 0;
368                 }
369                 switch (this.getPrec()) {
370                         case sci_int8:
371                         case sci_uint8:
372                                 if (byteData == null) {
373                                         return 0;
374                                 }
375                                 return byteData[0].length;
376                         case sci_int16:
377                         case sci_uint16:
378                                 if (shortData == null) {
379                                         return 0;
380                                 }
381                                 return shortData[0].length;
382                         case sci_int32:
383                         case sci_uint32:
384                                 if (intData == null) {
385                                         return 0;
386                                 }
387                                 return intData[0].length;
388                         case sci_int64:
389                         case sci_uint64:
390                                 if (longData == null) {
391                                         return 0;
392                                 }
393                                 return longData[0].length;
394                         default:
395                                 return 0;
396                 }
397         }
398
399         // int32(X), int8(x) , int16([x,x,x;x,x,x])
400         //       uint32(X), uint8(x) , uint16([x,x,x;x,x,x])
401         
402         /**
403          * @return true, if there is no values; false otherwise.
404          */
405         public boolean isEmpty() {
406                 if (this.getPrec() == null) {
407                         return true;
408                 }
409                 switch (this.getPrec()) {
410                         case sci_int8:
411                         case sci_uint8:
412                                 return byteData == null;
413                         case sci_int16:
414                         case sci_uint16:
415                                 return shortData == null;
416                         case sci_int32:
417                         case sci_uint32:
418                                 return intData == null;
419                         case sci_int64:
420                         case sci_uint64:
421                                 return longData == null;
422                         default:
423                                 return true;
424                 }
425         }
426         
427         /**
428          * @see org.scilab.modules.types.ScilabType#equals(Object)
429          */
430         public boolean equals(Object obj) {
431                 if (obj instanceof ScilabInteger) {
432                         return Arrays.deepEquals(this.getData(), ((ScilabInteger)obj).getData());
433                 } else {
434                         return false;
435                 }
436         }
437
438         /**
439          * Display the representation in the Scilab language of the type
440          * @return the pretty-printed values 
441          * @see java.lang.Object#toString()
442          */
443         @Override
444         public String toString() {
445                 StringBuilder result = new StringBuilder();
446                 
447                 
448                 if (isEmpty()) {
449                         result.append("int([])");
450                         return result.toString();
451                 }
452                 
453                 if (isUnsigned()) {
454                         result.append("u");
455                 }
456                 result.append("int");
457                 
458                 switch (this.getPrec()) {
459                         case sci_int8:
460                         case sci_uint8:
461                                 result.append("8");
462                         break;
463
464                         case sci_int16:
465                         case sci_uint16:
466                                 result.append("16");
467                                 break;
468                         
469                         case sci_int32:
470                         case sci_uint32:
471                                 result.append("32");
472                                 break;
473                         
474                         case sci_int64:
475                         case sci_uint64:
476                                 result.append("64");
477                                 break;
478                         
479                         default:
480                                 break;
481                 }
482
483                 result.append("([");
484                 appendData(result);
485                 result.append("])");
486         
487                 return result.toString();
488         }
489
490         /**
491          * Put each value on the buffer.
492          * 
493          * @param result the current buffer
494          */
495         private void appendData(StringBuilder result) {
496                 for (int i = 0; i < getHeight(); ++i) {
497                         for (int j = 0; j < getWidth(); ++j) {
498
499                                 result.append(getData()[i][j]);
500                                 
501                                 
502                                 if (j != getWidth() - 1) {
503                                         result.append(", ");
504                                 }
505                         }
506                         if (i != getHeight() - 1) {
507                                 result.append(" ; ");
508                         }
509                 }
510         }
511         
512 }