diff --git a/src/main/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java b/src/main/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java index 9f0d849f3..46d32815d 100644 --- a/src/main/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java +++ b/src/main/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2014 the original author or authors. + * Copyright 2011-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. @@ -24,6 +24,7 @@ import org.springframework.data.repository.core.CrudMethods; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.util.QueryExecutionConverters; import org.springframework.data.util.ClassTypeInformation; +import org.springframework.data.util.ReflectionUtils; import org.springframework.data.util.TypeInformation; import org.springframework.util.Assert; @@ -125,7 +126,7 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata { Class> rawType = type.getType(); boolean needToUnwrap = Iterable.class.isAssignableFrom(rawType) || rawType.isArray() - || QueryExecutionConverters.supports(rawType); + || QueryExecutionConverters.supports(rawType) || ReflectionUtils.isJava8StreamType(rawType); return needToUnwrap ? unwrapWrapperTypes(type.getComponentType()) : rawType; } diff --git a/src/main/java/org/springframework/data/repository/query/QueryMethod.java b/src/main/java/org/springframework/data/repository/query/QueryMethod.java index f1c704f3a..e92b08eb8 100644 --- a/src/main/java/org/springframework/data/repository/query/QueryMethod.java +++ b/src/main/java/org/springframework/data/repository/query/QueryMethod.java @@ -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. @@ -26,6 +26,7 @@ import org.springframework.data.domain.Slice; import org.springframework.data.domain.Sort; import org.springframework.data.repository.core.EntityMetadata; import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.data.util.ReflectionUtils; import org.springframework.util.Assert; /** @@ -33,6 +34,7 @@ import org.springframework.util.Assert; * with specific information that is necessary to construct {@link RepositoryQuery}s for the method. * * @author Oliver Gierke + * @author Thomas Darimont */ public class QueryMethod { @@ -220,4 +222,12 @@ public class QueryMethod { public String toString() { return method.toString(); } + + /** + * @return + * @since 1.10 + */ + public boolean isStreamQuery() { + return ReflectionUtils.isJava8StreamType(method.getReturnType()); + } } diff --git a/src/main/java/org/springframework/data/util/CloseableIterator.java b/src/main/java/org/springframework/data/util/CloseableIterator.java new file mode 100644 index 000000000..84d6798c5 --- /dev/null +++ b/src/main/java/org/springframework/data/util/CloseableIterator.java @@ -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}. + *
+ *
+ * @author Thomas Darimont
+ * @param
+ * Can be used in conjunction with streams as close action via:
+ *
+ *
+ * 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
+ * CloseableIterator
+ *
+ * @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();
+ }
+}
diff --git a/src/main/java/org/springframework/data/util/Java8StreamUtils.java b/src/main/java/org/springframework/data/util/Java8StreamUtils.java
new file mode 100644
index 000000000..4072c10be
--- /dev/null
+++ b/src/main/java/org/springframework/data/util/Java8StreamUtils.java
@@ -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}.
+ *