-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternStore.java
More file actions
77 lines (62 loc) · 2.04 KB
/
PatternStore.java
File metadata and controls
77 lines (62 loc) · 2.04 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
77
import java.io.*;
import java.net.*;
import java.util.*;
public class PatternStore {
private ArrayList<Pattern> patterns = new ArrayList<>();
public PatternStore(String source) throws Exception {
if (source.startsWith("http://") || source.startsWith("https://")) {
loadFromURL(source);
} else {
loadFromDisk(source);
}
}
public PatternStore(Reader source) throws Exception {
load(source);
}
private void load(Reader r) throws Exception {
BufferedReader bufRead = null;
bufRead = new BufferedReader(r);
String line;
while ((line = bufRead.readLine()) != null) {
try {
patterns.add(new Pattern(line));
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
bufRead.close();
}
private void loadFromURL(String url) throws Exception {
URL destination = new URL(url);
URLConnection conn = destination.openConnection();
Reader r = new InputStreamReader(conn.getInputStream());
load(r);
}
private void loadFromDisk(String filename) throws Exception {
Reader r = new FileReader(filename);
load(r);
}
public ArrayList<Pattern> getPatternsNameSorted(){
ArrayList<Pattern> patternsCopy = new ArrayList<>(this.patterns);
Collections.sort(patternsCopy);
return patternsCopy;
}
public String[] getPatternAuthors() {
String[] authors = new String[patterns.size()];
for (int i = 0; i < patterns.size(); i++) {
authors[i] = patterns.get(i).getAuthor();
}
Arrays.sort(authors);
return authors;
}
public String[] getPatternNames() {
return getPatternAuthors();
}
public static void main(String args[]) throws Exception {
PatternStore p = new PatternStore(args[0]);
String[] names = p.getPatternAuthors();
for (String s : names) {
System.out.println(s);
}
}
}