2 * Sone - MemoryDatabase.java - Copyright © 2011 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.database.memory;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
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;
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.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class MemoryDatabase implements Database {
41 /** The local Sones. */
42 private final Map<String, Sone> localSones = new HashMap<String, Sone>();
44 /** The remote Sones. */
45 private final Map<String, Sone> remoteSones = new HashMap<String, Sone>();
51 public boolean isLocalSone(Sone sone) throws DatabaseException {
52 Validation.begin().isNotNull("Sone", sone).check();
53 return isLocalSone(sone.getId());
60 public boolean isLocalSone(String id) throws DatabaseException {
61 Validation.begin().isNotNull("Sone ID", id).check();
62 synchronized (localSones) {
63 return localSones.containsKey(id);
71 public boolean isRemoteSone(Sone sone) throws DatabaseException {
72 Validation.begin().isNotNull("Sone", sone).check();
73 return isRemoteSone(sone.getId());
80 public boolean isRemoteSone(String id) throws DatabaseException {
81 Validation.begin().isNotNull("Sone ID", id).check();
82 synchronized (remoteSones) {
83 return remoteSones.containsKey(id);
91 public Sone getSone(String id, boolean create) throws DatabaseException {
92 if (isLocalSone(id)) {
93 return getLocalSone(id, create);
95 return getRemoteSone(id, create);
102 public Collection<Sone> getSones() throws DatabaseException {
103 return new SetBuilder<Sone>().addAll(getLocalSones()).addAll(getRemoteSones()).get();
110 public Collection<Sone> getLocalSones() throws DatabaseException {
111 synchronized (localSones) {
112 return Collections.unmodifiableCollection(localSones.values());
120 public Collection<Sone> getRemoteSones() throws DatabaseException {
121 synchronized (remoteSones) {
122 return Collections.unmodifiableCollection(remoteSones.values());
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)) {
134 localSones.put(id, new Sone(id));
136 return localSones.get(id);
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)) {
148 remoteSones.put(id, new Sone(id));
150 return remoteSones.get(id);
158 public void saveSone(Sone sone) throws DatabaseException {
159 if (isLocalSone(sone)) {
160 synchronized (localSones) {
161 localSones.put(sone.getId(), sone);
164 synchronized (remoteSones) {
165 Validation.begin().is("Sone is from this database", remoteSones.containsKey(sone.getId())).check();
166 remoteSones.put(sone.getId(), sone);
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());
186 public void removeSone(String id) throws DatabaseException {
187 Map<String, Sone> sones = isLocalSone(id) ? localSones : remoteSones;
188 synchronized (sones) {