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;
42 private static final int VERSION = 0;
44 private String[][] data;
45 private String varName;
46 private boolean swaped;
51 public ScilabString() {
56 * Constructor with data.
59 * the associated data.
61 public ScilabString(String[][] data) {
66 * Constructor with data.
68 * @param varName the variable name
70 * the associated data.
71 * @param swaped true if the matrices are stored row by row
73 public ScilabString(String varName, String[][] data, boolean swaped) {
74 this.varName = varName;
80 * Constructor with vector data.
83 * the column vector data
85 public ScilabString(String[] data) {
86 if (data == null || data.length == 0) {
87 this.data = new String[1][1];
90 this.data = new String[1][data.length];
91 for (int i = 0; i < data.length; i++) {
92 this.data[0][i] = data[i];
98 * Constructor with a unique value
103 public ScilabString(String string) {
104 if (string == null) {
105 throw new IllegalArgumentException("string == null");
107 this.data = new String[1][1];
108 this.data[0][0] = string;
117 public void setData(String[][] data) {
122 * Return the type of Scilab
124 * @return the type of Scilab
128 public ScilabTypeEnum getType() {
129 return ScilabTypeEnum.sci_strings;
133 * @return the associated values
135 public String[][] getData() {
143 public String getVarName() {
151 public boolean isSwaped() {
159 public boolean isReference() {
164 * @return the height of the data matrix
165 * @see org.scilab.modules.types.ScilabType#getHeight()
168 public int getHeight() {
176 * @return the width of the data matrix
177 * @see org.scilab.modules.types.ScilabType#getWidth()
180 public int getWidth() {
184 return data[0].length;
188 * Check the emptiness of the associated data.
190 * @return true, if the associated data array is empty.
193 public boolean isEmpty() {
194 return (data == null);
198 public int hashCode() {
199 final int prime = 31;
201 result = prime * result + Arrays.deepHashCode(data);
206 * @see org.scilab.modules.types.ScilabType#equals(Object)
209 public boolean equals(Object obj) {
210 if (obj instanceof ScilabString) {
211 return Arrays.deepEquals(this.getData(), ((ScilabString) obj).getData());
221 public Object getSerializedObject() {
226 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
227 int version = in.readInt();
230 data = (String[][]) in.readObject();
231 varName = (String) in.readObject();
232 swaped = in.readBoolean();
235 throw new ClassNotFoundException("A class ScilabString with a version " + version + " does not exists");
240 public void writeExternal(ObjectOutput out) throws IOException {
241 out.writeInt(VERSION);
242 out.writeObject(data);
243 out.writeObject(varName);
244 out.writeBoolean(swaped);
248 * Display the representation in the Scilab language of the type<BR>
249 * Note that the representation can be copied/pasted straight into Scilab
251 * @return a Scilab-like String representation of the data.
252 * @see java.lang.Object#toString()
255 public String toString() {
256 StringBuilder result = new StringBuilder();
262 for (int i = 0; i < getHeight(); ++i) {
263 for (int j = 0; j < getWidth(); ++j) {
265 result.append(data[i][j].replaceAll("[\"\']", "\"\""));
268 if (j != getWidth() - 1) {
272 if (i != getHeight() - 1) {
273 result.append(" ; ");
277 return result.toString();