From 9a727406f8647dffc6598ed11b7cc7bef82b38c5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Oct 2010 14:03:03 +0200 Subject: [PATCH] Use a more generic Shell-aware cache. --- .../net/pterodactylus/sone/core/PostCache.java | 66 ---------------------- .../net/pterodactylus/sone/data/ShellCache.java | 64 +++++++++++++++++++++ 2 files changed, 64 insertions(+), 66 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/core/PostCache.java create mode 100644 src/main/java/net/pterodactylus/sone/data/ShellCache.java diff --git a/src/main/java/net/pterodactylus/sone/core/PostCache.java b/src/main/java/net/pterodactylus/sone/core/PostCache.java deleted file mode 100644 index 5f830ad..0000000 --- a/src/main/java/net/pterodactylus/sone/core/PostCache.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Sone - PostCache.java - Copyright © 2010 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.core; - -import java.util.HashMap; -import java.util.Map; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostShell; -import net.pterodactylus.sone.data.Shell; - -/** - * {@link Shell}-aware {@link Map} from post IDs to {@link Post}s that exchanges - * an existing {@link Shell} against the real object once it’s available. - * - * @author David ‘Bombe’ Roden - */ -public class PostCache { - - /** The posts. */ - private final Map posts = new HashMap(); - - /** - * Stores the given post in this cache. If the given post is not a - * {@link Shell}, it is stored. If it is a {@link Shell} it is only stored - * if there is no post stored for the ID of the given post. If the given - * {@code post} is a {@link Shell}, it needs to have its - * {@link PostShell#setId(java.util.UUID) ID} set! - * - * @param post - * The post to store - */ - public void store(Post post) { - if (!(post instanceof Shell) || !posts.containsKey(post.getId())) { - posts.put(post.getId(), post); - } - } - - /** - * Returns the post with the given ID. - * - * @param id - * The ID of the post - * @return The post with the given ID, or {@code null} if there is no post - * with the given ID - */ - public Post get(String id) { - return posts.get(id); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/data/ShellCache.java b/src/main/java/net/pterodactylus/sone/data/ShellCache.java new file mode 100644 index 0000000..9da9a06 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/ShellCache.java @@ -0,0 +1,64 @@ +/* + * Sone - ShellCache.java - Copyright © 2010 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.data; + +import java.util.HashMap; +import java.util.Map; + +/** + * {@link Shell}-aware cache that will replace {@link Shell}s with the real + * objects but not the other way around. + * + * @param + * The type of the cached objects + * @author David ‘Bombe’ Roden + */ +public class ShellCache { + + /** The object cache. */ + private final Map cache = new HashMap(); + + /** + * Stores the given object in this cache. If the given object is not a + * {@link Shell}, it is stored. If it is a {@link Shell} it is only stored + * if there is no object stored for the given ID. + * + * @param id + * The ID of the object + * @param object + * The object to store + */ + public void put(String id, T object) { + if (!(object instanceof Shell) || !cache.containsKey(id)) { + cache.put(id, object); + } + } + + /** + * Returns the post with the given ID. + * + * @param id + * The ID of the post + * @return The post with the given ID, or {@code null} if there is no post + * with the given ID + */ + public T get(String id) { + return cache.get(id); + } + +} -- 2.7.4