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;
133 public byte getByteElement(final int i, final int j) {
134 return byteBuffer.get(i + nbRows * j);
140 public short getShortElement(final int i, final int j) {
141 return shortBuffer.get(i + nbRows * j);
147 public int getIntElement(final int i, final int j) {
148 return intBuffer.get(i + nbRows * j);
154 public long getLongElement(final int i, final int j) {
155 return longBuffer.get(i + nbRows * j);
161 public void setByteElement(final int i, final int j, final byte x) {
162 byteBuffer.put(i + nbRows * j, x);
168 public void setShortElement(final int i, final int j, final short x) {
169 shortBuffer.put(i + nbRows * j, x);
175 public void setIntElement(final int i, final int j, final int x) {
176 intBuffer.put(i + nbRows * j, x);
182 public void setLongElement(final int i, final int j, final long x) {
183 longBuffer.put(i + nbRows * j, x);
187 * Set the current values
192 * true, if these values are unsigned; false otherwise.
194 public void setData(byte[][] data, boolean bUnsigned) {
195 ScilabTypeUtils.setPart(byteBuffer, data);
197 this.precision = ScilabIntegerTypeEnum.sci_uint8;
199 this.precision = ScilabIntegerTypeEnum.sci_int8;
204 * Set the current values
209 * true, if these values are unsigned; false otherwise.
211 public void setData(short[][] data, boolean bUnsigned) {
212 ScilabTypeUtils.setPart(shortBuffer, data);
214 this.precision = ScilabIntegerTypeEnum.sci_uint16;
216 this.precision = ScilabIntegerTypeEnum.sci_int16;
221 * Set the current values
226 * true, if these values are unsigned; false otherwise.
228 public void setData(int[][] data, boolean bUnsigned) {
229 ScilabTypeUtils.setPart(intBuffer, data);
231 this.precision = ScilabIntegerTypeEnum.sci_uint32;
233 this.precision = ScilabIntegerTypeEnum.sci_int32;
238 * Set the current values
243 * true, if these values are unsigned; false otherwise.
245 public void setData(long[][] data, boolean bUnsigned) {
246 ScilabTypeUtils.setPart(longBuffer, data);
248 this.precision = ScilabIntegerTypeEnum.sci_uint64;
250 this.precision = ScilabIntegerTypeEnum.sci_int64;
255 * Returns the value as the form of short
257 * @return the values as short
259 public short[][] getDataAsShort() {
260 short[][] d = new short[nbRows][nbCols];
261 ScilabTypeUtils.setBuffer(d, shortBuffer);
267 * Returns the value as the form of byte
269 * @return the values as byte
271 public byte[][] getDataAsByte() {
272 byte[][] d = new byte[nbRows][nbCols];
273 ScilabTypeUtils.setBuffer(d, byteBuffer);
279 * Returns the value as the form of int
281 * @return the values as int
283 public int[][] getDataAsInt() {
284 int[][] d = new int[nbRows][nbCols];
285 ScilabTypeUtils.setBuffer(d, intBuffer);
291 * Returns the value as the form of long Only for Scilab 6.X
293 * @return the values as long
295 public long[][] getDataAsLong() {
296 long[][] d = new long[nbRows][nbCols];
297 ScilabTypeUtils.setBuffer(d, longBuffer);
303 * @return the height of the value matrix
304 * @see org.scilab.modules.types.ScilabType#getHeight()
307 public int getHeight() {
312 * @return the width of the value matrix
313 * @see org.scilab.modules.types.ScilabType#getWidth()
316 public int getWidth() {
321 * @return true, if there is no values; false otherwise.
324 public boolean isEmpty() {
325 return nbRows == 0 || nbCols == 0;
329 * @see org.scilab.modules.types.ScilabType#equals(Object)
332 public boolean equals(Object obj) {
333 if (obj instanceof ScilabInteger) {
334 ScilabInteger sciInt = (ScilabInteger) obj;
335 if (isEmpty() && sciInt.isEmpty()) {
339 if (this.getWidth() != sciInt.getWidth() || this.getHeight() != sciInt.getHeight()) {
343 return ScilabTypeUtils.equalsInteger(this.getRawData(), this.isSwaped(), sciInt.getRawData(), sciInt.isSwaped());
352 public Object getRawData() {
353 switch (this.getPrec()) {
374 public Object getCorrectData() {
375 switch (this.getPrec()) {
378 return getDataAsByte();
381 return getDataAsShort();
384 return getDataAsInt();
387 return getDataAsLong();
394 * When this object is deserialized we want a ScilabInteger, not a ScilabIntegerReference.
395 * @return a ScilabInteger
397 private Object readResolve() throws ObjectStreamException {
400 return new ScilabInteger(varName, byteData, false, swaped);
402 return new ScilabInteger(varName, byteData, true, swaped);
404 return new ScilabInteger(varName, shortData, false, swaped);
406 return new ScilabInteger(varName, shortData, true, swaped);
408 return new ScilabInteger(varName, intData, false, swaped);
410 return new ScilabInteger(varName, intData, true, swaped);
412 return new ScilabInteger(varName, longData, false, swaped);
414 return new ScilabInteger(varName, longData, true, swaped);