From: David ‘Bombe’ Roden Date: Wed, 12 Oct 2011 05:44:01 +0000 (+0200) Subject: Add in-memory implementation of Sone database. X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=f744cc1bff979d9f493b116cf5ad40b603065be7 Add in-memory implementation of Sone database. --- diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneDatabase.java new file mode 100644 index 0000000..0bdf02b --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneDatabase.java @@ -0,0 +1,66 @@ +/* + * Sone - MemorySoneDatabase.java - Copyright © 2011 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.database.memory; + +import java.util.HashMap; +import java.util.Map; + +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.DatabaseException; +import net.pterodactylus.sone.database.SoneDatabase; +import net.pterodactylus.util.validation.Validation; + +/** + * In-memory implementation of {@link SoneDatabase} that stores all its data in + * a {@link Map}. This is roughly equivalent to what {@link Core} does today and + * is mainly used while Sone is transitioning to using a real database. + * + * @author David ‘Bombe’ Roden + */ +public class MemorySoneDatabase implements SoneDatabase { + + /** The Sones. */ + private final Map sones = new HashMap(); + + /** + * {@inheritDoc} + */ + @Override + public Sone getSone(String id, boolean create) throws DatabaseException { + Validation.begin().isNotNull("Sone ID", id).check(); + synchronized (sones) { + if (!sones.containsKey(id)) { + sones.put(id, new Sone(id)); + } + return sones.get(id); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void saveSone(Sone sone) throws DatabaseException { + synchronized (sones) { + Validation.begin().isNotNull("Sone", sone).check().is("Sone is an in-memory Sone", sones.containsKey(sone.getId())).check(); + } + /* this is a no-op. */ + } + +}