pom
This commit is contained in:
@@ -1,66 +1,64 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<modelVersion>4.0.0</modelVersion>
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<artifactId>core-java-16</artifactId>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<artifactId>core-java-16</artifactId>
|
<name>core-java-16</name>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<packaging>jar</packaging>
|
||||||
<name>core-java-16</name>
|
<url>http://maven.apache.org</url>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<url>http://maven.apache.org</url>
|
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<relativePath>../../</relativePath>
|
<relativePath>../../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${commons-lang3.version}</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>${maven-compiler-plugin.version}</version>
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<release>${maven.compiler.release}</release>
|
<release>${maven.compiler.release}</release>
|
||||||
<compilerArgs>--enable-preview</compilerArgs>
|
<compilerArgs>--enable-preview</compilerArgs>
|
||||||
<source>${maven.compiler.source.version}</source>
|
<source>${maven.compiler.source.version}</source>
|
||||||
<target>${maven.compiler.target.version}</target>
|
<target>${maven.compiler.target.version}</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>${surefire.plugin.version}</version>
|
<version>${surefire.plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<argLine>--enable-preview</argLine>
|
<argLine>--enable-preview</argLine>
|
||||||
<forkCount>1</forkCount>
|
<forkCount>1</forkCount>
|
||||||
</configuration>
|
</configuration>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.maven.surefire</groupId>
|
<groupId>org.apache.maven.surefire</groupId>
|
||||||
<artifactId>surefire-api</artifactId>
|
<artifactId>surefire-api</artifactId>
|
||||||
<version>${surefire.plugin.version}</version>
|
<version>${surefire.plugin.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source.version>16</maven.compiler.source.version>
|
<maven.compiler.source.version>16</maven.compiler.source.version>
|
||||||
<maven.compiler.target.version>16</maven.compiler.target.version>
|
<maven.compiler.target.version>16</maven.compiler.target.version>
|
||||||
<maven.compiler.release>16</maven.compiler.release>
|
<maven.compiler.release>16</maven.compiler.release>
|
||||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -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();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.instanceofalternative.model;
|
||||||
|
|
||||||
|
public class Dinosaur {
|
||||||
|
|
||||||
|
public String move() {
|
||||||
|
return "default movement";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.instanceofalternative.visitorspattern;
|
||||||
|
|
||||||
|
public interface Dino {
|
||||||
|
|
||||||
|
String move(Visitor dinoMove);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.instanceofalternative.visitorspattern;
|
||||||
|
|
||||||
|
public interface Visitor {
|
||||||
|
|
||||||
|
String visit(Anatotitan anatotitan);
|
||||||
|
|
||||||
|
String visit(Euraptor euraptor);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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 "";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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 "";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user