From 6a6ac00db2a71d329e10afd8f09059884cc3ede8 Mon Sep 17 00:00:00 2001 From: Alex Tighe <599747+atighe@users.noreply.github.com> Date: Mon, 7 Aug 2023 13:38:05 -0400 Subject: [PATCH] BAEL-6458: Guide to the yield Keyword in Java (#14440) * BAEL-6229: committing new example for loading multiple yaml configuration files * BAEL-6229: updating README.md * BAEL-6229: adding example one code, commented out * Revert "BAEL-6229: updating README.md" This reverts commit 51cd2dcf97f797aa6a723888fd246ef0142242a3. * BAEL-6229: adding comments around commented out code for first example. * BAEL-6458: creating new module for conditionals and adding examples for yield * BAEL-6458: creating new module for conditionals and adding examples for yield * BAEL-6458: rename methods * BAEL-6458: clean up pom * BAEL-6458: convert checks to tests * BAEL-6458: convert checks to tests * BAEL-6458: convert checks to tests * BAEL-6458: move examples to just YieldTest and removed YieldExamples class. Also added assigning the value of the switch expression to a variable instead of returning from method. Also added a non-default exhaustive test. * BAEL-6458: Test clean up based on initial PR feedback. --- .../core-java-conditionals/README.md | 5 ++ .../core-java-conditionals/pom.xml | 58 +++++++++++++ .../com/baeldung/conditionals/YieldTest.java | 83 +++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 core-java-modules/core-java-conditionals/README.md create mode 100644 core-java-modules/core-java-conditionals/pom.xml create mode 100644 core-java-modules/core-java-conditionals/src/test/java/com/baeldung/conditionals/YieldTest.java diff --git a/core-java-modules/core-java-conditionals/README.md b/core-java-modules/core-java-conditionals/README.md new file mode 100644 index 0000000000..ae5694c3ba --- /dev/null +++ b/core-java-modules/core-java-conditionals/README.md @@ -0,0 +1,5 @@ +## Core Java Conditionals + +This module contains articles about Java Conditionals. + +### Relevant articles: diff --git a/core-java-modules/core-java-conditionals/pom.xml b/core-java-modules/core-java-conditionals/pom.xml new file mode 100644 index 0000000000..2a1290c98e --- /dev/null +++ b/core-java-modules/core-java-conditionals/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + core-java-conditionals + 0.1.0-SNAPSHOT + core-java-compiler + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.projectlombok + lombok + 1.18.24 + provided + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + --enable-preview + ${maven.compiler.source.version} + ${maven.compiler.target.version} + 14 + --enable-preview + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.plugin.version} + + --enable-preview + + + + + + + 14 + 3.8.1 + 14 + 3.0.0-M3 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-conditionals/src/test/java/com/baeldung/conditionals/YieldTest.java b/core-java-modules/core-java-conditionals/src/test/java/com/baeldung/conditionals/YieldTest.java new file mode 100644 index 0000000000..4ea9126cde --- /dev/null +++ b/core-java-modules/core-java-conditionals/src/test/java/com/baeldung/conditionals/YieldTest.java @@ -0,0 +1,83 @@ +package com.baeldung.conditionals; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +class YieldTest { + + enum Number { + ONE, TWO, THREE, FOUR + } + + @Test + void whenSwitchingOnNumberOne_thenWillReturnString() { + Number number = Number.ONE; + String message; + switch (number) { + case ONE: + message = "Got a 1"; + break; + case TWO: + message = "Got a 2"; + break; + default: + message = "More than 2"; + } + + assertEquals("Got a 1", message); + } + + @Test + void whenSwitchingWithArrowOnNumberTwo_thenWillReturnString() { + Number number = Number.TWO; + String message = switch (number) { + case ONE -> { + yield "Got a 1"; + } + case TWO -> { + yield "Got a 2"; + } + default -> { + yield "More than 2"; + } + }; + + assertEquals("Got a 2", message); + } + + @Test + void whenSwitchingWithArrowNoDefaultOnNumberTwo_thenWillReturnString() { + Number number = Number.TWO; + String message = switch (number) { + case ONE -> { + yield "Got a 1"; + } + case TWO -> { + yield "Got a 2"; + } + case THREE, FOUR -> { + yield "More than 2"; + } + }; + + assertEquals("Got a 2", message); + } + + @Test + void whenSwitchingWithColonOnNumberTwo_thenWillReturnString() { + Number number = Number.TWO; + String message = switch (number) { + case ONE: + yield "Got a 1"; + case TWO: + yield "Got a 2"; + default: + yield "More than 2"; + }; + + assertEquals("Got a 2", message); + } + +}