Only allow unshelling a post if its Sone is not a shell.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / ShellCache.java
1 /*
2  * Sone - ShellCache.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.data;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 /**
24  * {@link Shell}-aware cache that will replace {@link Shell}s with the real
25  * objects but not the other way around.
26  *
27  * @param <T>
28  *            The type of the cached objects
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class ShellCache<T> {
32
33         /** The object cache. */
34         private final Map<String, T> cache = new HashMap<String, T>();
35
36         /**
37          * Stores the given object in this cache. If the given object is not a
38          * {@link Shell}, it is stored. If it is a {@link Shell} it is only stored
39          * if there is no object stored for the given ID.
40          *
41          * @param id
42          *            The ID of the object
43          * @param object
44          *            The object to store
45          */
46         public void put(String id, T object) {
47                 if (!(object instanceof Shell<?>) || !cache.containsKey(id)) {
48                         cache.put(id, object);
49                 }
50         }
51
52         /**
53          * Returns the post with the given ID.
54          *
55          * @param id
56          *            The ID of the post
57          * @return The post with the given ID, or {@code null} if there is no post
58          *         with the given ID
59          */
60         public T get(String id) {
61                 return cache.get(id);
62         }
63
64 }