Merge pull request #8327 from nickmelis/master
Code samples for Java Switch Statement
This commit is contained in:
commit
f4420e8e99
|
@ -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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-13</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-13</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
<release>13</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M3</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>13</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>13</maven.compiler.target.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,76 @@
|
|||
package com.baeldung.switchExpression;
|
||||
|
||||
import static java.time.Month.AUGUST;
|
||||
import static java.time.Month.JUNE;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.Month;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class SwitchExpressionsUnitTest {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthJune_thenWillReturn3() {
|
||||
|
||||
var month = JUNE;
|
||||
|
||||
var result = switch (month) {
|
||||
case JANUARY, JUNE, JULY -> 3;
|
||||
case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
|
||||
case MARCH, MAY, APRIL -> 2;
|
||||
default -> 0;
|
||||
};
|
||||
|
||||
assertEquals(result, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthAugust_thenWillReturn24() {
|
||||
var month = AUGUST;
|
||||
|
||||
var result = switch (month) {
|
||||
case JANUARY, JUNE, JULY -> 3;
|
||||
case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
|
||||
case MARCH, MAY, APRIL, AUGUST -> {
|
||||
int monthLength = month.toString().length();
|
||||
yield monthLength * 4;
|
||||
}
|
||||
default -> 0;
|
||||
};
|
||||
|
||||
assertEquals(24, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthJanuary_thenWillReturn3() {
|
||||
|
||||
Function<Month, Integer> func = (month) -> {
|
||||
switch (month) {
|
||||
case JANUARY, JUNE, JULY -> { return 3; }
|
||||
default -> { return 0; }
|
||||
}
|
||||
};
|
||||
|
||||
assertEquals(Integer.valueOf(3), func.apply(Month.JANUARY));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthAugust_thenWillReturn2() {
|
||||
var month = AUGUST;
|
||||
|
||||
var result = switch (month) {
|
||||
case JANUARY, JUNE, JULY -> 3;
|
||||
case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
|
||||
case MARCH, MAY, APRIL, AUGUST -> 2;
|
||||
};
|
||||
|
||||
assertEquals(result, 2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue