From aa7daf7901d2951fc05fac9e5c352c1fe87dfc3a Mon Sep 17 00:00:00 2001 From: dupirefr Date: Tue, 28 Apr 2020 08:05:50 +0200 Subject: [PATCH] [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 --- .../com/baeldung/typeerasure/Example.java | 13 ++++++ .../abstractclasses/overview/BoardGame.java | 9 ++++ .../abstractclasses/overview/Checkers.java | 8 ++++ .../com/baeldung/interfaces/Computer.java | 9 ++++ .../com/baeldung/interfaces/Electronic.java | 22 ++++++++++ .../core-java-lang-oop-methods/pom.xml | 14 +++++++ .../baeldung/hashcode/apachecommons/User.java | 42 +++++++++++++++++++ .../com/baeldung/hashcode/eclipse/User.java | 42 +++++++++++++++++++ .../com/baeldung/hashcode/improved/User.java | 37 ++++++++++++++++ .../com/baeldung/hashcode/intellij/User.java | 40 ++++++++++++++++++ .../com/baeldung/hashcode/lombok/User.java | 22 ++++++++++ .../com/baeldung/hashcode/naive/User.java | 37 ++++++++++++++++ .../hashcode/{entities => standard}/User.java | 3 +- .../baeldung/equalshashcode/TeamUnitTest.java | 2 +- ...{StaticBlock.java => StaticBlockDemo.java} | 4 +- ...Test.java => StaticBlockDemoUnitTest.java} | 4 +- .../model/ComputerBuilder.java | 13 ++++++ .../model/StandardComputerBuilder.java | 13 ++++++ ...mousInner.java => AnonymousInnerTest.java} | 2 +- 19 files changed, 328 insertions(+), 8 deletions(-) create mode 100644 core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/Example.java create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/BoardGame.java create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/Checkers.java create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Computer.java create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Electronic.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/apachecommons/User.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/eclipse/User.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/improved/User.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/intellij/User.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/lombok/User.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/naive/User.java rename core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/{entities => standard}/User.java (89%) rename core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/{StaticBlock.java => StaticBlockDemo.java} (87%) rename core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/{StaticBlockUnitTest.java => StaticBlockDemoUnitTest.java} (80%) create mode 100644 core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/ComputerBuilder.java create mode 100644 core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardComputerBuilder.java rename core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/{AnonymousInner.java => AnonymousInnerTest.java} (92%) diff --git a/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/Example.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/Example.java new file mode 100644 index 0000000000..f816fd6d16 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/Example.java @@ -0,0 +1,13 @@ +package com.baeldung.typeerasure; + +public class Example { + + public static boolean containsElement(E [] elements, E element){ + for (E e : elements){ + if(e.equals(element)){ + return true; + } + } + return false; + } +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/BoardGame.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/BoardGame.java new file mode 100644 index 0000000000..e0395cec7e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/BoardGame.java @@ -0,0 +1,9 @@ +package com.baeldung.abstractclasses.overview; + +public abstract class BoardGame { + //... field declarations, constructors + + public abstract void play(); + + //... concrete methods +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/Checkers.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/Checkers.java new file mode 100644 index 0000000000..5c911649c8 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/Checkers.java @@ -0,0 +1,8 @@ +package com.baeldung.abstractclasses.overview; + +public class Checkers extends BoardGame { + @Override + public void play() { + //... implementation + } +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Computer.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Computer.java new file mode 100644 index 0000000000..94d39e2448 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Computer.java @@ -0,0 +1,9 @@ +package com.baeldung.interfaces; + +public class Computer implements Electronic { + + @Override + public int getElectricityUse() { + return 1000; + } +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Electronic.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Electronic.java new file mode 100644 index 0000000000..fcaf67dc3a --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Electronic.java @@ -0,0 +1,22 @@ +package com.baeldung.interfaces; + +public interface Electronic { + // Constant variable + String LED = "LED"; + + // Abstract method + int getElectricityUse(); + + // Static method + static boolean isEnergyEfficient(String electtronicType) { + if (electtronicType.equals(LED)) { + return true; + } + return false; + } + + //Default method + default void printDescription() { + System.out.println("Electronic Description"); + } +} diff --git a/core-java-modules/core-java-lang-oop-methods/pom.xml b/core-java-modules/core-java-lang-oop-methods/pom.xml index 77e8ded8e7..3590b85454 100644 --- a/core-java-modules/core-java-lang-oop-methods/pom.xml +++ b/core-java-modules/core-java-lang-oop-methods/pom.xml @@ -14,6 +14,17 @@ jar + + org.projectlombok + lombok + ${lombok.version} + + + commons-lang + commons-lang + ${commons-lang.version} + + org.assertj assertj-core @@ -29,6 +40,9 @@ + 1.18.12 + 2.6 + 3.10.0 3.0.3 diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/apachecommons/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/apachecommons/User.java new file mode 100644 index 0000000000..4be082019a --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/apachecommons/User.java @@ -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 + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/eclipse/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/eclipse/User.java new file mode 100644 index 0000000000..e852eef96e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/eclipse/User.java @@ -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 + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/improved/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/improved/User.java new file mode 100644 index 0000000000..773a2c7e45 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/improved/User.java @@ -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 + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/intellij/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/intellij/User.java new file mode 100644 index 0000000000..f7bf1ab735 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/intellij/User.java @@ -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 + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/lombok/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/lombok/User.java new file mode 100644 index 0000000000..e34246ce24 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/lombok/User.java @@ -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 + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/naive/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/naive/User.java new file mode 100644 index 0000000000..b0a33ad3e9 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/naive/User.java @@ -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 + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/entities/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/standard/User.java similarity index 89% rename from core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/entities/User.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/standard/User.java index 524f176e6b..f8fd5cedbd 100644 --- a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/entities/User.java +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/standard/User.java @@ -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 diff --git a/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java index 7dfc6d47a3..308004452b 100644 --- a/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java @@ -38,7 +38,7 @@ public class TeamUnitTest { } @Test - public void equalsContract() { + public void equalsHashCodeContracts() { EqualsVerifier.forClass(Team.class).verify(); } diff --git a/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/StaticBlock.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/StaticBlockDemo.java similarity index 87% rename from core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/StaticBlock.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/StaticBlockDemo.java index f3eb447bdd..637bb5e82a 100644 --- a/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/StaticBlock.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/StaticBlockDemo.java @@ -3,7 +3,7 @@ package com.baeldung.staticmodifier; import java.util.LinkedList; import java.util.List; -public class StaticBlock { +public class StaticBlockDemo { private static List ranks = new LinkedList<>(); static { @@ -23,6 +23,6 @@ public class StaticBlock { } public static void setRanks(List ranks) { - StaticBlock.ranks = ranks; + StaticBlockDemo.ranks = ranks; } } diff --git a/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/StaticBlockUnitTest.java b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/StaticBlockDemoUnitTest.java similarity index 80% rename from core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/StaticBlockUnitTest.java rename to core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/StaticBlockDemoUnitTest.java index 03f3e0440c..f31a8e8895 100644 --- a/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/StaticBlockUnitTest.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/StaticBlockDemoUnitTest.java @@ -7,11 +7,11 @@ import java.util.List; import org.junit.Test; -public class StaticBlockUnitTest { +public class StaticBlockDemoUnitTest { @Test public void whenAddedListElementsThroughStaticBlock_thenEnsureCorrectOrder() { - List actualList = StaticBlock.getRanks(); + List actualList = StaticBlockDemo.getRanks(); assertThat(actualList, contains("Lieutenant", "Captain", "Major", "Colonel", "General")); } } diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/ComputerBuilder.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/ComputerBuilder.java new file mode 100644 index 0000000000..4d8574e93f --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/ComputerBuilder.java @@ -0,0 +1,13 @@ +package com.baeldung.inheritancecomposition.model; + +public abstract class ComputerBuilder { + + public final void buildComputer() { + addProcessor(); + addMemory(); + } + + public abstract void addProcessor(); + + public abstract void addMemory(); +} diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardComputerBuilder.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardComputerBuilder.java new file mode 100644 index 0000000000..76c4732ace --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardComputerBuilder.java @@ -0,0 +1,13 @@ +package com.baeldung.inheritancecomposition.model; + +public class StandardComputerBuilder extends ComputerBuilder { + @Override + public void addProcessor() { + // method implementation + } + + @Override + public void addMemory() { + // method implementation + } +} diff --git a/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/AnonymousInner.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/AnonymousInnerTest.java similarity index 92% rename from core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/AnonymousInner.java rename to core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/AnonymousInnerTest.java index 9fa8ee9cd5..4e49bb5b41 100644 --- a/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/AnonymousInner.java +++ b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/AnonymousInnerTest.java @@ -6,7 +6,7 @@ abstract class SimpleAbstractClass { abstract void run(); } -public class AnonymousInner { +public class AnonymousInnerTest { @Test public void run() {