diff --git a/src/main/java/org/springframework/data/keyvalue/core/Entry.java b/src/main/java/org/springframework/data/keyvalue/core/Entry.java new file mode 100644 index 00000000..fde586fe --- /dev/null +++ b/src/main/java/org/springframework/data/keyvalue/core/Entry.java @@ -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 + * @param + */ +public interface Entry extends Map.Entry { + +} diff --git a/src/main/java/org/springframework/data/keyvalue/core/ForwardingKeyValueIterator.java b/src/main/java/org/springframework/data/keyvalue/core/ForwardingKeyValueIterator.java new file mode 100644 index 00000000..ce199798 --- /dev/null +++ b/src/main/java/org/springframework/data/keyvalue/core/ForwardingKeyValueIterator.java @@ -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 + * @param + */ +public class ForwardingKeyValueIterator implements KeyValueIterator { + + private final Iterator> delegate; + + public ForwardingKeyValueIterator(Iterator> delegate) { + this.delegate = delegate; + } + + @Override + public boolean hasNext() { + return delegate.hasNext(); + } + + @Override + public Entry next() { + return new ForwardingEntry(delegate.next()); + } + + @Override + public void close() throws IOException { + + } + + class ForwardingEntry implements Entry { + + private final Map.Entry entry; + + public ForwardingEntry(Map.Entry 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"; + } + + } +} diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueAdapter.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueAdapter.java index 1c0a0eef..70ccbaba 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/KeyValueAdapter.java +++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueAdapter.java @@ -25,6 +25,7 @@ * {@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 entries(Serializable keyspace); + /** * Remove all objects of given type. * diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueIterator.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueIterator.java new file mode 100644 index 00000000..9aec1296 --- /dev/null +++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueIterator.java @@ -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 + * @param + */ +public interface KeyValueIterator extends Iterator>, Closeable { + +} diff --git a/src/main/java/org/springframework/data/map/MapKeyValueAdapter.java b/src/main/java/org/springframework/data/map/MapKeyValueAdapter.java index 93c946e5..3e9695c5 100644 --- a/src/main/java/org/springframework/data/map/MapKeyValueAdapter.java +++ b/src/main/java/org/springframework/data/map/MapKeyValueAdapter.java @@ -22,7 +22,9 @@ 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 Collection getAllOf(Serializable keyspace) { return getKeySpaceMap(keyspace).values(); } + /* + * (non-Javadoc) + * @see org.springframework.data.keyvalue.core.KeyValueAdapter#entries(java.io.Serializable) + */ + @Override + public KeyValueIterator entries(Serializable keyspace) { + return new ForwardingKeyValueIterator(getKeySpaceMap(keyspace).entrySet().iterator()); + } + /* * (non-Javadoc) * @see org.springframework.data.keyvalue.core.KeyValueAdapter#deleteAllOf(java.io.Serializable) @@ -194,4 +205,5 @@ protected Map getKeySpaceMap(Serializable keyspace) { private void addMapForKeySpace(Serializable keyspace) { store.put(keyspace, CollectionFactory. createMap(keySpaceMapType, 1000)); } + } diff --git a/src/test/java/org/springframework/data/keyvalue/core/ForwardingIteratorUnitTests.java b/src/test/java/org/springframework/data/keyvalue/core/ForwardingIteratorUnitTests.java new file mode 100644 index 00000000..7ad0145a --- /dev/null +++ b/src/test/java/org/springframework/data/keyvalue/core/ForwardingIteratorUnitTests.java @@ -0,0 +1,89 @@ +/* + * 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 static org.hamcrest.core.Is.*; +import static org.hamcrest.core.IsNull.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; +import java.util.NoSuchElementException; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * @author Christoph Strobl + */ +@RunWith(MockitoJUnitRunner.class) +public class ForwardingIteratorUnitTests { + + @Mock Iterator> iteratorMock; + + /** + * @see DATAKV-99 + */ + @Test + public void hasNextShoudDelegateToWrappedIterator() { + + when(iteratorMock.hasNext()).thenReturn(true); + + assertThat(new ForwardingKeyValueIterator(iteratorMock).hasNext(), is(true)); + + verify(iteratorMock, times(1)).hasNext(); + } + + /** + * @see DATAKV-99 + */ + @Test + public void nextShoudDelegateToWrappedIterator() { + + when(iteratorMock.next()).thenReturn((Map.Entry) mock(Map.Entry.class)); + + assertThat(new ForwardingKeyValueIterator(iteratorMock).next(), notNullValue()); + + verify(iteratorMock, times(1)).next(); + } + + /** + * @see DATAKV-99 + */ + @Test(expected = NoSuchElementException.class) + public void nextShoudThrowErrorWhenWrappedIteratorHasNoMoreElements() { + + when(iteratorMock.next()).thenThrow(new NoSuchElementException()); + + new ForwardingKeyValueIterator(iteratorMock).next(); + } + + /** + * @see DATAKV-99 + */ + @Test + public void closeShouldDoNothing() throws IOException { + + new ForwardingKeyValueIterator(iteratorMock).close(); + + verifyZeroInteractions(iteratorMock); + } + +} diff --git a/src/test/java/org/springframework/data/keyvalue/test/util/IsEntry.java b/src/test/java/org/springframework/data/keyvalue/test/util/IsEntry.java new file mode 100644 index 00000000..8b3ef19d --- /dev/null +++ b/src/test/java/org/springframework/data/keyvalue/test/util/IsEntry.java @@ -0,0 +1,85 @@ +/* + * 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.test.util; + +import org.hamcrest.CustomMatcher; +import org.hamcrest.core.IsEqual; +import org.springframework.data.keyvalue.core.Entry; + +/** + * @author Christoph Strobl + */ +public class IsEntry extends CustomMatcher> { + + private final Entry expected; + + private IsEntry(Entry entry) { + super(String.format("an entry %s=%s.", entry != null ? entry.getKey() : "null", entry != null ? entry.getValue() + : "null")); + this.expected = entry; + } + + @Override + public boolean matches(Object item) { + + if (item == null && expected == null) { + return true; + } + + if (!(item instanceof Entry)) { + return false; + } + + Entry actual = (Entry) item; + + return new IsEqual(expected.getKey()).matches(actual.getKey()) + && new IsEqual(expected.getValue()).matches(actual.getValue()); + } + + public static IsEntry isEntry(Object key, Object value) { + return isEntry(new EntryImpl(key, value)); + } + + public static IsEntry isEntry(Entry entry) { + return new IsEntry(entry); + } + + private static class EntryImpl implements Entry { + + private final Object key; + private final Object value; + + private EntryImpl(Object key, Object value) { + this.key = key; + this.value = value; + } + + @Override + public Object getKey() { + return key; + } + + @Override + public Object getValue() { + return value; + } + + @Override + public Object setValue(Object value) { + throw new UnsupportedOperationException(); + } + } +} diff --git a/src/test/java/org/springframework/data/map/MapKeyValueAdapterUnitTests.java b/src/test/java/org/springframework/data/map/MapKeyValueAdapterUnitTests.java index b0b639fa..e36d640e 100644 --- a/src/test/java/org/springframework/data/map/MapKeyValueAdapterUnitTests.java +++ b/src/test/java/org/springframework/data/map/MapKeyValueAdapterUnitTests.java @@ -20,10 +20,13 @@ import static org.hamcrest.core.IsEqual.*; import static org.hamcrest.core.IsNull.*; import static org.junit.Assert.*; +import static org.springframework.data.keyvalue.test.util.IsEntry.*; + +import java.io.Serializable; import org.junit.Before; import org.junit.Test; -import org.springframework.data.map.MapKeyValueAdapter; +import org.springframework.data.keyvalue.core.KeyValueIterator; import org.springframework.util.ObjectUtils; /** @@ -186,6 +189,45 @@ public void deleteShouldReturnDeletedObject() { assertThat(adapter.delete("1", COLLECTION_1), is(object1)); } + /** + * @see DATAKV-99 + */ + @Test + public void scanShouldIterateOverAvailableEntries() { + + adapter.put("1", object1, COLLECTION_1); + adapter.put("2", object2, COLLECTION_1); + + KeyValueIterator iterator = adapter.entries(COLLECTION_1); + + assertThat(iterator.next(), isEntry("1", object1)); + assertThat(iterator.next(), isEntry("2", object2)); + assertThat(iterator.hasNext(), is(false)); + } + + /** + * @see DATAKV-99 + */ + @Test + public void scanShouldReturnEmptyIteratorWhenNoElementsAvailable() { + assertThat(adapter.entries(COLLECTION_1).hasNext(), is(false)); + } + + /** + * @see DATAKV-99 + */ + @Test + public void scanDoesNotMixResultsFromMultipleKeyspaces() { + + adapter.put("1", object1, COLLECTION_1); + adapter.put("2", object2, COLLECTION_2); + + KeyValueIterator iterator = adapter.entries(COLLECTION_1); + + assertThat(iterator.next(), isEntry("1", object1)); + assertThat(iterator.hasNext(), is(false)); + } + static class SimpleObject { protected String stringValue;