Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code improvement: Get rid of case classes copied between Java and Scala DSL #497

Open
nightscape opened this issue Apr 13, 2020 · 0 comments

Comments

@nightscape
Copy link
Contributor

I noticed that there is a whole bunch of case classes copied between com.ing.baker.runtime.scaladsl and com.ing.baker.runtime.javadsl.
The only difference seems to be the use of Scala's Option vs Java's Optional.
You could get rid of that by adding a companion object to every case class with a fromJava (or differently named) method which takes Optional arguments but constructs the Scala class with an Option. The corresponding Java getter method could convert back to an Optional.
Example:

/**
  * Event describing the fact that an event was received for a process.
  *
  * @param timeStamp The time that the event was received
  * @param recipeName The name of the recipe that interaction is part of
  * @param recipeId The recipe id
  * @param recipeInstanceId The id of the process
  * @param correlationId The (optional) correlation id of the event
  * @param event The event
  */
case class EventReceived(timeStamp: Long,
                         recipeName: String,
                         recipeId: String,
                         recipeInstanceId: String,
                         correlationId: Option[String],
                         event: EventInstance) extends BakerEvent with common.EventReceived {

  def getTimeStamp: Long = timeStamp
  def getRecipeName: String = recipeName
  def getRecipeId: String = recipeId
  def getRecipeInstanceId: String = recipeInstanceId
  def getCorrelationId: Optional[String] = Optional.ofNullable(correlationId.orNull)
  def getEvent: EventInstance = event
}

object EventReceived {
  def fromJava(timeStamp: Long,
                         recipeName: String,
                         recipeId: String,
                         recipeInstanceId: String,
                         correlationId: Optional[String],
                         event: EventInstance
  ) = new EventReceived(
                         timeStamp,
                         recipeName,
                         recipeId,
                         recipeInstanceId,
                         if (correlationId.isPresent) Some(correlationId.get) else None,
                         event: EventInstance
  )
}

The same thing would also work the other way round.

This approach would have a minor performance impact for converting back and forth between Option and Optional, but I would assume this is negligible under real-life scenarios.
If this is desirable, I could create a corresponding PR.

Let me know what you think!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant