+4
-1
@@ -87,4 +87,7 @@ transaction.log
|
||||
apache-cxf/cxf-aegis/baeldung.xml
|
||||
testing-modules/report-*.json
|
||||
|
||||
libraries-2/*.db
|
||||
libraries-2/*.db
|
||||
|
||||
# SDKMan
|
||||
.sdkmanrc
|
||||
|
||||
@@ -74,6 +74,7 @@
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## Core Java 11
|
||||
|
||||
This module contains articles about Java 11 core features
|
||||
|
||||
### Relevant articles
|
||||
- [Guide to Java 8 Optional](https://www.baeldung.com/java-optional)
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?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-11-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-11-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<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>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+3
-1
@@ -7,7 +7,9 @@ import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class OptionalChainingUnitTest {
|
||||
|
||||
+9
-1
@@ -9,7 +9,9 @@ import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class OptionalUnitTest {
|
||||
|
||||
@@ -262,6 +264,12 @@ public class OptionalUnitTest {
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void whenNoArgOrElseThrowWorks_thenCorrect() {
|
||||
String nullName = null;
|
||||
String name = Optional.ofNullable(nullName).orElseThrow();
|
||||
}
|
||||
|
||||
public String getMyDefault() {
|
||||
LOG.debug("Getting default value...");
|
||||
return "Default Value";
|
||||
@@ -107,7 +107,7 @@
|
||||
<uberjar.name>benchmarks</uberjar.name>
|
||||
<jmh.version>1.22</jmh.version>
|
||||
<eclipse.collections.version>10.0.0</eclipse.collections.version>
|
||||
<shade.plugin.version>10.0.0</shade.plugin.version>
|
||||
<shade.plugin.version>3.2.4</shade.plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.array.arraystoreexception;
|
||||
|
||||
public class ArrayStoreExampleCE {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//String array[] = new String[5]; //This will lead to compile-time error at line-8 when uncommented
|
||||
//array[0] = 2;
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.array.arraystoreexception;
|
||||
|
||||
public class ArrayStoreExceptionExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
Object array[] = new String[5];
|
||||
array[0] = 2;
|
||||
} catch (ArrayStoreException e) {
|
||||
// handle the exception
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.exceptions.illegalmonitorstate;
|
||||
|
||||
public class Data {
|
||||
private String message;
|
||||
|
||||
public void send(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String receive() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.exceptions.illegalmonitorstate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SynchronizedReceiver implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(SynchronizedReceiver.class);
|
||||
private final Data data;
|
||||
private String message;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
|
||||
public SynchronizedReceiver(Data data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (data) {
|
||||
try {
|
||||
data.wait();
|
||||
this.message = data.receive();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasIllegalMonitorStateExceptionOccurred() {
|
||||
return illegalMonitorStateExceptionOccurred;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.exceptions.illegalmonitorstate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SynchronizedSender implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(SynchronizedSender.class);
|
||||
private final Data data;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
|
||||
public SynchronizedSender(Data data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (data) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
|
||||
data.send("test");
|
||||
|
||||
data.notifyAll();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasIllegalMonitorStateExceptionOccurred() {
|
||||
return illegalMonitorStateExceptionOccurred;
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.exceptions.illegalmonitorstate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class UnsynchronizedReceiver implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(UnsynchronizedReceiver.class);
|
||||
private final Data data;
|
||||
private String message;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
|
||||
public UnsynchronizedReceiver(Data data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
data.wait();
|
||||
this.message = data.receive();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasIllegalMonitorStateExceptionOccurred() {
|
||||
return illegalMonitorStateExceptionOccurred;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.exceptions.illegalmonitorstate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class UnsynchronizedSender implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(UnsynchronizedSender.class);
|
||||
private final Data data;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
|
||||
public UnsynchronizedSender(Data data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
|
||||
data.send("test");
|
||||
|
||||
data.notifyAll();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasIllegalMonitorStateExceptionOccurred() {
|
||||
return illegalMonitorStateExceptionOccurred;
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.exceptions.illegalmonitorstate;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class IllegalMonitorStateExceptionUnitTest {
|
||||
|
||||
@Test
|
||||
void whenSyncSenderAndSyncReceiverAreUsed_thenIllegalMonitorExceptionShouldNotBeThrown() throws InterruptedException {
|
||||
Data data = new Data();
|
||||
|
||||
SynchronizedReceiver receiver = new SynchronizedReceiver(data);
|
||||
Thread receiverThread = new Thread(receiver, "receiver-thread");
|
||||
receiverThread.start();
|
||||
|
||||
SynchronizedSender sender = new SynchronizedSender(data);
|
||||
Thread senderThread = new Thread(sender, "sender-thread");
|
||||
senderThread.start();
|
||||
|
||||
senderThread.join(1000);
|
||||
receiverThread.join(1000);
|
||||
|
||||
assertEquals("test", receiver.getMessage());
|
||||
assertFalse(sender.hasIllegalMonitorStateExceptionOccurred());
|
||||
assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSyncSenderAndUnSyncReceiverAreUsed_thenIllegalMonitorExceptionShouldNotBeThrown() throws InterruptedException {
|
||||
Data data = new Data();
|
||||
|
||||
UnsynchronizedReceiver receiver = new UnsynchronizedReceiver(data);
|
||||
Thread receiverThread = new Thread(receiver, "receiver-thread");
|
||||
receiverThread.start();
|
||||
|
||||
SynchronizedSender sender = new SynchronizedSender(data);
|
||||
Thread senderThread = new Thread(sender, "sender-thread");
|
||||
senderThread.start();
|
||||
|
||||
|
||||
receiverThread.join(1000);
|
||||
senderThread.join(1000);
|
||||
|
||||
assertNull(receiver.getMessage());
|
||||
assertFalse(sender.hasIllegalMonitorStateExceptionOccurred());
|
||||
assertTrue(receiver.hasIllegalMonitorStateExceptionOccurred());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUnSyncSenderAndSyncReceiverAreUsed_thenIllegalMonitorExceptionShouldBeThrown() throws InterruptedException {
|
||||
Data data = new Data();
|
||||
|
||||
SynchronizedReceiver receiver = new SynchronizedReceiver(data);
|
||||
Thread receiverThread = new Thread(receiver, "receiver-thread");
|
||||
receiverThread.start();
|
||||
|
||||
UnsynchronizedSender sender = new UnsynchronizedSender(data);
|
||||
Thread senderThread = new Thread(sender, "sender-thread");
|
||||
senderThread.start();
|
||||
|
||||
|
||||
receiverThread.join(1000);
|
||||
senderThread.join(1000);
|
||||
|
||||
assertNull(receiver.getMessage());
|
||||
assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred());
|
||||
assertTrue(sender.hasIllegalMonitorStateExceptionOccurred());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="15 seconds" debug="false">
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="WARN">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.tempdirectory;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.FileAttribute;
|
||||
import java.nio.file.attribute.PosixFileAttributes;
|
||||
import java.nio.file.attribute.PosixFilePermission;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/**
|
||||
* Tests several possibilities on how to create
|
||||
* temporary directories.
|
||||
*
|
||||
* @author Rui Vilao ([email protected])
|
||||
*/
|
||||
public class TemporaryDirectoriesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTempDirWithPrefixNoTargetSpecified_whenCreateWithPlainJava_thenInsideOSTempDirStructure() throws IOException {
|
||||
final String tmpdir = Files.createTempDirectory("tmpDirPrefix").toFile().getAbsolutePath();
|
||||
final String tmpDirsLocation = System.getProperty("java.io.tmpdir");
|
||||
|
||||
assertThat(tmpdir).startsWith(tmpDirsLocation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTempDirWithPrefixNoTargetSpecified_whenCreateWithGuava_thenInsideOSTempDirStructure() throws IOException {
|
||||
final String tmpdir = com.google.common.io.Files.createTempDir().getAbsolutePath();
|
||||
final String tmpDirsLocation = System.getProperty("java.io.tmpdir");
|
||||
|
||||
assertThat(tmpdir).startsWith(tmpDirsLocation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTempDirWithPrefixNoTargetSpecified_whenCreateWithApacheCommonsIo_thenInsideOSTempDirStructure() throws IOException {
|
||||
final String tmpDirsLocation = System.getProperty("java.io.tmpdir");
|
||||
final Path path = Paths.get(FileUtils.getTempDirectory().getAbsolutePath(), UUID.randomUUID().toString());
|
||||
final String tmpdir = Files.createDirectories(path).toFile().getAbsolutePath();
|
||||
|
||||
assertThat(tmpdir).startsWith(tmpDirsLocation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTempDirWithPrefixWithTargetSpecified_whenCreatePlainJava_thenInsideTarget() throws IOException {
|
||||
final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix");
|
||||
assertThat(tmpdir.toFile().getPath()).startsWith("target");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTempDirWithPrefixWithTargetSpecifiedWithDeleteOnExit_whenCreatePlainJava_thenInsideTarget() throws IOException {
|
||||
final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix");
|
||||
assertThat(tmpdir.toFile().getPath()).startsWith("target");
|
||||
tmpdir.toFile().deleteOnExit();
|
||||
// we can really assert this test, just as an example.
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTempDirWithPrefixWithFileAttrs_whenCreatePlainJava_thenAttributesAreSet() throws IOException {
|
||||
final FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r--------"));
|
||||
|
||||
final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix", attrs);
|
||||
assertThat(tmpdir.toFile().getPath()).startsWith("target");
|
||||
assertThat(tmpdir.toFile().canWrite()).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ This module contains articles about Java Optional.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Java Optional as Return Type](https://www.baeldung.com/java-optional-return)
|
||||
- [Guide to Java 8 Optional](https://www.baeldung.com/java-optional)
|
||||
- [Java Optional – orElse() vs orElseGet()](https://www.baeldung.com/java-optional-or-else-vs-or-else-get)
|
||||
- [Transforming an Empty String into an Empty Optional](https://www.baeldung.com/java-empty-string-to-empty-optional)
|
||||
- [Filtering a Stream of Optionals in Java](https://www.baeldung.com/java-filter-stream-of-optional)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## Core Kotlin Collections
|
||||
|
||||
This module contains articles about core Kotlin collections.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?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-kotlin-collections-2</artifactId>
|
||||
<name>core-kotlin-collections-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-kotlin-modules</groupId>
|
||||
<artifactId>core-kotlin-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<kotlin.version>1.3.30</kotlin.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package com.baeldung.aggregate
|
||||
|
||||
class AggregateOperations {
|
||||
private val numbers = listOf(1, 15, 3, 8)
|
||||
|
||||
fun countList(): Int {
|
||||
return numbers.count()
|
||||
}
|
||||
|
||||
fun sumList(): Int {
|
||||
return numbers.sum()
|
||||
}
|
||||
|
||||
fun averageList(): Double {
|
||||
return numbers.average()
|
||||
}
|
||||
|
||||
fun maximumInList(): Int? {
|
||||
return numbers.max()
|
||||
}
|
||||
|
||||
fun minimumInList(): Int? {
|
||||
return numbers.min()
|
||||
}
|
||||
|
||||
fun maximumByList(): Int? {
|
||||
return numbers.maxBy { it % 5 }
|
||||
}
|
||||
|
||||
fun minimumByList(): Int? {
|
||||
return numbers.minBy { it % 5 }
|
||||
}
|
||||
|
||||
fun maximumWithList(): String? {
|
||||
val strings = listOf("Berlin", "Kolkata", "Prague", "Barcelona")
|
||||
return strings.maxWith(compareBy { it.length % 4 })
|
||||
}
|
||||
|
||||
fun minimumWithList(): String? {
|
||||
val strings = listOf("Berlin", "Kolkata", "Prague", "Barcelona")
|
||||
return strings.minWith(compareBy { it.length % 4 })
|
||||
}
|
||||
|
||||
fun sumByList(): Int {
|
||||
return numbers.sumBy { it * 5 }
|
||||
}
|
||||
|
||||
fun sumByDoubleList(): Double {
|
||||
return numbers.sumByDouble { it.toDouble() / 8 }
|
||||
}
|
||||
|
||||
fun foldList(): Int {
|
||||
return numbers.fold(100) { total, it ->
|
||||
println("total = $total, it = $it")
|
||||
total - it
|
||||
} // ((((100 - 1)-15)-3)-8) = 73
|
||||
}
|
||||
|
||||
fun foldRightList(): Int {
|
||||
return numbers.foldRight(100) { it, total ->
|
||||
println("total = $total, it = $it")
|
||||
total - it
|
||||
} // ((((100-8)-3)-15)-1) = 73
|
||||
}
|
||||
|
||||
fun foldIndexedList(): Int {
|
||||
return numbers.foldIndexed(100) { index, total, it ->
|
||||
println("total = $total, it = $it, index = $index")
|
||||
if (index.minus(2) >= 0) total - it else total
|
||||
} // ((100 - 3)-8) = 89
|
||||
}
|
||||
|
||||
fun foldRightIndexedList(): Int {
|
||||
return numbers.foldRightIndexed(100) { index, it, total ->
|
||||
println("total = $total, it = $it, index = $index")
|
||||
if (index.minus(2) >= 0) total - it else total
|
||||
} // ((100 - 8)-3) = 89
|
||||
}
|
||||
|
||||
fun reduceList(): Int {
|
||||
return numbers.reduce { total, it ->
|
||||
println("total = $total, it = $it")
|
||||
total - it
|
||||
} // (((1 - 15)-3)-8) = -25
|
||||
}
|
||||
|
||||
fun reduceRightList(): Int {
|
||||
return numbers.reduceRight() { it, total ->
|
||||
println("total = $total, it = $it")
|
||||
total - it
|
||||
} // ((8-3)-15)-1) = -11
|
||||
}
|
||||
|
||||
fun reduceIndexedList(): Int {
|
||||
return numbers.reduceIndexed { index, total, it ->
|
||||
println("total = $total, it = $it, index = $index")
|
||||
if (index.minus(2) >= 0) total - it else total
|
||||
} // ((1-3)-8) = -10
|
||||
}
|
||||
|
||||
fun reduceRightIndexedList(): Int {
|
||||
return numbers.reduceRightIndexed { index, it, total ->
|
||||
println("total = $total, it = $it, index = $index")
|
||||
if (index.minus(2) >= 0) total - it else total
|
||||
} // ((8-3) = 5
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package com.baeldung.aggregate
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class AggregateOperationsUnitTest {
|
||||
|
||||
private val classUnderTest: AggregateOperations = AggregateOperations()
|
||||
|
||||
@Test
|
||||
fun whenCountOfList_thenReturnsValue() {
|
||||
assertEquals(4, classUnderTest.countList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenSumOfList_thenReturnsTotalValue() {
|
||||
assertEquals(27, classUnderTest.sumList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenAverageOfList_thenReturnsValue() {
|
||||
assertEquals(6.75, classUnderTest.averageList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenMaximumOfList_thenReturnsMaximumValue() {
|
||||
assertEquals(15, classUnderTest.maximumInList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenMinimumOfList_thenReturnsMinimumValue() {
|
||||
assertEquals(1, classUnderTest.minimumInList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenMaxByList_thenReturnsLargestValue() {
|
||||
assertEquals(3, classUnderTest.maximumByList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenMinByList_thenReturnsSmallestValue() {
|
||||
assertEquals(15, classUnderTest.minimumByList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenMaxWithList_thenReturnsLargestValue(){
|
||||
assertEquals("Kolkata", classUnderTest.maximumWithList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenMinWithList_thenReturnsSmallestValue(){
|
||||
assertEquals("Barcelona", classUnderTest.minimumWithList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenSumByList_thenReturnsIntegerValue(){
|
||||
assertEquals(135, classUnderTest.sumByList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenSumByDoubleList_thenReturnsDoubleValue(){
|
||||
assertEquals(3.375, classUnderTest.sumByDoubleList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenFoldList_thenReturnsValue(){
|
||||
assertEquals(73, classUnderTest.foldList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenFoldRightList_thenReturnsValue(){
|
||||
assertEquals(73, classUnderTest.foldRightList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenFoldIndexedList_thenReturnsValue(){
|
||||
assertEquals(89, classUnderTest.foldIndexedList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenFoldRightIndexedList_thenReturnsValue(){
|
||||
assertEquals(89, classUnderTest.foldRightIndexedList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReduceList_thenReturnsValue(){
|
||||
assertEquals(-25, classUnderTest.reduceList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReduceRightList_thenReturnsValue(){
|
||||
assertEquals(-11, classUnderTest.reduceRightList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReduceIndexedList_thenReturnsValue(){
|
||||
assertEquals(-10, classUnderTest.reduceIndexedList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReduceRightIndexedList_thenReturnsValue(){
|
||||
assertEquals(5, classUnderTest.reduceRightIndexedList())
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,30 @@ fun main() {
|
||||
numbers.each { println(random * it) } // capturing the random variable
|
||||
}
|
||||
|
||||
fun namedFunction(): Int {
|
||||
return 42
|
||||
}
|
||||
|
||||
fun anonymous(): () -> Int {
|
||||
return fun(): Int {
|
||||
return 42
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> List<T>.eachIndexed(f: (Int, T) -> Unit) {
|
||||
for (i in indices) {
|
||||
f(i, this[i])
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> List<T>.indexOf(x: T): Int {
|
||||
eachIndexed { index, value ->
|
||||
if (value == x) return index
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random number.
|
||||
*/
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
<module>core-kotlin-advanced</module>
|
||||
<module>core-kotlin-annotations</module>
|
||||
<module>core-kotlin-collections</module>
|
||||
<module>core-kotlin-collections-2</module>
|
||||
<module>core-kotlin-concurrency</module>
|
||||
<module>core-kotlin-date-time</module>
|
||||
<module>core-kotlin-design-patterns</module>
|
||||
|
||||
@@ -6,3 +6,4 @@ This module contains articles about Drools
|
||||
|
||||
- [Introduction to Drools](https://www.baeldung.com/drools)
|
||||
- [An Example of Backward Chaining in Drools](https://www.baeldung.com/drools-backward-chaining)
|
||||
- [Drools Using Rules from Excel Files](https://www.baeldung.com/drools-excel)
|
||||
|
||||
+9
-6
@@ -1,21 +1,24 @@
|
||||
package com.baeldung.arrayconversion;
|
||||
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class ArrayToListConversionUnitTest {
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void givenAnArray_whenConvertingToList_returnUnmodifiableListUnitTest() {
|
||||
String[] stringArray = new String[] { "A", "B", "C", "D" };
|
||||
List<String> stringList = Arrays.asList(stringArray);
|
||||
System.out.println(stringList);
|
||||
stringList.set(0, "E");
|
||||
System.out.println(stringList);
|
||||
System.out.println(Arrays.toString(stringArray));
|
||||
assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D"));
|
||||
assertArrayEquals(stringArray, new String[] { "E", "B", "C", "D" });
|
||||
stringList.add("F");
|
||||
}
|
||||
|
||||
@@ -23,10 +26,10 @@ public class ArrayToListConversionUnitTest {
|
||||
public void givenAnArray_whenConvertingToList_returnModifiableListUnitTest() {
|
||||
String[] stringArray = new String[] { "A", "B", "C", "D" };
|
||||
List<String> stringList = new ArrayList<>(Arrays.asList(stringArray));
|
||||
System.out.println(stringList);
|
||||
stringList.set(0, "E");
|
||||
System.out.println(stringList);
|
||||
System.out.println(Arrays.toString(stringArray));
|
||||
assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D"));
|
||||
assertArrayEquals(stringArray, new String[] { "A", "B", "C", "D" });
|
||||
stringList.add("F");
|
||||
assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D", "F"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,6 +237,26 @@
|
||||
<build>
|
||||
<defaultGoal>spring-boot:run</defaultGoal>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>properties-maven-plugin</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>set-system-properties</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<properties>
|
||||
<property>
|
||||
<name>org.slf4j.simpleLogger.log.com.github.eirslett</name>
|
||||
<value>error</value>
|
||||
</property>
|
||||
</properties>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<!-- Jhipster project is autogenerated and upgrading it will need a newly generated project.
|
||||
Also, we already have jhipster-5 which has new version of boot-2. So, this project should be left as a legacy version. -->
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user