2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2009 - DIGITEO - Antoine ELIAS
4 * Copyright (C) 2011-2011 - DIGITEO - Calixte DENIZET
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
17 package org.scilab.modules.types;
19 import java.io.IOException;
20 import java.io.ObjectInput;
21 import java.io.ObjectOutput;
22 import java.util.Arrays;
25 * This class provides a representation on the Scilab Integer datatype<br>
27 * This class is {@link java.io.Serializable} and any modification could impact
28 * load and store of data (Xcos files, Javasci saved data, etc...).<br>
32 * byte [][]a={{32,42,41}, {12,13,32}};<BR>
33 * ScilabInteger aMatrix = new ScilabInteger(a, true); // true = unsigned
36 * @see org.scilab.modules.javasci.Scilab
38 public class ScilabInteger implements ScilabType {
40 private static final long serialVersionUID = 1759633801332932450L;
41 private static final ScilabTypeEnum type = ScilabTypeEnum.sci_ints;
43 private static final int VERSION = 0;
45 protected long[][] longData;
46 protected short[][] shortData;
47 protected int[][] intData;
48 protected byte[][] byteData;
49 protected ScilabIntegerTypeEnum precision;
50 protected String varName;
51 protected boolean swaped;
52 transient protected boolean byref;
57 public ScilabInteger() { }
60 * Constructor with values
65 * true, if the values are unsigned; false if they are signed.
67 public ScilabInteger(byte[][] data, boolean bUnsigned) {
68 this.setData(data, bUnsigned);
72 * Constructor with values
77 * true, if the values are unsigned; false if they are signed.
79 public ScilabInteger(short[][] data, boolean bUnsigned) {
80 this.setData(data, bUnsigned);
84 * Constructor with values
89 * true, if the values are unsigned; false if they are signed.
91 public ScilabInteger(int[][] data, boolean bUnsigned) {
92 this.setData(data, bUnsigned);
96 * Constructor with values
101 * true, if the values are unsigned; false if they are signed.
103 public ScilabInteger(long[][] data, boolean bUnsigned) {
104 this.setData(data, bUnsigned);
108 * Constructor with values
113 * true, if the values are unsigned; false if they are signed.
115 public ScilabInteger(String varName, byte[][] data, boolean bUnsigned, boolean swaped) {
116 this.setData(data, bUnsigned);
117 this.varName = varName;
118 this.swaped = swaped;
122 * Constructor with values
127 * true, if the values are unsigned; false if they are signed.
129 public ScilabInteger(String varName, short[][] data, boolean bUnsigned, boolean swaped) {
130 this.setData(data, bUnsigned);
131 this.varName = varName;
132 this.swaped = swaped;
136 * Constructor with values
141 * true, if the values are unsigned; false if they are signed.
143 public ScilabInteger(String varName, int[][] data, boolean bUnsigned, boolean swaped) {
144 this.setData(data, bUnsigned);
145 this.varName = varName;
146 this.swaped = swaped;
150 * Constructor with values
155 * true, if the values are unsigned; false if they are signed.
157 public ScilabInteger(String varName, long[][] data, boolean bUnsigned, boolean swaped) {
158 this.setData(data, bUnsigned);
159 this.varName = varName;
160 this.swaped = swaped;
164 * Constructor with single signed value
169 public ScilabInteger(byte value) {
170 this.byteData = new byte[1][1];
171 this.byteData[0][0] = value;
172 this.precision = ScilabIntegerTypeEnum.sci_int8;
176 * Constructor with single signed value
181 public ScilabInteger(short value) {
182 this.shortData = new short[1][1];
183 this.shortData[0][0] = value;
184 this.precision = ScilabIntegerTypeEnum.sci_int16;
188 * Constructor with single signed value
193 public ScilabInteger(int value) {
194 this.intData = new int[1][1];
195 this.intData[0][0] = value;
196 this.precision = ScilabIntegerTypeEnum.sci_int32;
200 * Constructor with single signed value
205 public ScilabInteger(long value) {
206 this.longData = new long[1][1];
207 this.longData[0][0] = value;
208 this.precision = ScilabIntegerTypeEnum.sci_int64;
212 * Constructor with single signed value
217 public ScilabInteger(byte value, boolean bUnsigned) {
219 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint8 : ScilabIntegerTypeEnum.sci_int8;
223 * Constructor with single signed value
228 public ScilabInteger(short value, boolean bUnsigned) {
230 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint16 : ScilabIntegerTypeEnum.sci_int16;
234 * Constructor with single signed value
239 public ScilabInteger(int value, boolean bUnsigned) {
241 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint32 : ScilabIntegerTypeEnum.sci_int32;
245 * Constructor with single signed value
250 public ScilabInteger(long value, boolean bUnsigned) {
252 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint64 : ScilabIntegerTypeEnum.sci_int64;
256 * Set the current values
261 * true, if these values are unsigned; false otherwise.
263 public void setData(byte[][] data, boolean bUnsigned) {
264 this.byteData = data;
266 this.precision = ScilabIntegerTypeEnum.sci_uint8;
268 this.precision = ScilabIntegerTypeEnum.sci_int8;
273 * Set the current values
278 * true, if these values are unsigned; false otherwise.
280 public void setData(short[][] data, boolean bUnsigned) {
281 this.shortData = data;
283 this.precision = ScilabIntegerTypeEnum.sci_uint16;
285 this.precision = ScilabIntegerTypeEnum.sci_int16;
290 * Set the current values
295 * true, if these values are unsigned; false otherwise.
297 public void setData(int[][] data, boolean bUnsigned) {
300 this.precision = ScilabIntegerTypeEnum.sci_uint32;
302 this.precision = ScilabIntegerTypeEnum.sci_int32;
307 * Set the current values
312 * true, if these values are unsigned; false otherwise.
314 public void setData(long[][] data, boolean bUnsigned) {
315 this.longData = data;
317 this.precision = ScilabIntegerTypeEnum.sci_uint64;
319 this.precision = ScilabIntegerTypeEnum.sci_int64;
324 * Return the type of Scilab
326 * @return the type of Scilab
330 public ScilabTypeEnum getType() {
335 * If the precision is not 64, all values will be converted to long
336 * (attention, the convertion can be long) if precision is 64, just return
341 public long[][] getData() {
342 long[][] convertedMatrix = new long[this.getHeight()][this.getWidth()];
343 switch (this.getPrec()) {
347 for (int i = 0; i < this.getHeight(); i++) {
348 for (int j = 0; j < this.getWidth(); j++) {
349 convertedMatrix[i][j] = Long.valueOf(getByteElement(i, j));
352 return convertedMatrix;
355 for (int i = 0; i < this.getHeight(); i++) {
356 for (int j = 0; j < this.getWidth(); j++) {
357 convertedMatrix[i][j] = Long.valueOf(getShortElement(i, j));
360 return convertedMatrix;
363 for (int i = 0; i < this.getHeight(); i++) {
364 for (int j = 0; j < this.getWidth(); j++) {
365 convertedMatrix[i][j] = Long.valueOf(getIntElement(i, j));
368 return convertedMatrix;
377 * Returns the value as the form of short
379 * @return the values as short
381 public short[][] getDataAsShort() {
386 * Returns the value as the form of byte
388 * @return the values as byte
390 public byte[][] getDataAsByte() {
395 * Returns the value as the form of int
397 * @return the values as int
399 public int[][] getDataAsInt() {
404 * Returns the value as the form of long Only for Scilab 6.X
406 * @return the values as long
408 public long[][] getDataAsLong() {
413 * @return the precision of the values
415 public ScilabIntegerTypeEnum getPrec() {
420 * @return true, if the values are signed, false otherwise.
422 public boolean isUnsigned() {
439 * Manage the old representation of IntegerType
442 * the typeName (TYPE8, TYPE16, TYPE32, TYPE64)
445 * @return the converted type to ScilabIntegerTypeEnum. null is cannot
448 public static ScilabIntegerTypeEnum convertOldType(String typeName, boolean unsigned) {
449 if (typeName.equals("TYPE8")) {
451 return ScilabIntegerTypeEnum.sci_uint8;
453 return ScilabIntegerTypeEnum.sci_int8;
456 if (typeName.equals("TYPE16")) {
458 return ScilabIntegerTypeEnum.sci_uint16;
460 return ScilabIntegerTypeEnum.sci_int16;
463 if (typeName.equals("TYPE32")) {
465 return ScilabIntegerTypeEnum.sci_uint32;
467 return ScilabIntegerTypeEnum.sci_int32;
470 if (typeName.equals("TYPE64")) {
472 return ScilabIntegerTypeEnum.sci_uint64;
474 return ScilabIntegerTypeEnum.sci_int64;
481 * @return the height of the value matrix
482 * @see org.scilab.modules.types.ScilabType#getHeight()
485 public int getHeight() {
486 if (this.getPrec() == null) {
489 switch (this.getPrec()) {
492 if (byteData == null) {
495 return byteData.length;
498 if (shortData == null) {
501 return shortData.length;
504 if (intData == null) {
507 return intData.length;
510 if (longData == null) {
513 return longData.length;
520 * @return the width of the value matrix
521 * @see org.scilab.modules.types.ScilabType#getWidth()
524 public int getWidth() {
525 if (this.getPrec() == null) {
528 switch (this.getPrec()) {
531 if (byteData == null) {
534 return byteData[0].length;
537 if (shortData == null) {
540 return shortData[0].length;
543 if (intData == null) {
546 return intData[0].length;
549 if (longData == null) {
552 return longData[0].length;
561 public boolean isReference() {
566 * @return true, if there is no values; false otherwise.
569 public boolean isEmpty() {
570 if (this.getPrec() == null) {
573 switch (this.getPrec()) {
576 return byteData == null;
579 return shortData == null;
582 return intData == null;
585 return longData == null;
594 public String getVarName() {
601 public boolean isSwaped() {
606 * Get the byte element at position (i, j)
607 * @param i the row index
608 * @param j the column index
611 public byte getByteElement(final int i, final int j) {
612 return byteData[i][j];
616 * Get the short element at position (i, j)
617 * @param i the row index
618 * @param j the column index
621 public short getShortElement(final int i, final int j) {
622 return shortData[i][j];
626 * Get the int element at position (i, j)
627 * @param i the row index
628 * @param j the column index
631 public int getIntElement(final int i, final int j) {
632 return intData[i][j];
636 * Get the long element at position (i, j)
637 * @param i the row index
638 * @param j the column index
641 public long getLongElement(final int i, final int j) {
642 return longData[i][j];
646 * Set the byte element at position (i, j)
647 * @param i the row index
648 * @param j the column index
649 * @param x the byte to set
651 public void setByteElement(final int i, final int j, final byte x) {
656 * Set the short element at position (i, j)
657 * @param i the row index
658 * @param j the column index
659 * @param x the short to set
661 public void setShortElement(final int i, final int j, final short x) {
666 * Set the int element at position (i, j)
667 * @param i the row index
668 * @param j the column index
669 * @param x the int to set
671 public void setIntElement(final int i, final int j, final int x) {
676 * Set the long element at position (i, j)
677 * @param i the row index
678 * @param j the column index
679 * @param x the long to set
681 public void setLongElement(final int i, final int j, final long x) {
687 * Get the element at position (i, j) as a long
688 * @param i the row index
689 * @param j the column index
692 public long getElement(final int i, final int j) {
693 switch (this.getPrec()) {
696 return getByteElement(i, j);
699 return getShortElement(i, j);
702 return getIntElement(i, j);
705 return getLongElement(i, j);
712 * Set the element at position (i, j)
713 * @param i the row index
714 * @param j the column index
717 public void setElement(final int i, final int j, final long x) {
718 switch (this.getPrec()) {
721 setByteElement(i, j, (byte) x);
725 setShortElement(i, j, (short) x);
729 setIntElement(i, j, (int) x);
733 setLongElement(i, j, x);
739 public int hashCode() {
740 final int prime = 31;
742 result = prime * result + Arrays.deepHashCode(byteData);
743 result = prime * result + Arrays.deepHashCode(intData);
744 result = prime * result + Arrays.deepHashCode(longData);
745 result = prime * result + ((precision == null) ? 0 : precision.hashCode());
746 result = prime * result + Arrays.deepHashCode(shortData);
751 * @see org.scilab.modules.types.ScilabType#equals(Object)
754 public boolean equals(Object obj) {
755 if (obj instanceof ScilabInteger) {
756 ScilabInteger sciInt = (ScilabInteger) obj;
757 if (isEmpty() && sciInt.isEmpty()) {
761 if (this.getWidth() != sciInt.getWidth() || this.getHeight() != sciInt.getHeight()) {
765 return ScilabTypeUtils.equalsInteger(this.getRawData(), this.isSwaped(), sciInt.getRawData(), sciInt.isSwaped());
772 * Get the data as a array of arrays
775 public Object getCorrectData() {
776 switch (this.getPrec()) {
794 * Get the data as they are
797 public Object getRawData() {
798 return getCorrectData();
804 public Object getSerializedObject() {
805 return new Object[] { new int[] { this.getPrec().swigValue() }, getCorrectData() };
809 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
810 int version = in.readInt();
813 precision = ScilabIntegerTypeEnum.swigToEnum(in.readInt());
814 Object data = in.readObject();
818 byteData = (byte[][]) data;
822 shortData = (short[][]) data;
826 intData = (int[][]) data;
830 longData = (long[][]) data;
833 varName = (String) in.readObject();
834 swaped = in.readBoolean();
837 throw new ClassNotFoundException("A class ScilabInteger with a version " + version + " does not exists");
842 public void writeExternal(ObjectOutput out) throws IOException {
843 out.writeInt(VERSION);
844 out.writeInt(getPrec().swigValue());
845 out.writeObject(getCorrectData());
846 out.writeObject(varName);
847 out.writeBoolean(swaped);
851 * Display the representation in the Scilab language of the type<BR>
852 * Note that the representation can be copied/pasted straight into Scilab
854 * @return the pretty-printed values
855 * @see java.lang.Object#toString()
858 public String toString() {
859 StringBuilder result = new StringBuilder();
868 result.append("int");
870 switch (this.getPrec()) {
899 return result.toString();
903 * Put each value on the buffer.
908 private void appendData(StringBuilder result) {
909 long[][] d = getData();
910 for (int i = 0; i < getHeight(); ++i) {
911 for (int j = 0; j < getWidth(); ++j) {
913 result.append(d[i][j]);
915 if (j != getWidth() - 1) {
919 if (i != getHeight() - 1) {
920 result.append(" ; ");