2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET
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 String 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 * String [][]a={{"This","is","my","string"},{"and","I want to", "compare"," them"}};<BR>
33 * ScilabString aMatrix = new ScilabString(a);
36 * @see org.scilab.modules.javasci.Scilab
38 public class ScilabString implements ScilabType {
40 private static final long serialVersionUID = 359802519980180085L;
41 private static final ScilabTypeEnum type = ScilabTypeEnum.sci_strings;
43 private static final int VERSION = 0;
45 private String[][] data;
46 private String varName;
47 private boolean swaped;
52 public ScilabString() {
57 * Constructor with data.
60 * the associated data.
62 public ScilabString(String[][] data) {
67 * Constructor with data.
70 * the associated data.
72 public ScilabString(String varName, String[][] data, boolean swaped) {
73 this.varName = varName;
79 * Constructor with vector data.
82 * the column vector data
84 public ScilabString(String[] data) {
85 if (data == null || data.length == 0) {
86 this.data = new String[1][1];
89 this.data = new String[1][data.length];
90 for (int i = 0; i < data.length; i++) {
91 this.data[0][i] = data[i];
97 * Constructor with a unique value
102 public ScilabString(String string) {
103 if (string == null) {
104 throw new IllegalArgumentException("string == null");
106 this.data = new String[1][1];
107 this.data[0][0] = string;
116 public void setData(String[][] data) {
121 * Return the type of Scilab
123 * @return the type of Scilab
127 public ScilabTypeEnum getType() {
132 * @return the associated values
134 public String[][] getData() {
141 public String getVarName() {
148 public boolean isSwaped() {
155 public boolean isReference() {
160 * @return the height of the data matrix
161 * @see org.scilab.modules.types.ScilabType#getHeight()
164 public int getHeight() {
172 * @return the width of the data matrix
173 * @see org.scilab.modules.types.ScilabType#getWidth()
176 public int getWidth() {
180 return data[0].length;
184 * Check the emptiness of the associated data.
186 * @return true, if the associated data array is empty.
189 public boolean isEmpty() {
190 return (data == null);
194 public int hashCode() {
195 final int prime = 31;
197 result = prime * result + Arrays.deepHashCode(data);
202 * @see org.scilab.modules.types.ScilabType#equals(Object)
205 public boolean equals(Object obj) {
206 if (obj instanceof ScilabString) {
207 return Arrays.deepEquals(this.getData(), ((ScilabString) obj).getData());
216 public Object getSerializedObject() {
221 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
222 int version = in.readInt();
225 data = (String[][]) in.readObject();
226 varName = (String) in.readObject();
227 swaped = in.readBoolean();
230 throw new ClassNotFoundException("A class ScilabString with a version " + version + " does not exists");
235 public void writeExternal(ObjectOutput out) throws IOException {
236 out.writeInt(VERSION);
237 out.writeObject(data);
238 out.writeObject(varName);
239 out.writeBoolean(swaped);
243 * Display the representation in the Scilab language of the type<BR>
244 * Note that the representation can be copied/pasted straight into Scilab
246 * @return a Scilab-like String representation of the data.
247 * @see java.lang.Object#toString()
250 public String toString() {
251 StringBuilder result = new StringBuilder();
257 for (int i = 0; i < getHeight(); ++i) {
258 for (int j = 0; j < getWidth(); ++j) {
260 result.append(data[i][j].replaceAll("[\"\']", "\"\""));
263 if (j != getWidth() - 1) {
267 if (i != getHeight() - 1) {
268 result.append(" ; ");
272 return result.toString();