source: trunk/gli/src/org/greenstone/gatherer/util/ArrayTools.java@ 5785

Last change on this file since 5785 was 5785, checked in by mdewsnip, 21 years ago

Commented out about 60 unused functions.

  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 KB
Line 
1/* GPL_HEADER */
2package org.greenstone.gatherer.util;
3
4/**************************************************************************************
5 * Title: Gatherer
6 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
7 * Company: The University of Waikato
8 * Written: / /01
9 * Revised: 16/08/02 Improved
10 **************************************************************************************/
11import java.awt.*;
12import java.io.*;
13import java.util.*;
14import javax.swing.filechooser.*;
15import org.greenstone.gatherer.file.FileNode;
16import org.greenstone.gatherer.msm.Metadata;
17import org.w3c.dom.*;
18
19/** This utility class contains a series of static methods for common array type manipulations including appending, casting and changing between containers.
20 * @author John Thompson
21 * @version 2.3
22 */
23public class ArrayTools {
24
25 /** Append a Component onto an array of Components. */
26 static public Component[] add(Component[] a, Component b) {
27 Component[] c = null;
28 if(a != null && b != null) {
29 c = new Component[a.length + 1];
30 System.arraycopy(a, 0, c, 0, a.length);
31 c[c.length - 1] = b;
32 }
33 else if(a == null && b != null) {
34 c = new Component[1];
35 c[0] = b;
36 }
37 else if(a != null && b == null) {
38 c = a;
39 }
40 return c;
41 }
42 /** This method efficiently appends a new element onto the end of a element array.
43 * @param a The initial <strong>Element[]</strong>.
44 * @param b The new <strong>Element</strong>.
45 * @return An <strong>Element[]</strong> containing a followed by b.
46 */
47 static public Element[] add(Element a[], Element b) {
48 Element[] c = null;
49 if(a != null && b != null) {
50 c = new Element[a.length + 1];
51 System.arraycopy(a, 0, c, 0, a.length);
52 c[c.length - 1] = b;
53 }
54 else if(a == null && b != null) {
55 c = new Element[1];
56 c[0] = b;
57 }
58 else if(a != null && b == null) {
59 c = a;
60 }
61 return c;
62 }
63 /** This method efficently appends one element array onto the end of another.
64 * @param a The initial <strong>Element[]</strong>.
65 * @param b The <strong>Element[]</strong> to append.
66 * @return An <strong>Element[]</strong> containing a followed by b.
67 */
68 static public Element[] add(Element a[], Element b[]) {
69 Element[] c = null;
70 if(a != null && b != null) {
71 c = new Element[a.length + b.length];
72 System.arraycopy(a, 0, c, 0, a.length);
73 System.arraycopy(b, 0, c, a.length, b.length);
74 }
75 else if(a == null && b != null) {
76 c = b;
77 }
78 else if(a != null && b == null) {
79 c = a;
80 }
81 return c;
82 }
83 /** This method efficiently appends a new file onto the end of a file array.
84 * @param a The initial <strong>File[]</strong>.
85 * @param b The new <strong>File</strong>.
86 * @return A <strong>File[]</strong> containing a followed by b.
87 */
88 static public File[] add(File a[], File b) {
89 File[] c = null;
90 if(a != null && b != null) {
91 c = new File[a.length + 1];
92 System.arraycopy(a, 0, c, 0, a.length);
93 c[c.length - 1] = b;
94 }
95 else if(a == null && b != null) {
96 c = new File[1];
97 c[0] = b;
98 }
99 else if(a != null && b == null) {
100 c = a;
101 }
102 return c;
103 }
104 /** This method efficently appends one file array onto the end of another.
105 * @param a The initial <strong>File[]</strong>.
106 * @param b The <strong>File[]</strong> to append.
107 * @return A <strong>File[]</strong> containing a followed by b.
108 */
109 static public File[] add(File a[], File b[]) {
110 File[] c = null;
111 if(a != null && b != null) {
112 c = new File[a.length + b.length];
113 System.arraycopy(a, 0, c, 0, a.length);
114 System.arraycopy(b, 0, c, a.length, b.length);
115 }
116 else if(a == null && b != null) {
117 c = b;
118 }
119 else if(a != null && b == null) {
120 c = a;
121 }
122 return c;
123 }
124
125 /** This method efficiently appends a new FileNode onto the end of a FileNode array.
126 * @param a The initial <strong>FileNode[]</strong>.
127 * @param b The new <strong>FileNode</strong>.
128 * @return An <strong>FileNode[]</strong> containing a followed by b.
129 */
130 static public FileNode[] add(FileNode a[], FileNode b) {
131 FileNode[] c = null;
132 if(a != null && b != null) {
133 c = new FileNode[a.length + 1];
134 System.arraycopy(a, 0, c, 0, a.length);
135 c[c.length - 1] = b;
136 }
137 else if(a == null && b != null) {
138 c = new FileNode[1];
139 c[0] = b;
140 }
141 else if(a != null && b == null) {
142 c = a;
143 }
144 return c;
145 }
146 /** This method efficently appends one FileNode array onto the end of another FileNode array.
147 * @param a The initial <strong>FileNode[]</strong>.
148 * @param b The <strong>FileNode[]</strong> to append.
149 * @return An <strong>FileNode[]</strong> containing a followed by b.
150 */
151 static final public FileNode[] add(FileNode a[], FileNode b[]) {
152 FileNode[] c = null;
153 if(a != null && b != null) {
154 c = new FileNode[a.length + b.length];
155 System.arraycopy(a, 0, c, 0, a.length);
156 System.arraycopy(b, 0, c, a.length, b.length);
157 }
158 else if(a == null && b != null) {
159 c = b;
160 }
161 else if(a != null && b == null) {
162 c = a;
163 }
164 return c;
165 }
166 /** Determine if a certain FileNode is present in an array of FileNodes.
167 * @param array The <strong>FileNode[]</strong>.
168 * @param a The <strong>FileNode</strong> we are searching for.
169 * @return <i>true</i> if the FileNode is in the array, <i>false</i> otherwise.
170 */
171 static final public boolean contains(FileNode array[], FileNode a) {
172 for(int i = 0; i < array.length; i++) {
173 if(array[i].equals(a)) {
174 return true;
175 }
176 }
177 return false;
178 }
179 /** Remove the first element from an array, and return the remaining 'tail'.
180 * @param a A <strong>FileNode[]</strong>.
181 * @return The same <strong>FileNode[]</strong> sans its head element.
182 */
183 /* static final private FileNode[] tail(FileNode a[]) {
184 if(a.length == 1) {
185 return null;
186 }
187 FileNode b[] = new FileNode[a.length - 1];
188 System.arraycopy(a, 1, b, 0, b.length);
189 return b;
190 } */
191
192 /** Append a Metadata object onto an array of Metadata objects. */
193 static public Metadata[] add(Metadata[] a, Metadata b) {
194 Metadata[] c = null;
195 if(a != null && b != null) {
196 c = new Metadata[a.length + 1];
197 System.arraycopy(a, 0, c, 0, a.length);
198 c[c.length - 1] = b;
199 }
200 else if(a == null && b != null) {
201 c = new Metadata[1];
202 c[0] = b;
203 }
204 else if(a != null && b == null) {
205 c = a;
206 }
207 return c;
208 }
209 /** Append a Metadata object array onto an array of Metadata objects. */
210 static public Metadata[] add(Metadata[] a, Metadata[] b) {
211 Metadata[] c = null;
212 if(a != null && b != null) {
213 c = new Metadata[a.length + b.length];
214 System.arraycopy(a, 0, c, 0, a.length);
215 System.arraycopy(b, 0, c, a.length, b.length);
216 }
217 else if(a == null && b != null) {
218 c = b;
219 }
220 else if(a != null && b == null) {
221 c = a;
222 }
223 return c;
224 }
225 /** Remove the Metadata object at the given index form the specified Metadata object array. */
226 static public Metadata[] remove(Metadata[] a, int index) {
227 Metadata[] c = null;
228 if(a != null) {
229 c = new Metadata[a.length - 1];
230 System.arraycopy(a, 0, c, 0, index);
231 System.arraycopy(a, index + 1, c, index, c.length - index);
232 }
233 return c;
234 }
235
236 /** This method efficiently appends a new node onto the end of a node array.
237 * @param a The initial <strong>Node[]</strong>.
238 * @param b The new <strong>Node</strong>.
239 * @return A <strong>Node[]</strong> containing a followed by b.
240 */
241 static public Node[] add(Node a[], Node b) {
242 Node[] c = null;
243 if(a != null && b != null) {
244 c = new Node[a.length + 1];
245 System.arraycopy(a, 0, c, 0, a.length);
246 c[c.length - 1] = b;
247 }
248 else if(a == null && b != null) {
249 c = new Node[1];
250 c[0] = b;
251 }
252 else if(a != null && b == null) {
253 c = a;
254 }
255 return c;
256 }
257 /** This method efficently appends one node array onto the end of another.
258 * @param a The initial <strong>Node[]</strong>.
259 * @param b The <strong>Node[]</strong> to append.
260 * @return A <strong>Node[]</strong> containing a followed by b.
261 */
262 static public Node[] add(Node a[], Node b[]) {
263 Node[] c = null;
264 if(a != null && b != null) {
265 c = new Node[a.length + b.length];
266 System.arraycopy(a, 0, c, 0, a.length);
267 System.arraycopy(b, 0, c, a.length, b.length);
268 }
269 else if(a == null && b != null) {
270 c = b;
271 }
272 else if(a != null && b == null) {
273 c = a;
274 }
275 return c;
276 }
277
278 /** This method efficiently appends a new string onto the end of a string array.
279 * @param a The initial <strong>String[]</strong>.
280 * @param b The new <strong>String</strong>.
281 * @return A <strong>String[]</strong> containing a followed by b.
282 */
283 static public String[] add(String a[], String b) {
284 String[] c = null;
285 if(a != null && b != null) {
286 c = new String[a.length + 1];
287 System.arraycopy(a, 0, c, 0, a.length);
288 c[c.length - 1] = b;
289 }
290 else if(a == null && b != null) {
291 c = new String[1];
292 c[0] = b;
293 }
294 else if(a != null && b == null) {
295 c = a;
296 }
297 return c;
298 }
299 /** This method efficently appends one string array onto the end of another.
300 * @param a The initial <strong>String[]</strong>.
301 * @param b The <strong>String[]</strong> to append.
302 * @return A <strong>String[]</strong> containing a followed by b.
303 */
304 static public String[] add(String a[], String b[]) {
305 String[] c = null;
306 if(a != null && b != null) {
307 c = new String[a.length + b.length];
308 System.arraycopy(a, 0, c, 0, a.length);
309 System.arraycopy(b, 0, c, a.length, b.length);
310 }
311 else if(a == null && b != null) {
312 c = b;
313 }
314 else if(a != null && b == null) {
315 c = a;
316 }
317 return c;
318 }
319 /** This method takes an array list and creates a string array.
320 * @param a An <strong>ArrayList</strong> containing hopefully <strong>String</strong> or else this will fail.
321 * @return A <strong>String[]</strong> or <i>null</i> if the array could not be created.
322 */
323 static public String[] arrayListToStringArray(ArrayList a) {
324 String array[] = new String[a.size()];
325 for(int i = 0; i < array.length; i++) {
326 array[i] = (String)a.get(i);
327 }
328 return array;
329 }
330
331 static public File[] filter(File[] files, String pattern, boolean exclude) {
332 int write_ptr = 0;
333 ///ystem.err.println("Filtering by '" + pattern + "', Exclude? " + exclude + " :");
334 for(int read_ptr = 0; read_ptr < files.length; read_ptr++) {
335 File current = files[read_ptr];
336 files[write_ptr] = current;
337 ///ystem.err.print("Testing " + current.getName() + " -> ");
338 // Determine whether we move the write pointer or not.
339 if(current.getName().toLowerCase().matches(pattern)) {
340 if(!exclude) {
341 ///ystem.err.println("Match, Exclude.");
342 write_ptr++;
343 }
344 else {
345 ///ystem.err.println("Match, Include.");
346 }
347 }
348 else {
349 // You can't exclude folders with an inclusion filter!
350 if(exclude || current.isDirectory()) {
351 ///ystem.err.println("Nonmatch, Exclude.");
352 write_ptr++;
353 }
354 else {
355 ///ystem.err.println("Nonmatch, Include.");
356 }
357 }
358 }
359 File[] result = new File[write_ptr];
360 System.arraycopy(files, 0, result, 0, result.length);
361 pattern = null;
362 files = null;
363 return result;
364 }
365
366 /** Method to convert a list of nodes from the DOM model into a node array. This is quite useful as using a <strong>NodeList</strong> is problematic if you wish to traverse the tree while editing. Because they are 'live' representations of the model you are editing, it is easy to drop out of for loops early or run off the ends of arrays.
367 * @param node_list A <strong>NodeList</strong> representing the <strong>Node</strong>s we want to iterate through.
368 * @return A brand new <strong>Node[]</strong> with the first <strong>Node</strong> in the <strong>NodeList</strong> at the head.
369 * @see org.w3c.dom.Node
370 * @see org.w3c.dom.NodeList
371 */
372 static public Node[] nodeListToNodeArray(NodeList node_list) {
373 Node nodes[] = null;
374 for(int i = 0; i < node_list.getLength(); i++) {
375 nodes = add(nodes, node_list.item(i));
376 }
377 return nodes;
378 }
379 /** Transforms an Object array into a single string of the form '[o1,o2,...,on]'.
380 * @param objects An <strong>Object[]</strong>. Note that the objects in this array must support toString() reasonably.
381 * @return A <strong>String</strong> representing the given array.
382 */
383 static public String objectArrayToString(Object objects[]) {
384 StringBuffer result = new StringBuffer("[");
385 for(int i = 0; i < objects.length; i++) {
386 result.append(objects[i].toString());
387 if(i < objects.length - 1) {
388 result.append(",");
389 }
390 }
391 result.append("]");
392 return result.toString();
393 }
394
395 /** Remove the file at the given index from the array of files. */
396 static public File[] remove(File[] files, int index) {
397 if(files.length == 1) {
398 return new File[0];
399 }
400 File[] temp = new File[files.length - 1];
401 if(index > 0) {
402 System.arraycopy(files, 0, temp, 0, index);
403 }
404 if(index < files.length - 1) {
405 System.arraycopy(files, index + 1, temp, index, files.length - 1 - index);
406 }
407 return temp;
408 }
409
410 /** Sorts an array of files. */
411 static public void sort(File[] files) {
412 sort(files, false);
413 }
414
415 /** Sorts an array of files. Can also to instructed to list directories first. Case insensitive.
416 * @param files The File[] to be sorted.
417 * @param directories_first true if you want to directories to be listed first.
418 */
419 static public void sort(File[] files, boolean directories_first) {
420 if(files != null && files.length > 1) {
421 FileComparator comparator = new FileComparator(directories_first);
422 Arrays.sort(files, comparator);
423 }
424 }
425
426 /** Comparator used to order files. */
427 static private class FileComparator
428 implements Comparator {
429 private boolean directories_first = false;
430
431 public FileComparator(boolean directories_first) {
432 this.directories_first = directories_first;
433 }
434 /** Compare two files in terms of ordering of their paths.
435 * @param o1 The <strong>Object</strong> that represents the first file.
436 * @param o2 The other <strong>Object</strong>, also a file.
437 * @return An <i>int</i> which is <1, 0 or >1 if o1 is < o2, = o2 or > o2 respectively.
438 */
439 public int compare(Object o1, Object o2) {
440 int result = 0;
441 File f1 = (File) o1;
442 String n1 = f1.getName().toLowerCase();
443 File f2 = (File) o2;
444 String n2 = f2.getName().toLowerCase();
445 // Special checks for system roots as these include removable media drives and thus we should never attempt to call isDirectory until the user explicitly attempts to map drive.
446 boolean f1_system_root = FileSystemView.getFileSystemView().isFileSystemRoot(f1);
447 boolean f2_system_root = FileSystemView.getFileSystemView().isFileSystemRoot(f2);
448 if((f1_system_root && f2_system_root) || (!f2_system_root && f1_system_root && f2.isDirectory()) || (!f1_system_root && f1.isDirectory() && f2_system_root) || (f1.isDirectory() && f2.isDirectory()) || (f1.isFile() && f2.isFile())) {
449 result = n1.compareTo(n2);
450 }
451 else if(f1_system_root || f1.isDirectory()) {
452 result = -1;
453 }
454 else {
455 result = 1;
456 }
457 return result;
458 }
459 /** Compare two files for equality, in terms of their file paths.
460 * @param object The <strong>Object</strong> representing the file we are about to compare ourselves to.
461 * @return <i>true</i> if we equal the given file, <i>false</i> otherwise.
462 */
463 public boolean equals(Object obj) {
464 return (compare(this, obj) == 0);
465 }
466 }
467}
Note: See TracBrowser for help on using the repository browser.