|
1 |
| -package net.lecousin.framework.memory; |
2 |
| - |
3 |
| -import java.util.ArrayList; |
4 |
| -import java.util.Collection; |
5 |
| -import java.util.HashMap; |
6 |
| -import java.util.List; |
7 |
| -import java.util.Map; |
8 |
| - |
9 |
| -import net.lecousin.framework.event.Listener; |
10 |
| -import net.lecousin.framework.util.CloseableListenable; |
11 |
| - |
12 |
| -/** Implementation of CacheManager that associate each cached data to a key. |
13 |
| - * @param <Key> type of key |
14 |
| - * @param <Value> type of cached data |
15 |
| - */ |
16 |
| -public class Cache<Key,Value> implements CacheManager { |
17 |
| - |
18 |
| - /** Constructor. */ |
19 |
| - public Cache(String description, Listener<Value> freer) { |
20 |
| - this.description = description; |
21 |
| - this.freer = freer; |
22 |
| - MemoryManager.register(this); |
23 |
| - } |
24 |
| - |
25 |
| - private String description; |
26 |
| - private Listener<Value> freer; |
27 |
| - |
28 |
| - private static class Data<Value> implements CachedData { |
29 |
| - public ArrayList<CloseableListenable> users = new ArrayList<>(); |
30 |
| - public Value value; |
31 |
| - public long lastUsage = 0; |
32 |
| - |
33 |
| - @Override |
34 |
| - public int cachedDataCurrentUsage() { |
35 |
| - return users.size(); |
36 |
| - } |
37 |
| - |
38 |
| - @Override |
39 |
| - public long cachedDataLastUsage() { |
40 |
| - return lastUsage; |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - private HashMap<Key,Data<Value>> map = new HashMap<>(); |
45 |
| - private HashMap<Value,Data<Value>> values = new HashMap<>(); |
46 |
| - |
47 |
| - /** Return the cached data corresponding to the given key, or null if it does not exist. |
48 |
| - * The given user is added as using the cached data. Once closed, the user is automatically |
49 |
| - * removed from the list of users. |
50 |
| - */ |
51 |
| - public synchronized Value get(Key key, CloseableListenable user) { |
52 |
| - Data<Value> data = map.get(key); |
53 |
| - if (data == null) return null; |
54 |
| - data.users.add(user); |
55 |
| - if (user != null) |
56 |
| - user.addCloseListener(new Listener<CloseableListenable>() { |
57 |
| - @Override |
58 |
| - public void fire(CloseableListenable event) { |
59 |
| - event.removeCloseListener(this); |
60 |
| - free(data.value, event); |
61 |
| - } |
62 |
| - }); |
63 |
| - return data.value; |
64 |
| - } |
65 |
| - |
66 |
| - /** Singla that the given cached data is not anymore used by the given user. */ |
67 |
| - public synchronized void free(Value value, CloseableListenable user) { |
68 |
| - Data<Value> data = values.get(value); |
69 |
| - if (data == null) return; |
70 |
| - data.lastUsage = System.currentTimeMillis(); |
71 |
| - data.users.remove(user); |
72 |
| - } |
73 |
| - |
74 |
| - /** Add the given data to this cache, and add the given first user to it. */ |
75 |
| - public synchronized void put(Key key, Value value, CloseableListenable firstUser) { |
76 |
| - if (map.containsKey(key)) |
77 |
| - throw new IllegalStateException("Cannot put a value in Cache with an existing key: " + key); |
78 |
| - if (values.containsKey(value)) |
79 |
| - throw new IllegalStateException("Cannot put 2 times the same value in Cache: " + value); |
80 |
| - Data<Value> data = new Data<Value>(); |
81 |
| - data.value = value; |
82 |
| - data.lastUsage = System.currentTimeMillis(); |
83 |
| - data.users.add(firstUser); |
84 |
| - map.put(key, data); |
85 |
| - values.put(value, data); |
86 |
| - } |
87 |
| - |
88 |
| - @Override |
89 |
| - public Collection<? extends CachedData> getCachedData() { |
90 |
| - return map.values(); |
91 |
| - } |
92 |
| - |
93 |
| - // skip checkstyle: OverloadMethodsDeclarationOrder |
94 |
| - @Override |
95 |
| - public synchronized boolean free(CachedData data) { |
96 |
| - @SuppressWarnings("unchecked") |
97 |
| - Value value = ((Data<Value>)data).value; |
98 |
| - values.remove(value); |
99 |
| - Key key = null; |
100 |
| - for (Map.Entry<Key,Data<Value>> e : map.entrySet()) |
101 |
| - if (e.getValue() == data) { |
102 |
| - key = e.getKey(); |
103 |
| - break; |
104 |
| - } |
105 |
| - map.remove(key); |
106 |
| - if (freer != null) freer.fire(value); |
107 |
| - return true; |
108 |
| - } |
109 |
| - |
110 |
| - @Override |
111 |
| - public String getDescription() { |
112 |
| - return description; |
113 |
| - } |
114 |
| - |
115 |
| - @Override |
116 |
| - public synchronized List<String> getItemsDescription() { |
117 |
| - ArrayList<String> list = new ArrayList<>(values.size()); |
118 |
| - for (Data<Value> d : values.values()) { |
119 |
| - StringBuilder s = new StringBuilder(); |
120 |
| - s.append(d.value.toString()); |
121 |
| - s.append(" (last used ").append((System.currentTimeMillis() - d.lastUsage)).append("ms. ago)"); |
122 |
| - s.append(" ").append(d.cachedDataCurrentUsage()).append(" users:"); |
123 |
| - for (CloseableListenable user : d.users) |
124 |
| - s.append("\r\n => " + user); |
125 |
| - list.add(s.toString()); |
126 |
| - } |
127 |
| - return list; |
128 |
| - } |
129 |
| - |
130 |
| - /** Close this cache, and free any data it holds. */ |
131 |
| - public synchronized void close() { |
132 |
| - for (Data<Value> d : map.values()) |
133 |
| - freer.fire(d.value); |
134 |
| - map.clear(); |
135 |
| - values.clear(); |
136 |
| - MemoryManager.unregister(this); |
137 |
| - } |
138 |
| -} |
| 1 | +package net.lecousin.framework.memory; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import net.lecousin.framework.event.Listener; |
| 10 | +import net.lecousin.framework.util.CloseableListenable; |
| 11 | + |
| 12 | +/** Implementation of CacheManager that associate each cached data to a key. |
| 13 | + * @param <Key> type of key |
| 14 | + * @param <Value> type of cached data |
| 15 | + */ |
| 16 | +public class Cache<Key,Value> implements CacheManager { |
| 17 | + |
| 18 | + /** Constructor. */ |
| 19 | + public Cache(String description, Listener<Value> freer) { |
| 20 | + this.description = description; |
| 21 | + this.freer = freer; |
| 22 | + MemoryManager.register(this); |
| 23 | + } |
| 24 | + |
| 25 | + private String description; |
| 26 | + private Listener<Value> freer; |
| 27 | + |
| 28 | + private static class Data<Value> implements CachedData { |
| 29 | + public ArrayList<CloseableListenable> users = new ArrayList<>(); |
| 30 | + public Value value; |
| 31 | + public long lastUsage = 0; |
| 32 | + |
| 33 | + @Override |
| 34 | + public int cachedDataCurrentUsage() { |
| 35 | + return users.size(); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public long cachedDataLastUsage() { |
| 40 | + return lastUsage; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private HashMap<Key,Data<Value>> map = new HashMap<>(); |
| 45 | + private HashMap<Value,Data<Value>> values = new HashMap<>(); |
| 46 | + |
| 47 | + /** Return the cached data corresponding to the given key, or null if it does not exist. |
| 48 | + * The given user is added as using the cached data. Once closed, the user is automatically |
| 49 | + * removed from the list of users. |
| 50 | + */ |
| 51 | + public synchronized Value get(Key key, CloseableListenable user) { |
| 52 | + Data<Value> data = map.get(key); |
| 53 | + if (data == null) return null; |
| 54 | + data.users.add(user); |
| 55 | + if (user != null) |
| 56 | + user.addCloseListener(new Listener<CloseableListenable>() { |
| 57 | + @Override |
| 58 | + public void fire(CloseableListenable event) { |
| 59 | + event.removeCloseListener(this); |
| 60 | + free(data.value, event); |
| 61 | + } |
| 62 | + }); |
| 63 | + return data.value; |
| 64 | + } |
| 65 | + |
| 66 | + /** Signal that the given cached data is not anymore used by the given user. */ |
| 67 | + public synchronized void free(Value value, CloseableListenable user) { |
| 68 | + Data<Value> data = values.get(value); |
| 69 | + if (data == null) return; |
| 70 | + data.lastUsage = System.currentTimeMillis(); |
| 71 | + data.users.remove(user); |
| 72 | + } |
| 73 | + |
| 74 | + /** Add the given data to this cache, and add the given first user to it. */ |
| 75 | + public synchronized void put(Key key, Value value, CloseableListenable firstUser) { |
| 76 | + if (map.containsKey(key)) |
| 77 | + throw new IllegalStateException("Cannot put a value in Cache with an existing key: " + key); |
| 78 | + if (values.containsKey(value)) |
| 79 | + throw new IllegalStateException("Cannot put 2 times the same value in Cache: " + value); |
| 80 | + Data<Value> data = new Data<Value>(); |
| 81 | + data.value = value; |
| 82 | + data.lastUsage = System.currentTimeMillis(); |
| 83 | + data.users.add(firstUser); |
| 84 | + map.put(key, data); |
| 85 | + values.put(value, data); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Collection<? extends CachedData> getCachedData() { |
| 90 | + return map.values(); |
| 91 | + } |
| 92 | + |
| 93 | + // skip checkstyle: OverloadMethodsDeclarationOrder |
| 94 | + @Override |
| 95 | + public synchronized boolean free(CachedData data) { |
| 96 | + @SuppressWarnings("unchecked") |
| 97 | + Value value = ((Data<Value>)data).value; |
| 98 | + values.remove(value); |
| 99 | + Key key = null; |
| 100 | + for (Map.Entry<Key,Data<Value>> e : map.entrySet()) |
| 101 | + if (e.getValue() == data) { |
| 102 | + key = e.getKey(); |
| 103 | + break; |
| 104 | + } |
| 105 | + map.remove(key); |
| 106 | + if (freer != null) freer.fire(value); |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getDescription() { |
| 112 | + return description; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public synchronized List<String> getItemsDescription() { |
| 117 | + ArrayList<String> list = new ArrayList<>(values.size()); |
| 118 | + for (Data<Value> d : values.values()) { |
| 119 | + StringBuilder s = new StringBuilder(); |
| 120 | + s.append(d.value.toString()); |
| 121 | + s.append(" (last used ").append((System.currentTimeMillis() - d.lastUsage)).append("ms. ago)"); |
| 122 | + s.append(" ").append(d.cachedDataCurrentUsage()).append(" users:"); |
| 123 | + for (CloseableListenable user : d.users) |
| 124 | + s.append("\r\n => " + user); |
| 125 | + list.add(s.toString()); |
| 126 | + } |
| 127 | + return list; |
| 128 | + } |
| 129 | + |
| 130 | + /** Close this cache, and free any data it holds. */ |
| 131 | + public synchronized void close() { |
| 132 | + for (Data<Value> d : map.values()) |
| 133 | + freer.fire(d.value); |
| 134 | + map.clear(); |
| 135 | + values.clear(); |
| 136 | + MemoryManager.unregister(this); |
| 137 | + } |
| 138 | +} |
0 commit comments