diff --git a/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/PatternCaseLabels.java b/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/PatternCaseLabels.java new file mode 100644 index 0000000000..7b64a382bc --- /dev/null +++ b/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/PatternCaseLabels.java @@ -0,0 +1,32 @@ +package com.baeldung.java21; + +public class PatternCaseLabels { + + static String processInputOld(String input) { + String output; + switch (input) { + case null -> output = "Oops, null"; + case String s -> { + if ("Yes".equalsIgnoreCase(s)) { + output = "It's Yes"; + } else if ("No".equalsIgnoreCase(s)) { + output = "It's No"; + } else { + output = "Try Again"; + } + } + } + return output; + } + + static String processInputNew(String input) { + String output; + switch (input) { + case null -> output = "Oops, null"; + case String s when "Yes".equalsIgnoreCase(s) -> output = "It's Yes"; + case String s when "No".equalsIgnoreCase(s) -> output = "It's No"; + case String s -> output = "Try Again"; + } + return output; + } +} diff --git a/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/RecordPattern.java b/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/RecordPattern.java new file mode 100644 index 0000000000..e06539f888 --- /dev/null +++ b/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/RecordPattern.java @@ -0,0 +1,36 @@ +package com.baeldung.java21; + +public class RecordPattern { + + record Point(int x, int y) {} + + public static int beforeRecordPattern(Object obj) { + int sum = 0; + if(obj instanceof Point p) { + int x = p.x(); + int y = p.y(); + sum = x+y; + } + return sum; + } + + public static int afterRecordPattern(Object obj) { + if(obj instanceof Point(int x, int y)) { + return x+y; + } + return 0; + } + + enum Color {RED, GREEN, BLUE} + + record ColoredPoint(Point point, Color color) {} + + record RandomPoint(ColoredPoint cp) {} + + public static Color getRamdomPointColor(RandomPoint r) { + if(r instanceof RandomPoint(ColoredPoint cp)) { + return cp.color(); + } + return null; + } +} diff --git a/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/StringTemplates.java b/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/StringTemplates.java new file mode 100644 index 0000000000..19dffd001b --- /dev/null +++ b/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/StringTemplates.java @@ -0,0 +1,9 @@ +package com.baeldung.java21; + +public class StringTemplates { + + public String getStringTemplate() { + String name = "Baeldung"; + return STR."Welcome to \{name}"; + } +} diff --git a/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/SwitchPattern.java b/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/SwitchPattern.java new file mode 100644 index 0000000000..9ef7527415 --- /dev/null +++ b/core-java-modules/core-java-21/src/main/java/com/baeldung/java21/SwitchPattern.java @@ -0,0 +1,56 @@ +package com.baeldung.java21; + +public class SwitchPattern { + + static class Account{ + double getBalance() { + return 0; + } + } + + static class SavingsAccount extends Account { + @Override + double getBalance() { + return 100; + } + } + + static class TermAccount extends Account { + @Override + double getBalance() { + return 1000; + } + } + static class CurrentAccount extends Account { + @Override + double getBalance() { + return 10000; + } + } + + static double getBalanceWithOutSwitchPattern(Account account) { + double balance = 0; + if(account instanceof SavingsAccount sa) { + balance = sa.getBalance(); + } + else if(account instanceof TermAccount ta) { + balance = ta.getBalance(); + } + else if(account instanceof CurrentAccount ca) { + balance = ca.getBalance(); + } + return balance; + } + + static double getBalanceWithSwitchPattern(Account account) { + double result; + switch (account) { + case null -> throw new IllegalArgumentException("Oops, account is null"); + case SavingsAccount sa -> result = sa.getBalance(); + case TermAccount ta -> result = ta.getBalance(); + case CurrentAccount ca -> result = ca.getBalance(); + default -> result = account.getBalance(); + } + return result; + } +} diff --git a/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/PatternCaseLabelsUnitTest.java b/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/PatternCaseLabelsUnitTest.java new file mode 100644 index 0000000000..07b11ff50c --- /dev/null +++ b/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/PatternCaseLabelsUnitTest.java @@ -0,0 +1,48 @@ +package com.baeldung.java21; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +class PatternCaseLabelsUnitTest { + + @Test + void whenProcessInputOldWayWithYes_thenReturnOutput() { + assertEquals("It's Yes", PatternCaseLabels.processInputOld("Yes")); + } + + @Test + void whenProcessInputOldWayWithNo_thenReturnOutput() { + assertEquals("It's No", PatternCaseLabels.processInputOld("No")); + } + + @Test + void whenProcessInputOldWayWithNull_thenReturnOutput() { + assertEquals("Oops, null", PatternCaseLabels.processInputOld(null)); + } + + @Test + void whenProcessInputOldWayWithInvalidOption_thenReturnOutput() { + assertEquals("Try Again", PatternCaseLabels.processInputOld("Invalid Option")); + } + + @Test + void whenProcessInputNewWayWithYes_thenReturnOutput() { + assertEquals("It's Yes", PatternCaseLabels.processInputNew("Yes")); + } + + @Test + void whenProcessInputNewWayWithNo_thenReturnOutput() { + assertEquals("It's No", PatternCaseLabels.processInputNew("No")); + } + + @Test + void whenProcessInputNewWayWithNull_thenReturnOutput() { + assertEquals("Oops, null", PatternCaseLabels.processInputNew(null)); + } + + @Test + void whenProcessInputNewWayWithInvalidOption_thenReturnOutput() { + assertEquals("Try Again", PatternCaseLabels.processInputNew("Invalid Option")); + } + +} diff --git a/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/RecordPatternUnitTest.java b/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/RecordPatternUnitTest.java new file mode 100644 index 0000000000..da8f4395e0 --- /dev/null +++ b/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/RecordPatternUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.java21; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +import com.baeldung.java21.RecordPattern.Color; +import com.baeldung.java21.RecordPattern.ColoredPoint; +import com.baeldung.java21.RecordPattern.Point; +import com.baeldung.java21.RecordPattern.RandomPoint; + +class RecordPatternUnitTest { + + @Test + void whenNoRecordPattern_thenReturnOutput() { + assertEquals(5, RecordPattern.beforeRecordPattern(new Point(2, 3))); + } + + @Test + void whenRecordPattern_thenReturnOutput() { + assertEquals(5, RecordPattern.afterRecordPattern(new Point(2, 3))); + } + + @Test + void whenRecordPattern_thenReturnColorOutput() { + ColoredPoint coloredPoint = new ColoredPoint(new Point(2, 3), Color.GREEN); + RandomPoint randomPoint = new RandomPoint(coloredPoint); + assertEquals(Color.GREEN, RecordPattern.getRamdomPointColor(randomPoint)); + } +} diff --git a/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/StringTemplateUnitTest.java b/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/StringTemplateUnitTest.java new file mode 100644 index 0000000000..340e9b54d9 --- /dev/null +++ b/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/StringTemplateUnitTest.java @@ -0,0 +1,13 @@ +package com.baeldung.java21; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +class StringTemplateUnitTest { + + @Test + void whenNoSwitchPattern_thenReturnSavingsAccountBalance() { + StringTemplates stringTemplates = new StringTemplates(); + assertEquals("Welcome to Baeldung", stringTemplates.getStringTemplate()); + } +} diff --git a/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/SwitchPatternUnitTest.java b/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/SwitchPatternUnitTest.java new file mode 100644 index 0000000000..7429b303e9 --- /dev/null +++ b/core-java-modules/core-java-21/src/test/java/com/baeldung/java21/SwitchPatternUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.java21; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +class SwitchPatternUnitTest { + + @Test + void whenNoSwitchPattern_thenReturnSavingsAccountBalance() { + SwitchPattern.SavingsAccount savingsAccount = new SwitchPattern.SavingsAccount(); + assertEquals(100, SwitchPattern.getBalanceWithOutSwitchPattern(savingsAccount), 0); + } + + @Test + void whenSwitchPattern_thenReturnSavingsAccountBalance() { + SwitchPattern.SavingsAccount savingsAccount = new SwitchPattern.SavingsAccount(); + assertEquals(100, SwitchPattern.getBalanceWithSwitchPattern(savingsAccount), 0); + } +}