|
| 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 | + * @author Christoph Strobl |
| 29 | + * @param <K> |
| 30 | + * @param <V> |
| 31 | + */ |
| 32 | +public class AdapterBackedKeyValueStore<K extends Serializable, V> implements KeyValueStore<K, V> { |
| 33 | + |
| 34 | + private final Serializable keyspace; |
| 35 | + private final KeyValueAdapter adapter; |
| 36 | + |
| 37 | + public AdapterBackedKeyValueStore(KeyValueAdapter adapter, Serializable keyspace) { |
| 38 | + |
| 39 | + Assert.notNull(adapter, "Adapter must not be null!"); |
| 40 | + Assert.notNull(keyspace, "Keyspace must not be null!"); |
| 41 | + this.adapter = adapter; |
| 42 | + this.keyspace = keyspace; |
| 43 | + } |
| 44 | + |
| 45 | + /* |
| 46 | + * (non-Javadoc) |
| 47 | + * @see org.springframework.data.keyvalue.core.KeyValueStore#put(java.io.Serializable, java.lang.Object) |
| 48 | + */ |
| 49 | + @Override |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + public V put(K id, V item) { |
| 52 | + return (V) adapter.put(id, item, keyspace); |
| 53 | + } |
| 54 | + |
| 55 | + /* |
| 56 | + * (non-Javadoc) |
| 57 | + * @see org.springframework.data.keyvalue.core.KeyValueStore#contains(java.io.Serializable) |
| 58 | + */ |
| 59 | + @Override |
| 60 | + public boolean contains(K id) { |
| 61 | + return adapter.contains(id, keyspace); |
| 62 | + } |
| 63 | + |
| 64 | + /* |
| 65 | + * (non-Javadoc) |
| 66 | + * @see org.springframework.data.keyvalue.core.KeyValueStore#get(java.io.Serializable) |
| 67 | + */ |
| 68 | + @Override |
| 69 | + @SuppressWarnings("unchecked") |
| 70 | + public V get(K id) { |
| 71 | + return (V) adapter.get(id, keyspace); |
| 72 | + } |
| 73 | + |
| 74 | + /* |
| 75 | + * (non-Javadoc) |
| 76 | + * @see org.springframework.data.keyvalue.core.KeyValueStore#getAll(java.util.Collection) |
| 77 | + */ |
| 78 | + @Override |
| 79 | + public Iterable<V> getAll(Collection<K> keys) { |
| 80 | + |
| 81 | + if (CollectionUtils.isEmpty(keys)) { |
| 82 | + return Collections.emptySet(); |
| 83 | + } |
| 84 | + |
| 85 | + List<V> result = new ArrayList<V>(keys.size()); |
| 86 | + for (K key : keys) { |
| 87 | + result.add(get(key)); |
| 88 | + } |
| 89 | + return result; |
| 90 | + } |
| 91 | + |
| 92 | + /* |
| 93 | + * (non-Javadoc) |
| 94 | + * @see org.springframework.data.keyvalue.core.KeyValueStore#delete(java.io.Serializable) |
| 95 | + */ |
| 96 | + @Override |
| 97 | + @SuppressWarnings("unchecked") |
| 98 | + public V remove(K id) { |
| 99 | + return (V) adapter.delete(id, keyspace); |
| 100 | + } |
| 101 | + |
| 102 | + /* |
| 103 | + * (non-Javadoc) |
| 104 | + * @see org.springframework.data.keyvalue.core.KeyValueStore#removeAll(java.util.Collection) |
| 105 | + */ |
| 106 | + @Override |
| 107 | + public Iterable<V> removeAll(Collection<K> keys) { |
| 108 | + |
| 109 | + if (CollectionUtils.isEmpty(keys)) { |
| 110 | + return Collections.emptySet(); |
| 111 | + } |
| 112 | + |
| 113 | + List<V> result = new ArrayList<V>(keys.size()); |
| 114 | + for (K key : keys) { |
| 115 | + result.add(remove(key)); |
| 116 | + } |
| 117 | + return result; |
| 118 | + } |
| 119 | + |
| 120 | + /* |
| 121 | + * (non-Javadoc) |
| 122 | + * @see org.springframework.data.keyvalue.core.KeyValueStore#size() |
| 123 | + */ |
| 124 | + @Override |
| 125 | + public long size() { |
| 126 | + return adapter.count(keyspace); |
| 127 | + } |
| 128 | + |
| 129 | + /* |
| 130 | + * (non-Javadoc) |
| 131 | + * @see org.springframework.data.keyvalue.core.KeyValueStore#clear() |
| 132 | + */ |
| 133 | + @Override |
| 134 | + public void clear() { |
| 135 | + adapter.deleteAllOf(keyspace); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments