From abd9f8a38be0112dea4136a8ce414eddbb55ef84 Mon Sep 17 00:00:00 2001 From: Michael Olayemi Date: Tue, 13 Dec 2022 02:56:43 +0000 Subject: [PATCH] BAEL-5979 Instanceof Alternatives (#13082) * simple-thymeleaf-expression * expression-update * instanceof * instanceof * instanceof * update * Update pom.xml * Update pom.xml * instanceof * update * instanceofnew * instanceofnew * instanceofnew * instanceofnew * instanceofnew * instanceofnew * update * pom * Revert "pom" This reverts commit c5075e6ba7e1e91159864d57bcbc18dd90314e16. * Revert "Update pom.xml" This reverts commit 7dd02df1aea201572fb64d8bc4a2990a94b6b4c0. * Revert "Revert "Update pom.xml"" This reverts commit a02a1f67d16eb0bc781cc588af48f36fcc93d601. * Update pom.xml * Revert "Update pom.xml" This reverts commit 7dd02df1aea201572fb64d8bc4a2990a94b6b4c0. * Update pom.xml * JAVA-8436: Remove AssertJ depenendency from the child modules - part 4 * Update pom.xml * JAVA-8436: Remove AssertJ depenendency from the child modules - part 4 * BAEL-5979 Instanceof Alternatives --- core-java-modules/core-java-16/pom.xml | 2 +- .../enumallt/DinosaurEnum.java | 19 ++++++++++ .../model/Anatotitan.java | 15 ++++++++ .../model/Dinosaur.java | 9 +++++ .../model/Euraptor.java | 15 ++++++++ .../visitorspattern/Anatotitan.java | 14 ++++++++ .../visitorspattern/Dino.java | 7 ++++ .../visitorspattern/DinoVisitorImpl.java | 15 ++++++++ .../visitorspattern/Euraptor.java | 14 ++++++++ .../visitorspattern/Visitor.java | 9 +++++ .../EnumUnitTest.java | 26 ++++++++++++++ .../ExampleSetupUnitTest.java | 35 ++++++++++++++++++ .../GetClassUnitTest.java | 36 +++++++++++++++++++ .../PolymorphismUnitTest.java | 27 ++++++++++++++ .../VisitorsPatternUnitTest.java | 29 +++++++++++++++ .../core-java-lang-operators-2/pom.xml | 2 +- 16 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/enumallt/DinosaurEnum.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Anatotitan.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Dinosaur.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Euraptor.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Anatotitan.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Dino.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/DinoVisitorImpl.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Euraptor.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Visitor.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/EnumUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/ExampleSetupUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/GetClassUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/PolymorphismUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/VisitorsPatternUnitTest.java diff --git a/core-java-modules/core-java-16/pom.xml b/core-java-modules/core-java-16/pom.xml index 4adc3ee6d1..a2c0d4855b 100644 --- a/core-java-modules/core-java-16/pom.xml +++ b/core-java-modules/core-java-16/pom.xml @@ -63,4 +63,4 @@ 3.0.0-M5 - \ No newline at end of file + diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/enumallt/DinosaurEnum.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/enumallt/DinosaurEnum.java new file mode 100644 index 0000000000..a50f79947c --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/enumallt/DinosaurEnum.java @@ -0,0 +1,19 @@ +package com.baeldung.instanceofalternative.enumallt; + +public enum DinosaurEnum { + Anatotitan { + @Override + public String move() { + return "running"; + } + }, + Euraptor { + @Override + public String move() { + return "flying"; + } + }; + + public abstract String move(); + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Anatotitan.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Anatotitan.java new file mode 100644 index 0000000000..ce59b58ad7 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Anatotitan.java @@ -0,0 +1,15 @@ +package com.baeldung.instanceofalternative.model; + +public class Anatotitan extends Dinosaur { + // polymorphism + @Override + public String move() { + return "running"; + } + + // non-polymorphism + public String run() { + return "running"; + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Dinosaur.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Dinosaur.java new file mode 100644 index 0000000000..38055054f5 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Dinosaur.java @@ -0,0 +1,9 @@ +package com.baeldung.instanceofalternative.model; + +public class Dinosaur { + + public String move() { + return "default movement"; + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Euraptor.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Euraptor.java new file mode 100644 index 0000000000..1de5257a0d --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Euraptor.java @@ -0,0 +1,15 @@ +package com.baeldung.instanceofalternative.model; + +public class Euraptor extends Dinosaur { + // polymorphism + @Override + public String move() { + return "flying"; + } + + // non-polymorphism + public String flies() { + return "flying"; + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Anatotitan.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Anatotitan.java new file mode 100644 index 0000000000..84d93e7350 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Anatotitan.java @@ -0,0 +1,14 @@ +package com.baeldung.instanceofalternative.visitorspattern; + +public class Anatotitan implements Dino { + + String run() { + return "running"; + } + + @Override + public String move(Visitor dinobehave) { + return dinobehave.visit(this); + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Dino.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Dino.java new file mode 100644 index 0000000000..ef33baf2a4 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Dino.java @@ -0,0 +1,7 @@ +package com.baeldung.instanceofalternative.visitorspattern; + +public interface Dino { + + String move(Visitor dinoMove); + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/DinoVisitorImpl.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/DinoVisitorImpl.java new file mode 100644 index 0000000000..6fd71374fa --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/DinoVisitorImpl.java @@ -0,0 +1,15 @@ +package com.baeldung.instanceofalternative.visitorspattern; + +public class DinoVisitorImpl implements Visitor { + + @Override + public String visit(Anatotitan anatotitan) { + return anatotitan.run(); + } + + @Override + public String visit(Euraptor euraptor) { + return euraptor.flies(); + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Euraptor.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Euraptor.java new file mode 100644 index 0000000000..fdce1e6c0b --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Euraptor.java @@ -0,0 +1,14 @@ +package com.baeldung.instanceofalternative.visitorspattern; + +public class Euraptor implements Dino { + + String flies() { + return "flying"; + } + + @Override + public String move(Visitor dinobehave) { + return dinobehave.visit(this); + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Visitor.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Visitor.java new file mode 100644 index 0000000000..75fada3533 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Visitor.java @@ -0,0 +1,9 @@ +package com.baeldung.instanceofalternative.visitorspattern; + +public interface Visitor { + + String visit(Anatotitan anatotitan); + + String visit(Euraptor euraptor); + +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/EnumUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/EnumUnitTest.java new file mode 100644 index 0000000000..73faa2a1ef --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/EnumUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.instanceoftest; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.baeldung.instanceofalternative.enumallt.*; + +public class EnumUnitTest { + @Test + public void givenADinosaurSpecie_whenUsingEnum_thenGetMovementOfEuraptor() { + + assertEquals("flying", moveDinosaurUsingEnum(DinosaurEnum.Euraptor)); + } + + @Test + public void givenADinosaurSpecie_whenUsingEnum_thenGetMovementOfAnatotitan() { + assertEquals("running", moveDinosaurUsingEnum(DinosaurEnum.Anatotitan)); + } + + public static String moveDinosaurUsingEnum(DinosaurEnum dinosaurenum) { + return dinosaurenum.move(); + + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/ExampleSetupUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/ExampleSetupUnitTest.java new file mode 100644 index 0000000000..c42f77849b --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/ExampleSetupUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.instanceoftest; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.baeldung.instanceofalternative.model.*; + +public class ExampleSetupUnitTest { + + @Test + public void givenADinosaurSpecie_whenUsingInstancof_thenGetMovementOfAnatotitan() { + + assertEquals("running", moveDinosaurUsingInstanceof(new Anatotitan())); + } + + @Test + public void givenADinosaurSpecie_whenUsingInstanceof_thenGetMovementOfEuraptor() { + assertEquals("flying", moveDinosaurUsingInstanceof(new Euraptor())); + } + + public static String moveDinosaurUsingInstanceof(Dinosaur dinosaur) { + + if (dinosaur instanceof Anatotitan) { + + Anatotitan anatotitan = (Anatotitan) dinosaur; + return anatotitan.run(); + } else if (dinosaur instanceof Euraptor) { + Euraptor euraptor = (Euraptor) dinosaur; + return euraptor.flies(); + } + return ""; + } + +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/GetClassUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/GetClassUnitTest.java new file mode 100644 index 0000000000..6a4886c8a3 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/GetClassUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.instanceoftest; + +import static org.junit.Assert.*; +import org.junit.Test; + +import com.baeldung.instanceofalternative.model.*; + +public class GetClassUnitTest { + + @Test + public void givenADinosaurSpecie_whenUsingGetClass_thenGetMovementOfAnatotitan() { + + assertEquals("running", moveDinosaurUsingGetClass(new Anatotitan())); + } + + @Test + public void givenADinosaurSpecie_whenUsingGetClass_thenGetMovementOfEuraptor() { + assertEquals("flying", moveDinosaurUsingGetClass(new Euraptor())); + } + + public static String moveDinosaurUsingGetClass(Dinosaur dinosaur) { + + if (dinosaur.getClass() + .equals(Anatotitan.class)) { + + Anatotitan anatotitan = (Anatotitan) dinosaur; + return anatotitan.run(); + } else if (dinosaur.getClass() + .equals(Euraptor.class)) { + Euraptor euraptor = (Euraptor) dinosaur; + return euraptor.flies(); + } + return ""; + } + +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/PolymorphismUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/PolymorphismUnitTest.java new file mode 100644 index 0000000000..960ed34c82 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/PolymorphismUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.instanceoftest; + +import static org.junit.Assert.*; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baeldung.instanceofalternative.model.*; + +public class PolymorphismUnitTest { + + @Test + public void givenADinosaurSpecie_whenUsingPolymorphism_thenGetMovementOfAnatotitan() { + + assertEquals("running", moveDinosaurUsingPolymorphism(new Anatotitan())); + } + + @Test + public void givenADinosaurSpecie_whenUsingPolymorphism_thenGetMovementOfEuraptor() { + assertEquals("flying", moveDinosaurUsingPolymorphism(new Euraptor())); + } + + public static String moveDinosaurUsingPolymorphism(Dinosaur dinosaur) { + return dinosaur.move(); + } + +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/VisitorsPatternUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/VisitorsPatternUnitTest.java new file mode 100644 index 0000000000..287f7df798 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/VisitorsPatternUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.instanceoftest; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.baeldung.instanceofalternative.visitorspattern.*; + +public class VisitorsPatternUnitTest { + + @Test + public void givenADinosaurSpecie_whenUsingVisitorPattern_thenGetMovementOfAnatotitan() { + + assertEquals("running", moveDinosaurUsingVisitorPattern((Dino) new Anatotitan())); + } + + @Test + public void givenADinosaurSpecie_whenUsingVisitorPattern_thenGetMovementOfEuraptor() { + + assertEquals("flying", moveDinosaurUsingVisitorPattern((Dino) new Euraptor())); + } + + public static String moveDinosaurUsingVisitorPattern(Dino dinosaur) { + Visitor visitor = new DinoVisitorImpl(); + + return dinosaur.move(visitor); + } + +} diff --git a/core-java-modules/core-java-lang-operators-2/pom.xml b/core-java-modules/core-java-lang-operators-2/pom.xml index c36250f1ae..9d925c553a 100644 --- a/core-java-modules/core-java-lang-operators-2/pom.xml +++ b/core-java-modules/core-java-lang-operators-2/pom.xml @@ -38,4 +38,4 @@ - \ No newline at end of file +