Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
public class ChainOfRespLambda {

public static Optional<String> parseText(File file) {
return Optional.ofNullable(file)
.filter(f -> f.getType() == File.Type.TEXT )
.map(f -> "Text file: " + f.getContent());
return file.getType() == File.Type.TEXT ?
Optional.of("Text file: " + file.getContent()) :
Optional.empty();
}

public static Optional<String> parsePresentation(File file) {
return Optional.ofNullable(file)
.filter(f -> f.getType() == File.Type.PRESENTATION )
.map(f -> "Presentation file: " + f.getContent());
return file.getType() == File.Type.PRESENTATION ?
Optional.of("Presentation file: " + file.getContent()) :
Optional.empty();
}

public static Optional<String> parseAudio(File file) {
return Optional.ofNullable(file)
.filter(f -> f.getType() == File.Type.AUDIO )
.map(f -> "Audio file: " + f.getContent());
return file.getType() == File.Type.AUDIO ?
Optional.of("Audio file: " + file.getContent()) :
Optional.empty();
}

public static Optional<String> parseVideo(File file) {
return Optional.ofNullable(file)
.filter(f -> f.getType() == File.Type.VIDEO )
.map(f -> "Video file: " + f.getContent());
return file.getType() == File.Type.VIDEO ?
Optional.of("Video file: " + file.getContent()) :
Optional.empty();
}

public static void main( String[] args ) {
Expand All @@ -40,8 +40,7 @@ public static void main( String[] args ) {
ChainOfRespLambda::parseAudio,
ChainOfRespLambda::parseVideo )
.map(f -> f.apply( file ))
.filter( Optional::isPresent )
.map( Optional::get )
.flatMap(o -> o.map(Stream::of).orElseGet(Stream::empty)) // jdk9: Optional#stream()
.findFirst()
.orElseThrow( () -> new RuntimeException( "Unknown file: " + file ) )
);
Expand Down