Skip to content

Commit 02759f5

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
DATAKV-99 - Polishing.
Removed dedicated Entry<K,V> interface, since Map.Entry<K,V> is already sufficient. Favoured CloseableIterator over KeyValueIterator since it offers no additional functionality but less generic parameter clutter. Added missing JavaDoc.
1 parent 952a901 commit 02759f5

File tree

9 files changed

+116
-172
lines changed

9 files changed

+116
-172
lines changed

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

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.util.Iterator;
19+
import java.util.Map;
20+
21+
import org.springframework.data.util.CloseableIterator;
22+
23+
/**
24+
* Forwards {@link CloseableIterator} invocations to the configured {@link Iterator} delegate.
25+
*
26+
* @author Christoph Strobl
27+
* @author Thomas Darimont
28+
* @param <K>
29+
* @param <V>
30+
*/
31+
public class ForwardingCloseableIterator<K, V> implements CloseableIterator<Map.Entry<K, V>> {
32+
33+
private final Iterator<? extends Map.Entry<K, V>> delegate;
34+
private final Runnable closeHandler;
35+
36+
/**
37+
* Creates a new {@link ForwardingCloseableIterator}.
38+
*
39+
* @param delegate must not be {@literal null}
40+
*/
41+
public ForwardingCloseableIterator(Iterator<? extends Map.Entry<K, V>> delegate) {
42+
this(delegate, null);
43+
}
44+
45+
/**
46+
* Creates a new {@link ForwardingCloseableIterator} that invokes the configured {@code closeHanlder} on {@link #close()}.
47+
*
48+
* @param delegate must not be {@literal null}
49+
* @param closeHandler may be {@literal null}
50+
*/
51+
public ForwardingCloseableIterator(Iterator<? extends Map.Entry<K, V>> delegate, Runnable closeHandler) {
52+
this.delegate = delegate;
53+
this.closeHandler = closeHandler;
54+
}
55+
56+
@Override
57+
public boolean hasNext() {
58+
return delegate.hasNext();
59+
}
60+
61+
@Override
62+
public Map.Entry<K, V> next() {
63+
return delegate.next();
64+
}
65+
66+
@Override
67+
public void close() {
68+
if (closeHandler != null) {
69+
closeHandler.run();
70+
}
71+
}
72+
}

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

-79
This file was deleted.

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
import java.io.Serializable;
1919
import java.util.Collection;
20+
import java.util.Map;
2021

2122
import org.springframework.beans.factory.DisposableBean;
2223
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
24+
import org.springframework.data.util.CloseableIterator;
2325

2426
/**
2527
* {@link KeyValueAdapter} unifies access and shields the underlying key/value specific implementation.
@@ -79,7 +81,7 @@ public interface KeyValueAdapter extends DisposableBean {
7981
* @param keyspace
8082
* @return
8183
*/
82-
KeyValueIterator<? extends Serializable, ?> entries(Serializable keyspace);
84+
CloseableIterator<Map.Entry<Serializable, Object>> entries(Serializable keyspace);
8385

8486
/**
8587
* Remove all objects of given type.

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

-28
This file was deleted.

src/main/java/org/springframework/data/map/MapKeyValueAdapter.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717

1818
import java.io.Serializable;
1919
import java.util.Collection;
20+
import java.util.Iterator;
2021
import java.util.Map;
22+
import java.util.Map.Entry;
2123
import java.util.concurrent.ConcurrentHashMap;
2224

2325
import org.springframework.core.CollectionFactory;
2426
import org.springframework.data.keyvalue.core.AbstractKeyValueAdapter;
25-
import org.springframework.data.keyvalue.core.ForwardingKeyValueIterator;
27+
import org.springframework.data.keyvalue.core.ForwardingCloseableIterator;
2628
import org.springframework.data.keyvalue.core.KeyValueAdapter;
27-
import org.springframework.data.keyvalue.core.KeyValueIterator;
29+
import org.springframework.data.util.CloseableIterator;
2830
import org.springframework.util.Assert;
2931
import org.springframework.util.ClassUtils;
3032

@@ -104,7 +106,6 @@ public boolean contains(Serializable id, Serializable keyspace) {
104106
return get(id, keyspace) != null;
105107
}
106108

107-
108109
/* (non-Javadoc)
109110
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#count(java.io.Serializable)
110111
*/
@@ -149,8 +150,8 @@ public Collection<?> getAllOf(Serializable keyspace) {
149150
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#entries(java.io.Serializable)
150151
*/
151152
@Override
152-
public KeyValueIterator<Serializable, ?> entries(Serializable keyspace) {
153-
return new ForwardingKeyValueIterator<Serializable, Object>(getKeySpaceMap(keyspace).entrySet().iterator());
153+
public CloseableIterator<Map.Entry<Serializable, Object>> entries(Serializable keyspace) {
154+
return new ForwardingCloseableIterator<Serializable, Object>(getKeySpaceMap(keyspace).entrySet().iterator());
154155
}
155156

156157
/*

src/test/java/org/springframework/data/keyvalue/core/ForwardingIteratorUnitTests.java renamed to src/test/java/org/springframework/data/keyvalue/core/ForwardingCloseableIteratorUnitTests.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.junit.Assert.*;
2121
import static org.mockito.Mockito.*;
2222

23-
import java.io.IOException;
2423
import java.util.Iterator;
2524
import java.util.Map;
2625
import java.util.NoSuchElementException;
@@ -32,11 +31,13 @@
3231

3332
/**
3433
* @author Christoph Strobl
34+
* @author Thomas Darimont
3535
*/
3636
@RunWith(MockitoJUnitRunner.class)
37-
public class ForwardingIteratorUnitTests<K, V> {
37+
public class ForwardingCloseableIteratorUnitTests<K, V> {
3838

3939
@Mock Iterator<Map.Entry<K, V>> iteratorMock;
40+
@Mock Runnable closeActionMock;
4041

4142
/**
4243
* @see DATAKV-99
@@ -46,7 +47,7 @@ public void hasNextShoudDelegateToWrappedIterator() {
4647

4748
when(iteratorMock.hasNext()).thenReturn(true);
4849

49-
assertThat(new ForwardingKeyValueIterator<K, V>(iteratorMock).hasNext(), is(true));
50+
assertThat(new ForwardingCloseableIterator<K, V>(iteratorMock).hasNext(), is(true));
5051

5152
verify(iteratorMock, times(1)).hasNext();
5253
}
@@ -59,7 +60,7 @@ public void nextShoudDelegateToWrappedIterator() {
5960

6061
when(iteratorMock.next()).thenReturn((Map.Entry<K, V>) mock(Map.Entry.class));
6162

62-
assertThat(new ForwardingKeyValueIterator<K, V>(iteratorMock).next(), notNullValue());
63+
assertThat(new ForwardingCloseableIterator<K, V>(iteratorMock).next(), notNullValue());
6364

6465
verify(iteratorMock, times(1)).next();
6566
}
@@ -72,18 +73,28 @@ public void nextShoudThrowErrorWhenWrappedIteratorHasNoMoreElements() {
7273

7374
when(iteratorMock.next()).thenThrow(new NoSuchElementException());
7475

75-
new ForwardingKeyValueIterator<K, V>(iteratorMock).next();
76+
new ForwardingCloseableIterator<K, V>(iteratorMock).next();
7677
}
7778

7879
/**
7980
* @see DATAKV-99
8081
*/
8182
@Test
82-
public void closeShouldDoNothing() throws IOException {
83+
public void closeShouldDoNothingByDefault() {
8384

84-
new ForwardingKeyValueIterator<K, V>(iteratorMock).close();
85+
new ForwardingCloseableIterator<K, V>(iteratorMock).close();
8586

8687
verifyZeroInteractions(iteratorMock);
8788
}
8889

90+
/**
91+
* @see DATAKV-99
92+
*/
93+
@Test
94+
public void closeShouldInvokeConfiguredCloseAction() {
95+
96+
new ForwardingCloseableIterator<K, V>(iteratorMock, closeActionMock).close();
97+
98+
verify(closeActionMock, times(1)).run();
99+
}
89100
}

0 commit comments

Comments
 (0)