Merge branch 'last-working' into next
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryFriendDatabase.java
1 package net.pterodactylus.sone.database.memory;
2
3 import java.util.Collection;
4 import java.util.concurrent.locks.ReadWriteLock;
5 import java.util.concurrent.locks.ReentrantReadWriteLock;
6
7 import com.google.common.collect.HashMultimap;
8 import com.google.common.collect.Multimap;
9
10 /**
11  * In-memory implementation of friend-related functionality.
12  *
13  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
14  */
15 class MemoryFriendDatabase {
16
17         private final ReadWriteLock lock = new ReentrantReadWriteLock();
18         private final ConfigurationLoader configurationLoader;
19         private final Multimap<String, String> soneFriends = HashMultimap.create();
20
21         MemoryFriendDatabase(ConfigurationLoader configurationLoader) {
22                 this.configurationLoader = configurationLoader;
23         }
24
25         Collection<String> getFriends(String localSoneId) {
26                 loadFriends(localSoneId);
27                 lock.readLock().lock();
28                 try {
29                         return soneFriends.get(localSoneId);
30                 } finally {
31                         lock.readLock().unlock();
32                 }
33         }
34
35         boolean isFriend(String localSoneId, String friendSoneId) {
36                 loadFriends(localSoneId);
37                 lock.readLock().lock();
38                 try {
39                         return soneFriends.containsEntry(localSoneId, friendSoneId);
40                 } finally {
41                         lock.readLock().unlock();
42                 }
43         }
44
45         void addFriend(String localSoneId, String friendSoneId) {
46                 loadFriends(localSoneId);
47                 lock.writeLock().lock();
48                 try {
49                         if (soneFriends.put(localSoneId, friendSoneId)) {
50                                 configurationLoader.saveFriends(localSoneId, soneFriends.get(localSoneId));
51                         }
52                 } finally {
53                         lock.writeLock().unlock();
54                 }
55         }
56
57         void removeFriend(String localSoneId, String friendSoneId) {
58                 loadFriends(localSoneId);
59                 lock.writeLock().lock();
60                 try {
61                         if (soneFriends.remove(localSoneId, friendSoneId)) {
62                                 configurationLoader.saveFriends(localSoneId, soneFriends.get(localSoneId));
63                         }
64                 } finally {
65                         lock.writeLock().unlock();
66                 }
67         }
68
69         private void loadFriends(String localSoneId) {
70                 lock.writeLock().lock();
71                 try {
72                         if (soneFriends.containsKey(localSoneId)) {
73                                 return;
74                         }
75                         soneFriends.putAll(localSoneId, configurationLoader.loadFriends(localSoneId));
76                 } finally {
77                         lock.writeLock().unlock();
78                 }
79         }
80
81 }