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