DATAKV-99 - Add KeyValueIterator.
We now offer the possibility to iterate through available key/value pairs via a KeyValueIterator. The default implementation for java.util.Map based Adapters is a ForwardingKeyValueIterator delegating to the underlying entrySet iterator. Original pull request: #7.
This commit is contained in:
committed by
Thomas Darimont
parent
aa3204e464
commit
16dc4b5e7d
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2015 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.keyvalue.core;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public interface Entry<K, V> extends Map.Entry<K, V> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2015 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.keyvalue.core;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public class ForwardingKeyValueIterator<K, V> implements KeyValueIterator<K, V> {
|
||||
|
||||
private final Iterator<? extends Map.Entry<K, V>> delegate;
|
||||
|
||||
public ForwardingKeyValueIterator(Iterator<? extends java.util.Map.Entry<K, V>> delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return delegate.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entry<K, V> next() {
|
||||
return new ForwardingEntry(delegate.next());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
|
||||
}
|
||||
|
||||
class ForwardingEntry implements Entry<K, V> {
|
||||
|
||||
private final Map.Entry<K, V> entry;
|
||||
|
||||
public ForwardingEntry(Map.Entry<K, V> entry) {
|
||||
this.entry = entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K getKey() {
|
||||
return entry.getKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getValue() {
|
||||
return entry.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V setValue(V value) {
|
||||
return entry.setValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return entry != null ? entry.toString() : "null";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import org.springframework.data.keyvalue.core.query.KeyValueQuery;
|
||||
* {@link KeyValueAdapter} unifies access and shields the underlying key/value specific implementation.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public interface KeyValueAdapter extends DisposableBean {
|
||||
|
||||
@@ -72,6 +73,14 @@ public interface KeyValueAdapter extends DisposableBean {
|
||||
*/
|
||||
Collection<?> getAllOf(Serializable keyspace);
|
||||
|
||||
/**
|
||||
* Returns a {@link KeyValueIterator} that iterates over all entries.
|
||||
*
|
||||
* @param keyspace
|
||||
* @return
|
||||
*/
|
||||
KeyValueIterator<? extends Serializable, ?> entries(Serializable keyspace);
|
||||
|
||||
/**
|
||||
* Remove all objects of given type.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2015 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.keyvalue.core;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public interface KeyValueIterator<K, V> extends Iterator<Entry<K, V>>, Closeable {
|
||||
|
||||
}
|
||||
@@ -22,7 +22,9 @@ 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;
|
||||
|
||||
@@ -133,6 +135,15 @@ public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
|
||||
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)
|
||||
@@ -194,4 +205,5 @@ public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
|
||||
private void addMapForKeySpace(Serializable keyspace) {
|
||||
store.put(keyspace, CollectionFactory.<Serializable, Object> createMap(keySpaceMapType, 1000));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user