Stop logging when starting the plugin failed.
[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.Collection;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 /**
25  * {@link Shell}-aware cache that will replace {@link Shell}s with the real
26  * objects but not the other way around.
27  *
28  * @param <T>
29  *            The type of the cached objects
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class ShellCache<T> {
33
34         /** The object cache. */
35         private final Map<String, T> objectCache = new HashMap<String, T>();
36
37         /** The shell cache. */
38         private final Map<String, Shell<T>> shellCache = new HashMap<String, Shell<T>>();
39
40         /** The shell creator. */
41         private final ShellCreator<T> shellCreator;
42
43         /**
44          * Creates a new shell cache.
45          *
46          * @param shellCreator
47          *            The creator for new shells
48          */
49         public ShellCache(ShellCreator<T> shellCreator) {
50                 this.shellCreator = shellCreator;
51         }
52
53         //
54         // ACCESSORS
55         //
56
57         /**
58          * Stores the given object in this cache. If the given object is not a
59          * {@link Shell}, it is stored. If it is a {@link Shell} it is only stored
60          * if there is no object stored for the given ID.
61          *
62          * @param id
63          *            The ID of the object
64          * @param object
65          *            The object to store
66          * @return The real object, or the shell if there is no real object yet
67          */
68         @SuppressWarnings("unchecked")
69         public T put(String id, T object) {
70                 if (!(object instanceof Shell<?>)) {
71                         objectCache.put(id, object);
72                         shellCache.remove(id);
73                         return object;
74                 }
75                 if (objectCache.containsKey(id)) {
76                         return objectCache.get(id);
77                 }
78                 shellCache.put(id, (Shell<T>) object);
79                 return object;
80         }
81
82         /**
83          * Returns the post with the given ID.
84          *
85          * @param id
86          *            The ID of the post
87          * @return The post with the given ID, or {@code null} if there is no post
88          *         with the given ID
89          */
90         public T get(String id) {
91                 if (!objectCache.containsKey(id)) {
92                         if (!shellCache.containsKey(id)) {
93                                 Shell<T> shell = shellCreator.createShell(id);
94                                 shellCache.put(id, shell);
95                         }
96                         return shellCache.get(id).getShelled();
97                 }
98                 return objectCache.get(id);
99         }
100
101         /**
102          * Returns all cached shells.
103          *
104          * @return All cached shells
105          */
106         public Collection<Shell<T>> getShells() {
107                 return shellCache.values();
108         }
109
110 }