From ae827f312222cc4e5ba97cd13fe2eecfe421e859 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 5 Aug 2017 22:07:50 +0200 Subject: [PATCH] Refactor hashcode() samples (#2377) --- core-java/hashcode/pom.xml | 39 ------------------- .../com/baeldung/application/Application.java | 23 ----------- .../main/java/com/baeldung/entities/User.java | 38 ------------------ .../baeldung/application/ApplicationTest.java | 30 -------------- .../java/com/baeldung/entities/UserTest.java | 34 ---------------- .../hashcode/application/Application.java | 24 ------------ .../hashcode/application/ApplicationTest.java | 30 +++++++------- 7 files changed, 15 insertions(+), 203 deletions(-) delete mode 100644 core-java/hashcode/pom.xml delete mode 100644 core-java/hashcode/src/main/java/com/baeldung/application/Application.java delete mode 100644 core-java/hashcode/src/main/java/com/baeldung/entities/User.java delete mode 100644 core-java/hashcode/src/test/java/com/baeldung/application/ApplicationTest.java delete mode 100644 core-java/hashcode/src/test/java/com/baeldung/entities/UserTest.java delete mode 100644 core-java/src/main/java/com/baeldung/hashcode/application/Application.java diff --git a/core-java/hashcode/pom.xml b/core-java/hashcode/pom.xml deleted file mode 100644 index 393aa69153..0000000000 --- a/core-java/hashcode/pom.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - 4.0.0 - com.baeldung.hashcode - hashcode - 1.0 - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - - - - junit - junit - 4.12 - test - - - org.slf4j - slf4j-api - 1.7.25 - - - org.slf4j - slf4j-simple - 1.7.25 - - - \ No newline at end of file diff --git a/core-java/hashcode/src/main/java/com/baeldung/application/Application.java b/core-java/hashcode/src/main/java/com/baeldung/application/Application.java deleted file mode 100644 index 08c670c82f..0000000000 --- a/core-java/hashcode/src/main/java/com/baeldung/application/Application.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.application; - -import com.baeldung.entities.User; -import java.util.HashMap; -import java.util.Map; - -public class Application { - - public static void main(String[] args) { - Map users = new HashMap<>(); - User user1 = new User(1L, "John", "john@domain.com"); - User user2 = new User(2L, "Jennifer", "jennifer@domain.com"); - User user3 = new User(3L, "Mary", "mary@domain.com"); - - users.put(user1, user1); - users.put(user2, user2); - users.put(user3, user3); - - if (users.containsKey(user1)) { - System.out.print("User found in the collection"); - } - } -} diff --git a/core-java/hashcode/src/main/java/com/baeldung/entities/User.java b/core-java/hashcode/src/main/java/com/baeldung/entities/User.java deleted file mode 100644 index a976233562..0000000000 --- a/core-java/hashcode/src/main/java/com/baeldung/entities/User.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.entities; - -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 hash = 7; - 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 -} diff --git a/core-java/hashcode/src/test/java/com/baeldung/application/ApplicationTest.java b/core-java/hashcode/src/test/java/com/baeldung/application/ApplicationTest.java deleted file mode 100644 index dcd853f451..0000000000 --- a/core-java/hashcode/src/test/java/com/baeldung/application/ApplicationTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.application; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import static org.junit.Assert.assertEquals; - -public class ApplicationTest { - - private ByteArrayOutputStream outContent; - - @Before - public void setUpPrintStreamInstance() throws Exception { - this.outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); - } - - @After - public void tearDownByteArrayOutputStream() throws Exception { - outContent = null; - } - - @Test - public void main_NoInputState_TextPrintedToConsole() throws Exception { - Application.main(new String[]{}); - assertEquals("User found in the collection", outContent.toString()); - } -} \ No newline at end of file diff --git a/core-java/hashcode/src/test/java/com/baeldung/entities/UserTest.java b/core-java/hashcode/src/test/java/com/baeldung/entities/UserTest.java deleted file mode 100644 index 01f6085d7e..0000000000 --- a/core-java/hashcode/src/test/java/com/baeldung/entities/UserTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.entities; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -public class UserTest { - - private User user; - private User comparisonUser; - - @Before - public void setUpUserInstances() { - this.user = new User(1L, "test", "test@domain.com"); - this.comparisonUser = this.user; - } - - @After - public void tearDownUserInstances() { - user = null; - comparisonUser = null; - } - - @Test - public void equals_EqualUserInstance_TrueAssertion(){ - Assert.assertTrue(user.equals(comparisonUser)); - } - - @Test - public void hashCode_UserHash_TrueAssertion() { - Assert.assertEquals(1792276941, user.hashCode()); - } -} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/hashcode/application/Application.java b/core-java/src/main/java/com/baeldung/hashcode/application/Application.java deleted file mode 100644 index 8e6125045e..0000000000 --- a/core-java/src/main/java/com/baeldung/hashcode/application/Application.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.hashcode.application; - -import java.util.HashMap; -import java.util.Map; - -import com.baeldung.hashcode.entities.User; - -public class Application { - - public static void main(String[] args) { - Map users = new HashMap<>(); - User user1 = new User(1L, "John", "john@domain.com"); - User user2 = new User(2L, "Jennifer", "jennifer@domain.com"); - User user3 = new User(3L, "Mary", "mary@domain.com"); - - users.put(user1, user1); - users.put(user2, user2); - users.put(user3, user3); - - if (users.containsKey(user1)) { - System.out.print("User found in the collection"); - } - } -} diff --git a/core-java/src/test/java/com/baeldung/hashcode/application/ApplicationTest.java b/core-java/src/test/java/com/baeldung/hashcode/application/ApplicationTest.java index bf7a24347d..04e32e5fa0 100644 --- a/core-java/src/test/java/com/baeldung/hashcode/application/ApplicationTest.java +++ b/core-java/src/test/java/com/baeldung/hashcode/application/ApplicationTest.java @@ -1,30 +1,30 @@ package com.baeldung.hashcode.application; +import com.baeldung.hashcode.entities.User; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.HashMap; +import java.util.Map; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class ApplicationTest { - private ByteArrayOutputStream outContent; - - @Before - public void setUpPrintStreamInstance() throws Exception { - this.outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); - } - - @After - public void tearDownByteArrayOutputStream() throws Exception { - outContent = null; - } - @Test public void main_NoInputState_TextPrintedToConsole() throws Exception { - Application.main(new String[] {}); - assertEquals("User found in the collection", outContent.toString()); + Map users = new HashMap<>(); + User user1 = new User(1L, "John", "john@domain.com"); + User user2 = new User(2L, "Jennifer", "jennifer@domain.com"); + User user3 = new User(3L, "Mary", "mary@domain.com"); + + users.put(user1, user1); + users.put(user2, user2); + users.put(user3, user3); + + assertTrue(users.containsKey(user1)); } } \ No newline at end of file