DATACMNS-650 - Polishing.
Added stream to the list of supported query method prefixes. Allow Stream to be used as return type for paginating queries, too. Renamed Java8StreamUtils to StreamUtils. Some additional JavaDoc. Original pull request: #116.
This commit is contained in:
@@ -20,8 +20,8 @@ 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>
|
||||
* can be wrapped in a Java 8 {@link java.util.stream.Stream}. This allows implementations to clean up any resources
|
||||
* they need to keep open to iterate over elements.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @param <T>
|
||||
@@ -29,10 +29,10 @@ import java.util.Iterator;
|
||||
*/
|
||||
public interface CloseableIterator<T> extends Iterator<T>, Closeable {
|
||||
|
||||
/**
|
||||
* Closes this iterator, freeing any resources created.
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.io.Closeable#close()
|
||||
*/
|
||||
@Override
|
||||
void close();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,57 +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.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();
|
||||
}
|
||||
}
|
||||
@@ -225,9 +225,9 @@ public abstract class ReflectionUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether the given {@code type} is assignable from a Java 8 {@link Stream}.
|
||||
* Tests whether the given type is assignable to a Java 8 {@link Stream}.
|
||||
*
|
||||
* @param type may be {@literal null}
|
||||
* @param type can be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static boolean isJava8StreamType(Class<?> type) {
|
||||
|
||||
@@ -27,11 +27,12 @@ import org.springframework.util.Assert;
|
||||
* Spring Data specific Java {@link Stream} utility methods and classes.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.8
|
||||
* @author Oliver Gierke
|
||||
* @since 1.10
|
||||
*/
|
||||
public class Java8StreamUtils {
|
||||
public class StreamUtils {
|
||||
|
||||
private Java8StreamUtils() {}
|
||||
private StreamUtils() {}
|
||||
|
||||
/**
|
||||
* Returns a {@link Stream} backed by the given {@link Iterator}.
|
||||
@@ -53,4 +54,47 @@ public class Java8StreamUtils {
|
||||
return iterator instanceof CloseableIterator ? stream.onClose(new CloseableIteratorDisposingRunnable(
|
||||
(CloseableIterator<T>) iterator)) : stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @author Oliver Gierke
|
||||
* @since 1.10
|
||||
*/
|
||||
private static class CloseableIteratorDisposingRunnable implements Runnable {
|
||||
|
||||
private CloseableIterator<?> closeable;
|
||||
|
||||
/**
|
||||
* Creates a new {@link CloseableIteratorDisposingRunnable} for the given {@link CloseableIterator}.
|
||||
*
|
||||
* @param closeable can be {@literal null}.
|
||||
*/
|
||||
public CloseableIteratorDisposingRunnable(CloseableIterator<?> closeable) {
|
||||
this.closeable = closeable;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Runnable#run()
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
if (closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user