5ab1b28ce0f769b4db6f1e7a0e48cccc59d89518
[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                                 if (configurationLoader.getSoneFollowingTime(friendSoneId) == null) {
52                                         configurationLoader.setSoneFollowingTime(friendSoneId, System.currentTimeMillis());
53                                 }
54                         }
55                 } finally {
56                         lock.writeLock().unlock();
57                 }
58         }
59
60         void removeFriend(String localSoneId, String friendSoneId) {
61                 loadFriends(localSoneId);
62                 lock.writeLock().lock();
63                 try {
64                         if (soneFriends.remove(localSoneId, friendSoneId)) {
65                                 configurationLoader.saveFriends(localSoneId, soneFriends.get(localSoneId));
66                         }
67                 } finally {
68                         lock.writeLock().unlock();
69                 }
70         }
71
72         private void loadFriends(String localSoneId) {
73                 lock.writeLock().lock();
74                 try {
75                         if (soneFriends.containsKey(localSoneId)) {
76                                 return;
77                         }
78                         soneFriends.putAll(localSoneId, configurationLoader.loadFriends(localSoneId));
79                 } finally {
80                         lock.writeLock().unlock();
81                 }
82         }
83
84 }