-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard2.java
332 lines (287 loc) · 15.2 KB
/
Board2.java
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import java.util.*;
public class Board2<E extends Data<?>> implements DataBoard<E> {
/**
*
* Overview: contenitore di oggetti generici che estendono il tipo di dato Data. Ogni
* dato presente nella bacheca ha associato la categoria del dato.
*
*
* categories = dataset.keySet() = friends.keySet()
*
* AF: <password, { el_0, ..., el_dim }, dim> con
* forall i = 0, ..., dim |
* el_i = <categories.get(i), dataSet.get(categories.get(i)), friends.get(categories.get(i))>
* categories.get(i) != null
*
* IR: password != null && dim = categories.size() &&
* ( forall i = 1, ..., dim |
* categories.get(i) != null
* && ( forall j = 0, ..., dim | categories.get(i) != categories.get(j) ) )
* && ( forall k, l = 0, ..., dim | k != l => friends.get(categories.get(i)).get(k) != friends.get(categories.get(i)).get(l) )
* && ( forall m, n = 0, ..., dim | m != n => dataSet.get(categories.get(i)).get(m) != dataSet.get(categories.get(i)).get(n) ) )
* && dataset.keySet() = friends.keySet()
*
*/
private int dim;
private String password;
private HashMap<String, List<E>> dataSet;
private HashMap<String, List<String>> friends;
public Board2(String password)
{
this.password = password;
this.dim = 0;
this.dataSet = new HashMap<String, List<E>>();
this.friends = new HashMap<String, List<String>>();
}
/**
* Crea l’identità una categoria di dati
*
* @param Category t.c. !this.dataSet.containsKey(Category)
* @param passw t.c. password = passw
* @modifies this.elems
* @throws NullPointerException if Category = null
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws ExistingCategoryException if this.dataSet.containsKey(Category)
* @effects post(this.elems) = pre(this.el_i) U <Category, null, null>
*/
public void createCategory(String Category, String passw) throws NullPointerException, InvalidPasswordException, ExistingCategoryException {
if(Category == null) throw new NullPointerException();
if(!this.password.equals(passw)) throw new InvalidPasswordException();
if(this.dataSet.containsKey(Category)) throw new ExistingCategoryException();
dataSet.put(Category, new ArrayList<E>());
friends.put(Category, new ArrayList<String>());
this.dim++;
}
// Rimuove l’identità una categoria di dati
/**
*
* @param Category t.c. this.dataSet.containsKey(Category)
* @param passw t.c. password = this.passw
* @modifies this.elems
* @throws InvalidCategoryExcetpion if !this.dataSet.containsKey(Category)
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws NullPointerException if Category = null
* @effects post(this.elems) = pre(this.elems) \ el_i
*/
public void removeCategory(String Category, String passw) throws InvalidCategoryExcetpion, InvalidPasswordException {
if(Category == null) throw new NullPointerException();
if(!this.dataSet.containsKey(Category)) throw new InvalidCategoryExcetpion();
if(!this.password.equals(passw)) throw new InvalidPasswordException();
this.dataSet.remove(Category);
this.friends.remove(Category);
}
// Aggiunge un amico ad una categoria di dati
/**
*
* @param Category t.c. this.friends.containsKey(Category)
* @param passw t.c. password = this.passw
* @param friend t.c. !this.friends.get(Category).contains(friend)
* @modifies this.el_i.friends
* @throws InvalidCategoryExcetpion if !this.friends.containsKey(Category)
* @throws NullPointerException if Category = null
* @throws ExistingFriendException if this.friends.get(Category).contains(friend)
* @throws InvalidPasswordException if !this.password.equals(passw)
* @effects post(this.el_i.friends) = pre(this.el_i.friends) U friend
*/
public void addFriend(String Category, String passw, String friend) throws InvalidCategoryExcetpion, ExistingFriendException, InvalidPasswordException {
if(Category == null) throw new NullPointerException();
if(!this.friends.containsKey(Category)) throw new InvalidCategoryExcetpion();
if(this.friends.get(Category).contains(friend)) throw new ExistingFriendException();
if(!this.password.equals(passw)) throw new InvalidPasswordException();
this.friends.get(Category).add(friend);
}
// rimuove un amico ad una categoria di dati
/**
*
* @param Category t.c. this.friends.containsKey(Category)
* @param friend t.c. this.friends.get(Category).contains(friend)
* @param passw t.c. password = this.passw
* @modifies this.el_i.friends
* @throws InvalidCategoryExcetpion if !this.friends.containsKey(Category)
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws InvalidFriendException if !this.friends.get(Category).contains(friend)
* @throws NullPointerException if friend = null or Category = null
* @effects post(this.el_i.friends) = pre(this.el_i.friends) \ friend
*/
public void removeFriend(String Category, String passw, String friend) throws InvalidCategoryExcetpion, InvalidFriendException, InvalidPasswordException {
if(!this.friends.containsKey(Category)) throw new InvalidCategoryExcetpion();
if(!this.password.equals(passw)) throw new InvalidPasswordException();
if(friend == null || Category == null) throw new NullPointerException();
if(!this.friends.get(Category).contains(friend)) throw new InvalidFriendException();
this.friends.get(Category).remove(friend);
}
// Inserisce un dato in bacheca
// se vengono rispettati i controlli di identità
/**
*
* @param Category t.c. this.dataSet.containsKey(categoria)
* @param dato t.c. !this.dataSet.get(categoria).contains(dato)
* @param passw t.c. password = this.passw
* @modifies this.el_i.data
* @throws InvalidCategoryExcetpion if !this.dataSet.containsKey(categoria)
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws DuplicateDataException if this.dataSet.get(categoria).contains(dato)
* @throws NullPointerException if categoria = null or dato = null
* @effects post(this.el_i.data) = pre(this.el_i.dataSet) U dato
*/
public boolean put(String passw, E dato, String categoria) throws DuplicateDataException, InvalidCategoryExcetpion, InvalidPasswordException {
if(!this.dataSet.containsKey(categoria)) throw new InvalidCategoryExcetpion();
if(!this.password.equals(passw)) throw new InvalidPasswordException();
if(this.dataSet.get(categoria).contains(dato)) throw new DuplicateDataException();
if(dato == null || categoria == null) throw new NullPointerException();
this.dataSet.get(categoria).add(dato);
return this.dataSet.get(categoria).contains(dato);
}
// Ottiene una copia del del dato in bacheca
// se vengono rispettati i controlli di identità
/**
*
* @param passw this.password = passw
* @param dato t.c. exist category = categories.get(1), ..., categories.get(dim) | this.dataSet.get(category).contains(dato)
* @throws InvalidDataException if forall category = categories.get(1), ..., categories.get(dim) | !this.dataSet.get(category).contains(dato)
* @throws InvalidPasswordException if !this.password.equals(passw)
* @return this.el_i.data[j]
*/
public E get(String passw, E dato) throws InvalidDataException, InvalidPasswordException {
if(!this.password.equals(passw)) throw new InvalidPasswordException();
Collection<String> categories = this.dataSet.keySet();
for(String category : categories) {
if(this.dataSet.get(category).contains(dato))
return (E) dato.cloneData();
}
throw new InvalidDataException();
}
// Rimuove il dato dalla bacheca
// se vengono rispettati i controlli di identità
/**
*
* @param dato t.c. exist category = categories.get(1), ..., categories.get(dim) | this.dataSet.get(category).contains(dato)
* @param passw t.c. password = this.password
* @modifies this.el_i.data
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws InvalidDataException if forall category = categories.get(1), ..., categories.get(dim) | !this.dataSet.get(category).contains(dato)
* @effects post(this.el_i.data) = pre(this.el_i.dataSet) \ dato forall i = 1, ...., numCategories | ( exist j = 1, ..., numData(el_i.categoryName) | el_i.data[j] = dato )
* @return this.el_i.data[j]
*/
public E remove(String passw, E dato) throws InvalidDataException, InvalidPasswordException {
if(!this.password.equals(passw)) throw new InvalidPasswordException();
boolean found = false;
for(String category : this.dataSet.keySet()) {
this.dataSet.get(category).remove(dato);
found = true;
}
if(found) return dato;
throw new InvalidDataException();
}
// Crea la lista dei dati in bacheca su una determinata categoria
// se vengono rispettati i controlli di identità
/**
*
* @param Category t.c. this.dataSet.containsKey(Category)
* @param passw t.c. password = this.passw
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws InvalidCategoryExcetpion if !this.dataSet.containsKey(Category)
* @throws NullPointerException if Category = null
* @return { data[1], ..., data[numData(el_i.categoryName)] }
*/
public List<E> getDataCategory(String passw, String Category) throws InvalidCategoryExcetpion, InvalidPasswordException {
if(!this.password.equals(passw)) throw new InvalidPasswordException();
if(Category == null) throw new NullPointerException();
if(!this.dataSet.containsKey(Category)) throw new InvalidCategoryExcetpion();
return this.dataSet.get(Category);
}
// restituisce un iteratore (senza remove) che genera tutti i dati in
// bacheca ordinati rispetto al numero di like.
/**
*
* @param passw t.c. password = this.passw
* @modifies this.elems
* @throws InvalidPasswordException if !this.password.equals(passw)
* @return iteratore di data[iter_1], ...., data[iter_n], lista ordinata con
* n = numData(el_1.categoryName) + ... + numData(el_numCategories().categoryName) &&
* forall i = 1, ..., n | ( exist j = 1, ...., numCategories() | ( exist k = 1, ..., numData(el_j.categoryName) | el_j.data[k] = data[iter_i] ) ) &&
* (forall i,j = 1, ...., n | i < j => data[iter_i].likes < data[iter_j].likes)
*/
public Iterator<E> getIterator(String passw) {
SortedSet<E> bacheca = new TreeSet<E>(new SortByLikes());
for(String category : this.dataSet.keySet())
bacheca.addAll(this.dataSet.get(category));
return Collections.unmodifiableSortedSet(bacheca).iterator();
}
// Aggiunge un like a un dato
/**
*
* @param friend t.c. exist category = categories.get(1), ..., categories.get(dim) | this.friends.get(category).contains(friend)
* @param dato t.c. exist category = categories.get(1), ..., categories.get(dim) | (this.friends.get(category).contains(friend) && this.friends.get(category).contains(dato))
* @throws InvalidFriendException if forall category = categories.get(1), ..., categories.get(dim) | !this.friends.get(category).contains(friend)
* @throws InvalidDataException if forall category = categories.get(1), ..., categories.get(dim) | !(this.friends.get(category).contains(friend) && this.friends.get(category).contains(dato))
* @modifies this.el_i.data[k].likes
* @effects post(this.el_i.data[k].likes) = pre(this.el_i.data[k].likes) + 1
*/
public void insertLike(String friend, E dato) throws InvalidFriendException, InvalidDataException {
if(friend == null || dato == null) throw new NullPointerException();
boolean foundFriend = false, foundPost = false;
for(String category : this.dataSet.keySet()) {
if(this.friends.get(category).contains(friend)) {
foundFriend = true;
if(this.dataSet.get(category).contains(dato)) {
foundPost = true;
try {
dato.insertLike(friend);
} catch(DuplicateLikeException e) {
System.out.println(friend + " ha gia' messo like al post");
}
return;
}
}
}
if(!foundFriend) throw new InvalidFriendException();
if(!foundPost) throw new InvalidDataException();
}
// Legge un dato condiviso
// restituisce un iteratore (senza remove) che genera tutti i dati in
// bacheca condivisi.
/**
*
* @param friend t.c. exist category = categories.get(1), ..., categories.get(dim) | this.friends.get(category).contains(friend)
* @throws InvalidFriendException if forall category = categories.get(1), ..., categories.get(dim) | !this.friends.get(category).contains(friend)
* @throws NullPointerException if friend = null
* @return iteratore di data[iter_1], ...., data[iter_n], lista ordinata con
* n = numData(el_cat_1.categoryName) + ... + numData(el_cat_m) &&
* m = #{ i | 0 < i < numCategories() && exist j = 1, ..., numFriends(el_i.categoryName t.c. el_i.friend[j] = friend) }
* forall i = 1, ..., n | ( exist j = 1, ...., numCategories() | ( exist k = 1, ..., numData(el_j.categoryName) | el_j.data[k] = data[iter_i] ) )
*/
public Iterator<E> getFriendIterator(String friend) throws InvalidFriendException {
if(friend == null) throw new NullPointerException();
Set<E> bacheca = new HashSet<E>();
for(String category : this.dataSet.keySet())
if(friends.get(category).contains(friend)) bacheca.addAll(this.dataSet.get(category));
if(bacheca.isEmpty()) throw new InvalidFriendException();
return Collections.unmodifiableSet(bacheca).iterator();
}
/**
*
* @return dim
*/
public int numCategories() {
return this.dim;
}
/**
*
* @param category t.c. this.dataSet.containsKey(Category)
* @throws InvalidCategoryExcetpion if !this.dataSet.containsKey(Category)
* @return this.friends.get(category).size()
*/
public int numFriends(String category) throws InvalidCategoryExcetpion {
return this.friends.get(category).size();
}
/**
*
* @param category t.c. this.dataSet.containsKey(Category)
* @throws InvalidCategoryExcetpion if !this.dataSet.containsKey(Category)
* @return this.dataSet.get(category).size()
*/
public int numData(String category) throws InvalidCategoryExcetpion {
return this.dataSet.get(category).size();
}
}