Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -63,4 +63,4 @@
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
+19
@@ -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();
|
||||
|
||||
}
|
||||
+15
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.instanceofalternative.model;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
public String move() {
|
||||
return "default movement";
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.instanceofalternative.visitorspattern;
|
||||
|
||||
public interface Dino {
|
||||
|
||||
String move(Visitor dinoMove);
|
||||
|
||||
}
|
||||
+15
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.instanceofalternative.visitorspattern;
|
||||
|
||||
public interface Visitor {
|
||||
|
||||
String visit(Anatotitan anatotitan);
|
||||
|
||||
String visit(Euraptor euraptor);
|
||||
|
||||
}
|
||||
+26
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -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 "";
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -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 "";
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
+29
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+2
-7
@@ -1,14 +1,9 @@
|
||||
package com.baeldung.exception.exceptions_vs_errors;
|
||||
|
||||
public class ErrorExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
overflow();
|
||||
}
|
||||
|
||||
public static void overflow() {
|
||||
System.out.println("overflow...");
|
||||
overflow();
|
||||
public static void main(String[] args) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -3,11 +3,11 @@ package com.baeldung.exception.exceptions_vs_errors;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
// Unit test for the ErrorExample class.
|
||||
public class ErrorExampleUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenMainMethodIsRun_thenStackOverflowError() {
|
||||
Assertions.assertThrows(StackOverflowError.class,
|
||||
() -> ErrorExample.main(null));
|
||||
Assertions.assertThrows(AssertionError.class,
|
||||
() -> ErrorExample.main(null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,5 +14,4 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
||||
- [An Introduction to the Constant Pool in the JVM](https://www.baeldung.com/jvm-constant-pool)
|
||||
- [List All the Classes Loaded in the JVM](https://www.baeldung.com/jvm-list-all-classes-loaded)
|
||||
- [Static Fields and Garbage Collection](https://www.baeldung.com/java-static-fields-gc)
|
||||
- [Difference Between Class.getResource() and ClassLoader.getResource()](https://www.baeldung.com/java-class-vs-classloader-getresource)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm) [[next -->]](/core-java-modules/core-java-jvm-3)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
## Core Java JVM Cookbooks and Examples
|
||||
|
||||
This module contains articles about working with the Java Virtual Machine (JVM).
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Difference Between Class.getResource() and ClassLoader.getResource()](https://www.baeldung.com/java-class-vs-classloader-getresource)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm-2)
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-jvm-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-jvm-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.anonymousclass;
|
||||
|
||||
public class EmailSenderService implements SenderService {
|
||||
|
||||
@Override
|
||||
public String callSender(Sender sender) {
|
||||
return sender.send("Email Notification");
|
||||
}
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.anonymousclass;
|
||||
|
||||
public interface Sender {
|
||||
|
||||
String send(final String message);
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.anonymousclass;
|
||||
|
||||
public interface SenderService {
|
||||
|
||||
String callSender(Sender sender);
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.anonymousclass;
|
||||
|
||||
public class SmsSenderService implements SenderService {
|
||||
|
||||
@Override
|
||||
public String callSender(Sender sender) {
|
||||
return sender.send("SMS Notification");
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.anonymousclass;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AnonymousClassToLambdaIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenPassingAnonymousClass_thenSuccess() {
|
||||
final SenderService emailSenderService = new EmailSenderService();
|
||||
|
||||
final String emailNotif = emailSenderService.callSender(new Sender() {
|
||||
@Override
|
||||
public String send(String message) {
|
||||
return message;
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(emailNotif, "Email Notification");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassingLambdaExpression_thenSuccess() {
|
||||
final SenderService smsSenderService = new SmsSenderService();
|
||||
|
||||
final String smsNotif = smsSenderService.callSender((String message) -> {
|
||||
return message;
|
||||
});
|
||||
|
||||
assertEquals(smsNotif, "SMS Notification");
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user