-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
323 lines (279 loc) · 15.1 KB
/
Board.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
import java.util.*;
public class Board<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 = elements.values()
*
* AF: <password, { el_0, ..., el_dim }, dim> con
* forall i = 0, ..., dim |
* el_i = <categories.get(i).getCategoryName(), categories.get(i).getData, categories.get(i).getFriends()>
* categories.get(i).categoryName != null
*
* IR: password != null && dim = categories.size() &&
* ( forall i = 1, ..., dim |
* categories.get(i).categoryName != null
* && ( forall j = 0, ..., dim | categories.get(i).getCategoryName() != categories.get(j).categoryName() ) )
* && ( forall k, l = 0, ..., dim | k != l => categories.get(i).getFriend(k) != categories.get(i).getFriend(l) )
* && ( forall m, n = 0, ..., dim | m != n => categories.get(i).getData(m) != categories.get(i).getData(n) ) )
*
*/
private HashMap<String, Category<E>> elements;
private int dim;
private String password;
public Board(String password)
{
this.password = password;
this.dim = 0;
this.elements = new HashMap<String, Category<E>>();
}
/**
* Crea l’identità una categoria di dati
*
* @param Category t.c. !this.elements.containsKey(Category)
* @param passw t.c. password = passw
* @modifies this.elements
* @throws NullPointerException if Category = null
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws ExistingCategoryException if this.elements.containsKey(Category)
* @effects post(this.elements) = 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.elements.containsKey(Category)) throw new ExistingCategoryException();
elements.put(Category, new Category<E>(Category));
if(this.elements.containsKey(Category)) dim++;
}
// Rimuove l’identità una categoria di dati
/**
*
* @param Category t.c. this.elements.containsKey(Category)
* @param passw t.c. this.password.equals(passw)
* @modifies this.elements
* @throws InvalidCategoryExcetpion !this.elements.containsKey(Category)
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws NullPointerException if Category = null
* @effects post(this.elements) = pre(this.elements) \ el_i
*/
public void removeCategory(String Category, String passw) throws InvalidCategoryExcetpion, InvalidPasswordException, NullPointerException {
if(Category == null) throw new NullPointerException();
if(!this.elements.containsKey(Category)) throw new InvalidCategoryExcetpion();
if(!this.password.equals(passw)) throw new InvalidPasswordException();
elements.remove(Category);
}
// Aggiunge un amico ad una categoria di dati
/**
*
* @param Category t.c. this.elements.containsKey(Category)
* @param passw t.c. password = this.passw
* @param friend t.c. !this.elements.get(Category).contains(friend)
* @modifies this.el_i.friends
* @throws InvalidCategoryExcetpion if !this.elements.containsKey(Category)
* @throws NullPointerException if Category = null
* @throws ExistingFriendException if this.elements.get(Category).contains(friend)
* @throws InvalidPasswordException if !this.password.equals(passw)
* @effects post(this.elements.get(Category).getFriends()) = pre(this.elements.get(Category).getFriends()) U friend
*/
public void addFriend(String Category, String passw, String friend) throws InvalidCategoryExcetpion, ExistingFriendException, InvalidPasswordException, NullPointerException {
if(Category == null) throw new NullPointerException();
if(!this.elements.containsKey(Category)) throw new InvalidCategoryExcetpion();
if(!this.password.equals(passw)) throw new InvalidPasswordException();
this.elements.get(Category).addFriend(friend);
}
// rimuove un amico ad una categoria di dati
/**
*
* @param Category t.c. this.elements.containsKey(Category)
* @param friend t.c. this.elements.get(Category).contains(friend)
* @param passw t.c. password = this.passw
* @modifies this.el_i.friends
* @throws InvalidCategoryExcetpion if (forall j = 1, ..., numCategories() | el_j.categoryName != Category)
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws InvalidFriendException if !this.elements.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, InvalidPasswordException, InvalidFriendException, NullPointerException {
if(!this.elements.containsKey(Category)) throw new InvalidCategoryExcetpion();
if(!this.password.equals(passw)) throw new InvalidPasswordException();
if(friend == null || Category == null) throw new NullPointerException();
if(!this.elements.get(Category).removeFriend(friend)) throw new InvalidFriendException();
}
// Inserisce un dato in bacheca
// se vengono rispettati i controlli di identità
/**
*
* @param Category t.c. this.elements.containsKey(categoria)
* @param dato t.c. dato != null
* @param passw t.c. password = this.passw
* @modifies this.el_i.data
* @throws InvalidCategoryExcetpion if !this.elements.containsKey(categoria)
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws DuplicateDataException if this.elements.get(categoria).contains(dato)
* @throws NullPointerException if dato = null or Category = 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, DuplicateDataException, InvalidPasswordException, NullPointerException {
if(!this.elements.containsKey(categoria)) throw new InvalidCategoryExcetpion();
if(!this.password.equals(passw)) throw new InvalidPasswordException();
if(dato == null || categoria == null) throw new NullPointerException();
this.elements.get(categoria).addData(dato);
return this.elements.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.elements.get(category).contains(dato)
* @throws InvalidDataException if forall category = category.get(1), ..., category.get(dim) | !this.elements.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<Category<E>> categories = this.elements.values();
for(Category<E> category: categories) {
if(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 = category.get(1), ..., category.get(dim) | this.elements.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 = category.get(1), ..., category.get(dim) | !this.elements.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 {
boolean found = false;
for(Category<E> category : this.elements.values()) {
if(category.removeDataIfExists(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 this.elements.containsKey(Category)
* @param passw t.c. password = this.passw
* @throws InvalidPasswordException if !this.password.equals(passw)
* @throws InvalidCategoryExcetpion !this.elements.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.elements.containsKey(Category)) throw new InvalidCategoryExcetpion();
return this.elements.get(Category).getData();
}
// 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) throws InvalidPasswordException {
if(!this.password.equals(passw)) throw new InvalidPasswordException();
SortedSet<E> bacheca = new TreeSet<E>(new SortByLikes());
for(Category<E> category : this.elements.values()) {
if(category.getData() != null)
bacheca.addAll(category.getData());
}
return Collections.unmodifiableSortedSet(bacheca).iterator();
}
// Aggiunge un like a un dato
/**
*
* @param friend t.c. exist category = categories.get(1), ..., categories.get(dim) | this.elements.get(category).contains(friend)
* @param dato t.c. exist category = categories.get(1), ..., categories.get(dim) | (this.elements.get(category).contains(friend) && this.elements.get(category).contains(dato))
* @throws InvalidFriendException if forall category = categories.get(1), ..., categories.get(dim) | !this.elements.get(category).contains(friend)
* @throws InvalidDataException if forall category = categories.get(1), ..., categories.get(dim) | !(this.elements.get(category).contains(friend) && this.elements.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, DuplicateLikeException {
if(friend == null || dato == null) throw new NullPointerException();
boolean foundFriend = false, foundPost = false;
for(Category<E> category : this.elements.values()) {
if(category.contains(friend)) {
foundFriend = true;
if(category.contains(dato)){
foundPost = true;
if(category.contains(dato) && category.contains(friend)) {
dato.insertLike(friend);
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.elements.get(category).contains(friend)
* @throws InvalidFriendException if forall category = categories.get(1), ..., categories.get(dim) | !this.elements.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(Category<E> category : this.elements.values())
if(category.getFriends().contains(friend)) bacheca.addAll(category.getData());
if(bacheca.isEmpty()) throw new InvalidFriendException();
return Collections.unmodifiableSet(bacheca).iterator();
}
/**
*
* @return dim
*/
public int numCategories() {
return this.dim;
}
/**
*
* @param category t.c. this.elements.containsKey(category)
* @throws InvalidCategoryExcetpion if !this.elements.containsKey(category)
* @return this.elements.get(category).numFriends()
*/
public int numFriends(String category) throws InvalidCategoryExcetpion {
if(!this.elements.containsKey(category)) throw new InvalidCategoryExcetpion();
return this.elements.get(category).numFriends();
}
/**
*
* @param category t.c. this.elements.containsKey(category)
* @throws InvalidCategoryExcetpion if !this.elements.containsKey(category)
* @return this.elements.get(category).numData()
*/
public int numData(String category) throws InvalidCategoryExcetpion {
if(!this.elements.containsKey(category)) throw new InvalidCategoryExcetpion();
return this.elements.get(category).numData();
}
}