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:
committed by
Oliver Gierke
parent
64db95fb1e
commit
e92c235fdf
@@ -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<String> input = Arrays.asList("a", "b", "c");
|
||||
Stream<String> stream = Java8StreamUtils.createStreamFromIterator(input.iterator());
|
||||
List<String> output = stream.collect(Collectors.<String> toList());
|
||||
|
||||
assertThat(input, is(equalTo(output)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user