-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board2.java
179 lines (158 loc) · 6.85 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
import java.util.*;
public class Board2<E extends Data<?>> implements DataBoard<E> {
private String password;
private HashMap<String,Category<E>> board;
public Board2(String password){
this.password= password;
this.board = new HashMap<String,Category<E>>();
}
public void createCategory(String Category, String passw) throws ImpossibleToPerform {
if(Category == null || passw == null)
throw new NullPointerException();
if(this.board.containsKey(Category))
throw new ImpossibleToPerform("Categoria gia' presente");
if(!this.password.equals(passw))
throw new ImpossibleToPerform("Password errata");
this.board.put(Category,new Category<>(Category));
}
@Override
public void removeCategory(String Category, String passw) throws ImpossibleToPerform {
if(Category == null || passw == null)
throw new NullPointerException();
if(!this.board.containsKey(Category))
throw new ImpossibleToPerform("Non esiste la categoria richiesta");
if(!this.password.equals(passw))
throw new ImpossibleToPerform("Password errata");
this.board.remove(Category);
}
public void addFriend(String Category, String passw, String friend) throws ImpossibleToPerform, FriendAlreadyPresent {
if(Category == null || passw == null || friend == null)
throw new NullPointerException();
if(!this.board.containsKey(Category))
throw new ImpossibleToPerform("Categoria non presente");
if(!this.password.equals(passw))
throw new ImpossibleToPerform("Password errata");
if(this.board.get(Category).firendsList().contains(friend))
throw new FriendAlreadyPresent("Amico presente nella categoria");
this.board.get(Category).addFriend(friend);
}
@Override
public void removeFriend(String Category, String passw, String friend) throws ImpossibleToPerform {
if(Category == null || passw == null || friend == null)
throw new NullPointerException();
if(!this.board.containsKey(Category))
throw new ImpossibleToPerform("Categoria non presente");
if(!this.password.equals(passw))
throw new ImpossibleToPerform("Password errata");
if(!this.board.get(Category).firendsList().contains(friend))
throw new ImpossibleToPerform("Amico non presente");
this.board.get(Category).firendsList().remove(friend);
}
public boolean put(String passw, E dato, String categoria) throws ImpossibleToPerform {
if(passw == null || dato == null || categoria == null)
throw new NullPointerException();
if(!this.password.equals(passw))
throw new ImpossibleToPerform("Password errata");
if(!this.board.containsKey(categoria))
throw new ImpossibleToPerform("Categoria non presente");
if(this.board.get(categoria).datasList().contains(dato))
throw new ImpossibleToPerform("Dato gia' presente");
this.board.get(categoria).datasList().add(dato);
return true;
}
@Override
public E get(String passw, E dato) throws ImpossibleToPerform {
if(passw== null || dato == null)
throw new NullPointerException();
if(!this.password.equals(passw))
throw new ImpossibleToPerform("Password errata");
boolean finder=true;
for(Category<E> cat: board.values()){
if(cat.datasList().contains(dato))
finder=true;
}
if(!finder)
throw new ImpossibleToPerform("Dato non presente");
return (E) dato.cloneData();
}
@Override
public E remove(String passw, E dato) throws ImpossibleToPerform {
if (passw == null || dato == null)
throw new NullPointerException();
if (!this.password.equals(passw))
throw new ImpossibleToPerform("Password errata");
boolean finder = false;
for (Category<E> cat : this.board.values()) {
if(cat.datasList().contains(dato)) {
cat.datasList().remove(dato);
finder = true;
}
}
if(finder) return (E) dato.cloneData();
throw new ImpossibleToPerform("Dato non presente");
}
@Override
public List<E> getDataCategory(String passw, String Category) throws ImpossibleToPerform {
if(passw == null || Category == null)
throw new NullPointerException();
if(!this.password.equals(passw))
throw new ImpossibleToPerform("Password errata");
if(!this.board.containsKey(Category))
throw new ImpossibleToPerform("Categoria non presente");
List<E> myList= this.board.get(Category).datasList();
return myList;
}
public Iterator<E> getIterator(String passw) throws ImpossibleToPerform {
if(passw == null)
throw new NullPointerException();
if(!this.password.equals(passw))
throw new ImpossibleToPerform("Password errata");
SortedSet<E> showcase = new TreeSet<E>((Comparator<? super E>) new Sort());
for(Category cat : this.board.values()){
if(cat.datasList()!=null)
showcase.addAll(cat.datasList());
}
return Collections.unmodifiableSortedSet(showcase).iterator();
}
@Override
public void insertLike(String friend, E data) throws LikeAlredypresent, ImpossibleToPerform {
if(friend == null || data == null)
throw new NullPointerException();
boolean findFriend=false;
boolean findData=false;
for(Category cat : board.values()){
if(cat.firendsList().contains(friend))
findFriend=true;
if(cat.datasList().contains(data))
findData=true;
}
if(findFriend==true && findData==true)
data.insertLikes(friend);
if(!findFriend) throw new ImpossibleToPerform("Amico non presente");
if(!findData) throw new ImpossibleToPerform("Dato non presente");
}
@Override
public Iterator<E> getFriendIterator(String friend) throws ImpossibleToPerform {
if(friend == null)
throw new NullPointerException();
boolean findFriend=false;
ArrayList<E> all_datas = new ArrayList<E>();
for(Category cat : board.values()){
if(cat.firendsList().contains(friend)){
all_datas.addAll(cat.datasList());
findFriend=true;
}
}
if(!findFriend)
throw new ImpossibleToPerform("Amico non presente");
return all_datas.iterator();
}
class Sort implements Comparator<MyData<?>> {
@Override
public int compare(MyData<?> o1, MyData<?> o2) {
if (o2 == null) return -1;
if (o1 == null) return 1;
return o2.getLikes() - o1.getLikes();
}
}
}