Merge branch 'next' into new-database-38
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
1 /*
2  * Sone - MemoryDatabase.java - Copyright © 2011 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.database.memory;
19
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import net.pterodactylus.sone.core.Core;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.database.Database;
28 import net.pterodactylus.sone.database.DatabaseException;
29 import net.pterodactylus.util.collection.SetBuilder;
30 import net.pterodactylus.util.validation.Validation;
31
32 /**
33  * In-memory implementation of {@link Database} that stores all its data in a
34  * {@link Map}. This is roughly equivalent to what {@link Core} does today and
35  * is mainly used while Sone is transitioning to using a real database.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class MemoryDatabase implements Database {
40
41         /** The local Sones. */
42         private final Map<String, Sone> localSones = new HashMap<String, Sone>();
43
44         /** The remote Sones. */
45         private final Map<String, Sone> remoteSones = new HashMap<String, Sone>();
46
47         /**
48          * {@inheritDoc}
49          */
50         @Override
51         public Sone getSone(String id, boolean create) throws DatabaseException {
52                 if (getLocalSone(id, false) != null) {
53                         return getLocalSone(id, create);
54                 }
55                 return getRemoteSone(id, create);
56         }
57
58         /**
59          * {@inheritDoc}
60          */
61         @Override
62         public Collection<Sone> getSones() throws DatabaseException {
63                 return new SetBuilder<Sone>().addAll(getLocalSones()).addAll(getRemoteSones()).get();
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         @Override
70         public Collection<Sone> getLocalSones() throws DatabaseException {
71                 synchronized (localSones) {
72                         return Collections.unmodifiableCollection(localSones.values());
73                 }
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         @Override
80         public Collection<Sone> getRemoteSones() throws DatabaseException {
81                 synchronized (remoteSones) {
82                         return Collections.unmodifiableCollection(remoteSones.values());
83                 }
84         }
85
86         /**
87          * {@inheritDoc}
88          */
89         @Override
90         public Sone getLocalSone(String id, boolean create) throws DatabaseException {
91                 Validation.begin().isNotNull("Sone ID", id).check();
92                 synchronized (localSones) {
93                         if (!localSones.containsKey(id) && create) {
94                                 localSones.put(id, new MemorySone(id, true));
95                         }
96                         return localSones.get(id);
97                 }
98         }
99
100         /**
101          * {@inheritDoc}
102          */
103         @Override
104         public Sone getRemoteSone(String id, boolean create) throws DatabaseException {
105                 Validation.begin().isNotNull("Sone ID", id).check();
106                 synchronized (remoteSones) {
107                         if (!remoteSones.containsKey(id) && create) {
108                                 remoteSones.put(id, new MemorySone(id, false));
109                         }
110                         return remoteSones.get(id);
111                 }
112         }
113
114         /**
115          * {@inheritDoc}
116          */
117         @Override
118         public void saveSone(Sone sone) throws DatabaseException {
119                 if (sone.isLocal()) {
120                         synchronized (localSones) {
121                                 localSones.put(sone.getId(), sone);
122                         }
123                 } else {
124                         synchronized (remoteSones) {
125                                 Validation.begin().is("Sone is from this database", remoteSones.containsKey(sone.getId())).check();
126                                 remoteSones.put(sone.getId(), sone);
127                         }
128                 }
129         }
130
131         /**
132          * {@inheritDoc}
133          */
134         @Override
135         public void removeSone(Sone sone) throws DatabaseException {
136                 Map<String, Sone> sones = sone.isLocal() ? localSones : remoteSones;
137                 synchronized (sones) {
138                         sones.remove(sone.getId());
139                 }
140         }
141
142         /**
143          * {@inheritDoc}
144          */
145         @Override
146         public void removeSone(String id) throws DatabaseException {
147                 Map<String, Sone> sones = ((getSone(id, false) != null) && getSone(id, false).isLocal()) ? localSones : remoteSones;
148                 synchronized (sones) {
149                         sones.remove(id);
150                 }
151         }
152
153 }