DATACMNS-867 - Introduced monadic methods on Streamable.

Streamable now exposes map(…), flatMap(…) and filter(…) to allow easy conversion into new Streamables. Introduced LazyStreamable to back those implementations. Simplified implementation of methods in PartTree to use those new methods.

Changed Page and Slice map(…) method to take a Function over a Converter so that act as specialization of the newly introduced map(…) method.
This commit is contained in:
Oliver Gierke
2017-03-13 09:47:27 +01:00
parent 133e46f6b4
commit 628dc8b9c8
9 changed files with 128 additions and 27 deletions

View File

@@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.core.convert.converter.Converter;
@@ -156,11 +157,11 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
* @param converter must not be {@literal null}.
* @return
*/
protected <U> List<U> getConvertedContent(Converter<? super T, ? extends U> converter) {
protected <U> List<U> getConvertedContent(Function<? super T, ? extends U> converter) {
Assert.notNull(converter, "Converter must not be null!");
return this.stream().map(converter::convert).collect(Collectors.toList());
return this.stream().map(converter::apply).collect(Collectors.toList());
}
/*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2015 the original author or authors.
* Copyright 2008-2017 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.
@@ -15,6 +15,8 @@
*/
package org.springframework.data.domain;
import java.util.function.Function;
import org.springframework.core.convert.converter.Converter;
/**
@@ -47,5 +49,5 @@ public interface Page<T> extends Slice<T> {
* @return a new {@link Page} with the content of the current one mapped by the given {@link Converter}.
* @since 1.10
*/
<U> Page<U> map(Converter<? super T, ? extends U> converter);
<U> Page<U> map(Function<? super T, ? extends U> converter);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2016 the original author or authors.
* Copyright 2008-2017 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.
@@ -17,8 +17,7 @@ package org.springframework.data.domain;
import java.util.List;
import java.util.Optional;
import org.springframework.core.convert.converter.Converter;
import java.util.function.Function;
/**
* Basic {@code Page} implementation.
@@ -106,7 +105,7 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
* @see org.springframework.data.domain.Slice#transform(org.springframework.core.convert.converter.Converter)
*/
@Override
public <U> Page<U> map(Converter<? super T, ? extends U> converter) {
public <U> Page<U> map(Function<? super T, ? extends U> converter) {
return new PageImpl<>(getConvertedContent(converter), pageable, total);
}

View File

@@ -16,6 +16,7 @@
package org.springframework.data.domain;
import java.util.List;
import java.util.function.Function;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.util.Streamable;
@@ -124,5 +125,5 @@ public interface Slice<T> extends Streamable<T> {
* @return a new {@link Slice} with the content of the current one mapped by the given {@link Converter}.
* @since 1.10
*/
<U> Slice<U> map(Converter<? super T, ? extends U> converter);
<U> Slice<U> map(Function<? super T, ? extends U> converter);
}

View File

@@ -16,8 +16,7 @@
package org.springframework.data.domain;
import java.util.List;
import org.springframework.core.convert.converter.Converter;
import java.util.function.Function;
/**
* Default implementation of {@link Slice}.
@@ -70,7 +69,7 @@ public class SliceImpl<T> extends Chunk<T> {
* @see org.springframework.data.domain.Slice#transform(org.springframework.core.convert.converter.Converter)
*/
@Override
public <U> Slice<U> map(Converter<? super T, ? extends U> converter) {
public <U> Slice<U> map(Function<? super T, ? extends U> converter) {
return new SliceImpl<>(getConvertedContent(converter), pageable, hasNext);
}