Is there a "nice" way to create a copy of an Optional object?
For example, say I have a method
Optional<Obj> doSomeStuff(Optional<Obj> input){
if(something){
return Optional.copyOf(input); // does this exist in some Guava library or something?
}
// ...
}
Basically, I want to have immutability so that I don't pass the same Optional around, and if something
gets triggered then I want to make sure that I create a brand new Optional with same contents of the input
Optional (whether it be empty or not).
Is there any clean way to do it? The Optional.copyOf
method does not exist.
Optional
, Why should Java 8's Optional not be used in arguments, Sonar RSPEC-3553Optional
since Java 8.