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);
java java-8 optional
Lars Hartviksen Nov 15 '16 at 1:51 on 2016-11-15 13:51
source share