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