2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2009 - DIGITEO - Antoine ELIAS
4 * Copyright (C) 2011 - DIGITEO - Calixte DENIZET
5 * Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
7 * Copyright (C) 2012 - 2016 - Scilab Enterprises
9 * This file is hereby licensed under the terms of the GNU GPL v2.0,
10 * pursuant to article 5.3.4 of the CeCILL v.2.1.
11 * This file was originally licensed under the terms of the CeCILL v2.1,
12 * and continues to be available under such terms.
13 * For more information, see the COPYING file which you should have received
14 * along with this program.
18 package org.scilab.modules.types;
20 import java.io.ObjectStreamException;
21 import java.nio.ByteBuffer;
22 import java.nio.IntBuffer;
23 import java.nio.LongBuffer;
24 import java.nio.ShortBuffer;
27 * This class provides a representation on the Scilab Integer datatype<br>
29 * This class is {@link java.io.Serializable} and any modification could impact
30 * load and store of data (Xcos files, Javasci saved data, etc...).<br>
34 * byte [][]a={{32,42,41}, {12,13,32}};<BR>
35 * ScilabInteger aMatrix = new ScilabInteger(a, true); // true = unsigned
38 * @see org.scilab.modules.javasci.Scilab
40 public class ScilabIntegerReference extends ScilabInteger {
42 private LongBuffer longBuffer;
43 private IntBuffer intBuffer;
44 private ShortBuffer shortBuffer;
45 private ByteBuffer byteBuffer;
46 private final int nbRows;
47 private final int nbCols;
52 public ScilabIntegerReference() {
59 * Constructor with values
64 * true, if the values are unsigned; false if they are signed.
66 ScilabIntegerReference(String varName, ByteBuffer data, int nbRows, int nbCols, boolean bUnsigned) {
67 this.varName = varName;
68 this.byteBuffer = data;
71 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint8 : ScilabIntegerTypeEnum.sci_int8;
77 * Constructor with values
82 * true, if the values are unsigned; false if they are signed.
84 ScilabIntegerReference(String varName, ShortBuffer data, int nbRows, int nbCols, boolean bUnsigned) {
85 this.varName = varName;
86 this.shortBuffer = data;
89 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint16 : ScilabIntegerTypeEnum.sci_int16;
95 * Constructor with values
100 * true, if the values are unsigned; false if they are signed.
102 ScilabIntegerReference(String varName, IntBuffer data, int nbRows, int nbCols, boolean bUnsigned) {
103 this.varName = varName;
104 this.intBuffer = data;
105 this.nbRows = nbRows;
106 this.nbCols = nbCols;
107 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint32 : ScilabIntegerTypeEnum.sci_int32;
113 * Constructor with values
118 * true, if the values are unsigned; false if they are signed.
120 ScilabIntegerReference(String varName, LongBuffer data, int nbRows, int nbCols, boolean bUnsigned) {
121 this.varName = varName;
122 this.longBuffer = data;
123 this.nbRows = nbRows;
124 this.nbCols = nbCols;
125 this.precision = bUnsigned ? ScilabIntegerTypeEnum.sci_uint64 : ScilabIntegerTypeEnum.sci_int64;
134 public byte getByteElement(final int i, final int j) {
135 return byteBuffer.get(i + nbRows * j);
142 public short getShortElement(final int i, final int j) {
143 return shortBuffer.get(i + nbRows * j);
150 public int getIntElement(final int i, final int j) {
151 return intBuffer.get(i + nbRows * j);
158 public long getLongElement(final int i, final int j) {
159 return longBuffer.get(i + nbRows * j);
166 public void setByteElement(final int i, final int j, final byte x) {
167 byteBuffer.put(i + nbRows * j, x);
174 public void setShortElement(final int i, final int j, final short x) {
175 shortBuffer.put(i + nbRows * j, x);
182 public void setIntElement(final int i, final int j, final int x) {
183 intBuffer.put(i + nbRows * j, x);
190 public void setLongElement(final int i, final int j, final long x) {
191 longBuffer.put(i + nbRows * j, x);
195 * Set the current values
200 * true, if these values are unsigned; false otherwise.
203 public void setData(byte[][] data, boolean bUnsigned) {
204 ScilabTypeUtils.setPart(byteBuffer, data);
206 this.precision = ScilabIntegerTypeEnum.sci_uint8;
208 this.precision = ScilabIntegerTypeEnum.sci_int8;
213 * Set the current values
218 * true, if these values are unsigned; false otherwise.
221 public void setData(short[][] data, boolean bUnsigned) {
222 ScilabTypeUtils.setPart(shortBuffer, data);
224 this.precision = ScilabIntegerTypeEnum.sci_uint16;
226 this.precision = ScilabIntegerTypeEnum.sci_int16;
231 * Set the current values
236 * true, if these values are unsigned; false otherwise.
239 public void setData(int[][] data, boolean bUnsigned) {
240 ScilabTypeUtils.setPart(intBuffer, data);
242 this.precision = ScilabIntegerTypeEnum.sci_uint32;
244 this.precision = ScilabIntegerTypeEnum.sci_int32;
249 * Set the current values
254 * true, if these values are unsigned; false otherwise.
257 public void setData(long[][] data, boolean bUnsigned) {
258 ScilabTypeUtils.setPart(longBuffer, data);
260 this.precision = ScilabIntegerTypeEnum.sci_uint64;
262 this.precision = ScilabIntegerTypeEnum.sci_int64;
267 * Returns the value as the form of short
269 * @return the values as short
272 public short[][] getDataAsShort() {
273 short[][] d = new short[nbRows][nbCols];
274 ScilabTypeUtils.setBuffer(d, shortBuffer);
280 * Returns the value as the form of byte
282 * @return the values as byte
285 public byte[][] getDataAsByte() {
286 byte[][] d = new byte[nbRows][nbCols];
287 ScilabTypeUtils.setBuffer(d, byteBuffer);
293 * Returns the value as the form of int
295 * @return the values as int
298 public int[][] getDataAsInt() {
299 int[][] d = new int[nbRows][nbCols];
300 ScilabTypeUtils.setBuffer(d, intBuffer);
306 * Returns the value as the form of long Only for Scilab 6.X
308 * @return the values as long
311 public long[][] getDataAsLong() {
312 long[][] d = new long[nbRows][nbCols];
313 ScilabTypeUtils.setBuffer(d, longBuffer);
319 * @return the height of the value matrix
320 * @see org.scilab.modules.types.ScilabType#getHeight()
323 public int getHeight() {
328 * @return the width of the value matrix
329 * @see org.scilab.modules.types.ScilabType#getWidth()
332 public int getWidth() {
337 * @return true, if there is no values; false otherwise.
340 public boolean isEmpty() {
341 return nbRows == 0 || nbCols == 0;
345 * @see org.scilab.modules.types.ScilabType#equals(Object)
348 public boolean equals(Object obj) {
349 if (obj instanceof ScilabInteger) {
350 ScilabInteger sciInt = (ScilabInteger) obj;
351 if (isEmpty() && sciInt.isEmpty()) {
355 if (this.getWidth() != sciInt.getWidth() || this.getHeight() != sciInt.getHeight()) {
359 return ScilabTypeUtils.equalsInteger(this.getRawData(), this.isSwaped(), sciInt.getRawData(), sciInt.isSwaped());
369 public Object getRawData() {
370 switch (this.getPrec()) {
392 public Object getCorrectData() {
393 switch (this.getPrec()) {
396 return getDataAsByte();
399 return getDataAsShort();
402 return getDataAsInt();
405 return getDataAsLong();
412 * When this object is deserialized we want a ScilabInteger, not a ScilabIntegerReference.
413 * @return a ScilabInteger
415 private Object readResolve() throws ObjectStreamException {
418 return new ScilabInteger(varName, byteData, false, swaped);
420 return new ScilabInteger(varName, byteData, true, swaped);
422 return new ScilabInteger(varName, shortData, false, swaped);
424 return new ScilabInteger(varName, shortData, true, swaped);
426 return new ScilabInteger(varName, intData, false, swaped);
428 return new ScilabInteger(varName, intData, true, swaped);
430 return new ScilabInteger(varName, longData, false, swaped);
432 return new ScilabInteger(varName, longData, true, swaped);