[JAVA-621] Added missing code

* https://www.baeldung.com/java-type-erasure introduction code

* https://www.baeldung.com/java-interfaces Electronic interface and Computer class

* https://www.baeldung.com/java-abstract-class BoardGame and Checkers classes

* https://www.baeldung.com/java-hashcode different ways of implementing hashCode

* https://www.baeldung.com/java-inheritance-composition ComputerBuilder and StandardComputerBuilder classes

* https://www.baeldung.com/java-equals-hashcode-contracts Renamed method to match article

* https://www.baeldung.com/java-static Renamed class to match article

* https://www.baeldung.com/java-nested-classes Renamed class to match article
This commit is contained in:
dupirefr
2020-04-28 08:05:50 +02:00
parent 53f6659143
commit c39e3a6a7d
19 changed files with 328 additions and 8 deletions

View File

@@ -0,0 +1,42 @@
package com.baeldung.hashcode.apachecommons;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;
User user = (User) o;
return id == user.id && (name.equals(user.name) && email.equals(user.email));
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37).
append(id).
append(name).
append(email).
toHashCode();
}
// getters and setters here
}

View File

@@ -0,0 +1,42 @@
package com.baeldung.hashcode.eclipse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;
User user = (User) o;
return id == user.id && (name.equals(user.name) && email.equals(user.email));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
// getters and setters here
}

View File

@@ -0,0 +1,37 @@
package com.baeldung.hashcode.improved;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;
User user = (User) o;
return id == user.id && (name.equals(user.name) && email.equals(user.email));
}
@Override
public int hashCode() {
return (int) id * name.hashCode() * email.hashCode();
}
// getters and setters here
}

View File

@@ -0,0 +1,40 @@
package com.baeldung.hashcode.intellij;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;
User user = (User) o;
return id == user.id && (name.equals(user.name) && email.equals(user.email));
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + name.hashCode();
result = 31 * result + email.hashCode();
return result;
}
// getters and setters here
}

View File

@@ -0,0 +1,22 @@
package com.baeldung.hashcode.lombok;
import lombok.EqualsAndHashCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@EqualsAndHashCode
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
// getters and setters here
}

View File

@@ -0,0 +1,37 @@
package com.baeldung.hashcode.naive;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;
User user = (User) o;
return id == user.id && (name.equals(user.name) && email.equals(user.email));
}
@Override
public int hashCode() {
return 1;
}
// getters and setters here
}

View File

@@ -1,4 +1,4 @@
package com.baeldung.hashcode.entities;
package com.baeldung.hashcode.standard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -34,7 +34,6 @@ public class User {
hash = 31 * hash + (int) id;
hash = 31 * hash + (name == null ? 0 : name.hashCode());
hash = 31 * hash + (email == null ? 0 : email.hashCode());
logger.info("hashCode() method called - Computed hash: " + hash);
return hash;
}
// getters and setters here

View File

@@ -38,7 +38,7 @@ public class TeamUnitTest {
}
@Test
public void equalsContract() {
public void equalsHashCodeContracts() {
EqualsVerifier.forClass(Team.class).verify();
}