-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternStore.java
More file actions
176 lines (154 loc) · 4.57 KB
/
Copy pathPatternStore.java
File metadata and controls
176 lines (154 loc) · 4.57 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package uk.ac.cam.efxlb2.oop.tick5;
import java.io.*;
import java.net.*;
import java.util.*;
public class PatternStore {
private List<Pattern> patterns = new LinkedList<>();
private Map<String,List<Pattern>> mapAuths = new HashMap<>();
private Map<String,Pattern> mapName = new HashMap<>();
public List<Pattern> getpatterns(){
return patterns;
}
public Map<String,List<Pattern>> getmapAuths(){
return mapAuths;
}
public Map<String,Pattern> getmapName(){
return mapName;
}
public PatternStore(String source) throws IOException {
try {
if (source.startsWith("https://")||source.startsWith("http://")) {
loadFromURL(source);
}
else {
loadFromDisk(source);
}
}
catch (PatternFormatException p) {
System.out.println(p.getMessage());
}
}
public PatternStore(Reader source) throws IOException {
try {
load(source);
}
catch (PatternFormatException p) {
System.out.println(p.getMessage());
}
}
private void load(Reader r) throws IOException,PatternFormatException{
BufferedReader b = new BufferedReader(r);
String line;
while ( (line = b.readLine()) != null) {
try {
Pattern pattern = new Pattern(line);
patterns.add(pattern);
mapName.put(pattern.getName(),pattern);
authormap(pattern);
}
catch(PatternFormatException p) {
System.out.println("Warning, "+line+"is a malformed pattern");
}
}
}
private void authormap(Pattern pattern){
if (mapAuths.get(pattern.getAuthor())==null){
List<Pattern> authlist = new ArrayList<>();
authlist.add(pattern);
mapAuths.put(pattern.getAuthor(),authlist);
}
else {
List<Pattern> authlist = new ArrayList<>(mapAuths.get(pattern.getAuthor()));
authlist.add(pattern);
mapAuths.put(pattern.getAuthor(),authlist);
}
}
private void loadFromURL(String url) throws IOException,PatternFormatException {
try {
URL destination = new URL(url);
URLConnection conn = destination.openConnection();
Reader r = new InputStreamReader(conn.getInputStream());
load(r);
}
catch (PatternFormatException p) {
System.out.println(p.getMessage());
}
}
private void loadFromDisk(String filename) throws IOException,PatternFormatException {
try {
Reader r = new FileReader(filename);
load(r);
}
catch (PatternFormatException p) {
System.out.println(p.getMessage());
}
}
public static void main(String[] args) throws IOException,PatternFormatException{
PatternStore ps = new PatternStore("https://www.cl.cam.ac.uk/teaching/1819/OOProg/ticks/lifetest.txt");
}
public List<Pattern> getPatternsNameSorted() {
List<Pattern> output = new LinkedList<>(patterns);
Collections.sort(output);
return output;
}
public List<Pattern> getPatternsAuthorSorted() throws PatternNotFound{
try {
List<String> authors = new LinkedList<>(getPatternAuthors());
List<Pattern> output = new LinkedList<>();
for (int i=0;i<authors.size();i++) {
if ((getPatternsByAuthor(authors.get(i)))==null) {
throw new PatternNotFound("There are no patterns for this author");
}
output.addAll(getPatternsByAuthor(authors.get(i)));
}
return output;
}
catch (PatternNotFound p) {
System.out.println(p.getMessage());
List<Pattern> noutput = new LinkedList<>();
return noutput;
}
}
public List<Pattern> getPatternsByAuthor(String author) throws PatternNotFound {
try {
List<Pattern> output = new LinkedList<>();
if (mapAuths.get(author)==null) {
throw new PatternNotFound("No patterns were found for this author");
}
output = mapAuths.get(author);
Collections.sort(output);
return output;
}
catch (PatternNotFound p) {
System.out.println(p.getMessage());
List<Pattern> output = new LinkedList<>();
return output;
}
}
public Pattern getPatternByName(String name) throws PatternNotFound {
try{
if (mapName.get(name)==null) {
throw new PatternNotFound("No patterns were found with this name");
}
Pattern output = mapName.get(name);
return output;
}
catch (PatternNotFound p) {
System.out.println(p.getMessage());
Pattern pat =null;
return pat;
}
}
public List<String> getPatternAuthors() {
List<String> output = new LinkedList<>();
output.addAll(mapAuths.keySet());
Collections.sort(output);
return output;
}
public List<String> getPatternNames() {
List<String> output = new LinkedList<>();
output.addAll(mapName.keySet());
Collections.sort(output);
return output;
}
}