2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2010-2010 - DIGITEO - Clement DAVID
5 * Copyright (C) 2011-2011 - DIGITEO - 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.IOException;
21 import java.io.ObjectInput;
22 import java.io.ObjectOutput;
23 import java.util.Arrays;
26 * This class provides a representation on the Scilab boolean datatype<br>
28 * This class is {@link java.io.Serializable} and any modification could impact
29 * load and store of data (Xcos files, Javasci saved data, etc...).<br>
33 * boolean [][]a={{true,false,true}, {true,true,true}};<BR>
34 * ScilabBoolean aMatrix = new ScilabBoolean(a);
37 * @see org.scilab.modules.javasci.Scilab
39 public class ScilabBoolean implements ScilabType {
41 private static final long serialVersionUID = 6511497080095473901L;
42 private static final ScilabTypeEnum type = ScilabTypeEnum.sci_boolean;
44 private static final int VERSION = 0;
46 /* the boolean data */
47 protected boolean[][] data;
48 protected String varName;
49 protected boolean swaped;
50 transient protected boolean byref;
53 * Create an empty object
55 public ScilabBoolean() {
60 * Create an object from an array of array of boolean
63 * the array of boolean
65 public ScilabBoolean(boolean[][] data) {
70 * Create an object from an array of array of boolean
73 * the array of boolean
75 public ScilabBoolean(String varName, boolean[][] data, boolean swaped) {
76 this.varName = varName;
82 * Create a scalar boolean from a boolean
87 public ScilabBoolean(boolean value) {
88 this.data = new boolean[1][1];
89 this.data[0][0] = value;
95 public boolean isReference() {
100 * Change the value with the provided data
105 public void setData(boolean[][] data) {
112 * @return the array of array of boolean
114 public boolean[][] getData() {
121 public Object getRawData() {
126 * Get the element at position (i, j)
127 * @param i the first coordinate
128 * @param j the second coordinate
129 * @return the corresponding boolean
131 public boolean getElement(final int i, final int j) {
136 * Set the element at position (i, j)
137 * @param i the first coordinate
138 * @param j the second coordinate
139 * @param x the new value
141 public void setElement(final int i, final int j, final boolean x) {
148 public String getVarName() {
155 public boolean isSwaped() {
160 * Return the type of Scilab
162 * @return the type of Scilab
166 public ScilabTypeEnum getType() {
171 * Return the height (number of element) of the stored data
176 public int getHeight() {
184 * Return the width (number of element) of the stored data
189 public int getWidth() {
193 return data[0].length;
197 * If it is empty / uninitialized
199 * @return true if empty
202 public boolean isEmpty() {
207 public int hashCode() {
208 return Arrays.deepHashCode(data);
212 * @see org.scilab.modules.types.ScilabType#equals(Object)
215 public boolean equals(Object obj) {
216 if (obj instanceof ScilabBoolean) {
217 ScilabBoolean sciBool = (ScilabBoolean) obj;
218 if (isEmpty() && sciBool.isEmpty()) {
222 if (this.getWidth() != sciBool.getWidth() || this.getHeight() != sciBool.getHeight()) {
226 return ScilabTypeUtils.equalsBoolean(this.getRawData(), this.isSwaped(), sciBool.getRawData(), sciBool.isSwaped());
235 public Object getSerializedObject() {
240 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
241 int version = in.readInt();
244 data = (boolean[][]) in.readObject();
245 varName = (String) in.readObject();
246 swaped = in.readBoolean();
249 throw new ClassNotFoundException("A class ScilabBoolean with a version " + version + " does not exists");
254 public void writeExternal(ObjectOutput out) throws IOException {
255 out.writeInt(VERSION);
256 out.writeObject(getData());
257 out.writeObject(varName);
258 out.writeBoolean(swaped);
262 * Display the representation in the Scilab language of the type<BR>
263 * Note that the representation can be copied/pasted straight into Scilab
265 * @return the pretty print
268 public String toString() {
269 StringBuilder result = new StringBuilder();
276 for (int i = 0; i < getHeight(); ++i) {
277 for (int j = 0; j < getWidth(); ++j) {
278 if (getElement(i, j)) {
283 if (j != getWidth() - 1) {
287 if (i != getHeight() - 1) {
288 result.append(" ; ");
292 return result.toString();