Move friend-related functionality into the database.
[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                 lock.writeLock().lock();
47                 try {
48                         if (soneFriends.put(localSoneId, friendSoneId)) {
49                                 configurationLoader.saveFriends(localSoneId, soneFriends.get(localSoneId));
50                         }
51                 } finally {
52                         lock.writeLock().unlock();
53                 }
54         }
55
56         void removeFriend(String localSoneId, String friendSoneId) {
57                 lock.writeLock().lock();
58                 try {
59                         if (soneFriends.remove(localSoneId, friendSoneId)) {
60                                 configurationLoader.saveFriends(localSoneId, soneFriends.get(localSoneId));
61                         }
62                 } finally {
63                         lock.writeLock().unlock();
64                 }
65         }
66
67         private void loadFriends(String localSoneId) {
68                 lock.writeLock().lock();
69                 try {
70                         if (soneFriends.containsKey(localSoneId)) {
71                                 return;
72                         }
73                         soneFriends.putAll(localSoneId, configurationLoader.loadFriends(localSoneId));
74                 } finally {
75                         lock.writeLock().unlock();
76                 }
77         }
78
79 }