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