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.
This commit is contained in:
parent
966d488013
commit
6a6ac00db2
|
@ -0,0 +1,5 @@
|
||||||
|
## Core Java Conditionals
|
||||||
|
|
||||||
|
This module contains articles about Java Conditionals.
|
||||||
|
|
||||||
|
### Relevant articles:
|
|
@ -0,0 +1,58 @@
|
||||||
|
<?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-conditionals</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-compiler</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.24</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<compilerArgs>--enable-preview</compilerArgs>
|
||||||
|
<source>${maven.compiler.source.version}</source>
|
||||||
|
<target>${maven.compiler.target.version}</target>
|
||||||
|
<release>14</release>
|
||||||
|
<compilerArgs>--enable-preview</compilerArgs>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${surefire.plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<argLine>--enable-preview</argLine>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source.version>14</maven.compiler.source.version>
|
||||||
|
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||||
|
<maven.compiler.target.version>14</maven.compiler.target.version>
|
||||||
|
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue