2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2009 - DIGITEO - Antoine ELIAS
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
13 package org.scilab.modules.types;
15 import java.util.Arrays;
18 * This class provides a representation on the Scilab Integer datatype
22 * byte [][]a={{32,42,41}, {12,13,32}};<br />
23 * ScilabInteger aMatrix = new ScilabInteger(a, true); // true = unsigned
25 * @see org.scilab.modules.javasci.Scilab
27 public class ScilabInteger implements ScilabType {
29 private static final long serialVersionUID = 1759633801332932450L;
31 private long[][] longData = null;
32 private short[][] shortData = null;
33 private int[][] intData = null;
34 private byte[][] byteData = null;
35 private ScilabIntegerTypeEnum type;
40 public ScilabInteger() {
45 * Constructor with values
47 * @param data the values
48 * @param bUnsigned true, if the values are unsigned; false if they are signed.
50 public ScilabInteger(byte[][] data, boolean bUnsigned) {
51 this.setData(data, bUnsigned);
55 * Constructor with values
57 * @param data the values
58 * @param bUnsigned true, if the values are unsigned; false if they are signed.
60 public ScilabInteger(short[][] data, boolean bUnsigned) {
61 this.setData(data, bUnsigned);
65 * Constructor with values
67 * @param data the values
68 * @param bUnsigned true, if the values are unsigned; false if they are signed.
70 public ScilabInteger(int[][] data, boolean bUnsigned) {
71 this.setData(data, bUnsigned);
75 * Constructor with values
77 * @param data the values
78 * @param bUnsigned true, if the values are unsigned; false if they are signed.
80 public ScilabInteger(long[][] data, boolean bUnsigned) {
81 this.setData(data, bUnsigned);
85 * Constructor with single signed value
87 * @param value the unique value
89 public ScilabInteger(byte value) {
90 this.byteData = new byte[1][1];
91 this.byteData[0][0] = value;
92 this.type = ScilabIntegerTypeEnum.sci_int8;
96 * Constructor with single signed value
98 * @param value the unique value
100 public ScilabInteger(short value) {
101 this.shortData = new short[1][1];
102 this.shortData[0][0] = value;
103 this.type = ScilabIntegerTypeEnum.sci_int16;
107 * Constructor with single signed value
109 * @param value the unique value
111 public ScilabInteger(int value) {
112 this.intData = new int[1][1];
113 this.intData[0][0] = value;
114 this.type = ScilabIntegerTypeEnum.sci_int32;
118 * Constructor with single signed value
120 * @param value the unique value
122 public ScilabInteger(long value) {
123 this.longData = new long[1][1];
124 this.longData[0][0] = value;
125 this.type = ScilabIntegerTypeEnum.sci_int64;
129 * Set the current values
131 * @param data the values to set
132 * @param bUnsigned true, if these values are unsigned; false otherwise.
134 public void setData(byte[][] data, boolean bUnsigned) {
135 this.byteData = data;
137 this.type = ScilabIntegerTypeEnum.sci_uint8;
139 this.type = ScilabIntegerTypeEnum.sci_int8;
144 * Set the current values
146 * @param data the values to set
147 * @param bUnsigned true, if these values are unsigned; false otherwise.
149 public void setData(short[][] data, boolean bUnsigned) {
150 this.shortData = data;
152 this.type = ScilabIntegerTypeEnum.sci_uint16;
154 this.type = ScilabIntegerTypeEnum.sci_int16;
159 * Set the current values
161 * @param data the values to set
162 * @param bUnsigned true, if these values are unsigned; false otherwise.
164 public void setData(int[][] data, boolean bUnsigned) {
167 this.type = ScilabIntegerTypeEnum.sci_uint32;
169 this.type = ScilabIntegerTypeEnum.sci_int32;
174 * Set the current values
176 * @param data the values to set
177 * @param bUnsigned true, if these values are unsigned; false otherwise.
179 public void setData(long[][] data, boolean bUnsigned) {
180 this.longData = data;
182 this.type = ScilabIntegerTypeEnum.sci_uint64;
184 this.type = ScilabIntegerTypeEnum.sci_int64;
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
194 public long[][] getData() {
195 long[][] convertedMatrix = new long[this.getHeight()][this.getWidth()];
196 switch (this.getPrec()) {
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]);
205 return convertedMatrix;
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]);
213 return convertedMatrix;
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]);
221 return convertedMatrix;
231 * Returns the value as the form of short
232 * @return the values as short
234 public short[][] getDataAsShort() {
239 * Returns the value as the form of byte
240 * @return the values as byte
242 public byte[][] getDataAsByte() {
247 * Returns the value as the form of int
248 * @return the values as int
250 public int[][] getDataAsInt() {
255 * Returns the value as the form of long
256 * Only for Scilab 6.X
257 * @return the values as long
259 public long[][] getDataAsLong() {
264 * @return the precision of the values
266 public ScilabIntegerTypeEnum getPrec() {
271 * @return true, if the values are signed, false otherwise.
273 public boolean isUnsigned() {
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
295 public static ScilabIntegerTypeEnum convertOldType(String typeName, boolean unsigned) {
297 if (typeName.equals("TYPE8")) {
299 return ScilabIntegerTypeEnum.sci_uint8;
301 return ScilabIntegerTypeEnum.sci_int8;
304 if (typeName.equals("TYPE16")) {
306 return ScilabIntegerTypeEnum.sci_uint16;
308 return ScilabIntegerTypeEnum.sci_int16;
311 if (typeName.equals("TYPE32")) {
313 return ScilabIntegerTypeEnum.sci_uint32;
315 return ScilabIntegerTypeEnum.sci_int32;
318 if (typeName.equals("TYPE64")) {
320 return ScilabIntegerTypeEnum.sci_uint64;
322 return ScilabIntegerTypeEnum.sci_int64;
331 * @return the height of the value matrix
332 * @see org.scilab.modules.types.ScilabType#getHeight()
335 public int getHeight() {
336 if (this.getPrec() == null) {
339 switch (this.getPrec()) {
342 if (byteData == null) {
345 return byteData.length;
348 if (shortData == null) {
351 return shortData.length;
354 if (intData == null) {
357 return intData.length;
360 if (longData == null) {
363 return longData.length;
370 * @return the width of the value matrix
371 * @see org.scilab.modules.types.ScilabType#getWidth()
374 public int getWidth() {
375 if (this.getPrec() == null) {
378 switch (this.getPrec()) {
381 if (byteData == null) {
384 return byteData[0].length;
387 if (shortData == null) {
390 return shortData[0].length;
393 if (intData == null) {
396 return intData[0].length;
399 if (longData == null) {
402 return longData[0].length;
408 // int32(X), int8(x) , int16([x,x,x;x,x,x])
409 // uint32(X), uint8(x) , uint16([x,x,x;x,x,x])
412 * @return true, if there is no values; false otherwise.
414 public boolean isEmpty() {
415 if (this.getPrec() == null) {
418 switch (this.getPrec()) {
421 return byteData == null;
424 return shortData == null;
427 return intData == null;
430 return longData == null;
437 * @see org.scilab.modules.types.ScilabType#equals(Object)
439 public boolean equals(Object obj) {
440 if (obj instanceof ScilabInteger) {
441 return Arrays.deepEquals(this.getData(), ((ScilabInteger)obj).getData());
448 * Display the representation in the Scilab language of the type<br />
449 * Note that the representation can be copied/pasted straight into Scilab
451 * @return the pretty-printed values
452 * @see java.lang.Object#toString()
455 public String toString() {
456 StringBuilder result = new StringBuilder();
460 result.append("int([])");
461 return result.toString();
467 result.append("int");
469 switch (this.getPrec()) {
498 return result.toString();
502 * Put each value on the buffer.
504 * @param result the current buffer
506 private void appendData(StringBuilder result) {
507 for (int i = 0; i < getHeight(); ++i) {
508 for (int j = 0; j < getWidth(); ++j) {
510 result.append(getData()[i][j]);
513 if (j != getWidth() - 1) {
517 if (i != getHeight() - 1) {
518 result.append(" ; ");