I am doing a big Java/Spring rewrite into a Java/Scala/Spring project. So I just want to check if this is the right sort of thing to do.
I am getting the Spring user details from the SecuritContext. The following code is called for those that are and are not logged in. I want to check that the check for null is the correct thing to do. Please advise because checking for nulls like this doesn’t feel right.
private def getUserDetails : Option[UserDetails] = {
if(userDetails == null && securityContextFacade.getSecurityContext.getAuthentication != null){
val details = securityContextFacade.getSecurityContext.getAuthentication.getPrincipal
userDetails = details.asInstanceOf[UserDetails]
}
if(userDetails != null){
Option(userDetails)
}
else{
None
}
}
There is no necessary for doing latter check, because Option(…) already do it inside and evaluates to None in case of null. So
can be written as