diff --git a/core-java-modules/core-java-lang-4/pom.xml b/core-java-modules/core-java-lang-4/pom.xml index 4e692ffa55..46ad335568 100644 --- a/core-java-modules/core-java-lang-4/pom.xml +++ b/core-java-modules/core-java-lang-4/pom.xml @@ -21,6 +21,16 @@ jmh-core ${jmh-core.version} + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + org.openjdk.jmh jmh-generator-annprocess diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/Article.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/Article.java new file mode 100644 index 0000000000..2eca40f055 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/Article.java @@ -0,0 +1,21 @@ +package com.baeldung.tieredcompilation; + +public class Article { + + private String name; + private String author; + + public Article(String name, String author) { + this.name = name; + this.author = author; + } + + public String getName() { + return name; + } + + public String getAuthor() { + return author; + } + +} diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/Formatter.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/Formatter.java new file mode 100644 index 0000000000..ee5d74d5fe --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/Formatter.java @@ -0,0 +1,7 @@ +package com.baeldung.tieredcompilation; + +public interface Formatter { + + String format(T object) throws Exception; + +} diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/JsonFormatter.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/JsonFormatter.java new file mode 100644 index 0000000000..9951b477da --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/JsonFormatter.java @@ -0,0 +1,15 @@ +package com.baeldung.tieredcompilation; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.json.JsonMapper; + +public class JsonFormatter implements Formatter { + + private static final JsonMapper mapper = new JsonMapper(); + + @Override + public String format(T object) throws JsonProcessingException { + return mapper.writeValueAsString(object); + } + +} diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/TieredCompilation.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/TieredCompilation.java new file mode 100644 index 0000000000..ea7178051e --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/TieredCompilation.java @@ -0,0 +1,17 @@ +package com.baeldung.tieredcompilation; + +public class TieredCompilation { + + public static void main(String[] args) throws Exception { + for (int i = 0; i < 1_000_000; i++) { + Formatter formatter; + if (i < 500_000) { + formatter = new JsonFormatter(); + } else { + formatter = new XmlFormatter(); + } + formatter.format(new Article("Tiered Compilation in JVM", "Baeldung")); + } + } + +} diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/XmlFormatter.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/XmlFormatter.java new file mode 100644 index 0000000000..8b9823cd88 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/tieredcompilation/XmlFormatter.java @@ -0,0 +1,15 @@ +package com.baeldung.tieredcompilation; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; + +public class XmlFormatter implements Formatter { + + private static final XmlMapper mapper = new XmlMapper(); + + @Override + public String format(T object) throws JsonProcessingException { + return mapper.writeValueAsString(object); + } + +}