-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathMapKeyValueAdapter.java
209 lines (177 loc) · 6.16 KB
/
MapKeyValueAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.map;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.core.CollectionFactory;
import org.springframework.data.keyvalue.core.AbstractKeyValueAdapter;
import org.springframework.data.keyvalue.core.ForwardingKeyValueIterator;
import org.springframework.data.keyvalue.core.KeyValueAdapter;
import org.springframework.data.keyvalue.core.KeyValueIterator;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* {@link KeyValueAdapter} implementation for {@link Map}.
*
* @author Christoph Strobl
*/
public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
@SuppressWarnings("rawtypes")//
private final Class<? extends Map> keySpaceMapType;
private final Map<Serializable, Map<Serializable, Object>> store;
/**
* Create new {@link MapKeyValueAdapter} using {@link ConcurrentHashMap} as backing store type.
*/
public MapKeyValueAdapter() {
this(ConcurrentHashMap.class);
}
/**
* Creates a new {@link MapKeyValueAdapter} using the given {@link Map} as backing store.
*
* @param mapType must not be {@literal null}.
*/
@SuppressWarnings("rawtypes")
public MapKeyValueAdapter(Class<? extends Map> mapType) {
this(CollectionFactory.<Serializable, Map<Serializable, Object>> createMap(mapType, 100), mapType);
}
/**
* Create new instance of {@link MapKeyValueAdapter} using given dataStore for persistence.
*
* @param store must not be {@literal null}.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public MapKeyValueAdapter(Map<Serializable, Map<Serializable, Object>> store) {
this(store, (Class<? extends Map>) ClassUtils.getUserClass(store));
}
/**
* Creates a new {@link MapKeyValueAdapter} with the given store and type to be used when creating key spaces.
*
* @param store must not be {@literal null}.
* @param keySpaceMapType must not be {@literal null}.
*/
@SuppressWarnings("rawtypes")
private MapKeyValueAdapter(Map<Serializable, Map<Serializable, Object>> store, Class<? extends Map> keySpaceMapType) {
Assert.notNull(store, "Store must not be null.");
Assert.notNull(keySpaceMapType, "Map type to be used for key spaces must not be null!");
this.store = store;
this.keySpaceMapType = keySpaceMapType;
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#put(java.io.Serializable, java.lang.Object, java.io.Serializable)
*/
@Override
public Object put(Serializable id, Object item, Serializable keyspace) {
Assert.notNull(id, "Cannot add item with null id.");
Assert.notNull(keyspace, "Cannot add item for null collection.");
return getKeySpaceMap(keyspace).put(id, item);
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#contains(java.io.Serializable, java.io.Serializable)
*/
@Override
public boolean contains(Serializable id, Serializable keyspace) {
return get(id, keyspace) != null;
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#get(java.io.Serializable, java.io.Serializable)
*/
@Override
public Object get(Serializable id, Serializable keyspace) {
Assert.notNull(id, "Cannot get item with null id.");
return getKeySpaceMap(keyspace).get(id);
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#delete(java.io.Serializable, java.io.Serializable)
*/
@Override
public Object delete(Serializable id, Serializable keyspace) {
Assert.notNull(id, "Cannot delete item with null id.");
return getKeySpaceMap(keyspace).remove(id);
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#getAllOf(java.io.Serializable)
*/
@Override
public Collection<?> getAllOf(Serializable keyspace) {
return getKeySpaceMap(keyspace).values();
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#entries(java.io.Serializable)
*/
@Override
public KeyValueIterator<Serializable, ?> entries(Serializable keyspace) {
return new ForwardingKeyValueIterator<Serializable, Object>(getKeySpaceMap(keyspace).entrySet().iterator());
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#deleteAllOf(java.io.Serializable)
*/
@Override
public void deleteAllOf(Serializable keyspace) {
getKeySpaceMap(keyspace).clear();
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#hasKeyspace(java.io.Serializable)
*/
@Override
public boolean hasKeyspace(Serializable keyspace) {
Assert.notNull(keyspace, "Collection must not be null for lookup.");
return store.containsKey(keyspace);
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#clear()
*/
@Override
public void clear() {
store.clear();
}
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.DisposableBean#destroy()
*/
@Override
public void destroy() throws Exception {
clear();
}
/**
* Get map associated with given keyspace.
*
* @param keyspace must not be {@literal null}.
* @return
*/
protected Map<Serializable, Object> getKeySpaceMap(Serializable keyspace) {
Assert.notNull(keyspace, "Collection must not be null for lookup.");
Map<Serializable, Object> map = store.get(keyspace);
if (map != null) {
return map;
}
addMapForKeySpace(keyspace);
return store.get(keyspace);
}
private void addMapForKeySpace(Serializable keyspace) {
store.put(keyspace, CollectionFactory.<Serializable, Object> createMap(keySpaceMapType, 1000));
}
}