View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class StringArray
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:05 $
11   * 
12   * $Id: StringArray.java,v 1.3 2004/03/03 11:53:05 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.data;
24  
25  /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26  // import foo.*;
27  
28  /*====================================================================
29                   Implementation of class StringArray                  
30  ====================================================================*/
31  /***
32   * StringArray is an array of character values.
33   * 
34   * @version $Revision: 1.3 $
35   * @author Masahiro Takatsuka (masa@jbeans.net)
36   * @see DataArray
37   */
38  
39  public class StringArray extends DataArray {
40  	/***
41  	 * Construct a new StringArray object.
42  	 * <PRE>
43  	 * </PRE>
44  	 * 
45  	 * @param dim the dimension of the tuple.  The <code>dim</code>
46  	 * is always 1.
47  	 * @return a new StringArray object.
48  	 */
49  	public StringArray(int dim) {
50  		super(1);
51  	}
52  
53  	public StringArray() {
54  		this(1);
55  	}
56  	
57  	public StringArray(DataArray da) {
58  		this(1);
59  		copy(da);
60  	}
61  
62  	public void copy(DataArray da) {
63  		super.copy(da);
64  		if (da instanceof StringArray) {
65  			StringArray ca = (StringArray) da;
66  			System.arraycopy((String[]) ca.array, 0, (String[]) this.array, 0, ((String[]) ca.array).length);
67  		}
68  	}
69  
70  	/***
71  	 * Virtual constructor creates object of same type as this object.
72  	 */
73  	public DataArray makeObject() {
74  		return new StringArray(this.dimension);
75  	}
76  
77  	/***
78  	 * Allocate memory for this array. Delete old storage only if necessary.
79  	 */
80  	public boolean allocate(int size) {
81  //  		if (size > this.size ) {
82  			this.size = size > 0 ? size : 1;
83  			this.array = new String[this.size];
84  			if (this.array == null) {
85  				return false;
86  			}
87  //  		}
88  		this.maxId = -1;
89  		
90  		return true;
91  	}
92  
93  	/***
94  	 * resize the array.
95  	 */
96  	protected Object resize(int size) {
97  		String[] newArray;
98  		int newSize;
99  		
100 		if (size > this.size ) {
101 			newSize = this.size + size;
102 		} else if (size == this.size) {
103 			return this.array;
104 		} else {
105 			newSize = size;
106 		}
107 		
108 		if (newSize <= 0) {
109 			initialize();
110 			return null;
111 		}
112 		
113 		newArray = new String[newSize];
114 		if (newArray == null ){
115 			return null;
116 		}
117 		
118 		if (this.array != null) {
119 		  System.arraycopy((String[]) this.array, 0, newArray, 0, (size < this.size ? size : this.size));
120 		}
121 		
122 		this.size = newSize;
123 		this.array = newArray;
124 		
125 		return this.array;
126 	}
127 
128 	/***
129 	 * Get the data type.
130 	 */
131 	public int getDataType() {
132 		return DataType.STRING;
133 	}
134 
135 	/***
136 	 * Set the number of tuples in the array. Note that this allocates space
137 	 * depending on the tuple dimension.
138 	 */
139 	public void setNumberOfTuples(int number) {
140 		setNumberOfValues(number);
141 	}
142 
143 	public void setNumberOfStrings(int number) {
144 		setNumberOfTuples(number);
145 	}
146 	
147 	/***
148 	 * Get a pointer to a tuple at the ith location. This is a dangerous method
149 	 * (it is not thread safe since a pointer is returned).
150 	 */
151 	public double[] getTuple(int i) {
152 		double[] result = new double[1];
153 		try {
154 			result[0] = Double.parseDouble(((String[]) this.array)[i]);
155 		} catch (NumberFormatException e) {
156 			result[0] = Double.NaN;
157 		}
158 		return result;
159 	}
160 	
161 	public String getString(int i) {
162 		return ((String[]) this.array)[i];
163 	}
164 
165 	/***
166 	 * Copy the tuple value into a user-provided array.
167 	 */
168 	public void getTuple(int i, double[] tuple) {
169 		try {
170 			tuple[0] = Double.parseDouble(((String[]) this.array)[i]);
171 		} catch (NumberFormatException e) {
172 			tuple[0] = Double.NaN;
173 		}
174 	}
175 
176 	public void getString(int i, String string) {
177 		string = ((String[]) this.array)[i];
178 	}
179 
180 	/***
181 	 * Set the tuple value at the ith location in the array.
182 	 */
183 	public void setTuple(int i, double[] tuple) {
184 		((String[]) this.array)[i] = Double.toString(tuple[0]);
185 	}
186 
187 	public void setString(int i, String string) {
188 		((String[]) this.array)[i] = string;
189 	}
190 	
191 	/***
192 	 * Insert (memory allocation performed) the tuple into the ith location
193 	 * in the array.
194 	 */
195 	public void insertTuple(int i, double[] tuple) {
196 		ensureSpace(i, this.dimension);
197 		((String[]) this.array)[i] = Double.toString(tuple[0]);
198 	}
199 
200 	public void insertString(int i, String string) {
201 		ensureSpace(i, this.dimension);
202 		((String[]) this.array)[i] = string;
203 	}
204 	
205 	/***
206 	 * Insert (memory allocation performed) the tuple onto the end of the
207 	 * array.
208 	 */
209 	public int insertNextTuple(double[] tuple) {
210 		int j = this.maxId + 1;
211 		ensureSpace(j, this.dimension);
212 		((String[]) this.array)[j] = Double.toString(tuple[0]);		
213 		return this.maxId / this.dimension;
214 	}
215 
216 	public int insertNextString(String string) {
217 		int j = this.maxId + 1;
218 		ensureSpace(j, this.dimension);
219 		((String[]) this.array)[j] = string;
220 		return this.maxId;// / this.dimension;
221 	}
222 	
223 	/***
224 	 * Return the data component at the ith tuple and jth component location.
225 	 * Note that i is less than NumberOfTuples and j is less than 
226 	 * NumberOfComponents.
227 	 */
228 	public double getElement(int i, int j) {
229 		double v = Double.NaN;
230 		try {
231 			v = Double.parseDouble(((String[]) this.array)[i]);
232 		} catch (NumberFormatException e) {
233 		}
234 		return v;
235 	}
236 	
237 	/***
238 	 * Set the data component at the ith tuple and jth component location.
239 	 * Note that i is less than NumberOfTuples and j is less than 
240 	 * NumberOfComponents. Make sure enough memory has been allocated 
241 	 * (use SetNumberOfTuples() and SetNumberOfComponents()).
242 	 */
243 	public void setElement(int i, int j, double c) {
244 		setValue(i * this.dimension, Double.toString(c));
245 	}
246 	
247 	/***
248 	 * Insert the data component at ith tuple and jth component location. 
249 	 * Note that memory allocation is performed as necessary to hold the data.
250 	 */
251 	public void insertElement(int i, int j, double c) {
252 		insertValue(i * this.dimension, Double.toString(c));
253 	}
254 	
255 	/***
256 	 * Get the data at a particular index.
257 	 */
258 	public String getValue(int id) {
259 		return ((String[]) this.array)[id];
260 	}
261 
262 	/***
263 	 * Set the data at a particular index. Does not do range checking.
264 	 * Make sure you use the method SetNumberOfValues() before inserting data.
265 	 */
266 	public void setValue(int id, String value) {
267 		((String[]) this.array)[id] = value;
268 	}
269 
270 	/***
271 	 * Specify the number of values for this object to hold. Does an
272 	 * allocation as well as setting the MaxId ivar. Used in conjunction with
273 	 * SetValue() method for fast insertion.
274 	 */
275 	public void setNumberOfValues(int number) {
276 		allocate(number);
277 		this.maxId = number - 1;
278 	}
279 	
280 	/***
281 	 * Insert data at a specified position in the array.
282 	 */
283 	public void insertValue(int id, String c) {
284 		if (id >= this.size) {
285 			resize(id + 1);
286 		}
287 		((String[]) this.array)[id] = c;
288 		if (id > this.maxId) {
289 			this.maxId = id;
290 		}
291 	}
292 
293 	/***
294 	 * Insert data at the end of the array. Return its location in the array.
295 	 */
296 	public int insertNextValue(String c) {
297 		insertValue(++this.maxId, c); 
298 		return this.maxId;
299 	}
300 
301 	/***
302 	 * This method lets the user specify data to be held by the array.  The 
303 	 * array argument is a pointer to the data.  size is the size of 
304 	 * the array supplied by the user.  Set save to 1 to keep the class
305 	 * from deleting the array when it cleans up or reallocates memory.
306 	 * The class uses the actual array provided; it does not copy the data 
307 	 * from the suppled array.
308 	 */
309 	public void setArray(String[] array) {
310 		this.array = array;
311 		this.size = ((String[]) this.array).length;
312 		this.maxId = this.size - 1;
313 	}
314 }
315