1 | package geniusweb.partiesserver.repository;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import tudelft.utilities.listener.DefaultListenable;
|
---|
9 | import tudelft.utilities.repository.Repository;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Stores all currently available parties that can be run.
|
---|
13 | */
|
---|
14 | public class AvailablePartiesRepo extends DefaultListenable<String>
|
---|
15 | implements Repository<String, AvailableParty> {
|
---|
16 | private static final AvailablePartiesRepo instance = new AvailablePartiesRepo();
|
---|
17 |
|
---|
18 | private Map<String, AvailableParty> availableParties = new HashMap<>();
|
---|
19 |
|
---|
20 | private AvailablePartiesRepo() {
|
---|
21 | }
|
---|
22 |
|
---|
23 | public static AvailablePartiesRepo instance() {
|
---|
24 | return instance;
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public Collection<AvailableParty> list() {
|
---|
29 | return Collections.unmodifiableCollection(availableParties.values());
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public AvailableParty get(String id) {
|
---|
34 | return availableParties.get(id);
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void put(AvailableParty entity) {
|
---|
39 | availableParties.put(entity.getID(), entity);
|
---|
40 | notifyListeners(entity.getID());
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void remove(String id) {
|
---|
45 | AvailableParty available = availableParties.get(id);
|
---|
46 | if (available != null) {
|
---|
47 | availableParties.remove(id);
|
---|
48 | available.free();
|
---|
49 | notifyListeners(id);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public void replace(AvailableParty entity) {
|
---|
55 | put(entity);
|
---|
56 | }
|
---|
57 |
|
---|
58 | }
|
---|