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;
42 private static final int VERSION = 0;
44 protected long[][] longData;
45 protected short[][] shortData;
46 protected int[][] intData;
47 protected byte[][] byteData;
48 protected ScilabIntegerTypeEnum precision;
49 protected String varName;
50 protected boolean swaped;
51 transient protected boolean byref;
56 public ScilabInteger() { }
59 * Constructor with values
64 * true, if the values are unsigned; false if they are signed.
66 public ScilabInteger(byte[][] data, boolean bUnsigned) {
67 this.setData(data, bUnsigned);
71 * Constructor with values
76 * true, if the values are unsigned; false if they are signed.
78 public ScilabInteger(short[][] data, boolean bUnsigned) {
79 this.setData(data, bUnsigned);
83 * Constructor with values
88 * true, if the values are unsigned; false if they are signed.
90 public ScilabInteger(int[][] data, boolean bUnsigned) {
91 this.setData(data, bUnsigned);
95 * Constructor with values
100 * true, if the values are unsigned; false if they are signed.
102 public ScilabInteger(long[][] data, boolean bUnsigned) {
103 this.setData(data, bUnsigned);
107 * Constructor with values
109 * @param varName the variable name
113 * true, if the values are unsigned; false if they are signed.
114 * @param swaped true if the matrices are stored row by row
116 public ScilabInteger(String varName, byte[][] data, boolean bUnsigned, boolean swaped) {
117 this.setData(data, bUnsigned);
118 this.varName = varName;
119 this.swaped = swaped;
123 * Constructor with values
125 * @param varName the variable name
129 * true, if the values are unsigned; false if they are signed.
130 * @param swaped true if the matrices are stored row by row
132 public ScilabInteger(String varName, short[][] data, boolean bUnsigned, boolean swaped) {
133 this.setData(data, bUnsigned);
134 this.varName = varName;
135 this.swaped = swaped;
139 * Constructor with values
141 * @param varName the variable name
145 * true, if the values are unsigned; false if they are signed.
146 * @param swaped true if the matrices are stored row by row
148 public ScilabInteger(String varName, int[][] data, boolean bUnsigned, boolean swaped) {
149 this.setData(data, bUnsigned);
150 this.varName = varName;
151 this.swaped = swaped;
155 * Constructor with values
157 * @param varName the variable name
161 * true, if the values are unsigned; false if they are signed.
162 * @param swaped true if the matrices are stored row by row
164 public ScilabInteger(String varName, long[][] data, boolean bUnsigned, boolean swaped) {
165 this.setData(data, bUnsigned);
166 this.varName = varName;
167 this.swaped = swaped;
171 * Constructor with single signed value
176 public ScilabInteger(byte value) {
177 this.byteData = new byte[1][1];
178 this.byteData[0][0] = value;
179 this.precision = ScilabIntegerTypeEnum.sci_int8;
183 * Constructor with single signed value
188 public ScilabInteger(short value) {
189 this.shortData = new short[1][1];
190 this.shortData[0][0] = value;
191 this.precision = ScilabIntegerTypeEnum.sci_int16;
195 * Constructor with single signed value
200 public ScilabInteger(int value) {
201 this.intData = new int[1][1];
202 this.intData[0][0] = value;
203 this.precision = ScilabIntegerTypeEnum.sci_int32;
207 * Constructor with single signed value
212 public ScilabInteger(long value) {
213 this.longData = new long[1][1];
214 this.longData[0][0] = value;
215 this.precision = ScilabIntegerTypeEnum.sci_int64;
219 * Constructor with single signed value
224 * true, if these values are unsigned; false otherwise.
226 public ScilabInteger(byte value, boolean bUnsigned) {
228 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint8 : ScilabIntegerTypeEnum.sci_int8;
232 * Constructor with single signed value
237 * true, if these values are unsigned; false otherwise.
239 public ScilabInteger(short value, boolean bUnsigned) {
241 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint16 : ScilabIntegerTypeEnum.sci_int16;
245 * Constructor with single signed value
250 * true, if these values are unsigned; false otherwise.
252 public ScilabInteger(int value, boolean bUnsigned) {
254 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint32 : ScilabIntegerTypeEnum.sci_int32;
258 * Constructor with single signed value
263 * true, if these values are unsigned; false otherwise.
265 public ScilabInteger(long value, boolean bUnsigned) {
267 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint64 : ScilabIntegerTypeEnum.sci_int64;
271 * Set the current values
276 * true, if these values are unsigned; false otherwise.
278 public void setData(byte[][] data, boolean bUnsigned) {
279 this.byteData = data;
281 this.precision = ScilabIntegerTypeEnum.sci_uint8;
283 this.precision = ScilabIntegerTypeEnum.sci_int8;
288 * Set the current values
293 * true, if these values are unsigned; false otherwise.
295 public void setData(short[][] data, boolean bUnsigned) {
296 this.shortData = data;
298 this.precision = ScilabIntegerTypeEnum.sci_uint16;
300 this.precision = ScilabIntegerTypeEnum.sci_int16;
305 * Set the current values
310 * true, if these values are unsigned; false otherwise.
312 public void setData(int[][] data, boolean bUnsigned) {
315 this.precision = ScilabIntegerTypeEnum.sci_uint32;
317 this.precision = ScilabIntegerTypeEnum.sci_int32;
322 * Set the current values
327 * true, if these values are unsigned; false otherwise.
329 public void setData(long[][] data, boolean bUnsigned) {
330 this.longData = data;
332 this.precision = ScilabIntegerTypeEnum.sci_uint64;
334 this.precision = ScilabIntegerTypeEnum.sci_int64;
339 * Return the type of Scilab
341 * @return the type of Scilab
345 public ScilabTypeEnum getType() {
346 return ScilabTypeEnum.sci_ints;
350 * If the precision is not 64, all values will be converted to long
351 * (attention, the convertion can be long) if precision is 64, just return
356 public long[][] getData() {
357 long[][] convertedMatrix = new long[this.getHeight()][this.getWidth()];
358 switch (this.getPrec()) {
362 for (int i = 0; i < this.getHeight(); i++) {
363 for (int j = 0; j < this.getWidth(); j++) {
364 convertedMatrix[i][j] = Long.valueOf(getByteElement(i, j));
367 return convertedMatrix;
370 for (int i = 0; i < this.getHeight(); i++) {
371 for (int j = 0; j < this.getWidth(); j++) {
372 convertedMatrix[i][j] = Long.valueOf(getShortElement(i, j));
375 return convertedMatrix;
378 for (int i = 0; i < this.getHeight(); i++) {
379 for (int j = 0; j < this.getWidth(); j++) {
380 convertedMatrix[i][j] = Long.valueOf(getIntElement(i, j));
383 return convertedMatrix;
392 * Returns the value as the form of short
394 * @return the values as short
396 public short[][] getDataAsShort() {
401 * Returns the value as the form of byte
403 * @return the values as byte
405 public byte[][] getDataAsByte() {
410 * Returns the value as the form of int
412 * @return the values as int
414 public int[][] getDataAsInt() {
419 * Returns the value as the form of long Only for Scilab 6.X
421 * @return the values as long
423 public long[][] getDataAsLong() {
428 * @return the precision of the values
430 public ScilabIntegerTypeEnum getPrec() {
435 * @return true, if the values are signed, false otherwise.
437 public boolean isUnsigned() {
454 * Manage the old representation of IntegerType
457 * the typeName (type8, type16, type32, type64)
460 * @return the converted type to ScilabIntegerTypeEnum. null is cannot
463 public static ScilabIntegerTypeEnum convertOldType(String typeName, boolean unsigned) {
464 if (typeName.equals("type8")) {
466 return ScilabIntegerTypeEnum.sci_uint8;
468 return ScilabIntegerTypeEnum.sci_int8;
471 if (typeName.equals("type16")) {
473 return ScilabIntegerTypeEnum.sci_uint16;
475 return ScilabIntegerTypeEnum.sci_int16;
478 if (typeName.equals("type32")) {
480 return ScilabIntegerTypeEnum.sci_uint32;
482 return ScilabIntegerTypeEnum.sci_int32;
485 if (typeName.equals("type64")) {
487 return ScilabIntegerTypeEnum.sci_uint64;
489 return ScilabIntegerTypeEnum.sci_int64;
496 * @return the height of the value matrix
497 * @see org.scilab.modules.types.ScilabType#getHeight()
500 public int getHeight() {
501 if (this.getPrec() == null) {
504 switch (this.getPrec()) {
507 if (byteData == null) {
510 return byteData.length;
513 if (shortData == null) {
516 return shortData.length;
519 if (intData == null) {
522 return intData.length;
525 if (longData == null) {
528 return longData.length;
535 * @return the width of the value matrix
536 * @see org.scilab.modules.types.ScilabType#getWidth()
539 public int getWidth() {
540 if (this.getPrec() == null) {
543 switch (this.getPrec()) {
546 if (byteData == null) {
549 return byteData[0].length;
552 if (shortData == null) {
555 return shortData[0].length;
558 if (intData == null) {
561 return intData[0].length;
564 if (longData == null) {
567 return longData[0].length;
577 public boolean isReference() {
582 * @return true, if there is no values; false otherwise.
585 public boolean isEmpty() {
586 if (this.getPrec() == null) {
589 switch (this.getPrec()) {
592 return byteData == null;
595 return shortData == null;
598 return intData == null;
601 return longData == null;
611 public String getVarName() {
619 public boolean isSwaped() {
624 * Get the byte element at position (i, j)
625 * @param i the row index
626 * @param j the column index
629 public byte getByteElement(final int i, final int j) {
630 return byteData[i][j];
634 * Get the short element at position (i, j)
635 * @param i the row index
636 * @param j the column index
639 public short getShortElement(final int i, final int j) {
640 return shortData[i][j];
644 * Get the int element at position (i, j)
645 * @param i the row index
646 * @param j the column index
649 public int getIntElement(final int i, final int j) {
650 return intData[i][j];
654 * Get the long element at position (i, j)
655 * @param i the row index
656 * @param j the column index
659 public long getLongElement(final int i, final int j) {
660 return longData[i][j];
664 * Set the byte element at position (i, j)
665 * @param i the row index
666 * @param j the column index
667 * @param x the byte to set
669 public void setByteElement(final int i, final int j, final byte x) {
674 * Set the short element at position (i, j)
675 * @param i the row index
676 * @param j the column index
677 * @param x the short to set
679 public void setShortElement(final int i, final int j, final short x) {
684 * Set the int element at position (i, j)
685 * @param i the row index
686 * @param j the column index
687 * @param x the int to set
689 public void setIntElement(final int i, final int j, final int x) {
694 * Set the long element at position (i, j)
695 * @param i the row index
696 * @param j the column index
697 * @param x the long to set
699 public void setLongElement(final int i, final int j, final long x) {
705 * Get the element at position (i, j) as a long
706 * @param i the row index
707 * @param j the column index
710 public long getElement(final int i, final int j) {
711 switch (this.getPrec()) {
714 return getByteElement(i, j);
717 return getShortElement(i, j);
720 return getIntElement(i, j);
723 return getLongElement(i, j);
730 * Set the element at position (i, j)
731 * @param i the row index
732 * @param j the column index
735 public void setElement(final int i, final int j, final long x) {
736 switch (this.getPrec()) {
739 setByteElement(i, j, (byte) x);
743 setShortElement(i, j, (short) x);
747 setIntElement(i, j, (int) x);
751 setLongElement(i, j, x);
757 public int hashCode() {
758 final int prime = 31;
760 result = prime * result + Arrays.deepHashCode(byteData);
761 result = prime * result + Arrays.deepHashCode(intData);
762 result = prime * result + Arrays.deepHashCode(longData);
763 result = prime * result + ((precision == null) ? 0 : precision.hashCode());
764 result = prime * result + Arrays.deepHashCode(shortData);
769 * @see org.scilab.modules.types.ScilabType#equals(Object)
772 public boolean equals(Object obj) {
773 if (obj instanceof ScilabInteger) {
774 ScilabInteger sciInt = (ScilabInteger) obj;
775 if (isEmpty() && sciInt.isEmpty()) {
779 if (this.getWidth() != sciInt.getWidth() || this.getHeight() != sciInt.getHeight()) {
783 return ScilabTypeUtils.equalsInteger(this.getRawData(), this.isSwaped(), sciInt.getRawData(), sciInt.isSwaped());
790 * Get the data as a array of arrays
793 public Object getCorrectData() {
794 switch (this.getPrec()) {
812 * Get the data as they are
815 public Object getRawData() {
816 return getCorrectData();
823 public Object getSerializedObject() {
824 return new Object[] { new int[] { this.getPrec().swigValue() }, getCorrectData() };
828 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
829 int version = in.readInt();
832 precision = ScilabIntegerTypeEnum.swigToEnum(in.readInt());
833 Object data = in.readObject();
837 byteData = (byte[][]) data;
841 shortData = (short[][]) data;
845 intData = (int[][]) data;
849 longData = (long[][]) data;
852 varName = (String) in.readObject();
853 swaped = in.readBoolean();
856 throw new ClassNotFoundException("A class ScilabInteger with a version " + version + " does not exists");
861 public void writeExternal(ObjectOutput out) throws IOException {
862 out.writeInt(VERSION);
863 out.writeInt(getPrec().swigValue());
864 out.writeObject(getCorrectData());
865 out.writeObject(varName);
866 out.writeBoolean(swaped);
870 * Display the representation in the Scilab language of the type<BR>
871 * Note that the representation can be copied/pasted straight into Scilab
873 * @return the pretty-printed values
874 * @see java.lang.Object#toString()
877 public String toString() {
878 StringBuilder result = new StringBuilder();
887 result.append("int");
889 switch (this.getPrec()) {
918 return result.toString();
922 * Put each value on the buffer.
927 private void appendData(StringBuilder result) {
928 long[][] d = getData();
929 for (int i = 0; i < getHeight(); ++i) {
930 for (int j = 0; j < getWidth(); ++j) {
932 result.append(d[i][j]);
934 if (j != getWidth() - 1) {
938 if (i != getHeight() - 1) {
939 result.append(" ; ");