I have the following class where the properties are an Option[T]
class User extends IdBaseEntity[UUID] { var id: Option[UUID] = None var name: Option[String] = None var createdOn: Option[Date] = None }
In some data access layer I need to assign these properties if they aren't set before the object is persisted to cassandra. Here are a few ways for the createdOn
property. Are any of these the best approach or is there something better I should be doing?
Example 1
entity.createdOn = Some(entity.createdOn.map(identity).getOrElse(new Date()))
Example 2
entity.createdOn = entity.createdOn.orElse(Some(new Date()))
Example 3
entity.createdOn = entity.createdOn match { case None => Some(new Date()) case _ => entity.createdOn }
Example 4
entity.createdOn = entity.createdOn match { case None => Some(new Date()) case Some(x) => Some(x) }
Example 5
entity.createdOn match { case None => entity.createdOn = Some(new Date()) case _ =>; }
case class
that yields an instance with all incomplete fields filled with the specified defaults. (I'd do it using the tactic in (2), as Chris B suggests). – Randall Schulz