DATACMNS-650 - Introduced CloseableIterator abstraction as a foundation for streaming of results.

A CloseableIterator abstracts the underlying result with support for releasing the associated resources via close() which can be transparently be used with try-with-resources in Java 7 since Closeable implements AutoCloseable from Java 7 on upwards. Added detector methods to QueryMethod to check whether the current method returns a Stream. 

Introduced ReflectionUtils.isJava8StreamType to safely detect whether the given type is assignable to a Java 8 Stream. Introduced CloseableIteratorDisposingRunnable that can be registered as a cleanup action on a Stream.

Original pull request: #116.
This commit is contained in:
Thomas Darimont
2015-02-24 13:51:49 +01:00
committed by Oliver Gierke
parent 64db95fb1e
commit e92c235fdf
7 changed files with 244 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
/*
* 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.util;
import java.io.Closeable;
import java.util.Iterator;
/**
* A {@link CloseableIterator} serves as a bridging data structure for the underlying data store specific results that
* can be wrapped in a Java 8 {@link java.util.stream.Stream}.
* <p>
*
* @author Thomas Darimont
* @param <T>
* @since 1.10
*/
public interface CloseableIterator<T> extends Iterator<T>, Closeable {
/**
* Closes this iterator, freeing any resources created.
*/
@Override
void close();
}

View File

@@ -0,0 +1,57 @@
/*
* 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.util;
/**
* A {@link Runnable} that closes the given {@link CloseableIterator} in its {@link #run()} method. If the given
* {@code closeable} is {@literal null} the {@link #run()} method effectively becomes a noop.
* <p>
* Can be used in conjunction with streams as close action via:
*
* <pre>
* CloseableIterator<T> result = ...;
* Spliterator<T> spliterator = ...;
*
* return StreamSupport.stream(spliterator, false).onClose(new CloseableIteratorDisposingRunnable(result));
* </pre>
*
* @author Thomas Darimont
*/
public class CloseableIteratorDisposingRunnable implements Runnable {
private CloseableIterator<?> closeable;
/**
* Creates a new {@link CloseableIteratorDisposingRunnable}.
*
* @param closeable may be {@literal null}
*/
public CloseableIteratorDisposingRunnable(CloseableIterator<?> closeable) {
this.closeable = closeable;
}
@Override
public void run() {
CloseableIterator<?> ci = closeable;
if (ci == null) {
return;
}
closeable = null;
ci.close();
}
}

View File

@@ -0,0 +1,56 @@
/*
* 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.util;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.springframework.util.Assert;
/**
* Spring Data specific Java {@link Stream} utility methods and classes.
*
* @author Thomas Darimont
* @since 1.8
*/
public class Java8StreamUtils {
private Java8StreamUtils() {}
/**
* Returns a {@link Stream} backed by the given {@link Iterator}.
* <p>
* If the given iterator is an {@link CloseableIterator} add a {@link CloseableIteratorDisposingRunnable} wrapping the
* given iterator to propagate {@link Stream#close()} accordingly.
*
* @param iterator must not be {@literal null}
* @return
* @since 1.8
*/
public static <T> Stream<T> createStreamFromIterator(Iterator<T> iterator) {
Assert.notNull(iterator, "Iterator must not be null!");
Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.NONNULL);
Stream<T> stream = StreamSupport.stream(spliterator, false);
return iterator instanceof CloseableIterator ? stream.onClose(new CloseableIteratorDisposingRunnable(
(CloseableIterator<T>) iterator)) : stream;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-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.
@@ -19,6 +19,7 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.stream.Stream;
import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.AnnotationUtils;
@@ -30,10 +31,24 @@ import org.springframework.util.ReflectionUtils.FieldFilter;
* Spring Data specific reflection utility methods and classes.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @since 1.5
*/
public abstract class ReflectionUtils {
private static final Class<?> JAVA8_STREAM_TYPE;
static {
Class<?> cls = null;
try {
cls = Class.forName("java.util.stream.Stream");
} catch (ClassNotFoundException ignore) {}
JAVA8_STREAM_TYPE = cls;
}
private ReflectionUtils() {}
/**
@@ -208,4 +223,19 @@ public abstract class ReflectionUtils {
org.springframework.util.ReflectionUtils.makeAccessible(field);
org.springframework.util.ReflectionUtils.setField(field, target, value);
}
/**
* Tests whether the given {@code type} is assignable from a Java 8 {@link Stream}.
*
* @param type may be {@literal null}
* @return
*/
public static boolean isJava8StreamType(Class<?> type) {
if (type == null || JAVA8_STREAM_TYPE == null) {
return false;
}
return JAVA8_STREAM_TYPE.isAssignableFrom(type);
}
}