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 + * @since 1.10 + */ +public interface CloseableIterator extends Iterator, Closeable { + + /** + * Closes this iterator, freeing any resources created. + */ + @Override + void close(); + +} diff --git a/src/main/java/org/springframework/data/util/CloseableIteratorDisposingRunnable.java b/src/main/java/org/springframework/data/util/CloseableIteratorDisposingRunnable.java new file mode 100644 index 000000000..546d934ce --- /dev/null +++ b/src/main/java/org/springframework/data/util/CloseableIteratorDisposingRunnable.java @@ -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. + *

+ * Can be used in conjunction with streams as close action via: + * + *

+ * CloseableIterator result = ...;
+ * Spliterator spliterator = ...;
+ * 
+ * return StreamSupport.stream(spliterator, false).onClose(new CloseableIteratorDisposingRunnable(result));
+ * 
+ * + * @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}. + *

+ * 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 Stream createStreamFromIterator(Iterator iterator) { + + Assert.notNull(iterator, "Iterator must not be null!"); + + Spliterator spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.NONNULL); + Stream stream = StreamSupport.stream(spliterator, false); + + return iterator instanceof CloseableIterator ? stream.onClose(new CloseableIteratorDisposingRunnable( + (CloseableIterator) iterator)) : stream; + } +} diff --git a/src/main/java/org/springframework/data/util/ReflectionUtils.java b/src/main/java/org/springframework/data/util/ReflectionUtils.java index 3570c9aa9..c511be100 100644 --- a/src/main/java/org/springframework/data/util/ReflectionUtils.java +++ b/src/main/java/org/springframework/data/util/ReflectionUtils.java @@ -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); + } } diff --git a/src/test/java/org/springframework/data/util/Java8StreamUtilsTests.java b/src/test/java/org/springframework/data/util/Java8StreamUtilsTests.java new file mode 100644 index 000000000..f536514d6 --- /dev/null +++ b/src/test/java/org/springframework/data/util/Java8StreamUtilsTests.java @@ -0,0 +1,48 @@ +/* + * 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 static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Test; + +/** + * Spring Data specific Java {@link Stream} utility methods and classes. + * + * @author Thomas Darimont + * @since 1.8 + */ +public class Java8StreamUtilsTests { + + /** + * @see DATACMNS-650 + */ + @Test + public void shouldConvertAnIteratorToAStream() { + + List input = Arrays.asList("a", "b", "c"); + Stream stream = Java8StreamUtils.createStreamFromIterator(input.iterator()); + List output = stream.collect(Collectors. toList()); + + assertThat(input, is(equalTo(output))); + } +}