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:
@@ -63,18 +63,22 @@ public class QueryMethod {
|
||||
}
|
||||
}
|
||||
|
||||
this.method = method;
|
||||
this.parameters = createParameters(method);
|
||||
this.metadata = metadata;
|
||||
|
||||
if (hasParameterOfType(method, Pageable.class)) {
|
||||
assertReturnTypeAssignable(method, Slice.class, Page.class, List.class);
|
||||
|
||||
if (!isStreamQuery()) {
|
||||
assertReturnTypeAssignable(method, Slice.class, Page.class, List.class);
|
||||
}
|
||||
|
||||
if (hasParameterOfType(method, Sort.class)) {
|
||||
throw new IllegalStateException(String.format("Method must not have Pageable *and* Sort parameter. "
|
||||
+ "Use sorting capabilities on Pageble instead! Offending method: %s", method.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
this.method = method;
|
||||
this.parameters = createParameters(method);
|
||||
this.metadata = metadata;
|
||||
|
||||
Assert.notNull(this.parameters);
|
||||
|
||||
if (isPageQuery()) {
|
||||
@@ -205,6 +209,16 @@ public class QueryMethod {
|
||||
return getDomainClass().isAssignableFrom(getReturnedObjectType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the method returns a Stream.
|
||||
*
|
||||
* @return
|
||||
* @since 1.10
|
||||
*/
|
||||
public boolean isStreamQuery() {
|
||||
return ReflectionUtils.isJava8StreamType(method.getReturnType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Parameters} wrapper to gain additional information about {@link Method} parameters.
|
||||
*
|
||||
@@ -222,12 +236,4 @@ public class QueryMethod {
|
||||
public String toString() {
|
||||
return method.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @since 1.10
|
||||
*/
|
||||
public boolean isStreamQuery() {
|
||||
return ReflectionUtils.isJava8StreamType(method.getReturnType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2014 the original author or authors.
|
||||
* Copyright 2008-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.
|
||||
@@ -50,7 +50,7 @@ public class PartTree implements Iterable<OrPart> {
|
||||
* @see http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#ubc
|
||||
*/
|
||||
private static final String KEYWORD_TEMPLATE = "(%s)(?=(\\p{Lu}|\\P{InBASIC_LATIN}))";
|
||||
private static final String QUERY_PATTERN = "find|read|get|query";
|
||||
private static final String QUERY_PATTERN = "find|read|get|query|stream";
|
||||
private static final String COUNT_PATTERN = "count";
|
||||
private static final String DELETE_PATTERN = "delete|remove";
|
||||
private static final Pattern PREFIX_TEMPLATE = Pattern.compile( //
|
||||
|
||||
@@ -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