Is there a class like Optional, but for optional?

It is convenient to declare functions for comparing values ​​and their consumption, if present.

In a situation where you have several required objects and several options, I find that I am wrapping the others in Optional.of (mandatory), so that I can use the same expressions for them without writing it all back.

Food vegetables = Food.someVegetables(); Optional<Food> condiment = Food.someCondiment(); Optional<Food> spices = Food.someSpices(); condiment.map(prepare).ifPresent(putOnPlate); spices.map(prepare).ifPresent(putOnPlate); 

But then I do not like this code:

 putOnPlate.accept(prepare.apply(vegetables)); 

so I will complete it:

 Optional.of(vegetables).map(prepare).ifPresent(putOnPlate); 

But this is simply wrong, because vegetables (in this example) are actually optional. They are very important, and I just gave everyone the impression that they are optional.

So my question is: is there some kind of class in java like java.util.Mandatory, so I can write:

 Mandatory.of(vegetables).map(prepare).definitelyPresentSo(putOnPlate); 
+42
java java-8 optional
Nov 15 '16 at
source share
4 answers

Yes, there is such an API. You can replace

 Optional.of(vegetables).map(prepare).ifPresent(putOnPlate); 

from

 Stream.of(vegetables).map(prepare).forEach(putOnPlate); 

Now we have to live with the fact that a single Stream element is a special case of a stream of arbitrary elements (including a possible empty stream).

But you can process all required elements at once

 Stream.of(mandatory1, mandatory2, mandatory3 /* etc */).map(prepare).forEach(putOnPlate); 

It would even be possible to include optional elements, but it will not be as convenient as it should be, since Optional.stream() will not be introduced before Java 9.

+28
Nov 15 '16 at 2:31 on
source share
β€” -

Optional main idea is to abstract zero probability (getting rid of zero checks) and provide a free API for working with optional values.

In the case of a value that is always present, there is nothing abstract (at least nothing with a practical meaning), so in pure Java there are no such tools.

In other functional languages, you have several β€œmonadic” tools, such as Optional , available for different use cases. If you want to bring them in Java, Javaslang is probably the best place to look. You can find tools like Option , Try , Lazy , Validation , Either , Future , Tuple and entire Collections API that allow you to code in the same way as you described.

+10
Nov 15 '16 at 2:02
source share

Maybe I'm wrong, but if you don't like to use Optional , you can just use Stream only one object. Therefore, instead of

 Optional.of(vegetables).map(prepare).ifPresent(putOnPlate); 

you can just use:

 Stream.of(vegetables).map(prepare).forEach(putOnPlate); 

The result will be identical. Both methods will throw NPE if vegetables are zero.

 Arrays.asList(10, null).forEach(value -> { Optional.of(value).map(x -> x.toString()).ifPresent(System.out::println); Stream.of(value).map(x -> x.toString()).forEach(System.out::println); }); 

The safe version of NPE will be

  Optional.ofNullable(value).map(x -> x.toString()).ifPresent(System.out::println); Stream.of(value).filter(Objects::nonNull).map(x -> x.toString()).forEach(System.out::println); 

Optional is just a container for an object that may or may not contain a non-zero value.

+4
Nov 15 '16 at 2:31 on
source share

You seem to be looking for type @NonNull Food .

0
Nov 16 '16 at 18:47
source share



All Articles