Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#962 Add support for mapping Java 8 Streams
- Loading branch information
Showing
82 changed files
with
4,691 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
[[mapping-streams]] | ||
== Mapping Streams | ||
|
||
The mapping of `java.util.Stream` is done in a similar way as the mapping of collection types, i.e. by defining mapping | ||
methods with the required source and target types in a mapper interface. | ||
|
||
The generated code will contain the creation of a `Stream` from the provided `Iterable`/array or will collect the | ||
provided `Stream` into an `Iterable`/array. If a mapping method or an implicit conversion for the source and target | ||
element types exists, then this conversion will be done in `Stream#map()`. The following shows an example: | ||
|
||
.Mapper with stream mapping methods | ||
==== | ||
[source, java, linenums] | ||
[subs="verbatim,attributes"] | ||
---- | ||
@Mapper | ||
public interface CarMapper { | ||
Set<String> integerStreamToStringSet(Stream<Integer> integers); | ||
List<CarDto> carsToCarDtos(Stream<Car> cars); | ||
CarDto carToCarDto(Car car); | ||
} | ||
---- | ||
==== | ||
|
||
The generated implementation of the `integerStreamToStringSet()` performs the conversion from `Integer` to `String` for | ||
each element, while the generated `carsToCarDtos()` method invokes the `carToCarDto()` method for each contained | ||
element as shown in the following: | ||
|
||
.Generated stream mapping methods | ||
==== | ||
[source, java, linenums] | ||
[subs="verbatim,attributes"] | ||
---- | ||
//GENERATED CODE | ||
@Override | ||
public Set<String> integerStreamToStringSet(Stream<Integer> integers) { | ||
if ( integers == null ) { | ||
return null; | ||
} | ||
return integers.stream().map( integer -> String.valueOf( integer ) ) | ||
.collect( Collectors.toCollection( HashSet<String>::new ) ); | ||
} | ||
@Override | ||
public List<CarDto> carsToCarDtos(Stream<Car> cars) { | ||
if ( cars == null ) { | ||
return null; | ||
} | ||
return integers.stream().map( car -> carToCarDto( car ) ) | ||
.collect( Collectors.toCollection( ArrayList<CarDto>::new ) ); | ||
} | ||
---- | ||
==== | ||
|
||
[WARNING] | ||
==== | ||
If a mapping from a `Stream` to an `Iterable` or an array is performed, then the passed `Stream` will be consumed | ||
and it will no longer be possible to consume it. | ||
==== | ||
|
||
The same implementation types as in <<implementation-types-for-collection-mappings>> are used for the creation of the | ||
collection when doing `Stream` to `Iterable` mapping. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.