-
Notifications
You must be signed in to change notification settings - Fork 8
/
AvatarService.java
57 lines (48 loc) · 1.76 KB
/
AvatarService.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
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
/**
* Переписать функции create, changeName и addPerk на Reflection. Т.е. все вызовы и все операции создания объектов (кроме NullPointerException)
* в этих методах должны быть сделаны через Reflection
*/
public class AvatarService {
private Map<Long, Avatar> avatars;
public static final class Avatar {
private final static AtomicLong ID_GEN = new AtomicLong(0);
private final Long id;
private String name;
private List<Object> perks;
//serialization constructor
public Avatar() {
id = ID_GEN.getAndIncrement();
}
public Avatar(String name, List<Object> perks) {
id = ID_GEN.getAndIncrement();
this.name = name;
this.perks = perks;
}
public void setName(String name) {
this.name = name;
}
public List<Object> getPerks() {
return perks;
}
}
public Avatar create(String name, List<Object> perks) {
Avatar avatar = new Avatar(name, perks);
avatars.put(avatar.id, avatar);
return avatar;
}
public void changeName(long id, String newName) {
Avatar avatar = avatars.get(id);
if (avatar == null)
throw new NullPointerException("No avatar with id = " + id + " found");
avatar.setName(newName);
}
public void addPerk(long id, Object perk) {
Avatar avatar = avatars.get(id);
if (avatar == null)
throw new NullPointerException("No avatar with id = " + id + " found");
avatar.getPerks().add(perk);
}
}