1

I want to store information of an open project in a web based project that denotes CSV-like data.

I have

 case class Project(user:String, sessionId : String, fileOpen : String, commands:ArrayBuffer[Command]) 

Question : will commands that have different elements change affect the meaning of equals?

If I do not want commands to take part in equals and hash code and don't want to over ride those methods too, can I declare commands after the class declaration :

case class Project(user:String, sessionId : String, fileOpen : String) ...{
     val commands:ArrayBuffer[Command]

Any other way to tell the compiler not to use it in the equals and hascode?

radumanolescu
  • 4,059
  • 2
  • 31
  • 44
tgkprog
  • 4,493
  • 4
  • 41
  • 70

1 Answers1

1

override equals to only compare the required fields for equality check and ignore commands.

Do not use ArrayBuffer instead use immutable solution.

If you want add commands to a exisiting Project instance use copy to do that

val newProject = project.copy(commands = Command("ls") :: project.commands)

assuming command is declared like this case class Command(name: String)

Overriding equals and hashCode

case class Project(user:String, sessionId : String, fileOpen : String, commands: List[Command]) {
  override def equals(obj: scala.Any): Boolean = obj match {
    case obj: Project => this.user == obj.user && this.sessionId == obj.sessionId && this.fileOpen == obj.fileOpen
    case _ => false
  }
  override def hashCode(): Int = 1013 * user.hashCode + 1009 * sessionId.hashCode + 1003 * fileOpen.hashCode
}
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40