Skip to content

Commit e605185

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
Polishing.
Removed dedicated Entry<K,V> interface, since Map.Entry<K,V> is already sufficient. Added missing JavaDoc.
1 parent a58251c commit e605185

File tree

9 files changed

+455
-44
lines changed

9 files changed

+455
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.keyvalue.core;
17+
18+
import java.io.Serializable;
19+
import java.util.ArrayList;
20+
import java.util.Collection;
21+
import java.util.Collections;
22+
import java.util.List;
23+
24+
import org.springframework.util.Assert;
25+
import org.springframework.util.CollectionUtils;
26+
27+
/**
28+
* A {@link KeyValueStore} scoped to a keyspace that stores the values in via a provided {@link KeyValueAdapter}.
29+
*
30+
* @author Christoph Strobl
31+
* @param <K>
32+
* @param <V>
33+
*/
34+
public class AdapterBackedKeyValueStore<K extends Serializable, V> implements KeyValueStore<K, V> {
35+
36+
private final Serializable keyspace;
37+
private final KeyValueAdapter adapter;
38+
39+
/**
40+
* @param adapter
41+
* @param keyspace
42+
*/
43+
public AdapterBackedKeyValueStore(KeyValueAdapter adapter, Serializable keyspace) {
44+
45+
Assert.notNull(adapter, "Adapter must not be null!");
46+
Assert.notNull(keyspace, "Keyspace must not be null!");
47+
this.adapter = adapter;
48+
this.keyspace = keyspace;
49+
}
50+
51+
/*
52+
* (non-Javadoc)
53+
* @see org.springframework.data.keyvalue.core.KeyValueStore#put(java.io.Serializable, java.lang.Object)
54+
*/
55+
@Override
56+
@SuppressWarnings("unchecked")
57+
public V put(K id, V item) {
58+
return (V) adapter.put(id, item, keyspace);
59+
}
60+
61+
/*
62+
* (non-Javadoc)
63+
* @see org.springframework.data.keyvalue.core.KeyValueStore#contains(java.io.Serializable)
64+
*/
65+
@Override
66+
public boolean contains(K id) {
67+
return adapter.contains(id, keyspace);
68+
}
69+
70+
/*
71+
* (non-Javadoc)
72+
* @see org.springframework.data.keyvalue.core.KeyValueStore#get(java.io.Serializable)
73+
*/
74+
@Override
75+
@SuppressWarnings("unchecked")
76+
public V get(K id) {
77+
return (V) adapter.get(id, keyspace);
78+
}
79+
80+
/*
81+
* (non-Javadoc)
82+
* @see org.springframework.data.keyvalue.core.KeyValueStore#getAll(java.util.Collection)
83+
*/
84+
@Override
85+
public Iterable<V> getAll(Collection<K> keys) {
86+
87+
if (CollectionUtils.isEmpty(keys)) {
88+
return Collections.emptySet();
89+
}
90+
91+
List<V> result = new ArrayList<V>(keys.size());
92+
for (K key : keys) {
93+
result.add(get(key));
94+
}
95+
return result;
96+
}
97+
98+
/*
99+
* (non-Javadoc)
100+
* @see org.springframework.data.keyvalue.core.KeyValueStore#delete(java.io.Serializable)
101+
*/
102+
@Override
103+
@SuppressWarnings("unchecked")
104+
public V remove(K id) {
105+
return (V) adapter.delete(id, keyspace);
106+
}
107+
108+
/*
109+
* (non-Javadoc)
110+
* @see org.springframework.data.keyvalue.core.KeyValueStore#removeAll(java.util.Collection)
111+
*/
112+
@Override
113+
public Iterable<V> removeAll(Collection<K> keys) {
114+
115+
if (CollectionUtils.isEmpty(keys)) {
116+
return Collections.emptySet();
117+
}
118+
119+
List<V> result = new ArrayList<V>(keys.size());
120+
for (K key : keys) {
121+
result.add(remove(key));
122+
}
123+
return result;
124+
}
125+
126+
/*
127+
* (non-Javadoc)
128+
* @see org.springframework.data.keyvalue.core.KeyValueStore#size()
129+
*/
130+
@Override
131+
public long size() {
132+
return adapter.count(keyspace);
133+
}
134+
135+
/*
136+
* (non-Javadoc)
137+
* @see org.springframework.data.keyvalue.core.KeyValueStore#clear()
138+
*/
139+
@Override
140+
public void clear() {
141+
adapter.deleteAllOf(keyspace);
142+
}
143+
144+
}

src/main/java/org/springframework/data/keyvalue/core/Entry.java

-27
This file was deleted.

src/main/java/org/springframework/data/keyvalue/core/ForwardingKeyValueIterator.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.keyvalue.core;
1717

18-
import java.io.IOException;
1918
import java.util.Iterator;
2019
import java.util.Map;
2120

@@ -38,16 +37,14 @@ public boolean hasNext() {
3837
}
3938

4039
@Override
41-
public Entry<K, V> next() {
40+
public Map.Entry<K, V> next() {
4241
return new ForwardingEntry(delegate.next());
4342
}
4443

4544
@Override
46-
public void close() throws IOException {
45+
public void close() {}
4746

48-
}
49-
50-
class ForwardingEntry implements Entry<K, V> {
47+
class ForwardingEntry implements Map.Entry<K, V> {
5148

5249
private final Map.Entry<K, V> entry;
5350

src/main/java/org/springframework/data/keyvalue/core/KeyValueIterator.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
*/
1616
package org.springframework.data.keyvalue.core;
1717

18-
import java.io.Closeable;
19-
import java.util.Iterator;
18+
import java.util.Map;
19+
20+
import org.springframework.data.util.CloseableIterator;
2021

2122
/**
2223
* @author Christoph Strobl
2324
* @param <K>
2425
* @param <V>
2526
*/
26-
public interface KeyValueIterator<K, V> extends Iterator<Entry<K, V>>, Closeable {
27+
public interface KeyValueIterator<K, V> extends CloseableIterator<Map.Entry<K, V>> {
2728

2829
}

src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java

+8
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ public interface KeyValueOperations extends DisposableBean {
169169
*/
170170
long count(KeyValueQuery<?> query, Class<?> type);
171171

172+
/**
173+
* Returns a {@link KeyValueStore} scoped to the given {@code keyspace}.
174+
*
175+
* @param keyspace must not be {@literal null}.
176+
* @return
177+
*/
178+
<K extends Serializable, V> KeyValueStore<K, V> getKeyValueStore(Serializable keyspace);
179+
172180
/**
173181
* @return mapping context in use.
174182
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.keyvalue.core;
17+
18+
import java.io.Serializable;
19+
import java.util.Collection;
20+
21+
/**
22+
* A key-value store that supports put, get, delete, and queries.
23+
*
24+
* @author Christoph Strobl
25+
* @author Thomas Darimont
26+
* @param <K>
27+
* @param <V>
28+
*/
29+
public interface KeyValueStore<K extends Serializable, V> {
30+
31+
/**
32+
* Add object with given key.
33+
*
34+
* @param key must not be {@literal null}.
35+
* @return the item previously associated with the key.
36+
*/
37+
V put(K key, V item);
38+
39+
/**
40+
* Check if given key is present in KeyValue store.
41+
*
42+
* @param key must not be {@literal null}.
43+
* @return
44+
*/
45+
boolean contains(K key);
46+
47+
/**
48+
* Get the Object associated with the given key.
49+
*
50+
* @param key must not be {@literal null}.
51+
* @return {@literal null} if key not available.
52+
*/
53+
V get(K key);
54+
55+
/**
56+
* Get all Objects associated with the given keys.
57+
*
58+
* @param keys must not be {@literal null}.
59+
* @return an empty {@link Iterable} if no matching key found.
60+
*/
61+
Iterable<V> getAll(Collection<K> keys);
62+
63+
/**
64+
* Delete and return the element associated with the given key.
65+
*
66+
* @param key must not be {@literal null}.
67+
* @return
68+
*/
69+
V remove(K key);
70+
71+
/**
72+
* Delete and return all elements associated with the given keys.
73+
*
74+
* @param keys must not be {@literal null}.
75+
* @return
76+
*/
77+
Iterable<V> removeAll(Collection<K> keys);
78+
79+
/**
80+
* Get the number of total elements contained.
81+
*
82+
* @return
83+
*/
84+
long size();
85+
86+
/**
87+
* Removes all elements. The {@link KeyValueStore} will be empty after this call returns.
88+
*/
89+
void clear();
90+
91+
}

src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java

+9
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,15 @@ public Long doInKeyValue(KeyValueAdapter adapter) {
428428
});
429429
}
430430

431+
/*
432+
* (non-Javadoc)
433+
* @see org.springframework.data.keyvalue.core.KeyValueOperations#getKeyValueStore(java.io.Serializable)
434+
*/
435+
@Override
436+
public <K extends Serializable, V> KeyValueStore<K, V> getKeyValueStore(Serializable keyspace) {
437+
return new AdapterBackedKeyValueStore<K, V>(this.adapter, keyspace);
438+
}
439+
431440
/*
432441
* (non-Javadoc)
433442
* @see org.springframework.data.keyvalue.core.KeyValueOperations#getMappingContext()

0 commit comments

Comments
 (0)