source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/util/ArrayTools.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 10.2 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.w3c.dom.*;
16
17/** This utility class contains a series of static methods for common array type manipulations including appending, casting and changing between containers.
18 * @author John Thompson
19 * @version 2.3
20 */
21public class ArrayTools {
22
23 /** Append a Component onto an array of Components. */
24 static public Component[] add(Component[] a, Component b) {
25 Component[] c = null;
26 if(a != null && b != null) {
27 c = new Component[a.length + 1];
28 System.arraycopy(a, 0, c, 0, a.length);
29 c[c.length - 1] = b;
30 }
31 else if(a == null && b != null) {
32 c = new Component[1];
33 c[0] = b;
34 }
35 else if(a != null && b == null) {
36 c = a;
37 }
38 return c;
39 }
40 /** This method efficiently appends a new element onto the end of a element array.
41 * @param a The initial <strong>Element[]</strong>.
42 * @param b The new <strong>Element</strong>.
43 * @return An <strong>Element[]</strong> containing a followed by b.
44 */
45 static public Element[] add(Element a[], Element b) {
46 Element[] c = null;
47 if(a != null && b != null) {
48 c = new Element[a.length + 1];
49 System.arraycopy(a, 0, c, 0, a.length);
50 c[c.length - 1] = b;
51 }
52 else if(a == null && b != null) {
53 c = new Element[1];
54 c[0] = b;
55 }
56 else if(a != null && b == null) {
57 c = a;
58 }
59 return c;
60 }
61 /** This method efficently appends one element array onto the end of another.
62 * @param a The initial <strong>Element[]</strong>.
63 * @param b The <strong>Element[]</strong> to append.
64 * @return An <strong>Element[]</strong> containing a followed by b.
65 */
66 static public Element[] add(Element a[], Element b[]) {
67 Element[] c = null;
68 if(a != null && b != null) {
69 c = new Element[a.length + b.length];
70 System.arraycopy(a, 0, c, 0, a.length);
71 System.arraycopy(b, 0, c, a.length, b.length);
72 }
73 else if(a == null && b != null) {
74 c = b;
75 }
76 else if(a != null && b == null) {
77 c = a;
78 }
79 return c;
80 }
81 /** This method efficiently appends a new file onto the end of a file array.
82 * @param a The initial <strong>File[]</strong>.
83 * @param b The new <strong>File</strong>.
84 * @return A <strong>File[]</strong> containing a followed by b.
85 */
86 static public File[] add(File a[], File b) {
87 File[] c = null;
88 if(a != null && b != null) {
89 c = new File[a.length + 1];
90 System.arraycopy(a, 0, c, 0, a.length);
91 c[c.length - 1] = b;
92 }
93 else if(a == null && b != null) {
94 c = new File[1];
95 c[0] = b;
96 }
97 else if(a != null && b == null) {
98 c = a;
99 }
100 return c;
101 }
102 /** This method efficently appends one file array onto the end of another.
103 * @param a The initial <strong>File[]</strong>.
104 * @param b The <strong>File[]</strong> to append.
105 * @return A <strong>File[]</strong> containing a followed by b.
106 */
107 static public File[] add(File a[], File b[]) {
108 File[] c = null;
109 if(a != null && b != null) {
110 c = new File[a.length + b.length];
111 System.arraycopy(a, 0, c, 0, a.length);
112 System.arraycopy(b, 0, c, a.length, b.length);
113 }
114 else if(a == null && b != null) {
115 c = b;
116 }
117 else if(a != null && b == null) {
118 c = a;
119 }
120 return c;
121 }
122
123
124 /** This method efficiently appends a new node onto the end of a node array.
125 * @param a The initial <strong>Node[]</strong>.
126 * @param b The new <strong>Node</strong>.
127 * @return A <strong>Node[]</strong> containing a followed by b.
128 */
129 static public Node[] add(Node a[], Node b) {
130 Node[] c = null;
131 if(a != null && b != null) {
132 c = new Node[a.length + 1];
133 System.arraycopy(a, 0, c, 0, a.length);
134 c[c.length - 1] = b;
135 }
136 else if(a == null && b != null) {
137 c = new Node[1];
138 c[0] = b;
139 }
140 else if(a != null && b == null) {
141 c = a;
142 }
143 return c;
144 }
145 /** This method efficently appends one node array onto the end of another.
146 * @param a The initial <strong>Node[]</strong>.
147 * @param b The <strong>Node[]</strong> to append.
148 * @return A <strong>Node[]</strong> containing a followed by b.
149 */
150 static public Node[] add(Node a[], Node b[]) {
151 Node[] c = null;
152 if(a != null && b != null) {
153 c = new Node[a.length + b.length];
154 System.arraycopy(a, 0, c, 0, a.length);
155 System.arraycopy(b, 0, c, a.length, b.length);
156 }
157 else if(a == null && b != null) {
158 c = b;
159 }
160 else if(a != null && b == null) {
161 c = a;
162 }
163 return c;
164 }
165
166 /** This method efficiently appends a new string onto the end of a string array.
167 * @param a The initial <strong>String[]</strong>.
168 * @param b The new <strong>String</strong>.
169 * @return A <strong>String[]</strong> containing a followed by b.
170 */
171 static public String[] add(String a[], String b) {
172 String[] c = null;
173 if(a != null && b != null) {
174 c = new String[a.length + 1];
175 System.arraycopy(a, 0, c, 0, a.length);
176 c[c.length - 1] = b;
177 }
178 else if(a == null && b != null) {
179 c = new String[1];
180 c[0] = b;
181 }
182 else if(a != null && b == null) {
183 c = a;
184 }
185 return c;
186 }
187 /** This method efficently appends one string array onto the end of another.
188 * @param a The initial <strong>String[]</strong>.
189 * @param b The <strong>String[]</strong> to append.
190 * @return A <strong>String[]</strong> containing a followed by b.
191 */
192 static public String[] add(String a[], String b[]) {
193 String[] c = null;
194 if(a != null && b != null) {
195 c = new String[a.length + b.length];
196 System.arraycopy(a, 0, c, 0, a.length);
197 System.arraycopy(b, 0, c, a.length, b.length);
198 }
199 else if(a == null && b != null) {
200 c = b;
201 }
202 else if(a != null && b == null) {
203 c = a;
204 }
205 return c;
206 }
207 /** This method takes an array list and creates a string array.
208 * @param a An <strong>ArrayList</strong> containing hopefully <strong>String</strong> or else this will fail.
209 * @return A <strong>String[]</strong> or <i>null</i> if the array could not be created.
210 */
211 static public String[] arrayListToStringArray(ArrayList a) {
212 String array[] = new String[a.size()];
213 for(int i = 0; i < array.length; i++) {
214 array[i] = (String)a.get(i);
215 }
216 return array;
217 }
218
219 static public File[] filter(File[] files, String pattern, boolean exclude) {
220 int write_ptr = 0;
221 ///ystem.err.println("Filtering by '" + pattern + "', Exclude? " + exclude + " :");
222 for(int read_ptr = 0; read_ptr < files.length; read_ptr++) {
223 File current = files[read_ptr];
224 files[write_ptr] = current;
225 ///ystem.err.print("Testing " + current.getName() + " -> ");
226 // Determine whether we move the write pointer or not.
227 if(current.getName().toLowerCase().matches(pattern)) {
228 if(!exclude) {
229 ///ystem.err.println("Match, Exclude.");
230 write_ptr++;
231 }
232 else {
233 ///ystem.err.println("Match, Include.");
234 }
235 }
236 else {
237 // You can't exclude folders with an inclusion filter!
238 if(exclude || current.isDirectory()) {
239 ///ystem.err.println("Nonmatch, Exclude.");
240 write_ptr++;
241 }
242 else {
243 ///ystem.err.println("Nonmatch, Include.");
244 }
245 }
246 }
247 File[] result = new File[write_ptr];
248 System.arraycopy(files, 0, result, 0, result.length);
249 pattern = null;
250 files = null;
251 return result;
252 }
253
254
255 /** Transforms an Object array into a single string of the form '[o1,o2,...,on]'.
256 * @param objects An <strong>Object[]</strong>. Note that the objects in this array must support toString() reasonably.
257 * @return A <strong>String</strong> representing the given array.
258 */
259 static public String objectArrayToString(Object objects[]) {
260 StringBuffer result = new StringBuffer("[");
261 for(int i = 0; i < objects.length; i++) {
262 result.append(objects[i].toString());
263 if(i < objects.length - 1) {
264 result.append(",");
265 }
266 }
267 result.append("]");
268 return result.toString();
269 }
270
271
272 /** Sorts an array of files, putting non-files first. Case insensitive.
273 * @param files_to_sort The File[] to be sorted.
274 */
275 static public void sort(File[] files_to_sort)
276 {
277 // Return if there is nothing to sort
278 if (files_to_sort == null || files_to_sort.length <= 1) {
279 return;
280 }
281
282 FileSystemView fileSystemView = FileSystemView.getFileSystemView();
283 FileComparator comparator = new FileComparator();
284
285 // Separate the file system roots and directories from the files, and sort separately
286 ArrayList files_list = new ArrayList((files_to_sort.length * 3) / 4);
287 ArrayList non_files_list = new ArrayList((files_to_sort.length) / 4);
288 for (int i = 0; i < files_to_sort.length; i++) {
289 File file = files_to_sort[i];
290
291 // File is a system root
292 if (fileSystemView.isFileSystemRoot(file)) {
293 non_files_list.add(file);
294 }
295 // File is a directory
296 else if (file.isDirectory()) {
297 non_files_list.add(file);
298 }
299 // File is an actual file
300 else {
301 files_list.add(file);
302 }
303 }
304
305 // Sort files
306 Object[] files = files_list.toArray();
307 Arrays.sort(files, comparator);
308
309 // Sort non-files
310 Object[] non_files = non_files_list.toArray();
311 Arrays.sort(non_files, comparator);
312
313 // Merge the results, putting non-files before files
314 int j = 0;
315 for (int i = 0; i < non_files.length; i++) {
316 files_to_sort[j++] = (File) non_files[i];
317 }
318 for (int i = 0; i < files.length; i++) {
319 files_to_sort[j++] = (File) files[i];
320 }
321 }
322
323
324 /** Comparator used to order files. */
325 static private class FileComparator
326 implements Comparator {
327
328 /** Compare two files in terms of ordering of their paths.
329 * @param o1 The <strong>Object</strong> that represents the first file.
330 * @param o2 The other <strong>Object</strong>, also a file.
331 * @return An <i>int</i> which is <1, 0 or >1 if o1 is <o2, =o2 or >o2 respectively.
332 */
333 public int compare(Object o1, Object o2)
334 {
335 File f1 = (File) o1;
336 File f2 = (File) o2;
337
338 return f1.getName().compareToIgnoreCase(f2.getName());
339 }
340 }
341}
Note: See TracBrowser for help on using the repository browser.