-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.java
More file actions
76 lines (56 loc) · 1.85 KB
/
Utils.java
File metadata and controls
76 lines (56 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.*;
/** Helper methods for this assignment.
*
* @author Marcel Turcotte (turcotte@eecs.uottawa.ca)
*/
public class Utils {
private static String type = "LINEAR";
/** The method is used to specify the type of object to be
* returned by the method getFrequencyTable.
*
* @param value the type of object to be returned by the method getFrequencyTable
*/
public static void setType(String value) {
if (value == null) {
throw new NullPointerException();
}
if (! value.equals("LINEAR") && ! value.equals("TREE")) {
throw new IllegalArgumentException(value);
}
type = value;
}
/** A factory method returning an object implementing the
* interface FrequencyTable. The actual type depends on the
* current selection.
*
* @return an object implementing the interface FrequencyTable
*/
public static FrequencyTable getFrequencyTable() {
if (type.equals("LINEAR")) {
return new LinearFrequencyTable();
} else if (type.equals("TREE")) {
return new TreeFrequencyTable();
} else {
throw new AssertionError(); // can't happen
}
}
/** Reads a file and returns its content as a String.
*
* @param name the name of the file
* @return a string
* @throws IOException if an I/O error occurs.
* @throws FileNotFoundException if the file cannot be found
*/
public static String readFile(String name) throws IOException, FileNotFoundException {
String line;
StringBuffer buffer;
BufferedReader input;
input = new BufferedReader(new InputStreamReader(new FileInputStream(name)));
buffer = new StringBuffer();
while ((line = input.readLine()) != null) {
buffer.append(line);
}
input.close();
return buffer.toString();
}
}