Reputation: 223
I have very little knowledge on Groovy. I am using Groovy 2.4.12
.
I would like to do null
check on a variable which can be primitive or object. So I am trying to use Optional
utility to do null check. I am getting Optional can not be resolved
.
if(Optional.ofNullable(${value}).orElse(null) != null) {};
${value}
will be mapped from java code.
Upvotes: 1
Views: 4346
Reputation: 1145
Optional
was introduced in Java 1.8 (https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html)
Groovy 2.4.12 uses Java 1.7 (here's the matric http://groovy-lang.org/download.html#requirements)
If you have Java 1.8+ installed, Groovy should be able to use Optional
, provided it java.util.*
or java.util.Optional
was imported.
With the code sample you provided it is unclear what you're actually trying to accomplish, but, strictly speaking, Optional
does not save you from checking for null
, you just do it differently.
Upvotes: 2