From 97298ded19346d98d6284c403e390ccbbc7e282c Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Tue, 30 Apr 2013 21:53:45 +0200 Subject: [PATCH] simplifying equals(...), bug fix hashCode(...) using attribute instead of getter --- .../src/main/java/component/address/User.java | 5 +---- .../HibIntro/src/main/java/component/email/User.java | 7 ++----- .../src/main/java/component/emails/Email.java | 7 ++----- .../HibIntro/src/main/java/component/emails/User.java | 7 ++----- .../src/main/java/component/emails/list/Email.java | 4 +--- .../src/main/java/component/emails/list/User.java | 7 ++----- ws/eclipse/HibIntro/src/main/java/session2/User.java | 11 ++++------- ws/eclipse/HibIntro/src/main/java/session3/User.java | 7 ++----- 8 files changed, 16 insertions(+), 39 deletions(-) diff --git a/ws/eclipse/HibIntro/src/main/java/component/address/User.java b/ws/eclipse/HibIntro/src/main/java/component/address/User.java index a69dd4d4d..0891a61d1 100644 --- a/ws/eclipse/HibIntro/src/main/java/component/address/User.java +++ b/ws/eclipse/HibIntro/src/main/java/component/address/User.java @@ -73,11 +73,8 @@ public class User { public boolean equals(Object other) { if (this == other) { return true; - } else if (getUid() == null) { - return false; } else if (other instanceof User) { - final User that = (User) other; - return this.getUid().equals( that.getUid() ); + return getUid().equals(((User) other).getUid()); } else { return false; } diff --git a/ws/eclipse/HibIntro/src/main/java/component/email/User.java b/ws/eclipse/HibIntro/src/main/java/component/email/User.java index ec9360e77..a0398ceef 100644 --- a/ws/eclipse/HibIntro/src/main/java/component/email/User.java +++ b/ws/eclipse/HibIntro/src/main/java/component/email/User.java @@ -68,11 +68,8 @@ public class User { public boolean equals(Object other) { if (this == other) { return true; - } else if (getUid() == null) { - return false; - } else if (other instanceof User) { - final User that = (User) other; - return this.getUid().equals( that.getUid() ); + } else if (null != other && other instanceof User) { + return this.getUid().equals(((User) other).getUid() ); } else { return false; } diff --git a/ws/eclipse/HibIntro/src/main/java/component/emails/Email.java b/ws/eclipse/HibIntro/src/main/java/component/emails/Email.java index bb486baf0..57967bf4d 100644 --- a/ws/eclipse/HibIntro/src/main/java/component/emails/Email.java +++ b/ws/eclipse/HibIntro/src/main/java/component/emails/Email.java @@ -25,11 +25,8 @@ public class Email { public boolean equals(Object obj) { if (null == getEmailAddress()) { return false; - } else if ( null == obj) { - return false; - } else if (obj instanceof Email) { - Email other = (Email) obj; - return getEmailAddress().equals(other.getEmailAddress()); + } else if ( null != obj && obj instanceof Email) { + return getEmailAddress().equals(((Email) obj).getEmailAddress()); } else { return false; } diff --git a/ws/eclipse/HibIntro/src/main/java/component/emails/User.java b/ws/eclipse/HibIntro/src/main/java/component/emails/User.java index 743d610db..dd8394109 100644 --- a/ws/eclipse/HibIntro/src/main/java/component/emails/User.java +++ b/ws/eclipse/HibIntro/src/main/java/component/emails/User.java @@ -71,11 +71,8 @@ public class User { public boolean equals(Object other) { if (this == other) { return true; - } else if (getUid() == null) { - return false; - } else if (other instanceof User) { - final User that = (User) other; - return this.getUid().equals( that.getUid() ); + } else if (null != other && other instanceof User) { + return this.getUid().equals(((User) other).getUid()); } else { return false; } diff --git a/ws/eclipse/HibIntro/src/main/java/component/emails/list/Email.java b/ws/eclipse/HibIntro/src/main/java/component/emails/list/Email.java index 6acaa8c83..6fb68b4a0 100644 --- a/ws/eclipse/HibIntro/src/main/java/component/emails/list/Email.java +++ b/ws/eclipse/HibIntro/src/main/java/component/emails/list/Email.java @@ -25,9 +25,7 @@ public class Email { public boolean equals(Object obj) { if (null == getEmailAddress()) { return false; - } else if ( null == obj) { - return false; - } else if (obj instanceof Email) { + } else if (null != obj && obj instanceof Email) { Email other = (Email) obj; return getEmailAddress().equals(other.getEmailAddress()); } else { diff --git a/ws/eclipse/HibIntro/src/main/java/component/emails/list/User.java b/ws/eclipse/HibIntro/src/main/java/component/emails/list/User.java index 054017fc3..5bfb50868 100644 --- a/ws/eclipse/HibIntro/src/main/java/component/emails/list/User.java +++ b/ws/eclipse/HibIntro/src/main/java/component/emails/list/User.java @@ -73,11 +73,8 @@ public class User { public boolean equals(Object other) { if (this == other) { return true; - } else if (getUid() == null) { - return false; - } else if (other instanceof User) { - final User that = (User) other; - return this.getUid().equals( that.getUid() ); + } else if (null != other && other instanceof User) { + return this.getUid().equals(((User) other).getUid()); } else { return false; } diff --git a/ws/eclipse/HibIntro/src/main/java/session2/User.java b/ws/eclipse/HibIntro/src/main/java/session2/User.java index 9eba5e466..6302e2db4 100644 --- a/ws/eclipse/HibIntro/src/main/java/session2/User.java +++ b/ws/eclipse/HibIntro/src/main/java/session2/User.java @@ -66,21 +66,18 @@ public class User { public boolean equals(Object other) { if (this == other) { return true; - } else if (id == null) { - return false; - } else if (other instanceof User) { - final User that = (User) other; - return this.id.equals( that.getId() ); + } else if (null != other && other instanceof User) { + return getId().equals(((User) other).getId()); } else { return false; } } @Override public int hashCode() { - if (null == id) { + if (null == getId()) { return System.identityHashCode(this); } else { - return id.hashCode(); + return getId().hashCode(); } } } \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/session3/User.java b/ws/eclipse/HibIntro/src/main/java/session3/User.java index 33413bb8d..6d5ed9ac5 100644 --- a/ws/eclipse/HibIntro/src/main/java/session3/User.java +++ b/ws/eclipse/HibIntro/src/main/java/session3/User.java @@ -62,11 +62,8 @@ public class User { public boolean equals(Object other) { if (this == other) { return true; - } else if (getUid() == null) { - return false; - } else if (other instanceof User) { - final User that = (User) other; - return this.getUid().equals( that.getUid() ); + } else if (null != other && other instanceof User) { + return getUid().equals(((User) other).getUid()); } else { return false; } -- GitLab