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.
This commit is contained in:
Christoph Strobl
2015-05-08 14:24:32 +02:00
committed by Thomas Darimont
parent 952a901164
commit 02759f58ea
9 changed files with 116 additions and 172 deletions

View File

@@ -1,27 +0,0 @@
/*
* 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> {
}

View File

@@ -0,0 +1,72 @@
/*
* 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.Iterator;
import java.util.Map;
import org.springframework.data.util.CloseableIterator;
/**
* Forwards {@link CloseableIterator} invocations to the configured {@link Iterator} delegate.
*
* @author Christoph Strobl
* @author Thomas Darimont
* @param <K>
* @param <V>
*/
public class ForwardingCloseableIterator<K, V> implements CloseableIterator<Map.Entry<K, V>> {
private final Iterator<? extends Map.Entry<K, V>> delegate;
private final Runnable closeHandler;
/**
* Creates a new {@link ForwardingCloseableIterator}.
*
* @param delegate must not be {@literal null}
*/
public ForwardingCloseableIterator(Iterator<? extends Map.Entry<K, V>> delegate) {
this(delegate, null);
}
/**
* Creates a new {@link ForwardingCloseableIterator} that invokes the configured {@code closeHanlder} on {@link #close()}.
*
* @param delegate must not be {@literal null}
* @param closeHandler may be {@literal null}
*/
public ForwardingCloseableIterator(Iterator<? extends Map.Entry<K, V>> delegate, Runnable closeHandler) {
this.delegate = delegate;
this.closeHandler = closeHandler;
}
@Override
public boolean hasNext() {
return delegate.hasNext();
}
@Override
public Map.Entry<K, V> next() {
return delegate.next();
}
@Override
public void close() {
if (closeHandler != null) {
closeHandler.run();
}
}
}

View File

@@ -1,79 +0,0 @@
/*
* 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";
}
}
}

View File

@@ -17,9 +17,11 @@ package org.springframework.data.keyvalue.core;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
import org.springframework.data.util.CloseableIterator;
/**
* {@link KeyValueAdapter} unifies access and shields the underlying key/value specific implementation.
@@ -79,7 +81,7 @@ public interface KeyValueAdapter extends DisposableBean {
* @param keyspace
* @return
*/
KeyValueIterator<? extends Serializable, ?> entries(Serializable keyspace);
CloseableIterator<Map.Entry<Serializable, Object>> entries(Serializable keyspace);
/**
* Remove all objects of given type.

View File

@@ -1,28 +0,0 @@
/*
* 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 {
}

View File

@@ -17,14 +17,16 @@ package org.springframework.data.map;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
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.ForwardingCloseableIterator;
import org.springframework.data.keyvalue.core.KeyValueAdapter;
import org.springframework.data.keyvalue.core.KeyValueIterator;
import org.springframework.data.util.CloseableIterator;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -104,7 +106,6 @@ public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
return get(id, keyspace) != null;
}
/* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#count(java.io.Serializable)
*/
@@ -149,8 +150,8 @@ public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
* @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());
public CloseableIterator<Map.Entry<Serializable, Object>> entries(Serializable keyspace) {
return new ForwardingCloseableIterator<Serializable, Object>(getKeySpaceMap(keyspace).entrySet().iterator());
}
/*

View File

@@ -20,7 +20,6 @@ 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;
@@ -32,11 +31,13 @@ import org.mockito.runners.MockitoJUnitRunner;
/**
* @author Christoph Strobl
* @author Thomas Darimont
*/
@RunWith(MockitoJUnitRunner.class)
public class ForwardingIteratorUnitTests<K, V> {
public class ForwardingCloseableIteratorUnitTests<K, V> {
@Mock Iterator<Map.Entry<K, V>> iteratorMock;
@Mock Runnable closeActionMock;
/**
* @see DATAKV-99
@@ -46,7 +47,7 @@ public class ForwardingIteratorUnitTests<K, V> {
when(iteratorMock.hasNext()).thenReturn(true);
assertThat(new ForwardingKeyValueIterator<K, V>(iteratorMock).hasNext(), is(true));
assertThat(new ForwardingCloseableIterator<K, V>(iteratorMock).hasNext(), is(true));
verify(iteratorMock, times(1)).hasNext();
}
@@ -59,7 +60,7 @@ public class ForwardingIteratorUnitTests<K, V> {
when(iteratorMock.next()).thenReturn((Map.Entry<K, V>) mock(Map.Entry.class));
assertThat(new ForwardingKeyValueIterator<K, V>(iteratorMock).next(), notNullValue());
assertThat(new ForwardingCloseableIterator<K, V>(iteratorMock).next(), notNullValue());
verify(iteratorMock, times(1)).next();
}
@@ -72,18 +73,28 @@ public class ForwardingIteratorUnitTests<K, V> {
when(iteratorMock.next()).thenThrow(new NoSuchElementException());
new ForwardingKeyValueIterator<K, V>(iteratorMock).next();
new ForwardingCloseableIterator<K, V>(iteratorMock).next();
}
/**
* @see DATAKV-99
*/
@Test
public void closeShouldDoNothing() throws IOException {
public void closeShouldDoNothingByDefault() {
new ForwardingKeyValueIterator<K, V>(iteratorMock).close();
new ForwardingCloseableIterator<K, V>(iteratorMock).close();
verifyZeroInteractions(iteratorMock);
}
/**
* @see DATAKV-99
*/
@Test
public void closeShouldInvokeConfiguredCloseAction() {
new ForwardingCloseableIterator<K, V>(iteratorMock, closeActionMock).close();
verify(closeActionMock, times(1)).run();
}
}

View File

@@ -15,18 +15,21 @@
*/
package org.springframework.data.keyvalue.test.util;
import java.util.AbstractMap;
import java.util.Map;
import org.hamcrest.CustomMatcher;
import org.hamcrest.core.IsEqual;
import org.springframework.data.keyvalue.core.Entry;
/**
* @author Christoph Strobl
* @author Thomas Darimont
*/
public class IsEntry extends CustomMatcher<Entry<?, ?>> {
public class IsEntry extends CustomMatcher<Map.Entry<?, ?>> {
private final Entry<?, ?> expected;
private final Map.Entry<?, ?> expected;
private IsEntry(Entry<?, ?> entry) {
private IsEntry(Map.Entry<?, ?> entry) {
super(String.format("an entry %s=%s.", entry != null ? entry.getKey() : "null", entry != null ? entry.getValue()
: "null"));
this.expected = entry;
@@ -39,11 +42,11 @@ public class IsEntry extends CustomMatcher<Entry<?, ?>> {
return true;
}
if (!(item instanceof Entry)) {
if (!(item instanceof Map.Entry)) {
return false;
}
Entry<?, ?> actual = (Entry<?, ?>) item;
Map.Entry<?, ?> actual = (Map.Entry<?, ?>) item;
return new IsEqual<Object>(expected.getKey()).matches(actual.getKey())
&& new IsEqual<Object>(expected.getValue()).matches(actual.getValue());
@@ -53,28 +56,16 @@ public class IsEntry extends CustomMatcher<Entry<?, ?>> {
return isEntry(new EntryImpl(key, value));
}
public static IsEntry isEntry(Entry<?, ?> entry) {
public static IsEntry isEntry(Map.Entry<?, ?> entry) {
return new IsEntry(entry);
}
private static class EntryImpl implements Entry<Object, Object> {
private static class EntryImpl extends AbstractMap.SimpleEntry<Object, Object> {
private final Object key;
private final Object value;
private static final long serialVersionUID = 1L;
private EntryImpl(Object key, Object value) {
this.key = key;
this.value = value;
}
@Override
public Object getKey() {
return key;
}
@Override
public Object getValue() {
return value;
super(key, value);
}
@Override

View File

@@ -23,10 +23,11 @@ import static org.junit.Assert.*;
import static org.springframework.data.keyvalue.test.util.IsEntry.*;
import java.io.Serializable;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.keyvalue.core.KeyValueIterator;
import org.springframework.data.util.CloseableIterator;
import org.springframework.util.ObjectUtils;
/**
@@ -198,7 +199,7 @@ public class MapKeyValueAdapterUnitTests {
adapter.put("1", object1, COLLECTION_1);
adapter.put("2", object2, COLLECTION_1);
KeyValueIterator<Serializable, ?> iterator = adapter.entries(COLLECTION_1);
CloseableIterator<Map.Entry<Serializable, Object>> iterator = adapter.entries(COLLECTION_1);
assertThat(iterator.next(), isEntry("1", object1));
assertThat(iterator.next(), isEntry("2", object2));
@@ -222,7 +223,7 @@ public class MapKeyValueAdapterUnitTests {
adapter.put("1", object1, COLLECTION_1);
adapter.put("2", object2, COLLECTION_2);
KeyValueIterator<Serializable, ?> iterator = adapter.entries(COLLECTION_1);
CloseableIterator<Map.Entry<Serializable, Object>> iterator = adapter.entries(COLLECTION_1);
assertThat(iterator.next(), isEntry("1", object1));
assertThat(iterator.hasNext(), is(false));