Merge branch 'master' of https://github.com/Maiklins/tutorials into JAVA-7244-Review_log_statements_for_projects
Conflicts: algorithms-searching/src/test/java/com/baeldung/algorithms/quadtree/QuadTreeSearchUnitTest.java
This commit is contained in:
@@ -8,3 +8,4 @@ This module contains articles about Java 11 core features
|
||||
- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors)
|
||||
- [New Features in Java 11](https://www.baeldung.com/java-11-new-features)
|
||||
- [Getting the Java Version at Runtime](https://www.baeldung.com/get-java-version-runtime)
|
||||
- [Invoking a SOAP Web Service in Java](https://www.baeldung.com/java-soap-web-service)
|
||||
|
||||
@@ -35,19 +35,19 @@
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -106,7 +106,6 @@
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
<guava.version>29.0-jre</guava.version>
|
||||
<junit.jupiter.version>5.7.0</junit.jupiter.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
<mockserver.version>5.11.1</mockserver.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
|
||||
@@ -1,60 +1,60 @@
|
||||
<?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-12</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-12</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
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-12</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-12</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>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.11.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.11.0</version>
|
||||
</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>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<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>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>12</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>12</maven.compiler.target.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
<properties>
|
||||
<maven.compiler.source.version>12</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>12</maven.compiler.target.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -14,7 +14,7 @@
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
@@ -35,7 +35,7 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
### Relevant articles:
|
||||
|
||||
- [Collect a Java Stream to an Immutable Collection](https://www.baeldung.com/java-stream-immutable-collection)
|
||||
- [Guide to mapMulti in Stream API](https://www.baeldung.com/java-mapmulti)
|
||||
- [Collecting Stream Elements into a List in Java](https://www.baeldung.com/java-stream-to-list-collecting)
|
||||
- [New Features in Java 16](https://www.baeldung.com/java-16-new-features)
|
||||
- [Guide to Java 8 groupingBy Collector](https://www.baeldung.com/java-groupingby-collector)
|
||||
|
||||
@@ -1,53 +1,86 @@
|
||||
<?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-16</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-16</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
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-16</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-16</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>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<release>${maven.compiler.release}</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
<argLine>--enable-preview</argLine>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>16</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>16</maven.compiler.target.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
<properties>
|
||||
<maven.compiler.source.version>16</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>16</maven.compiler.target.version>
|
||||
<maven.compiler.release>16</maven.compiler.release>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
interface HelloWorld {
|
||||
default String hello() {
|
||||
return "world";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import com.baeldung.features.record.Book;
|
||||
|
||||
public class OuterClass {
|
||||
class InnerClass {
|
||||
Book book = new Book("Title", "author", "isbn");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import jdk.incubator.vector.IntVector;
|
||||
|
||||
public class VectorExample {
|
||||
public int[] scalarComputation(int[] a, int[] b) {
|
||||
var c = new int[a.length];
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
c[i] = a[i] * b[i];
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
public int[] vectorComputation(int[] a, int[] b) {
|
||||
var c = new int[a.length];
|
||||
|
||||
var vectorA = IntVector.fromArray(IntVector.SPECIES_128, a, 0);
|
||||
var vectorB = IntVector.fromArray(IntVector.SPECIES_128, b, 0);
|
||||
var vectorC = vectorA.mul(vectorB);
|
||||
vectorC.intoArray(c, 0);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.features.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class Book {
|
||||
private final String title;
|
||||
private final String author;
|
||||
private final String isbn;
|
||||
|
||||
public Book(String title, String author, String isbn) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Book book = (Book) o;
|
||||
return Objects.equals(title, book.title) && Objects.equals(author, book.author) && Objects.equals(isbn, book.isbn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(title, author, isbn);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.features.record;
|
||||
|
||||
public record Book(String title, String author, String isbn) {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.features.sealed;
|
||||
|
||||
public sealed interface JungleAnimal permits Monkey, Snake {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.features.sealed;
|
||||
|
||||
public final class Monkey implements JungleAnimal {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.features.sealed;
|
||||
|
||||
public class Sealed {
|
||||
public static void main(String... args) {
|
||||
JungleAnimal j = new Monkey();
|
||||
|
||||
if (j instanceof Monkey m) {
|
||||
// do logic
|
||||
} else if (j instanceof Snake s) {
|
||||
// do logic
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.features.sealed;
|
||||
|
||||
public non-sealed class Snake implements JungleAnimal {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
module core.java {
|
||||
requires jdk.incubator.vector;
|
||||
requires org.apache.commons.lang3;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DayPeriodSupportUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenASpecificTime_whenFormattingUsingTheBSymbol_thenExpectVerbosePeriodOfDay() {
|
||||
LocalTime date = LocalTime.parse("15:25:08.690791");
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h B");
|
||||
assertThat(date.format(formatter)).isEqualTo("3 in the afternoon");
|
||||
|
||||
formatter = DateTimeFormatter.ofPattern("h BBBB");
|
||||
assertThat(date.format(formatter)).isEqualTo("3 in the afternoon");
|
||||
|
||||
formatter = DateTimeFormatter.ofPattern("h BBBBB");
|
||||
assertThat(date.format(formatter)).isEqualTo("3 in the afternoon");
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
import static java.lang.ClassLoader.getSystemClassLoader;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HelloWorldUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnInterfaceWithDefaulMethod_whenCreatingProxyInstance_thenCanInvokeDefaultMethod() throws Exception {
|
||||
Object proxy = Proxy.newProxyInstance(getSystemClassLoader(), new Class<?>[] { HelloWorld.class },
|
||||
(prox, method, args) -> {
|
||||
if (method.isDefault()) {
|
||||
return InvocationHandler.invokeDefault(prox, method, args);
|
||||
}
|
||||
return method.invoke(prox, args);
|
||||
}
|
||||
);
|
||||
Method method = proxy.getClass().getMethod("hello");
|
||||
assertThat(method.invoke(proxy)).isEqualTo("world");
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InstanceOfPatternMatchingUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTheNewPatternMatchingAbility_whenComparingAgainstTheTradiationalApproach_thenBothVariablesAreEqual() {
|
||||
Object obj = "TEST";
|
||||
|
||||
if (obj instanceof String a) {
|
||||
String b = (String) obj;
|
||||
assertThat(a).isEqualTo(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StreamToListUnitTest {
|
||||
|
||||
@Test
|
||||
void givenAStream_whenCreatingANewListFromStream_thenCollectorsOrInbuiltFunctionAreEquivalent() {
|
||||
List<String> integersAsString = Arrays.asList("1", "2", "3");
|
||||
List<Integer> ints = integersAsString.stream().map(Integer::parseInt).collect(Collectors.toList());
|
||||
List<Integer> intsEquivalent = integersAsString.stream().map(Integer::parseInt).toList();
|
||||
assertThat(ints).isEqualTo(intsEquivalent);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
class VectorExampleUnitTest {
|
||||
|
||||
@Test
|
||||
void givenAScalarComputation_whenCalculatingUsingVectorAPI_thenResultIsSameAsPreviousMethodOfComputation() {
|
||||
VectorExample objectUnderTest = new VectorExample();
|
||||
|
||||
int[] a = {1, 2, 3, 4};
|
||||
int[] b = {5, 6, 7, 8};
|
||||
|
||||
int[] result = objectUnderTest.scalarComputation(a, b);
|
||||
int[] result2 = objectUnderTest.vectorComputation(a, b);
|
||||
|
||||
assertArrayEquals(result, result2);
|
||||
}
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
package com.baeldung.streams;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class StreamToListComparisonWithCollectorsToListUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenAddingtoCollectToList_thenSuccess() {
|
||||
|
||||
List<String> result = Stream.of(Locale.getISOCountries())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
result.add("test");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSortingtoCollectToList_thenSuccess() {
|
||||
|
||||
List<String> result = Stream.of(Locale.getISOCountries())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
result.sort(String::compareToIgnoreCase);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddingCollectUnmodifiableToList_thenException() {
|
||||
|
||||
List<String> result = Stream.of(Locale.getISOCountries())
|
||||
.collect(Collectors.toUnmodifiableList());
|
||||
|
||||
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
|
||||
result.add("test");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSortingCollectUnmodifiableToList_thenSortException() {
|
||||
|
||||
List<String> result = Stream.of(Locale.getISOCountries())
|
||||
.collect(Collectors.toUnmodifiableList());
|
||||
|
||||
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
|
||||
result.sort(String::compareToIgnoreCase);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddingStreamToList_thenException() {
|
||||
|
||||
List<String> result = Stream.of(Locale.getISOCountries())
|
||||
.toList();
|
||||
|
||||
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
|
||||
result.add("test");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSortingStreamToList_thenException() {
|
||||
|
||||
List<String> result = Stream.of(Locale.getISOCountries())
|
||||
.toList();
|
||||
|
||||
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
|
||||
result.sort(String::compareToIgnoreCase);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingStreamToList_withCollectToList_thenEqual() {
|
||||
|
||||
List<String> streamToList = Stream.of(Locale.getISOCountries())
|
||||
.toList();
|
||||
List<String> collectToList = Stream.of(Locale.getISOCountries())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assertions.assertEquals(streamToList, collectToList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingStreamToList_withUnmodifiedCollectToList_thenEqual() {
|
||||
|
||||
List<String> streamToList = Stream.of(Locale.getISOCountries())
|
||||
.toList();
|
||||
List<String> collectToUnmodifiableList = Stream.of(Locale.getISOCountries())
|
||||
.collect(Collectors.toUnmodifiableList());
|
||||
|
||||
Assertions.assertEquals(streamToList, collectToUnmodifiableList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullCollectorsToList_thenSuccess() {
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
Stream.of(null, null)
|
||||
.collect(Collectors.toList());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullCollectorsUnmodifiableToList_thenException() {
|
||||
|
||||
Assertions.assertThrows(NullPointerException.class, () -> {
|
||||
Stream.of(null, null)
|
||||
.collect(Collectors.toUnmodifiableList());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullStreamToList_thenSuccess() {
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
Stream.of(null, null)
|
||||
.toList();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant articles:
|
||||
|
||||
- [Pattern Matching for Switch](https://www.baeldung.com/java-switch-pattern-matching)
|
||||
@@ -0,0 +1,81 @@
|
||||
<?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-17</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-17</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>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.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>
|
||||
<release>${maven.compiler.release}</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>17</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>17</maven.compiler.target.version>
|
||||
<maven.compiler.release>17</maven.compiler.release>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class GuardedPatterns {
|
||||
|
||||
static double getDoubleValueUsingIf(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> {
|
||||
if (s.length() > 0) {
|
||||
yield Double.parseDouble(s);
|
||||
} else {
|
||||
yield 0d;
|
||||
}
|
||||
}
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
static double getDoubleValueUsingGuardedPatterns(Object o) {
|
||||
return switch (o) {
|
||||
case String s && s.length() > 0 -> Double.parseDouble(s);
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class HandlingNullValues {
|
||||
|
||||
static double getDoubleUsingSwitchNullCase(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> Double.parseDouble(s);
|
||||
case null -> 0d;
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
static double getDoubleUsingSwitchTotalType(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> Double.parseDouble(s);
|
||||
case Object ob -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class ParenthesizedPatterns {
|
||||
|
||||
static double getDoubleValueUsingIf(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> {
|
||||
if (s.length() > 0) {
|
||||
if (s.contains("#") || s.contains("@")) {
|
||||
yield 0d;
|
||||
} else {
|
||||
yield Double.parseDouble(s);
|
||||
}
|
||||
} else {
|
||||
yield 0d;
|
||||
}
|
||||
}
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
static double getDoubleValueUsingParenthesizedPatterns(Object o) {
|
||||
return switch (o) {
|
||||
case String s && s.length() > 0 && !(s.contains("#") || s.contains("@")) -> Double.parseDouble(s);
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class PatternMatching {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Object o = args[0];
|
||||
if (o instanceof String s) {
|
||||
System.out.printf("Object is a string %s", s);
|
||||
} else if(o instanceof Number n) {
|
||||
System.out.printf("Object is a number %n", n);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class SwitchStatement {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final String b = "B";
|
||||
switch (args[0]) {
|
||||
case "A" -> System.out.println("Parameter is A");
|
||||
case b -> System.out.println("Parameter is b");
|
||||
default -> System.out.println("Parameter is unknown");
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class TypePatterns {
|
||||
|
||||
static double getDoubleUsingIf(Object o) {
|
||||
double result;
|
||||
|
||||
if (o instanceof Integer) {
|
||||
result = ((Integer) o).doubleValue();
|
||||
} else if (o instanceof Float) {
|
||||
result = ((Float) o).doubleValue();
|
||||
} else if (o instanceof String) {
|
||||
result = Double.parseDouble(((String) o));
|
||||
} else {
|
||||
result = 0d;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static double getDoubleUsingSwitch(Object o) {
|
||||
return switch (o) {
|
||||
case Integer i -> i.doubleValue();
|
||||
case Float f -> f.doubleValue();
|
||||
case String s -> Double.parseDouble(s);
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user