Only create Sones if create is actually true.
[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 boolean isLocalSone(Sone sone) throws DatabaseException {
52                 Validation.begin().isNotNull("Sone", sone).check();
53                 return isLocalSone(sone.getId());
54         }
55
56         /**
57          * {@inheritDoc}
58          */
59         @Override
60         public boolean isLocalSone(String id) throws DatabaseException {
61                 Validation.begin().isNotNull("Sone ID", id).check();
62                 synchronized (localSones) {
63                         return localSones.containsKey(id);
64                 }
65         }
66
67         /**
68          * {@inheritDoc}
69          */
70         @Override
71         public boolean isRemoteSone(Sone sone) throws DatabaseException {
72                 Validation.begin().isNotNull("Sone", sone).check();
73                 return isRemoteSone(sone.getId());
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         @Override
80         public boolean isRemoteSone(String id) throws DatabaseException {
81                 Validation.begin().isNotNull("Sone ID", id).check();
82                 synchronized (remoteSones) {
83                         return remoteSones.containsKey(id);
84                 }
85         }
86
87         /**
88          * {@inheritDoc}
89          */
90         @Override
91         public Sone getSone(String id, boolean create) throws DatabaseException {
92                 if (isLocalSone(id)) {
93                         return getLocalSone(id, create);
94                 }
95                 return getRemoteSone(id, create);
96         }
97
98         /**
99          * {@inheritDoc}
100          */
101         @Override
102         public Collection<Sone> getSones() throws DatabaseException {
103                 return new SetBuilder<Sone>().addAll(getLocalSones()).addAll(getRemoteSones()).get();
104         }
105
106         /**
107          * {@inheritDoc}
108          */
109         @Override
110         public Collection<Sone> getLocalSones() throws DatabaseException {
111                 synchronized (localSones) {
112                         return Collections.unmodifiableCollection(localSones.values());
113                 }
114         }
115
116         /**
117          * {@inheritDoc}
118          */
119         @Override
120         public Collection<Sone> getRemoteSones() throws DatabaseException {
121                 synchronized (remoteSones) {
122                         return Collections.unmodifiableCollection(remoteSones.values());
123                 }
124         }
125
126         /**
127          * {@inheritDoc}
128          */
129         @Override
130         public Sone getLocalSone(String id, boolean create) throws DatabaseException {
131                 Validation.begin().isNotNull("Sone ID", id).check();
132                 synchronized (localSones) {
133                         if (!localSones.containsKey(id) && create) {
134                                 localSones.put(id, new Sone(id));
135                         }
136                         return localSones.get(id);
137                 }
138         }
139
140         /**
141          * {@inheritDoc}
142          */
143         @Override
144         public Sone getRemoteSone(String id, boolean create) throws DatabaseException {
145                 Validation.begin().isNotNull("Sone ID", id).check();
146                 synchronized (remoteSones) {
147                         if (!remoteSones.containsKey(id) && create) {
148                                 remoteSones.put(id, new Sone(id));
149                         }
150                         return remoteSones.get(id);
151                 }
152         }
153
154         /**
155          * {@inheritDoc}
156          */
157         @Override
158         public void saveSone(Sone sone) throws DatabaseException {
159                 if (isLocalSone(sone)) {
160                         synchronized (localSones) {
161                                 localSones.put(sone.getId(), sone);
162                         }
163                 } else {
164                         synchronized (remoteSones) {
165                                 Validation.begin().is("Sone is from this database", remoteSones.containsKey(sone.getId())).check();
166                                 remoteSones.put(sone.getId(), sone);
167                         }
168                 }
169         }
170
171         /**
172          * {@inheritDoc}
173          */
174         @Override
175         public void removeSone(Sone sone) throws DatabaseException {
176                 Map<String, Sone> sones = isLocalSone(sone) ? localSones : remoteSones;
177                 synchronized (sones) {
178                         sones.remove(sone.getId());
179                 }
180         }
181
182         /**
183          * {@inheritDoc}
184          */
185         @Override
186         public void removeSone(String id) throws DatabaseException {
187                 Map<String, Sone> sones = isLocalSone(id) ? localSones : remoteSones;
188                 synchronized (sones) {
189                         sones.remove(id);
190                 }
191         }
192
193 }