X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FShellCache.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FShellCache.java;h=9da9a06093252509bf3aabd3474a113358bcc3a1;hb=9a727406f8647dffc6598ed11b7cc7bef82b38c5;hp=0000000000000000000000000000000000000000;hpb=97865d6020fd9ad90961bdf66941d35d792e6c8f;p=Sone.git 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); + } + +}