From 3c2e3ac747e678e0c72fecfd25a400d61593230d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Oct 2010 13:40:18 +0200 Subject: [PATCH] Add cache for Posts that is Shell-aware and throws Shells out if possible. --- .../net/pterodactylus/sone/core/PostCache.java | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/core/PostCache.java diff --git a/src/main/java/net/pterodactylus/sone/core/PostCache.java b/src/main/java/net/pterodactylus/sone/core/PostCache.java new file mode 100644 index 0000000..5f830ad --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/core/PostCache.java @@ -0,0 +1,66 @@ +/* + * 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); + } + +} -- 2.7.4