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 Sone getSone(String id, boolean create) throws DatabaseException {
52 if (getLocalSone(id, false) != null) {
53 return getLocalSone(id, create);
55 return getRemoteSone(id, create);
62 public Collection<Sone> getSones() throws DatabaseException {
63 return new SetBuilder<Sone>().addAll(getLocalSones()).addAll(getRemoteSones()).get();
70 public Collection<Sone> getLocalSones() throws DatabaseException {
71 synchronized (localSones) {
72 return Collections.unmodifiableCollection(localSones.values());
80 public Collection<Sone> getRemoteSones() throws DatabaseException {
81 synchronized (remoteSones) {
82 return Collections.unmodifiableCollection(remoteSones.values());
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 Sone(id, true));
96 return localSones.get(id);
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 Sone(id, false));
110 return remoteSones.get(id);
118 public void saveSone(Sone sone) throws DatabaseException {
119 if (sone.isLocal()) {
120 synchronized (localSones) {
121 localSones.put(sone.getId(), sone);
124 synchronized (remoteSones) {
125 Validation.begin().is("Sone is from this database", remoteSones.containsKey(sone.getId())).check();
126 remoteSones.put(sone.getId(), sone);
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());
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) {