Mercurial > hg > mep
comparison FileList.java @ 0:4031cbb02f08
Initial import.
Ignore-this: 87317e384f22bde48db996355191fa5f
author | Marcus Pearce <m.pearce@gold.ac.uk> |
---|---|
date | Tue, 18 May 2010 11:37:10 +0100 |
parents | |
children | 85b03f084d63 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4031cbb02f08 |
---|---|
1 /*============================================================================= | |
2 * File: FileList.java | |
3 * Author: Marcus Pearce <m.pearce@gold.ac.uk> | |
4 * Created: <2007-11-30 10:30:38 marcusp> | |
5 * Time-stamp: <2010-05-10 11:14:37 marcusp> | |
6 *============================================================================= | |
7 */ | |
8 | |
9 import java.util.ArrayList; | |
10 import java.util.Iterator; | |
11 | |
12 import java.io.*; | |
13 | |
14 import java.util.Random; | |
15 | |
16 public class FileList { | |
17 | |
18 private ArrayList files; | |
19 private ArrayList probes; | |
20 private int numFiles; | |
21 private int currentFileIndex; | |
22 | |
23 public int numFiles() { return numFiles; } | |
24 public void setFileIndex(int i) { currentFileIndex = i; } | |
25 | |
26 public String currentFile() { | |
27 if (currentFileIndex < numFiles) | |
28 return (String)files.get(currentFileIndex); | |
29 else | |
30 return null; | |
31 } | |
32 | |
33 public ArrayList currentProbes() { | |
34 if (currentFileIndex < numFiles) | |
35 return (ArrayList)probes.get(currentFileIndex); | |
36 else | |
37 return null; | |
38 } | |
39 | |
40 public void incrementFileIndex() { | |
41 currentFileIndex = currentFileIndex + 1; | |
42 } | |
43 | |
44 private void randomise() { | |
45 Random rgen = new Random(); | |
46 for (int i = 0; i < numFiles; i++) { | |
47 int randomPosition = rgen.nextInt(numFiles); | |
48 // System.out.println("i = " + i + "; rpos = " + randomPosition + | |
49 // "; files = " + files.size() + | |
50 // "; probes = " + probes.size()); | |
51 | |
52 String tempFile = (String)files.get(i); | |
53 files.set(i, files.get(randomPosition)); | |
54 files.set(randomPosition, tempFile); | |
55 | |
56 ArrayList tempProbe = (ArrayList)probes.get(i); | |
57 probes.set(i, probes.get(randomPosition)); | |
58 probes.set(randomPosition, tempProbe); | |
59 } | |
60 } | |
61 | |
62 private void importFileList(String filename) { | |
63 try { | |
64 LineNumberReader lnr = | |
65 new LineNumberReader(new FileReader(new File(filename))); | |
66 lnr.setLineNumber(1); | |
67 StreamTokenizer stok = new StreamTokenizer(lnr); | |
68 stok.parseNumbers(); | |
69 stok.wordChars('_','_'); | |
70 stok.wordChars('0','9'); | |
71 //stok.wordChars('/','/'); | |
72 stok.eolIsSignificant(true); | |
73 stok.nextToken(); | |
74 while (stok.ttype != StreamTokenizer.TT_EOF) { | |
75 int lineno = lnr.getLineNumber(); | |
76 int i = 0; | |
77 ArrayList iprobes = new ArrayList(); | |
78 while (stok.ttype != StreamTokenizer.TT_EOL) { | |
79 int j = 0; | |
80 if (stok.ttype == StreamTokenizer.TT_NUMBER) { | |
81 iprobes.add((int)stok.nval - 1); // zero indexing | |
82 } else if (stok.ttype == StreamTokenizer.TT_WORD) { | |
83 files.add(stok.sval); | |
84 } | |
85 stok.nextToken(); | |
86 } | |
87 i++; | |
88 numFiles++; | |
89 probes.add(iprobes); | |
90 stok.nextToken(); | |
91 } | |
92 } catch(Exception e) { | |
93 System.out.println("Exception: " + e); | |
94 } | |
95 } | |
96 | |
97 public void print() { | |
98 Iterator fi = files.iterator(); | |
99 Iterator pi = probes.iterator(); | |
100 while (fi.hasNext()) { | |
101 System.out.print((String)fi.next() + ": "); | |
102 ArrayList ip = (ArrayList)pi.next(); | |
103 Iterator ipi = ip.iterator(); | |
104 while (ipi.hasNext()) | |
105 System.out.print(ipi.next() + " "); | |
106 System.out.print("\n"); | |
107 } | |
108 } | |
109 | |
110 public FileList(String filename) { | |
111 files = new ArrayList(); | |
112 probes = new ArrayList(); | |
113 currentFileIndex = 0; | |
114 this.importFileList(filename); | |
115 this.randomise(); | |
116 this.print(); | |
117 } | |
118 } |