1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.util.storage;
18
19 import java.util.Hashtable;
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.Set;
23
24
25
26
27
28
29
30 public class MapBasedStorageService<KeyType, ValueType> implements StorageService<KeyType, ValueType> {
31
32
33 private Map<String, Map<KeyType, ValueType>> store;
34
35
36 public MapBasedStorageService() {
37 store = new Hashtable<String, Map<KeyType, ValueType>>();
38 }
39
40
41 public Iterator<String> getPartitions() {
42 Set<String> keys = store.keySet();
43 if (keys != null) {
44 return keys.iterator();
45 }
46
47 return null;
48 }
49
50
51 public Iterator<KeyType> getKeys(String partition) {
52 if (store.containsKey(partition)) {
53 Set<KeyType> keys = store.get(partition).keySet();
54 if (keys != null) {
55 return keys.iterator();
56 }
57 }
58
59 return null;
60 }
61
62
63 public boolean contains(String partition, KeyType key) {
64 if (key == null) {
65 return false;
66 }
67
68 if (store.containsKey(partition)) {
69 return store.get(partition).containsKey(key);
70 }
71
72 return false;
73 }
74
75
76 public ValueType get(String partition, KeyType key) {
77 if (key == null) {
78 return null;
79 }
80
81 if (store.containsKey(partition)) {
82 return store.get(partition).get(key);
83 }
84
85 return null;
86 }
87
88
89 public ValueType put(String partition, KeyType key, ValueType value) {
90 if (key == null) {
91 return null;
92 }
93
94 Map<KeyType, ValueType> partitionMap;
95 synchronized (store) {
96 partitionMap = store.get(partition);
97 if (partitionMap == null) {
98 partitionMap = new Hashtable<KeyType, ValueType>();
99 }
100 store.put(partition, partitionMap);
101 }
102
103 return partitionMap.put(key, value);
104 }
105
106
107 public ValueType remove(String partition, KeyType key) {
108 if (key == null) {
109 return null;
110 }
111
112 if (store.containsKey(partition)) {
113 return store.get(partition).remove(key);
114 }
115
116 return null;
117 }
118 }