Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
aa3204e
commit 16dc4b5
Showing
8 changed files
with
372 additions
and
1 deletion.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/org/springframework/data/keyvalue/core/Entry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> { | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/org/springframework/data/keyvalue/core/ForwardingKeyValueIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/org/springframework/data/keyvalue/core/KeyValueIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/test/java/org/springframework/data/keyvalue/core/ForwardingIteratorUnitTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<K, V> { | ||
|
||
@Mock Iterator<Map.Entry<K, V>> iteratorMock; | ||
|
||
/** | ||
* @see DATAKV-99 | ||
*/ | ||
@Test | ||
public void hasNextShoudDelegateToWrappedIterator() { | ||
|
||
when(iteratorMock.hasNext()).thenReturn(true); | ||
|
||
assertThat(new ForwardingKeyValueIterator<K, V>(iteratorMock).hasNext(), is(true)); | ||
|
||
verify(iteratorMock, times(1)).hasNext(); | ||
} | ||
|
||
/** | ||
* @see DATAKV-99 | ||
*/ | ||
@Test | ||
public void nextShoudDelegateToWrappedIterator() { | ||
|
||
when(iteratorMock.next()).thenReturn((Map.Entry<K, V>) mock(Map.Entry.class)); | ||
|
||
assertThat(new ForwardingKeyValueIterator<K, V>(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<K, V>(iteratorMock).next(); | ||
} | ||
|
||
/** | ||
* @see DATAKV-99 | ||
*/ | ||
@Test | ||
public void closeShouldDoNothing() throws IOException { | ||
|
||
new ForwardingKeyValueIterator<K, V>(iteratorMock).close(); | ||
|
||
verifyZeroInteractions(iteratorMock); | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
src/test/java/org/springframework/data/keyvalue/test/util/IsEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Entry<?, ?>> { | ||
|
||
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<Object>(expected.getKey()).matches(actual.getKey()) | ||
&& new IsEqual<Object>(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<Object, Object> { | ||
|
||
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.