Created
December 3, 2013 12:28
-
-
Save f81/7768418 to your computer and use it in GitHub Desktop.
第16章:Scalaの等価性 ref: http://qiita.com/f81@github/items/a90c16419645be5d7008
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object CaseClass { | |
def main(args: Array[String]): Unit = { | |
val domain1 = new Domain3(1, "富樫") | |
val domain2 = new Domain3(1, "富樫") | |
println("==:", domain1 == domain2) | |
println("!=:", domain1 != domain2) | |
val domain3 = new Domain3(2, "虎丸") | |
println("==:", domain1 == domain3) | |
println("!=:", domain1 != domain3) | |
} | |
case class Domain3(val id: Long, val name: String) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Equals { | |
public static void main(String[] args) { | |
final String a = new String("test"); | |
final String b = new String("test"); | |
System.out.println(a == b); | |
System.out.println(a.equals(b)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Equivalence { | |
def main(args: Array[String]): Unit = { | |
val domain1 = new Domain1(1, "富樫") | |
val domain2 = new Domain1(1, "富樫") | |
println(domain1 == domain2) | |
println(domain1 != domain2) | |
val domain3 = new Domain1(2, "虎丸") | |
println("==:", domain1 == domain3) | |
println("!=:", domain1 != domain3) | |
} | |
} | |
class Domain1(val id: Long, val name: String) { | |
def canEqual(other: Any) = { | |
other.isInstanceOf[Domain1] | |
} | |
override def equals(other: Any) = { | |
other match { | |
case that: Domain1 => | |
that.canEqual(Domain1.this) && id == that.id && name == that.name | |
case _ => false | |
} | |
} | |
override def hashCode() = { | |
val prime = 41 | |
prime * (prime + id.hashCode) + name.hashCode | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scala Equivalence | |
true // domain1 == domain2 | |
false // domain1 != domain2 | |
false // domain1 == domain3 | |
true // domain1 != domain3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scala Identity | |
true // domain1 eq domain2 | |
false // domain1 ne domain2 | |
false // domain1 eq domain3 | |
true // domain1 ne domain3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
false // a == b | |
true // a.equals(b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala CaseClass | |
true // domain1 == domain2 | |
false // domain1 != domain2 | |
false // domain1 == domain3 | |
true // domain1 != domain3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scala NoneCaseClass | |
true // domain1 == domain2 | |
false // domain1 != domain2 | |
false // domain1 == domain3 | |
true // domain1 != domain3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Identity { | |
def main(args: Array[String]): Unit = { | |
val domain1 = new Domain2(1, "富樫") | |
val domain2 = domain1 | |
println(domain1 eq domain2) | |
println(domain1 ne domain2) | |
val domain3 = new Domain2(1, "富樫") | |
println(domain1 eq domain3) | |
println(domain1 ne domain3) | |
} | |
} | |
class Domain2(val id: Long, val name: String) { | |
def canEqual(other: Any) = { | |
other.isInstanceOf[Domain2] | |
} | |
override def equals(other: Any) = { | |
other match { | |
case that: Domain2 => | |
that.canEqual(Domain2.this) && id == that.id && name == that.name | |
case _ => false | |
} | |
} | |
override def hashCode() = { | |
val prime = 41 | |
prime * (prime + id.hashCode) + name.hashCode | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object NoneCaseClass { | |
def main(args: Array[String]): Unit = { | |
val domain1 = new Domain4(1, "富樫") | |
val domain2 = domain1 | |
println("==:", domain1 == domain2) | |
println("!=:", domain1 != domain2) | |
val domain3 = new Domain4(2, "虎丸") | |
println("==:", domain1 == domain3) | |
println("!=:", domain1 != domain3) | |
} | |
class Domain4(val id: Long, val name: String) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment