commit
76aa1706ad
@ -6,10 +6,12 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
|
|||||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
|
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.MetadataSources;
|
import org.hibernate.boot.MetadataSources;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -22,11 +24,14 @@ import static org.hibernate.cfg.AvailableSettings.PASS;
|
|||||||
*/
|
*/
|
||||||
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
|
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
|
||||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||||
|
private SessionFactory sessionFactory = createSessionFactory();
|
||||||
|
|
||||||
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
|
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
|
||||||
try (SessionFactory sessionFactory = createSessionFactory()) {
|
try {
|
||||||
ShippingService service = new ShippingService(sessionFactory, new ShippingDao());
|
ShippingService service = new ShippingService(sessionFactory, new ShippingDao());
|
||||||
return routeRequest(input, service);
|
return routeRequest(input, service);
|
||||||
|
} finally {
|
||||||
|
flushConnectionPool();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,4 +110,12 @@ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|||||||
.buildMetadata()
|
.buildMetadata()
|
||||||
.buildSessionFactory();
|
.buildSessionFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void flushConnectionPool() {
|
||||||
|
ConnectionProvider connectionProvider = sessionFactory.getSessionFactoryOptions()
|
||||||
|
.getServiceRegistry()
|
||||||
|
.getService(ConnectionProvider.class);
|
||||||
|
HikariDataSource hikariDataSource = connectionProvider.unwrap(HikariDataSource.class);
|
||||||
|
hikariDataSource.getHikariPoolMXBean().softEvictConnections();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,4 +4,4 @@ This module contains articles about Java 15.
|
|||||||
|
|
||||||
### Relevant articles
|
### Relevant articles
|
||||||
|
|
||||||
- TODO: add article links here
|
- [Sealed Classes and Interfaces in Java 15](https://www.baeldung.com/java-sealed-classes-interfaces)
|
||||||
|
@ -51,8 +51,8 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<release>${maven.compiler.release}</release>
|
<release>${maven.compiler.release}</release>
|
||||||
<compilerArgs>--enable-preview</compilerArgs>
|
<compilerArgs>--enable-preview</compilerArgs>
|
||||||
<source>15</source>
|
<source>14</source>
|
||||||
<target>15</target>
|
<target>14</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.whatsnew.records;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Java record with a header indicating 2 fields.
|
||||||
|
*/
|
||||||
|
public record Person(String name, int age) {
|
||||||
|
/**
|
||||||
|
* Public constructor that does some basic validation.
|
||||||
|
*/
|
||||||
|
public Person {
|
||||||
|
if (age < 0) {
|
||||||
|
throw new IllegalArgumentException("Age cannot be negative");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.whatsnew.sealedclasses;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public non-sealed class Employee extends Person {
|
||||||
|
public Date getHiredDate() {
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.baeldung.whatsnew.sealedclasses;
|
||||||
|
|
||||||
|
public final class Manager extends Person {
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.whatsnew.sealedclasses;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public sealed class Person permits Employee, Manager {
|
||||||
|
/**
|
||||||
|
* Demonstration of pattern matching for instanceof
|
||||||
|
*
|
||||||
|
* @param person A Person object
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static void patternMatchingDemo(Person person) {
|
||||||
|
if(person instanceof Employee employee) {
|
||||||
|
Date hiredDate = employee.getHiredDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(person instanceof Employee employee && employee.getHiredDate() != null) {
|
||||||
|
Date hiredDate = employee.getHiredDate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,3 +7,4 @@ This module contains complete guides about arrays in Java
|
|||||||
- [Guide to the java.util.Arrays Class](https://www.baeldung.com/java-util-arrays)
|
- [Guide to the java.util.Arrays Class](https://www.baeldung.com/java-util-arrays)
|
||||||
- [What is \[Ljava.lang.Object;?](https://www.baeldung.com/java-tostring-array)
|
- [What is \[Ljava.lang.Object;?](https://www.baeldung.com/java-tostring-array)
|
||||||
- [Guide to ArrayStoreException](https://www.baeldung.com/java-arraystoreexception)
|
- [Guide to ArrayStoreException](https://www.baeldung.com/java-arraystoreexception)
|
||||||
|
- [Creating a Generic Array in Java](https://www.baeldung.com/java-generic-array)
|
||||||
|
6
core-java-modules/core-java-char/README.md
Normal file
6
core-java-modules/core-java-char/README.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
## Core Java Character
|
||||||
|
|
||||||
|
This module contains articles about Java Character Class
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- Character#isAlphabetic vs Character#isLetter
|
42
core-java-modules/core-java-char/pom.xml
Normal file
42
core-java-modules/core-java-char/pom.xml
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?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-char</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-char</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-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.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-core</artifactId>
|
||||||
|
<version>${openjdk.jmh.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
|
<version>${openjdk.jmh.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<openjdk.jmh.version>1.19</openjdk.jmh.version>
|
||||||
|
<assertj.version>3.11.1</assertj.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.character;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class CharacterGeneralCategoryTypeUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenACharacter_whenUpperCaseLetter_thenAssertTrue() {
|
||||||
|
assertTrue(Character.getType('U') == Character.UPPERCASE_LETTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenACharacter_whenLowerCaseLetter_thenAssertTrue() {
|
||||||
|
assertTrue(Character.getType('u') == Character.LOWERCASE_LETTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenACharacter_whenTitleCaseLetter_thenAssertTrue() {
|
||||||
|
assertTrue(Character.getType('\u01f2') == Character.TITLECASE_LETTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenACharacter_whenModifierLetter_thenAssertTrue() {
|
||||||
|
assertTrue(Character.getType('\u02b0') == Character.MODIFIER_LETTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenACharacter_whenOtherLetter_thenAssertTrue() {
|
||||||
|
assertTrue(Character.getType('\u05d0') == Character.OTHER_LETTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenACharacter_whenLetterNumber_thenAssertTrue() {
|
||||||
|
assertTrue(Character.getType('\u2164') == Character.LETTER_NUMBER);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.character;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
|
||||||
|
public class IsLetterOrAlphabetUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenACharacter_whenLetter_thenAssertIsLetterTrue() {
|
||||||
|
assertTrue(Character.isLetter('a'));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenACharacter_whenLetter_thenAssertIsAlphabeticTrue() {
|
||||||
|
assertTrue(Character.isAlphabetic('a'));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenACharacter_whenAlphabeticAndNotLetter_thenAssertIsLetterFalse() {
|
||||||
|
assertFalse(Character.isLetter('\u2164'));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenACharacter_whenAlphabeticAndNotLetter_thenAssertIsAlphabeticTrue() {
|
||||||
|
assertTrue(Character.isAlphabetic('\u2164'));
|
||||||
|
}
|
||||||
|
}
|
@ -8,4 +8,4 @@ This module contains articles about the Java ArrayList collection
|
|||||||
- [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist)
|
- [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist)
|
||||||
- [Multi Dimensional ArrayList in Java](https://www.baeldung.com/java-multi-dimensional-arraylist)
|
- [Multi Dimensional ArrayList in Java](https://www.baeldung.com/java-multi-dimensional-arraylist)
|
||||||
- [Removing an Element From an ArrayList](https://www.baeldung.com/java-arraylist-remove-element)
|
- [Removing an Element From an ArrayList](https://www.baeldung.com/java-arraylist-remove-element)
|
||||||
|
- [The Capacity of an ArrayList vs the Size of an Array in Java](https://www.baeldung.com/java-list-capacity-array-size)
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.listcapacityvsarraysize;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
class ArrayCreatorUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenSizeOfAnArrayIsNonZero_thenReturnNewArrayOfGivenSize() {
|
||||||
|
Integer[] array = new Integer[10];
|
||||||
|
assertEquals(10, array.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenSizeOfAnArrayIsLessThanZero_thenThrowException() {
|
||||||
|
assertThrows(NegativeArraySizeException.class, () -> { Integer[] array = new Integer[-1]; });
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.listcapacityvsarraysize;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class ArrayListCreatorUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenValidCapacityOfList_whenCreateListInvoked_thenCreateNewArrayListWithGivenCapacity() {
|
||||||
|
ArrayList<Integer> list = new ArrayList<>(100);
|
||||||
|
|
||||||
|
assertNotNull(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenInvalidCapacityOfList_whenCreateListInvoked_thenThrowException() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> new ArrayList<>(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenValidCapacityOfList_whenCreateListInvoked_thenCreateNewArrayListWithSizeZero() {
|
||||||
|
assertEquals(0, new ArrayList<Integer>(100).size());
|
||||||
|
}
|
||||||
|
}
|
@ -6,3 +6,4 @@
|
|||||||
- [AbstractMethodError in Java](https://www.baeldung.com/java-abstractmethoderror)
|
- [AbstractMethodError in Java](https://www.baeldung.com/java-abstractmethoderror)
|
||||||
- [Java IndexOutOfBoundsException “Source Does Not Fit in Dest”](https://www.baeldung.com/java-indexoutofboundsexception)
|
- [Java IndexOutOfBoundsException “Source Does Not Fit in Dest”](https://www.baeldung.com/java-indexoutofboundsexception)
|
||||||
- [Localizing Exception Messages in Java](https://www.baeldung.com/java-localize-exception-messages)
|
- [Localizing Exception Messages in Java](https://www.baeldung.com/java-localize-exception-messages)
|
||||||
|
- [Explanation of ClassCastException in Java](https://www.baeldung.com/java-classcastexception)
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package com.baeldung.exceptions.classcastexception;
|
package com.baeldung.exceptions.classcastexception;
|
||||||
|
|
||||||
public class Reptile implements Animal {
|
public class Amphibian implements Animal {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "Reptile";
|
return "Amphibian";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.exceptions.classcastexception;
|
package com.baeldung.exceptions.classcastexception;
|
||||||
|
|
||||||
public class Frog extends Reptile {
|
public class Frog extends Amphibian {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -16,12 +16,23 @@ public class UnzipFile {
|
|||||||
ZipEntry zipEntry = zis.getNextEntry();
|
ZipEntry zipEntry = zis.getNextEntry();
|
||||||
while (zipEntry != null) {
|
while (zipEntry != null) {
|
||||||
final File newFile = newFile(destDir, zipEntry);
|
final File newFile = newFile(destDir, zipEntry);
|
||||||
|
if (zipEntry.isDirectory()) {
|
||||||
|
if (!newFile.isDirectory() && !newFile.mkdirs()) {
|
||||||
|
throw new IOException("Failed to create directory " + newFile);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
File parent = newFile.getParentFile();
|
||||||
|
if (!parent.isDirectory() && !parent.mkdirs()) {
|
||||||
|
throw new IOException("Failed to create directory " + parent);
|
||||||
|
}
|
||||||
|
|
||||||
final FileOutputStream fos = new FileOutputStream(newFile);
|
final FileOutputStream fos = new FileOutputStream(newFile);
|
||||||
int len;
|
int len;
|
||||||
while ((len = zis.read(buffer)) > 0) {
|
while ((len = zis.read(buffer)) > 0) {
|
||||||
fos.write(buffer, 0, len);
|
fos.write(buffer, 0, len);
|
||||||
}
|
}
|
||||||
fos.close();
|
fos.close();
|
||||||
|
}
|
||||||
zipEntry = zis.getNextEntry();
|
zipEntry = zis.getNextEntry();
|
||||||
}
|
}
|
||||||
zis.closeEntry();
|
zis.closeEntry();
|
||||||
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.baeldung.iterationcounter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class IterationCounter {
|
||||||
|
public static final List<String> IMDB_TOP_MOVIES = Arrays.asList("The Shawshank Redemption",
|
||||||
|
"The Godfather", "The Godfather II", "The Dark Knight");
|
||||||
|
|
||||||
|
public static List<String> getRankingsWithForLoop(List<String> movies) {
|
||||||
|
List<String> rankings = new ArrayList<>();
|
||||||
|
for (int i = 0; i < movies.size(); i++) {
|
||||||
|
String ranking = (i + 1) + ": " + movies.get(i);
|
||||||
|
rankings.add(ranking);
|
||||||
|
}
|
||||||
|
return rankings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getRankingsWithForEachLoop(List<String> movies) {
|
||||||
|
List<String> rankings = new ArrayList<>();
|
||||||
|
int i = 0;
|
||||||
|
for (String movie : movies) {
|
||||||
|
String ranking = (i + 1) + ": " + movies.get(i);
|
||||||
|
rankings.add(ranking);
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return rankings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getRankingsWithFunctionalForEachLoop(List<String> movies) {
|
||||||
|
List<String> rankings = new ArrayList<>();
|
||||||
|
forEachWithCounter(movies, (i, movie) -> {
|
||||||
|
String ranking = (i + 1) + ": " + movie;
|
||||||
|
rankings.add(ranking);
|
||||||
|
});
|
||||||
|
|
||||||
|
return rankings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> void forEachWithCounter(Iterable<T> source, BiConsumer<Integer, T> consumer) {
|
||||||
|
int i = 0;
|
||||||
|
for (T item : source) {
|
||||||
|
consumer.accept(i, item);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getRankingsWithStream(Stream<String> movies) {
|
||||||
|
List<String> rankings = new ArrayList<>();
|
||||||
|
movies.forEach(withCounter((i, movie) -> {
|
||||||
|
String ranking = (i + 1) + ": " + movie;
|
||||||
|
rankings.add(ranking);
|
||||||
|
}));
|
||||||
|
|
||||||
|
return rankings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Consumer<T> withCounter(BiConsumer<Integer, T> consumer) {
|
||||||
|
AtomicInteger counter = new AtomicInteger(0);
|
||||||
|
return item -> consumer.accept(counter.getAndIncrement(), item);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.iterationcounter;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static com.baeldung.iterationcounter.IterationCounter.*;
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
public class IterationCounterUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenRankings_whenCalculateWithForLoop_thenRankingsCorrect() {
|
||||||
|
assertThat(getRankingsWithForLoop(IMDB_TOP_MOVIES))
|
||||||
|
.containsExactly("1: The Shawshank Redemption",
|
||||||
|
"2: The Godfather", "3: The Godfather II", "4: The Dark Knight");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRankings_whenCalculateWithForEachLoop_thenRankingsCorrect() {
|
||||||
|
assertThat(getRankingsWithForEachLoop(IMDB_TOP_MOVIES))
|
||||||
|
.containsExactly("1: The Shawshank Redemption",
|
||||||
|
"2: The Godfather", "3: The Godfather II", "4: The Dark Knight");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRankings_whenCalculateWithFunctionalForEach_thenRankingsCorrect() {
|
||||||
|
assertThat(getRankingsWithFunctionalForEachLoop(IMDB_TOP_MOVIES))
|
||||||
|
.containsExactly("1: The Shawshank Redemption",
|
||||||
|
"2: The Godfather", "3: The Godfather II", "4: The Dark Knight");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRankings_whenCalculateWithStream_thenRankingsCorrect() {
|
||||||
|
assertThat(getRankingsWithStream(IMDB_TOP_MOVIES.stream()))
|
||||||
|
.containsExactly("1: The Shawshank Redemption",
|
||||||
|
"2: The Godfather", "3: The Godfather II", "4: The Dark Knight");
|
||||||
|
}
|
||||||
|
}
|
@ -8,5 +8,5 @@ This module contains articles about string APIs.
|
|||||||
- [Guide to java.util.Formatter](https://www.baeldung.com/java-string-formatter)
|
- [Guide to java.util.Formatter](https://www.baeldung.com/java-string-formatter)
|
||||||
- [Guide to StreamTokenizer](https://www.baeldung.com/java-streamtokenizer)
|
- [Guide to StreamTokenizer](https://www.baeldung.com/java-streamtokenizer)
|
||||||
- [CharSequence vs. String in Java](https://www.baeldung.com/java-char-sequence-string)
|
- [CharSequence vs. String in Java](https://www.baeldung.com/java-char-sequence-string)
|
||||||
- [StringBuilder and StringBuffer in Java](https://www.baeldung.com/java-string-builder-string-buffer)
|
- [StringBuilder vs StringBuffer in Java](https://www.baeldung.com/java-string-builder-string-buffer)
|
||||||
- [Generate a Secure Random Password in Java](https://www.baeldung.com/java-generate-secure-password)
|
- [Generate a Secure Random Password in Java](https://www.baeldung.com/java-generate-secure-password)
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
<module>core-java-arrays-operations-basic</module>
|
<module>core-java-arrays-operations-basic</module>
|
||||||
<module>core-java-arrays-operations-advanced</module>
|
<module>core-java-arrays-operations-advanced</module>
|
||||||
|
|
||||||
|
<module>core-java-char</module>
|
||||||
<module>core-java-collections</module>
|
<module>core-java-collections</module>
|
||||||
<module>core-java-collections-2</module>
|
<module>core-java-collections-2</module>
|
||||||
<module>core-java-collections-3</module>
|
<module>core-java-collections-3</module>
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
## Core Kotlin Advanced
|
|
||||||
|
|
||||||
This module contains articles about advanced topics in Kotlin.
|
|
||||||
|
|
||||||
### Relevant articles:
|
|
||||||
- [Building DSLs in Kotlin](https://www.baeldung.com/kotlin-dsl)
|
|
||||||
- [Regular Expressions in Kotlin](https://www.baeldung.com/kotlin-regular-expressions)
|
|
||||||
- [Idiomatic Logging in Kotlin](https://www.baeldung.com/kotlin-logging)
|
|
||||||
- [Mapping of Data Objects in Kotlin](https://www.baeldung.com/kotlin-data-objects)
|
|
||||||
- [Reflection with Kotlin](https://www.baeldung.com/kotlin-reflection)
|
|
||||||
- [Kotlin Contracts](https://www.baeldung.com/kotlin-contracts)
|
|
@ -1,41 +0,0 @@
|
|||||||
<?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-advanced</artifactId>
|
|
||||||
<name>core-kotlin-advanced</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.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>
|
|
||||||
<assertj.version>3.10.0</assertj.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
|
@ -1,23 +0,0 @@
|
|||||||
package com.baeldung.contract
|
|
||||||
|
|
||||||
import kotlin.contracts.ExperimentalContracts
|
|
||||||
import kotlin.contracts.InvocationKind
|
|
||||||
import kotlin.contracts.contract
|
|
||||||
|
|
||||||
@ExperimentalContracts
|
|
||||||
inline fun <R> myRun(block: () -> R): R {
|
|
||||||
contract {
|
|
||||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
|
||||||
}
|
|
||||||
return block()
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExperimentalContracts
|
|
||||||
fun callsInPlace() {
|
|
||||||
val i: Int
|
|
||||||
myRun {
|
|
||||||
i = 1 // Without contract initialization is forbidden due to possible re-assignment
|
|
||||||
}
|
|
||||||
println(i) // Without contract variable might be uninitialized
|
|
||||||
}
|
|
||||||
|
|
@ -1,43 +0,0 @@
|
|||||||
package com.baeldung.contract
|
|
||||||
|
|
||||||
import kotlin.contracts.ExperimentalContracts
|
|
||||||
import kotlin.contracts.contract
|
|
||||||
|
|
||||||
data class Request(val arg: String)
|
|
||||||
|
|
||||||
class Service {
|
|
||||||
|
|
||||||
@ExperimentalContracts
|
|
||||||
fun process(request: Request?) {
|
|
||||||
validate(request)
|
|
||||||
println(request.arg)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExperimentalContracts
|
|
||||||
private fun validate(request: Request?) {
|
|
||||||
contract {
|
|
||||||
returns() implies (request != null)
|
|
||||||
}
|
|
||||||
if (request == null) {
|
|
||||||
throw IllegalArgumentException("Undefined request")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class MyEvent(val message: String)
|
|
||||||
|
|
||||||
@ExperimentalContracts
|
|
||||||
fun processEvent(event: Any?) {
|
|
||||||
if (isInterested(event)) {
|
|
||||||
println(event.message) // Compiler makes smart cast here with the help of contract
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExperimentalContracts
|
|
||||||
fun isInterested(event: Any?): Boolean {
|
|
||||||
contract {
|
|
||||||
returns(true) implies (event is MyEvent)
|
|
||||||
}
|
|
||||||
return event is MyEvent
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.baeldung.datamapping
|
|
||||||
|
|
||||||
data class User(
|
|
||||||
val firstName: String,
|
|
||||||
val lastName: String,
|
|
||||||
val street: String,
|
|
||||||
val houseNumber: String,
|
|
||||||
val phone: String,
|
|
||||||
val age: Int,
|
|
||||||
val password: String)
|
|
@ -1,22 +0,0 @@
|
|||||||
package com.baeldung.datamapping
|
|
||||||
|
|
||||||
import kotlin.reflect.full.memberProperties
|
|
||||||
|
|
||||||
fun User.toUserView() = UserView(
|
|
||||||
name = "$firstName $lastName",
|
|
||||||
address = "$street $houseNumber",
|
|
||||||
telephone = phone,
|
|
||||||
age = age
|
|
||||||
)
|
|
||||||
|
|
||||||
fun User.toUserViewReflection() = with(::UserView) {
|
|
||||||
val propertiesByName = User::class.memberProperties.associateBy { it.name }
|
|
||||||
callBy(parameters.associate { parameter ->
|
|
||||||
parameter to when (parameter.name) {
|
|
||||||
UserView::name.name -> "$firstName $lastName"
|
|
||||||
UserView::address.name -> "$street $houseNumber"
|
|
||||||
UserView::telephone.name -> phone
|
|
||||||
else -> propertiesByName[parameter.name]?.get(this@toUserViewReflection)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package com.baeldung.datamapping
|
|
||||||
|
|
||||||
data class UserView(
|
|
||||||
val name: String,
|
|
||||||
val address: String,
|
|
||||||
val telephone: String,
|
|
||||||
val age: Int
|
|
||||||
)
|
|
@ -1,114 +0,0 @@
|
|||||||
package com.baeldung.dsl
|
|
||||||
|
|
||||||
abstract class Condition {
|
|
||||||
|
|
||||||
fun and(initializer: Condition.() -> Unit) {
|
|
||||||
addCondition(And().apply(initializer))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun or(initializer: Condition.() -> Unit) {
|
|
||||||
addCondition(Or().apply(initializer))
|
|
||||||
}
|
|
||||||
|
|
||||||
infix fun String.eq(value: Any?) {
|
|
||||||
addCondition(Eq(this, value))
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract fun addCondition(condition: Condition)
|
|
||||||
}
|
|
||||||
|
|
||||||
open class CompositeCondition(private val sqlOperator: String) : Condition() {
|
|
||||||
private val conditions = mutableListOf<Condition>()
|
|
||||||
|
|
||||||
override fun addCondition(condition: Condition) {
|
|
||||||
conditions += condition
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
return if (conditions.size == 1) {
|
|
||||||
conditions.first().toString()
|
|
||||||
} else {
|
|
||||||
conditions.joinToString(prefix = "(", postfix = ")", separator = " $sqlOperator ") {
|
|
||||||
"$it"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class And : CompositeCondition("and")
|
|
||||||
|
|
||||||
class Or : CompositeCondition("or")
|
|
||||||
|
|
||||||
class Eq(private val column: String, private val value: Any?) : Condition() {
|
|
||||||
|
|
||||||
init {
|
|
||||||
if (value != null && value !is Number && value !is String) {
|
|
||||||
throw IllegalArgumentException("Only <null>, numbers and strings values can be used in the 'where' clause")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun addCondition(condition: Condition) {
|
|
||||||
throw IllegalStateException("Can't add a nested condition to the sql 'eq'")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
return when (value) {
|
|
||||||
null -> "$column is null"
|
|
||||||
is String -> "$column = '$value'"
|
|
||||||
else -> "$column = $value"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SqlSelectBuilder {
|
|
||||||
|
|
||||||
private val columns = mutableListOf<String>()
|
|
||||||
private lateinit var table: String
|
|
||||||
private var condition: Condition? = null
|
|
||||||
|
|
||||||
fun select(vararg columns: String) {
|
|
||||||
if (columns.isEmpty()) {
|
|
||||||
throw IllegalArgumentException("At least one column should be defined")
|
|
||||||
}
|
|
||||||
if (this.columns.isNotEmpty()) {
|
|
||||||
throw IllegalStateException("Detected an attempt to re-define columns to fetch. Current columns list: "
|
|
||||||
+ "${this.columns}, new columns list: $columns")
|
|
||||||
}
|
|
||||||
this.columns.addAll(columns)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun from(table: String) {
|
|
||||||
this.table = table
|
|
||||||
}
|
|
||||||
|
|
||||||
fun where(initializer: Condition.() -> Unit) {
|
|
||||||
condition = And().apply(initializer)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun build(): String {
|
|
||||||
if (!::table.isInitialized) {
|
|
||||||
throw IllegalStateException("Failed to build an sql select - target table is undefined")
|
|
||||||
}
|
|
||||||
return toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
val columnsToFetch =
|
|
||||||
if (columns.isEmpty()) {
|
|
||||||
"*"
|
|
||||||
} else {
|
|
||||||
columns.joinToString(", ")
|
|
||||||
}
|
|
||||||
val conditionString =
|
|
||||||
if (condition == null) {
|
|
||||||
""
|
|
||||||
} else {
|
|
||||||
" where $condition"
|
|
||||||
}
|
|
||||||
return "select $columnsToFetch from $table$conditionString"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun query(initializer: SqlSelectBuilder.() -> Unit): SqlSelectBuilder {
|
|
||||||
return SqlSelectBuilder().apply(initializer)
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
package com.baeldung.logging
|
|
||||||
|
|
||||||
import org.slf4j.Logger
|
|
||||||
|
|
||||||
open class LoggerAsExtensionOnAny {
|
|
||||||
val logger = logger()
|
|
||||||
|
|
||||||
fun log(s: String) {
|
|
||||||
logger().info(s)
|
|
||||||
logger.info(s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ExtensionSubclass : LoggerAsExtensionOnAny()
|
|
||||||
|
|
||||||
fun <T : Any> T.logger(): Logger = getLogger(getClassForLogging(javaClass))
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
|
||||||
LoggerAsExtensionOnAny().log("test")
|
|
||||||
ExtensionSubclass().log("sub")
|
|
||||||
"foo".logger().info("foo")
|
|
||||||
1.logger().info("uh-oh!")
|
|
||||||
SomeOtherClass().logger()
|
|
||||||
}
|
|
||||||
|
|
||||||
class SomeOtherClass {
|
|
||||||
fun logger(): String {
|
|
||||||
return "foo"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
package com.baeldung.logging
|
|
||||||
|
|
||||||
import org.slf4j.Logger
|
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
|
|
||||||
interface Logging
|
|
||||||
|
|
||||||
inline fun <reified T : Logging> T.logger(): Logger =
|
|
||||||
//Wrong logger name!
|
|
||||||
//LoggerFactory.getLogger(javaClass.name + " w/interface")
|
|
||||||
LoggerFactory.getLogger(getClassForLogging(T::class.java).name + " w/interface")
|
|
||||||
|
|
||||||
open class LoggerAsExtensionOnMarkerInterface : Logging {
|
|
||||||
companion object : Logging {
|
|
||||||
val logger = logger()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun log(s: String) {
|
|
||||||
logger().info(s)
|
|
||||||
logger.info(s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MarkerExtensionSubclass : LoggerAsExtensionOnMarkerInterface()
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
|
||||||
LoggerAsExtensionOnMarkerInterface().log("test")
|
|
||||||
MarkerExtensionSubclass().log("sub")
|
|
||||||
"foo".logger().info("foo")
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package com.baeldung.logging
|
|
||||||
|
|
||||||
open class LoggerAsProperty {
|
|
||||||
private val logger = getLogger(javaClass)
|
|
||||||
|
|
||||||
fun log(s: String) {
|
|
||||||
logger.info(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class PropertySubclass : LoggerAsProperty()
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
|
||||||
LoggerAsProperty().log("test")
|
|
||||||
PropertySubclass().log("sub")
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
package com.baeldung.logging
|
|
||||||
|
|
||||||
import org.slf4j.Logger
|
|
||||||
import kotlin.properties.ReadOnlyProperty
|
|
||||||
import kotlin.reflect.KProperty
|
|
||||||
|
|
||||||
open class LoggerAsPropertyDelegate {
|
|
||||||
private val lazyLogger by lazyLogger()
|
|
||||||
protected val logger by LoggerDelegate()
|
|
||||||
private val logger2 = logger
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private val lazyLoggerComp by lazyLogger()
|
|
||||||
private val loggerComp by LoggerDelegate()
|
|
||||||
}
|
|
||||||
|
|
||||||
open fun log(s: String) {
|
|
||||||
logger.info(s)
|
|
||||||
logger2.info(s)
|
|
||||||
lazyLogger.info(s)
|
|
||||||
loggerComp.info(s)
|
|
||||||
lazyLoggerComp.info(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class DelegateSubclass : LoggerAsPropertyDelegate() {
|
|
||||||
override fun log(s: String) {
|
|
||||||
logger.info("-- in sub")
|
|
||||||
super.log(s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun lazyLogger(forClass: Class<*>): Lazy<Logger> =
|
|
||||||
lazy { getLogger(getClassForLogging(forClass)) }
|
|
||||||
|
|
||||||
fun <T : Any> T.lazyLogger(): Lazy<Logger> = lazyLogger(javaClass)
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
|
||||||
LoggerAsPropertyDelegate().log("test")
|
|
||||||
DelegateSubclass().log("sub")
|
|
||||||
}
|
|
||||||
|
|
||||||
class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
|
||||||
override fun getValue(thisRef: R, property: KProperty<*>) =
|
|
||||||
getLogger(getClassForLogging(thisRef.javaClass))
|
|
||||||
}
|
|
@ -1,44 +0,0 @@
|
|||||||
package com.baeldung.logging
|
|
||||||
|
|
||||||
open class LoggerInCompanionObject {
|
|
||||||
companion object {
|
|
||||||
private val loggerWithExplicitClass = getLogger(LoggerInCompanionObject::class.java)
|
|
||||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
|
||||||
private val loggerWithWrongClass = getLogger(javaClass)
|
|
||||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
|
||||||
private val logger = getLogger(javaClass.enclosingClass)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun log(s: String) {
|
|
||||||
loggerWithExplicitClass.info(s)
|
|
||||||
loggerWithWrongClass.info(s)
|
|
||||||
logger.info(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
class Inner {
|
|
||||||
companion object {
|
|
||||||
private val loggerWithExplicitClass = getLogger(Inner::class.java)
|
|
||||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
|
||||||
@JvmStatic
|
|
||||||
private val loggerWithWrongClass = getLogger(javaClass)
|
|
||||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
|
||||||
@JvmStatic
|
|
||||||
private val logger = getLogger(javaClass.enclosingClass)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun log(s: String) {
|
|
||||||
loggerWithExplicitClass.info(s)
|
|
||||||
loggerWithWrongClass.info(s)
|
|
||||||
logger.info(s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class CompanionSubclass : LoggerInCompanionObject()
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
|
||||||
LoggerInCompanionObject().log("test")
|
|
||||||
LoggerInCompanionObject.Inner().log("test")
|
|
||||||
CompanionSubclass().log("sub")
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package com.baeldung.logging
|
|
||||||
|
|
||||||
import org.slf4j.Logger
|
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
import kotlin.reflect.full.companionObject
|
|
||||||
|
|
||||||
fun getLogger(forClass: Class<*>): Logger = LoggerFactory.getLogger(forClass)
|
|
||||||
|
|
||||||
fun <T : Any> getClassForLogging(javaClass: Class<T>): Class<*> {
|
|
||||||
return javaClass.enclosingClass?.takeIf {
|
|
||||||
it.kotlin.companionObject?.java == javaClass
|
|
||||||
} ?: javaClass
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package com.baeldung.datamapping
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import org.junit.jupiter.api.assertAll
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
class UserTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `maps User to UserResponse using extension function`() {
|
|
||||||
val p = buildUser()
|
|
||||||
val view = p.toUserView()
|
|
||||||
assertUserView(view)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `maps User to UserResponse using reflection`() {
|
|
||||||
val p = buildUser()
|
|
||||||
val view = p.toUserViewReflection()
|
|
||||||
assertUserView(view)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun buildUser(): User {
|
|
||||||
return User(
|
|
||||||
"Java",
|
|
||||||
"Duke",
|
|
||||||
"Javastreet",
|
|
||||||
"42",
|
|
||||||
"1234567",
|
|
||||||
30,
|
|
||||||
"s3cr37"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun assertUserView(pr: UserView) {
|
|
||||||
assertAll(
|
|
||||||
{ assertEquals("Java Duke", pr.name) },
|
|
||||||
{ assertEquals("Javastreet 42", pr.address) },
|
|
||||||
{ assertEquals("1234567", pr.telephone) },
|
|
||||||
{ assertEquals(30, pr.age) }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,73 +0,0 @@
|
|||||||
package com.baeldung.dsl
|
|
||||||
|
|
||||||
import org.assertj.core.api.Assertions.assertThat
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class SqlDslTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when no columns are specified then star is used`() {
|
|
||||||
doTest("select * from table1") {
|
|
||||||
from ("table1")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when no condition is specified then correct query is built`() {
|
|
||||||
doTest("select column1, column2 from table1") {
|
|
||||||
select("column1", "column2")
|
|
||||||
from ("table1")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = Exception::class)
|
|
||||||
fun `when no table is specified then an exception is thrown`() {
|
|
||||||
query {
|
|
||||||
select("column1")
|
|
||||||
}.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when a list of conditions is specified then it's respected`() {
|
|
||||||
doTest("select * from table1 where (column3 = 4 and column4 is null)") {
|
|
||||||
from ("table1")
|
|
||||||
where {
|
|
||||||
"column3" eq 4
|
|
||||||
"column4" eq null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when 'or' conditions are specified then they are respected`() {
|
|
||||||
doTest("select * from table1 where (column3 = 4 or column4 is null)") {
|
|
||||||
from ("table1")
|
|
||||||
where {
|
|
||||||
or {
|
|
||||||
"column3" eq 4
|
|
||||||
"column4" eq null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when either 'and' or 'or' conditions are specified then they are respected`() {
|
|
||||||
doTest("select * from table1 where ((column3 = 4 or column4 is null) and column5 = 42)") {
|
|
||||||
from ("table1")
|
|
||||||
where {
|
|
||||||
and {
|
|
||||||
or {
|
|
||||||
"column3" eq 4
|
|
||||||
"column4" eq null
|
|
||||||
}
|
|
||||||
"column5" eq 42
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun doTest(expected: String, sql: SqlSelectBuilder.() -> Unit) {
|
|
||||||
assertThat(query(sql).build()).isEqualTo(expected)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
package com.baeldung.reflection
|
|
||||||
|
|
||||||
import org.junit.Ignore
|
|
||||||
import org.junit.Test
|
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
|
|
||||||
@Ignore
|
|
||||||
class JavaReflectionTest {
|
|
||||||
private val LOG = LoggerFactory.getLogger(KClassTest::class.java)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun listJavaClassMethods() {
|
|
||||||
Exception::class.java.methods
|
|
||||||
.forEach { method -> LOG.info("Method: {}", method) }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun listKotlinClassMethods() {
|
|
||||||
JavaReflectionTest::class.java.methods
|
|
||||||
.forEach { method -> LOG.info("Method: {}", method) }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun listKotlinDataClassMethods() {
|
|
||||||
data class ExampleDataClass(val name: String, var enabled: Boolean)
|
|
||||||
|
|
||||||
ExampleDataClass::class.java.methods
|
|
||||||
.forEach { method -> LOG.info("Method: {}", method) }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,69 +0,0 @@
|
|||||||
package com.baeldung.reflection
|
|
||||||
|
|
||||||
import org.junit.Assert
|
|
||||||
import org.junit.Ignore
|
|
||||||
import org.junit.Test
|
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
import java.math.BigDecimal
|
|
||||||
import kotlin.reflect.full.*
|
|
||||||
|
|
||||||
class KClassTest {
|
|
||||||
private val LOG = LoggerFactory.getLogger(KClassTest::class.java)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testKClassDetails() {
|
|
||||||
val stringClass = String::class
|
|
||||||
Assert.assertEquals("kotlin.String", stringClass.qualifiedName)
|
|
||||||
Assert.assertFalse(stringClass.isData)
|
|
||||||
Assert.assertFalse(stringClass.isCompanion)
|
|
||||||
Assert.assertFalse(stringClass.isAbstract)
|
|
||||||
Assert.assertTrue(stringClass.isFinal)
|
|
||||||
Assert.assertFalse(stringClass.isSealed)
|
|
||||||
|
|
||||||
val listClass = List::class
|
|
||||||
Assert.assertEquals("kotlin.collections.List", listClass.qualifiedName)
|
|
||||||
Assert.assertFalse(listClass.isData)
|
|
||||||
Assert.assertFalse(listClass.isCompanion)
|
|
||||||
Assert.assertTrue(listClass.isAbstract)
|
|
||||||
Assert.assertFalse(listClass.isFinal)
|
|
||||||
Assert.assertFalse(listClass.isSealed)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testGetRelated() {
|
|
||||||
LOG.info("Companion Object: {}", TestSubject::class.companionObject)
|
|
||||||
LOG.info("Companion Object Instance: {}", TestSubject::class.companionObjectInstance)
|
|
||||||
LOG.info("Object Instance: {}", TestObject::class.objectInstance)
|
|
||||||
|
|
||||||
Assert.assertSame(TestObject, TestObject::class.objectInstance)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testNewInstance() {
|
|
||||||
val listClass = ArrayList::class
|
|
||||||
|
|
||||||
val list = listClass.createInstance()
|
|
||||||
Assert.assertTrue(list is ArrayList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Ignore
|
|
||||||
fun testMembers() {
|
|
||||||
val bigDecimalClass = BigDecimal::class
|
|
||||||
|
|
||||||
LOG.info("Constructors: {}", bigDecimalClass.constructors)
|
|
||||||
LOG.info("Functions: {}", bigDecimalClass.functions)
|
|
||||||
LOG.info("Properties: {}", bigDecimalClass.memberProperties)
|
|
||||||
LOG.info("Extension Functions: {}", bigDecimalClass.memberExtensionFunctions)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestSubject {
|
|
||||||
companion object {
|
|
||||||
val name = "TestSubject"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
object TestObject {
|
|
||||||
val answer = 42
|
|
||||||
}
|
|
@ -1,88 +0,0 @@
|
|||||||
package com.baeldung.reflection
|
|
||||||
|
|
||||||
import org.junit.Assert
|
|
||||||
import org.junit.Test
|
|
||||||
import java.io.ByteArrayInputStream
|
|
||||||
import java.nio.charset.Charset
|
|
||||||
import kotlin.reflect.KMutableProperty
|
|
||||||
import kotlin.reflect.full.starProjectedType
|
|
||||||
|
|
||||||
class KMethodTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testCallMethod() {
|
|
||||||
val str = "Hello"
|
|
||||||
val lengthMethod = str::length
|
|
||||||
|
|
||||||
Assert.assertEquals(5, lengthMethod())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testReturnType() {
|
|
||||||
val str = "Hello"
|
|
||||||
val method = str::byteInputStream
|
|
||||||
|
|
||||||
Assert.assertEquals(ByteArrayInputStream::class.starProjectedType, method.returnType)
|
|
||||||
Assert.assertFalse(method.returnType.isMarkedNullable)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testParams() {
|
|
||||||
val str = "Hello"
|
|
||||||
val method = str::byteInputStream
|
|
||||||
|
|
||||||
method.isSuspend
|
|
||||||
Assert.assertEquals(1, method.parameters.size)
|
|
||||||
Assert.assertTrue(method.parameters[0].isOptional)
|
|
||||||
Assert.assertFalse(method.parameters[0].isVararg)
|
|
||||||
Assert.assertEquals(Charset::class.starProjectedType, method.parameters[0].type)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testMethodDetails() {
|
|
||||||
val codePoints = String::codePoints
|
|
||||||
Assert.assertEquals("codePoints", codePoints.name)
|
|
||||||
Assert.assertFalse(codePoints.isSuspend)
|
|
||||||
Assert.assertFalse(codePoints.isExternal)
|
|
||||||
Assert.assertFalse(codePoints.isInline)
|
|
||||||
Assert.assertFalse(codePoints.isOperator)
|
|
||||||
|
|
||||||
val byteInputStream = String::byteInputStream
|
|
||||||
Assert.assertEquals("byteInputStream", byteInputStream.name)
|
|
||||||
Assert.assertFalse(byteInputStream.isSuspend)
|
|
||||||
Assert.assertFalse(byteInputStream.isExternal)
|
|
||||||
Assert.assertTrue(byteInputStream.isInline)
|
|
||||||
Assert.assertFalse(byteInputStream.isOperator)
|
|
||||||
}
|
|
||||||
|
|
||||||
val readOnlyProperty: Int = 42
|
|
||||||
lateinit var mutableProperty: String
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testPropertyDetails() {
|
|
||||||
val roProperty = this::readOnlyProperty
|
|
||||||
Assert.assertEquals("readOnlyProperty", roProperty.name)
|
|
||||||
Assert.assertFalse(roProperty.isLateinit)
|
|
||||||
Assert.assertFalse(roProperty.isConst)
|
|
||||||
Assert.assertFalse(roProperty is KMutableProperty<*>)
|
|
||||||
|
|
||||||
val mProperty = this::mutableProperty
|
|
||||||
Assert.assertEquals("mutableProperty", mProperty.name)
|
|
||||||
Assert.assertTrue(mProperty.isLateinit)
|
|
||||||
Assert.assertFalse(mProperty.isConst)
|
|
||||||
Assert.assertTrue(mProperty is KMutableProperty<*>)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testProperty() {
|
|
||||||
val prop = this::mutableProperty
|
|
||||||
|
|
||||||
Assert.assertEquals(String::class.starProjectedType, prop.getter.returnType)
|
|
||||||
|
|
||||||
prop.set("Hello")
|
|
||||||
Assert.assertEquals("Hello", prop.get())
|
|
||||||
|
|
||||||
prop.setter("World")
|
|
||||||
Assert.assertEquals("World", prop.getter())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,123 +0,0 @@
|
|||||||
package com.baeldung.regex
|
|
||||||
|
|
||||||
import org.junit.Test
|
|
||||||
import kotlin.test.*
|
|
||||||
|
|
||||||
class RegexTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenRegexIsInstantiated_thenIsEqualToToRegexMethod() {
|
|
||||||
val pattern = """a([bc]+)d?\\"""
|
|
||||||
|
|
||||||
assertEquals(Regex.fromLiteral(pattern).pattern, pattern)
|
|
||||||
assertEquals(pattern, Regex(pattern).pattern)
|
|
||||||
assertEquals(pattern, pattern.toRegex().pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenRegexMatches_thenResultIsTrue() {
|
|
||||||
val regex = """a([bc]+)d?""".toRegex()
|
|
||||||
|
|
||||||
assertTrue(regex.containsMatchIn("xabcdy"))
|
|
||||||
assertTrue(regex.matches("abcd"))
|
|
||||||
assertFalse(regex matches "xabcdy")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenCompletelyMatchingRegex_whenMatchResult_thenDestructuring() {
|
|
||||||
val regex = """a([bc]+)d?""".toRegex()
|
|
||||||
|
|
||||||
assertNull(regex.matchEntire("xabcdy"))
|
|
||||||
|
|
||||||
val matchResult = regex.matchEntire("abbccbbd")
|
|
||||||
|
|
||||||
assertNotNull(matchResult)
|
|
||||||
assertEquals(matchResult!!.value, matchResult.groupValues[0])
|
|
||||||
assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1))
|
|
||||||
assertEquals("bbccbb", matchResult.destructured.component1())
|
|
||||||
assertNull(matchResult.next())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenPartiallyMatchingRegex_whenMatchResult_thenGroups() {
|
|
||||||
val regex = """a([bc]+)d?""".toRegex()
|
|
||||||
var matchResult = regex.find("abcb abbd")
|
|
||||||
|
|
||||||
assertNotNull(matchResult)
|
|
||||||
assertEquals(matchResult!!.value, matchResult.groupValues[0])
|
|
||||||
assertEquals("abcb", matchResult.value)
|
|
||||||
assertEquals(IntRange(0, 3), matchResult.range)
|
|
||||||
assertEquals(listOf("abcb", "bcb"), matchResult.groupValues)
|
|
||||||
assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1))
|
|
||||||
|
|
||||||
matchResult = matchResult.next()
|
|
||||||
|
|
||||||
assertNotNull(matchResult)
|
|
||||||
assertEquals("abbd", matchResult!!.value)
|
|
||||||
assertEquals("bb", matchResult.groupValues[1])
|
|
||||||
|
|
||||||
matchResult = matchResult.next()
|
|
||||||
|
|
||||||
assertNull(matchResult)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenPartiallyMatchingRegex_whenMatchResult_thenDestructuring() {
|
|
||||||
val regex = """([\w\s]+) is (\d+) years old""".toRegex()
|
|
||||||
val matchResult = regex.find("Mickey Mouse is 95 years old")
|
|
||||||
val (name, age) = matchResult!!.destructured
|
|
||||||
|
|
||||||
assertEquals("Mickey Mouse", name)
|
|
||||||
assertEquals("95", age)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenNonMatchingRegex_whenFindCalled_thenNull() {
|
|
||||||
val regex = """a([bc]+)d?""".toRegex()
|
|
||||||
val matchResult = regex.find("foo")
|
|
||||||
|
|
||||||
assertNull(matchResult)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenNonMatchingRegex_whenFindAllCalled_thenEmptySet() {
|
|
||||||
val regex = """a([bc]+)d?""".toRegex()
|
|
||||||
val matchResults = regex.findAll("foo")
|
|
||||||
|
|
||||||
assertNotNull(matchResults)
|
|
||||||
assertTrue(matchResults.none())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenReplace_thenReplacement() {
|
|
||||||
val regex = """(red|green|blue)""".toRegex()
|
|
||||||
val beautiful = "Roses are red, Violets are blue"
|
|
||||||
val grim = regex.replace(beautiful, "dark")
|
|
||||||
val shiny = regex.replaceFirst(beautiful, "rainbow")
|
|
||||||
|
|
||||||
assertEquals("Roses are dark, Violets are dark", grim)
|
|
||||||
assertEquals("Roses are rainbow, Violets are blue", shiny)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenComplexReplace_thenReplacement() {
|
|
||||||
val regex = """(red|green|blue)""".toRegex()
|
|
||||||
val beautiful = "Roses are red, Violets are blue"
|
|
||||||
val reallyBeautiful = regex.replace(beautiful) {
|
|
||||||
matchResult -> matchResult.value.toUpperCase() + "!"
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals("Roses are RED!, Violets are BLUE!", reallyBeautiful)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenSplit_thenList() {
|
|
||||||
val regex = """\W+""".toRegex()
|
|
||||||
val beautiful = "Roses are red, Violets are blue"
|
|
||||||
|
|
||||||
assertEquals(listOf("Roses", "are", "red", "Violets", "are", "blue"), regex.split(beautiful))
|
|
||||||
assertEquals(listOf("Roses", "are", "red", "Violets are blue"), regex.split(beautiful, 4))
|
|
||||||
assertEquals(regex.toPattern().split(beautiful).asList(), regex.split(beautiful))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
## Core Kotlin Annotations
|
|
||||||
|
|
||||||
This module contains articles about core Kotlin annotations.
|
|
||||||
|
|
||||||
### Relevant articles:
|
|
||||||
- [Kotlin Annotations](https://www.baeldung.com/kotlin-annotations)
|
|
||||||
- [Guide to Kotlin @JvmField](https://www.baeldung.com/kotlin-jvm-field-annotation)
|
|
||||||
- [Guide to JVM Platform Annotations in Kotlin](https://www.baeldung.com/kotlin-jvm-annotations)
|
|
@ -1,41 +0,0 @@
|
|||||||
<?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-annotations</artifactId>
|
|
||||||
<name>core-kotlin-annotations</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.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>
|
|
||||||
<assertj.version>3.10.0</assertj.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.baeldung.annotations
|
|
||||||
|
|
||||||
@Target(AnnotationTarget.FIELD)
|
|
||||||
annotation class Positive
|
|
||||||
|
|
||||||
@Target(AnnotationTarget.FIELD)
|
|
||||||
annotation class AllowedNames(val names: Array<String>)
|
|
@ -1,3 +0,0 @@
|
|||||||
package com.baeldung.annotations
|
|
||||||
|
|
||||||
class Item(@Positive val amount: Float, @AllowedNames(["Alice", "Bob"]) val name: String)
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.baeldung.annotations
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
|
||||||
val item = Item(amount = 1.0f, name = "Bob")
|
|
||||||
val validator = Validator()
|
|
||||||
println("Is instance valid? ${validator.isValid(item)}")
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package com.baeldung.annotations
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Naive annotation-based validator.
|
|
||||||
* @author A.Shcherbakov
|
|
||||||
*/
|
|
||||||
class Validator() {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return true if every item's property annotated with @Positive is positive and if
|
|
||||||
* every item's property annotated with @AllowedNames has a value specified in that annotation.
|
|
||||||
*/
|
|
||||||
fun isValid(item: Item): Boolean {
|
|
||||||
val fields = item::class.java.declaredFields
|
|
||||||
for (field in fields) {
|
|
||||||
field.isAccessible = true
|
|
||||||
for (annotation in field.annotations) {
|
|
||||||
val value = field.get(item)
|
|
||||||
if (field.isAnnotationPresent(Positive::class.java)) {
|
|
||||||
val amount = value as Float
|
|
||||||
if (amount < 0) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (field.isAnnotationPresent(AllowedNames::class.java)) {
|
|
||||||
val allowedNames = field.getAnnotation(AllowedNames::class.java)?.names
|
|
||||||
val name = value as String
|
|
||||||
allowedNames?.let {
|
|
||||||
if (!it.contains(name)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package com.baeldung.jvmannotations
|
|
||||||
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
interface Document {
|
|
||||||
|
|
||||||
@JvmDefault
|
|
||||||
fun getTypeDefault() = "document"
|
|
||||||
|
|
||||||
fun getType() = "document"
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package com.baeldung.jvmannotations;
|
|
||||||
|
|
||||||
public class HtmlDocument implements Document {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getType() {
|
|
||||||
return "HTML";
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,66 +0,0 @@
|
|||||||
@file:JvmName("MessageHelper")
|
|
||||||
@file:JvmMultifileClass //used
|
|
||||||
package com.baeldung.jvmannotations
|
|
||||||
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
@JvmName("getMyUsername")
|
|
||||||
fun getMyName() : String {
|
|
||||||
return "myUserId"
|
|
||||||
}
|
|
||||||
|
|
||||||
object MessageBroker {
|
|
||||||
@JvmStatic
|
|
||||||
var totalMessagesSent = 0
|
|
||||||
|
|
||||||
const val maxMessageLength = 0
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun clearAllMessages() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
@JvmOverloads
|
|
||||||
@Throws(Exception::class)
|
|
||||||
fun findMessages(sender : String, type : String = "text", maxResults : Int = 10) : List<Message> {
|
|
||||||
if(sender.isEmpty()) {
|
|
||||||
throw Exception()
|
|
||||||
}
|
|
||||||
return ArrayList()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Message {
|
|
||||||
|
|
||||||
// this would cause a compilation error since sender is immutable
|
|
||||||
// @set:JvmName("setSender")
|
|
||||||
val sender = "myself"
|
|
||||||
|
|
||||||
// this works as name is overridden
|
|
||||||
@JvmName("getSenderName")
|
|
||||||
fun getSender() : String = "from:$sender"
|
|
||||||
|
|
||||||
@get:JvmName("getReceiverName")
|
|
||||||
@set:JvmName("setReceiverName")
|
|
||||||
var receiver : String = ""
|
|
||||||
|
|
||||||
@get:JvmName("getContent")
|
|
||||||
@set:JvmName("setContent")
|
|
||||||
var text = ""
|
|
||||||
|
|
||||||
// generates a warning
|
|
||||||
@get:JvmName("getId")
|
|
||||||
private val id = 0
|
|
||||||
|
|
||||||
@get:JvmName("hasAttachment")
|
|
||||||
var hasAttachment = true
|
|
||||||
|
|
||||||
var isEncrypted = true
|
|
||||||
|
|
||||||
fun setReceivers(receiverNames : List<String>) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmName("setReceiverIds")
|
|
||||||
fun setReceivers(receiverNames : List<Int>) {
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
@file:JvmMultifileClass
|
|
||||||
@file:JvmName("MessageHelper") //applies to all top level functions / variables / constants
|
|
||||||
package com.baeldung.jvmannotations
|
|
||||||
|
|
||||||
fun convert(message: Message) {
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
package com.baeldung.jvmannotations
|
|
||||||
|
|
||||||
import java.util.*
|
|
||||||
class TextDocument : Document {
|
|
||||||
override fun getType() = "text"
|
|
||||||
|
|
||||||
fun transformList(list : List<Number>) : List<Number> {
|
|
||||||
return list.filter { n -> n.toInt() > 1 }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun transformListInverseWildcards(list : List<@JvmSuppressWildcards Number>) : List<@JvmWildcard Number> {
|
|
||||||
return list.filter { n -> n.toInt() > 1 }
|
|
||||||
}
|
|
||||||
|
|
||||||
var list : List<@JvmWildcard Any> = ArrayList()
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
package com.baeldung.jvmannotations
|
|
||||||
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
class XmlDocument(d : Document) : Document by d
|
|
@ -1,12 +0,0 @@
|
|||||||
package com.baeldung.jvmfield
|
|
||||||
|
|
||||||
class JvmSample(text:String) {
|
|
||||||
@JvmField
|
|
||||||
val sampleText:String = text
|
|
||||||
}
|
|
||||||
|
|
||||||
class CompanionSample {
|
|
||||||
companion object {
|
|
||||||
@JvmField val MAX_LIMIT = 20
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
package com.baeldung.annotations
|
|
||||||
|
|
||||||
import org.junit.Test
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
import kotlin.test.assertFalse
|
|
||||||
|
|
||||||
class ValidationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenAmountIsOneAndNameIsAlice_thenTrue() {
|
|
||||||
assertTrue(Validator().isValid(Item(1f, "Alice")))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenAmountIsOneAndNameIsBob_thenTrue() {
|
|
||||||
assertTrue(Validator().isValid(Item(1f, "Bob")))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenAmountIsMinusOneAndNameIsAlice_thenFalse() {
|
|
||||||
assertFalse(Validator().isValid(Item(-1f, "Alice")))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenAmountIsMinusOneAndNameIsBob_thenFalse() {
|
|
||||||
assertFalse(Validator().isValid(Item(-1f, "Bob")))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenAmountIsOneAndNameIsTom_thenFalse() {
|
|
||||||
assertFalse(Validator().isValid(Item(1f, "Tom")))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenAmountIsMinusOneAndNameIsTom_thenFalse() {
|
|
||||||
assertFalse(Validator().isValid(Item(-1f, "Tom")))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package com.baeldung.range
|
|
||||||
|
|
||||||
import org.junit.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
import com.baeldung.jvmannotations.*;
|
|
||||||
|
|
||||||
class DocumentTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testDefaultMethod() {
|
|
||||||
|
|
||||||
val myDocument = TextDocument()
|
|
||||||
val myTextDocument = XmlDocument(myDocument)
|
|
||||||
|
|
||||||
assertEquals("text", myDocument.getType())
|
|
||||||
assertEquals("text", myTextDocument.getType())
|
|
||||||
assertEquals("document", myTextDocument.getTypeDefault())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package com.baeldung.jvmfield
|
|
||||||
|
|
||||||
import org.junit.Before
|
|
||||||
import org.junit.Test
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
class JvmSampleTest {
|
|
||||||
|
|
||||||
var sample = ""
|
|
||||||
|
|
||||||
@Before
|
|
||||||
fun setUp() {
|
|
||||||
sample = JvmSample("Hello!").sampleText
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenField_whenCheckValue_thenMatchesValue() {
|
|
||||||
assertTrue(sample == "Hello!")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenStaticVariable_whenCheckValue_thenMatchesValue() {
|
|
||||||
// Sample when is treated as a static variable
|
|
||||||
assertTrue(CompanionSample.MAX_LIMIT == 20)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
## Core Kotlin Collections
|
|
||||||
|
|
||||||
This module contains articles about core Kotlin collections.
|
|
||||||
|
|
||||||
## Relevant articles:
|
|
||||||
|
|
||||||
- [Aggregate Operations in Kotlin](https://www.baeldung.com/kotlin/aggregate-operations)
|
|
@ -1,47 +0,0 @@
|
|||||||
<?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>
|
|
@ -1,107 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,104 +0,0 @@
|
|||||||
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())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
## Core Kotlin Collections
|
|
||||||
|
|
||||||
This module contains articles about core Kotlin collections.
|
|
||||||
|
|
||||||
### Relevant articles:
|
|
||||||
|
|
||||||
- [Split a List Into Parts in Kotlin](https://www.baeldung.com/kotlin-split-list-into-parts)
|
|
||||||
- [Finding an Element in a List Using Kotlin](https://www.baeldung.com/kotlin-finding-element-in-list)
|
|
||||||
- [Overview of Kotlin Collections API](https://www.baeldung.com/kotlin-collections-api)
|
|
||||||
- [Converting a List to Map in Kotlin](https://www.baeldung.com/kotlin-list-to-map)
|
|
||||||
- [Filtering Kotlin Collections](https://www.baeldung.com/kotlin-filter-collection)
|
|
||||||
- [Collection Transformations in Kotlin](https://www.baeldung.com/kotlin-collection-transformations)
|
|
||||||
- [Difference between fold and reduce in Kotlin](https://www.baeldung.com/kotlin/fold-vs-reduce)
|
|
||||||
- [Guide to Sorting in Kotlin](https://www.baeldung.com/kotlin-sort)
|
|
||||||
- [Working With Lists in Kotlin](https://www.baeldung.com/kotlin/lists)
|
|
||||||
- [Iterating Collections by Index in Kotlin](https://www.baeldung.com/kotlin/iterating-collections-by-index)
|
|
@ -1,47 +0,0 @@
|
|||||||
<?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</artifactId>
|
|
||||||
<name>core-kotlin-collections</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>
|
|
@ -1,33 +0,0 @@
|
|||||||
package com.baeldung.index
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
|
|
||||||
// Index only
|
|
||||||
val colors = listOf("Red", "Green", "Blue")
|
|
||||||
for (i in colors.indices) {
|
|
||||||
println(colors[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
val colorArray = arrayOf("Red", "Green", "Blue")
|
|
||||||
for (i in colorArray.indices) {
|
|
||||||
println(colorArray[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
(0 until colors.size).forEach { println(colors[it]) }
|
|
||||||
for (i in 0 until colors.size) {
|
|
||||||
println(colors[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Index and Value
|
|
||||||
colors.forEachIndexed { i, v -> println("The value for index $i is $v") }
|
|
||||||
for (indexedValue in colors.withIndex()) {
|
|
||||||
println("The value for index ${indexedValue.index} is ${indexedValue.value}")
|
|
||||||
}
|
|
||||||
|
|
||||||
for ((i, v) in colors.withIndex()) {
|
|
||||||
println("The value for index $i is $v")
|
|
||||||
}
|
|
||||||
|
|
||||||
colors.filterIndexed { i, _ -> i % 2 == 0 }
|
|
||||||
colors.filterIndexed { _, v -> v == "RED" }
|
|
||||||
}
|
|
@ -1,204 +0,0 @@
|
|||||||
package com.baeldung.kotlin.collections
|
|
||||||
|
|
||||||
import kotlin.collections.List
|
|
||||||
|
|
||||||
class ListExample {
|
|
||||||
|
|
||||||
private val countries = listOf("Germany", "India", "Japan", "Brazil", "Australia")
|
|
||||||
private val cities = mutableListOf("Berlin", "Calcutta", "Seoul", "Sao Paulo", "Sydney")
|
|
||||||
|
|
||||||
fun createList(): List<String> {
|
|
||||||
val countryList = listOf("Germany", "India", "Japan", "Brazil")
|
|
||||||
return countryList
|
|
||||||
}
|
|
||||||
|
|
||||||
fun createMutableList(): MutableList<String> {
|
|
||||||
val cityList = mutableListOf("Berlin", "Calcutta", "Seoul", "Sao Paulo")
|
|
||||||
return cityList
|
|
||||||
}
|
|
||||||
|
|
||||||
fun iterateUsingForEachLoop(): List<Int> {
|
|
||||||
val countryLength = mutableListOf<Int>()
|
|
||||||
countries.forEach { it ->
|
|
||||||
print("$it ")
|
|
||||||
println(" Length: ${it.length}")
|
|
||||||
countryLength.add(it.length)
|
|
||||||
}
|
|
||||||
return countryLength
|
|
||||||
}
|
|
||||||
|
|
||||||
fun iterateUsingForLoop(): List<Int> {
|
|
||||||
val countryLength = mutableListOf<Int>()
|
|
||||||
for (country in countries) {
|
|
||||||
print("$country ")
|
|
||||||
println(" Length: ${country.length}")
|
|
||||||
countryLength.add(country.length)
|
|
||||||
}
|
|
||||||
return countryLength
|
|
||||||
}
|
|
||||||
|
|
||||||
fun iterateUsingForLoopRange(): List<Int> {
|
|
||||||
val countryLength = mutableListOf<Int>()
|
|
||||||
for (i in 0 until countries.size) {
|
|
||||||
print("${countries[i]} ")
|
|
||||||
println(" Length: ${countries[i].length}")
|
|
||||||
countryLength.add(countries[i].length)
|
|
||||||
}
|
|
||||||
return countryLength
|
|
||||||
}
|
|
||||||
|
|
||||||
fun iterateUsingForEachIndexedLoop(): List<Int> {
|
|
||||||
val countryLength = mutableListOf<Int>()
|
|
||||||
countries.forEachIndexed { i, e ->
|
|
||||||
println("country[$i] = $e")
|
|
||||||
print(" Index: $i")
|
|
||||||
println(" Length: ${e.length}")
|
|
||||||
countryLength.add(e.length)
|
|
||||||
}
|
|
||||||
return countryLength
|
|
||||||
}
|
|
||||||
|
|
||||||
fun iterateUsingListIterator() {
|
|
||||||
val iterator = countries.listIterator()
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
val country = iterator.next()
|
|
||||||
print("$country ")
|
|
||||||
}
|
|
||||||
println()
|
|
||||||
|
|
||||||
while (iterator.hasPrevious()) {
|
|
||||||
println("Index: ${iterator.previousIndex()}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun iterateUsingIterator() {
|
|
||||||
val iterator = cities.iterator()
|
|
||||||
iterator.next()
|
|
||||||
iterator.remove()
|
|
||||||
println(cities)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun iterateUsingMutableListIterator() {
|
|
||||||
val iterator = cities.listIterator(1)
|
|
||||||
iterator.next()
|
|
||||||
iterator.add("London")
|
|
||||||
iterator.next()
|
|
||||||
iterator.set("Milan")
|
|
||||||
println(cities)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun retrieveElementsInList(): String {
|
|
||||||
println(countries[2])
|
|
||||||
return countries[2]
|
|
||||||
}
|
|
||||||
|
|
||||||
fun retrieveElementsUsingGet(): String {
|
|
||||||
println(countries.get(3))
|
|
||||||
return countries.get(3)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun retrieveElementsFirstAndLast(): String? {
|
|
||||||
println(countries.first())
|
|
||||||
println(countries.last())
|
|
||||||
println(countries.first { it.length > 7 })
|
|
||||||
println(countries.last { it.startsWith("J") })
|
|
||||||
println(countries.firstOrNull { it.length > 8 })
|
|
||||||
return countries.firstOrNull { it.length > 8 }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun retrieveSubList(): List<String> {
|
|
||||||
val subList = countries.subList(1, 4)
|
|
||||||
println(subList)
|
|
||||||
return subList
|
|
||||||
}
|
|
||||||
|
|
||||||
fun retrieveListSliceUsingIndices(): List<String> {
|
|
||||||
val sliceList = countries.slice(1..4)
|
|
||||||
println(sliceList)
|
|
||||||
return sliceList
|
|
||||||
}
|
|
||||||
|
|
||||||
fun retrieveListSliceUsingIndicesList(): List<String> {
|
|
||||||
val sliceList = countries.slice(listOf(1, 4))
|
|
||||||
println(sliceList)
|
|
||||||
return sliceList
|
|
||||||
}
|
|
||||||
|
|
||||||
fun countList(): Int {
|
|
||||||
val count = countries.count()
|
|
||||||
println(count)
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
fun countListUsingPredicate(): Int {
|
|
||||||
val count = countries.count { it.length > 5 }
|
|
||||||
println(count)
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
fun countListUsingProperty(): Int {
|
|
||||||
val size = countries.size
|
|
||||||
println(size)
|
|
||||||
return size
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addToList(): List<String> {
|
|
||||||
cities.add("Barcelona")
|
|
||||||
println(cities)
|
|
||||||
cities.add(3, "London")
|
|
||||||
println(cities)
|
|
||||||
cities.addAll(listOf("Singapore", "Moscow"))
|
|
||||||
println(cities)
|
|
||||||
cities.addAll(2, listOf("Prague", "Amsterdam"))
|
|
||||||
println(cities)
|
|
||||||
return cities
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeFromList(): List<String> {
|
|
||||||
cities.remove("Seoul")
|
|
||||||
println(cities)
|
|
||||||
cities.removeAt(1)
|
|
||||||
println(cities)
|
|
||||||
return cities
|
|
||||||
}
|
|
||||||
|
|
||||||
fun replaceFromList(): List<String> {
|
|
||||||
cities.set(3, "Prague")
|
|
||||||
println(cities)
|
|
||||||
cities[4] = "Moscow"
|
|
||||||
println(cities)
|
|
||||||
cities.fill("Barcelona")
|
|
||||||
println(cities)
|
|
||||||
return cities
|
|
||||||
}
|
|
||||||
|
|
||||||
fun sortMutableList(): List<String> {
|
|
||||||
cities.sort()
|
|
||||||
println(cities)
|
|
||||||
cities.sortDescending()
|
|
||||||
println(cities)
|
|
||||||
return cities
|
|
||||||
}
|
|
||||||
|
|
||||||
fun sortList(): List<String> {
|
|
||||||
val sortedCountries = countries.sorted()
|
|
||||||
println("countries = $countries")
|
|
||||||
println("sortedCountries = $sortedCountries")
|
|
||||||
val sortedCountriesDescending = countries.sortedDescending()
|
|
||||||
println("countries = $countries")
|
|
||||||
println("sortedCountriesDescending = $sortedCountriesDescending")
|
|
||||||
return sortedCountriesDescending
|
|
||||||
}
|
|
||||||
|
|
||||||
fun checkOneElementInList(): Boolean {
|
|
||||||
return countries.contains("Germany")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun checkOneElementInListUsingOperator(): Boolean {
|
|
||||||
return "Spain" in countries
|
|
||||||
}
|
|
||||||
|
|
||||||
fun checkElementsInList(): Boolean {
|
|
||||||
return cities.containsAll(listOf("Calcutta", "Sao Paulo", "Sydney"))
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
package com.baeldung.sorting
|
|
||||||
|
|
||||||
fun sortMethodUsage() {
|
|
||||||
val sortedValues = mutableListOf(1, 2, 7, 6, 5, 6)
|
|
||||||
sortedValues.sort()
|
|
||||||
println(sortedValues)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun sortByMethodUsage() {
|
|
||||||
val sortedValues = mutableListOf(1 to "a", 2 to "b", 7 to "c", 6 to "d", 5 to "c", 6 to "e")
|
|
||||||
sortedValues.sortBy { it.second }
|
|
||||||
println(sortedValues)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun sortWithMethodUsage() {
|
|
||||||
val sortedValues = mutableListOf(1 to "a", 2 to "b", 7 to "c", 6 to "d", 5 to "c", 6 to "e")
|
|
||||||
sortedValues.sortWith(compareBy({it.second}, {it.first}))
|
|
||||||
println(sortedValues)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun <T : kotlin.Comparable<T>> getSimpleComparator() : Comparator<T> {
|
|
||||||
val ascComparator = naturalOrder<T>()
|
|
||||||
return ascComparator
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getComplexComparator() {
|
|
||||||
val complexComparator = compareBy<Pair<Int, String>>({it.first}, {it.second})
|
|
||||||
}
|
|
||||||
|
|
||||||
fun nullHandlingUsage() {
|
|
||||||
val sortedValues = mutableListOf(1 to "a", 2 to null, 7 to "c", 6 to "d", 5 to "c", 6 to "e")
|
|
||||||
sortedValues.sortWith(nullsLast(compareBy { it.second }))
|
|
||||||
println(sortedValues)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun extendedComparatorUsage() {
|
|
||||||
val students = mutableListOf(21 to "Helen", 21 to "Tom", 20 to "Jim")
|
|
||||||
|
|
||||||
val ageComparator = compareBy<Pair<Int, String?>> {it.first}
|
|
||||||
val ageAndNameComparator = ageComparator.thenByDescending {it.second}
|
|
||||||
println(students.sortedWith(ageAndNameComparator))
|
|
||||||
}
|
|
@ -1,212 +0,0 @@
|
|||||||
package com.baeldung.collections
|
|
||||||
|
|
||||||
import org.assertj.core.api.Assertions.assertThat
|
|
||||||
import org.junit.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertFalse
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
class CollectionsTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenUseDifferentCollections_thenSuccess() {
|
|
||||||
val theList = listOf("one", "two", "three")
|
|
||||||
assertTrue(theList.contains("two"))
|
|
||||||
|
|
||||||
val theMutableList = mutableListOf("one", "two", "three")
|
|
||||||
theMutableList.add("four")
|
|
||||||
assertTrue(theMutableList.contains("four"))
|
|
||||||
|
|
||||||
val theSet = setOf("one", "two", "three")
|
|
||||||
assertTrue(theSet.contains("three"))
|
|
||||||
|
|
||||||
val theMutableSet = mutableSetOf("one", "two", "three")
|
|
||||||
theMutableSet.add("four")
|
|
||||||
assertTrue(theMutableSet.contains("four"))
|
|
||||||
|
|
||||||
val theMap = mapOf(1 to "one", 2 to "two", 3 to "three")
|
|
||||||
assertEquals(theMap[2], "two")
|
|
||||||
|
|
||||||
val theMutableMap = mutableMapOf(1 to "one", 2 to "two", 3 to "three")
|
|
||||||
theMutableMap[4] = "four"
|
|
||||||
assertEquals(theMutableMap[4], "four")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenSliceCollection_thenSuccess() {
|
|
||||||
val theList = listOf("one", "two", "three")
|
|
||||||
val resultList = theList.slice(1..2)
|
|
||||||
|
|
||||||
assertEquals(2, resultList.size)
|
|
||||||
assertTrue(resultList.contains("two"))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenJoinTwoCollections_thenSuccess() {
|
|
||||||
val firstList = listOf("one", "two", "three")
|
|
||||||
val secondList = listOf("four", "five", "six")
|
|
||||||
val resultList = firstList + secondList
|
|
||||||
|
|
||||||
assertEquals(6, resultList.size)
|
|
||||||
assertTrue(resultList.contains("two"))
|
|
||||||
assertTrue(resultList.contains("five"))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenFilterNullValues_thenSuccess() {
|
|
||||||
val theList = listOf("one", null, "two", null, "three")
|
|
||||||
val resultList = theList.filterNotNull()
|
|
||||||
|
|
||||||
assertEquals(3, resultList.size)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenFilterNonPositiveValues_thenSuccess() {
|
|
||||||
val theList = listOf(1, 2, -3, -4, 5, -6)
|
|
||||||
val resultList = theList.filter { it > 0 }
|
|
||||||
//val resultList = theList.filter{ x -> x > 0}
|
|
||||||
|
|
||||||
assertEquals(3, resultList.size)
|
|
||||||
assertTrue(resultList.contains(1))
|
|
||||||
assertFalse(resultList.contains(-4))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenDropFirstItems_thenRemoved() {
|
|
||||||
val theList = listOf("one", "two", "three", "four")
|
|
||||||
val resultList = theList.drop(2)
|
|
||||||
|
|
||||||
assertEquals(2, resultList.size)
|
|
||||||
assertFalse(resultList.contains("one"))
|
|
||||||
assertFalse(resultList.contains("two"))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenDropFirstItemsBasedOnCondition_thenRemoved() {
|
|
||||||
val theList = listOf("one", "two", "three", "four")
|
|
||||||
val resultList = theList.dropWhile { it.length < 4 }
|
|
||||||
|
|
||||||
assertEquals(2, resultList.size)
|
|
||||||
assertFalse(resultList.contains("one"))
|
|
||||||
assertFalse(resultList.contains("two"))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenExcludeItems_thenRemoved() {
|
|
||||||
val firstList = listOf("one", "two", "three")
|
|
||||||
val secondList = listOf("one", "three")
|
|
||||||
val resultList = firstList - secondList
|
|
||||||
|
|
||||||
assertEquals(1, resultList.size)
|
|
||||||
assertTrue(resultList.contains("two"))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenSearchForExistingItem_thenFound() {
|
|
||||||
val theList = listOf("one", "two", "three")
|
|
||||||
|
|
||||||
assertTrue("two" in theList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenGroupItems_thenSuccess() {
|
|
||||||
val theList = listOf(1, 2, 3, 4, 5, 6)
|
|
||||||
val resultMap = theList.groupBy { it % 3 }
|
|
||||||
|
|
||||||
assertEquals(3, resultMap.size)
|
|
||||||
print(resultMap[1])
|
|
||||||
assertTrue(resultMap[1]!!.contains(1))
|
|
||||||
assertTrue(resultMap[2]!!.contains(5))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenApplyFunctionToAllItems_thenSuccess() {
|
|
||||||
val theList = listOf(1, 2, 3, 4, 5, 6)
|
|
||||||
val resultList = theList.map { it * it }
|
|
||||||
print(resultList)
|
|
||||||
assertEquals(4, resultList[1])
|
|
||||||
assertEquals(9, resultList[2])
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenApplyMultiOutputFunctionToAllItems_thenSuccess() {
|
|
||||||
val theList = listOf("John", "Tom")
|
|
||||||
val resultList = theList.flatMap { it.toLowerCase().toList() }
|
|
||||||
print(resultList)
|
|
||||||
assertEquals(7, resultList.size)
|
|
||||||
assertTrue(resultList.contains('j'))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenApplyFunctionToAllItemsWithStartingValue_thenSuccess() {
|
|
||||||
val theList = listOf(1, 2, 3, 4, 5, 6)
|
|
||||||
val finalResult = theList.fold(1000, { oldResult, currentItem -> oldResult + (currentItem * currentItem) })
|
|
||||||
print(finalResult)
|
|
||||||
assertEquals(1091, finalResult)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenApplyingChunked_thenShouldBreakTheCollection() {
|
|
||||||
val theList = listOf(1, 2, 3, 4, 5)
|
|
||||||
val chunked = theList.chunked(2)
|
|
||||||
|
|
||||||
assertThat(chunked.size).isEqualTo(3)
|
|
||||||
assertThat(chunked.first()).contains(1, 2)
|
|
||||||
assertThat(chunked[1]).contains(3, 4)
|
|
||||||
assertThat(chunked.last()).contains(5)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenApplyingChunkedWithTransformation_thenShouldBreakTheCollection() {
|
|
||||||
val theList = listOf(1, 2, 3, 4, 5)
|
|
||||||
val chunked = theList.chunked(3) { it.joinToString(", ") }
|
|
||||||
|
|
||||||
assertThat(chunked.size).isEqualTo(2)
|
|
||||||
assertThat(chunked.first()).isEqualTo("1, 2, 3")
|
|
||||||
assertThat(chunked.last()).isEqualTo("4, 5")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenApplyingWindowed_thenShouldCreateSlidingWindowsOfElements() {
|
|
||||||
val theList = (1..6).toList()
|
|
||||||
val windowed = theList.windowed(3)
|
|
||||||
|
|
||||||
assertThat(windowed.size).isEqualTo(4)
|
|
||||||
assertThat(windowed.first()).contains(1, 2, 3)
|
|
||||||
assertThat(windowed[1]).contains(2, 3, 4)
|
|
||||||
assertThat(windowed[2]).contains(3, 4, 5)
|
|
||||||
assertThat(windowed.last()).contains(4, 5, 6)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenApplyingWindowedWithTwoSteps_thenShouldCreateSlidingWindowsOfElements() {
|
|
||||||
val theList = (1..6).toList()
|
|
||||||
val windowed = theList.windowed(size = 3, step = 2)
|
|
||||||
|
|
||||||
assertThat(windowed.size).isEqualTo(2)
|
|
||||||
assertThat(windowed.first()).contains(1, 2, 3)
|
|
||||||
assertThat(windowed.last()).contains(3, 4, 5)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenApplyingPartialWindowedWithTwoSteps_thenShouldCreateSlidingWindowsOfElements() {
|
|
||||||
val theList = (1..6).toList()
|
|
||||||
val windowed = theList.windowed(size = 3, step = 2, partialWindows = true)
|
|
||||||
|
|
||||||
assertThat(windowed.size).isEqualTo(3)
|
|
||||||
assertThat(windowed.first()).contains(1, 2, 3)
|
|
||||||
assertThat(windowed[1]).contains(3, 4, 5)
|
|
||||||
assertThat(windowed.last()).contains(5, 6)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenApplyingTransformingWindows_thenShouldCreateSlidingWindowsOfElements() {
|
|
||||||
val theList = (1..6).toList()
|
|
||||||
val windowed = theList.windowed(size = 3, step = 2, partialWindows = true) { it.joinToString(", ") }
|
|
||||||
|
|
||||||
assertThat(windowed.size).isEqualTo(3)
|
|
||||||
assertThat(windowed.first()).isEqualTo("1, 2, 3")
|
|
||||||
assertThat(windowed[1]).isEqualTo("3, 4, 5")
|
|
||||||
assertThat(windowed.last()).isEqualTo("5, 6")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
package com.baeldung.collections.transformations
|
|
||||||
|
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class AssociateUnitTest {
|
|
||||||
@Test
|
|
||||||
fun testToMap() {
|
|
||||||
val input = listOf(Pair("one", 1), Pair("two", 2))
|
|
||||||
val map = input.toMap()
|
|
||||||
assertEquals(mapOf("one" to 1, "two" to 2), map)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testAssociateWith() {
|
|
||||||
val inputs = listOf("Hi", "there")
|
|
||||||
val map = inputs.associateWith { k -> k.length }
|
|
||||||
assertEquals(mapOf("Hi" to 2, "there" to 5), map)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testAssociateBy() {
|
|
||||||
val inputs = listOf("Hi", "there")
|
|
||||||
val map = inputs.associateBy { v -> v.length }
|
|
||||||
assertEquals(mapOf(2 to "Hi", 5 to "there"), map)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testAssociate() {
|
|
||||||
val inputs = listOf("Hi", "there")
|
|
||||||
val map = inputs.associate { e -> Pair(e.toUpperCase(), e.reversed()) }
|
|
||||||
assertEquals(mapOf("HI" to "iH", "THERE" to "ereht"), map)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testAssociateByDuplicateKeys() {
|
|
||||||
val inputs = listOf("one", "two")
|
|
||||||
val map = inputs.associateBy { v -> v.length }
|
|
||||||
assertEquals(mapOf(3 to "two"), map)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testGroupBy() {
|
|
||||||
val inputs = listOf("one", "two", "three")
|
|
||||||
val map = inputs.groupBy { v -> v.length }
|
|
||||||
assertEquals(mapOf(3 to listOf("one", "two"), 5 to listOf("three")), map)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package com.baeldung.collections.transformations
|
|
||||||
|
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class FilterUnitTest {
|
|
||||||
@Test
|
|
||||||
fun testFilterWithLambda() {
|
|
||||||
val input = listOf(1, 2, 3, 4, 5)
|
|
||||||
val filtered = input.filter { it <= 3 }
|
|
||||||
assertEquals(listOf(1, 2, 3), filtered)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testFilterWithMethodReference() {
|
|
||||||
val input = listOf(1, 2, 3, 4, 5)
|
|
||||||
val filtered = input.filter(this::isSmall)
|
|
||||||
assertEquals(listOf(1, 2, 3), filtered)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testFilterNotWithMethodReference() {
|
|
||||||
val input = listOf(1, 2, 3, 4, 5)
|
|
||||||
val filtered = input.filterNot(this::isSmall)
|
|
||||||
assertEquals(listOf(4, 5), filtered)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testFilterIndexed() {
|
|
||||||
val input = listOf(5, 4, 3, 2, 1)
|
|
||||||
val filtered = input.filterIndexed { index, element -> index < 3 }
|
|
||||||
assertEquals(listOf(5, 4, 3), filtered)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testFilterNotNull() {
|
|
||||||
val nullable: List<String?> = listOf("Hello", null, "World")
|
|
||||||
val nonnull: List<String> = nullable.filterNotNull()
|
|
||||||
assertEquals(listOf("Hello", "World"), nonnull)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isSmall(i: Int) = i <= 3
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package com.baeldung.collections.transformations
|
|
||||||
|
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class FlattenUnitTest {
|
|
||||||
@Test
|
|
||||||
fun testFlatten() {
|
|
||||||
val inputs = listOf("one", "two", "three")
|
|
||||||
val characters = inputs.map(String::toList)
|
|
||||||
val flattened = characters.flatten();
|
|
||||||
assertEquals(listOf('o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r', 'e', 'e'), flattened)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testFlatMap() {
|
|
||||||
val inputs = listOf("one", "two", "three")
|
|
||||||
val characters = inputs.flatMap(String::toList)
|
|
||||||
assertEquals(listOf('o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r', 'e', 'e'), characters)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
package com.baeldung.collections.transformations
|
|
||||||
|
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class JoinToUnitTest {
|
|
||||||
@Test
|
|
||||||
fun testJoinToString() {
|
|
||||||
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
|
|
||||||
|
|
||||||
val simpleString = inputs.joinToString()
|
|
||||||
assertEquals("Jan, Feb, Mar, Apr, May", simpleString)
|
|
||||||
|
|
||||||
val detailedString = inputs.joinToString(separator = ",", prefix="Months: ", postfix=".")
|
|
||||||
assertEquals("Months: Jan,Feb,Mar,Apr,May.", detailedString)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testJoinToStringLimits() {
|
|
||||||
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
|
|
||||||
|
|
||||||
val simpleString = inputs.joinToString(limit = 3)
|
|
||||||
assertEquals("Jan, Feb, Mar, ...", simpleString)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testJoinToStringTransform() {
|
|
||||||
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
|
|
||||||
|
|
||||||
val simpleString = inputs.joinToString(transform = String::toUpperCase)
|
|
||||||
assertEquals("JAN, FEB, MAR, APR, MAY", simpleString)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testJoinTo() {
|
|
||||||
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
|
|
||||||
|
|
||||||
val output = StringBuilder()
|
|
||||||
output.append("My ")
|
|
||||||
.append(inputs.size)
|
|
||||||
.append(" elements: ")
|
|
||||||
inputs.joinTo(output)
|
|
||||||
|
|
||||||
assertEquals("My 5 elements: Jan, Feb, Mar, Apr, May", output.toString())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,53 +0,0 @@
|
|||||||
package com.baeldung.collections.transformations
|
|
||||||
|
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class MapUnitTest {
|
|
||||||
@Test
|
|
||||||
fun testMapWithLambda() {
|
|
||||||
val input = listOf("one", "two", "three")
|
|
||||||
|
|
||||||
val reversed = input.map { it.reversed() }
|
|
||||||
assertEquals(listOf("eno", "owt", "eerht"), reversed)
|
|
||||||
|
|
||||||
val lengths = input.map { it.length }
|
|
||||||
assertEquals(listOf(3, 3, 5), lengths)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testMapIndexed() {
|
|
||||||
val input = listOf(3, 2, 1)
|
|
||||||
val result = input.mapIndexed { index, value -> index * value }
|
|
||||||
assertEquals(listOf(0, 2, 2), result)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testMapNotNull() {
|
|
||||||
val input = listOf(1, 2, 3, 4, 5)
|
|
||||||
val smallSquares = input.mapNotNull {
|
|
||||||
if (it <= 3) {
|
|
||||||
it * it
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assertEquals(listOf(1, 4, 9), smallSquares)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun mapMapKeys() {
|
|
||||||
val inputs = mapOf("one" to 1, "two" to 2, "three" to 3)
|
|
||||||
|
|
||||||
val uppercases = inputs.mapKeys { it.key.toUpperCase() }
|
|
||||||
assertEquals(mapOf("ONE" to 1, "TWO" to 2, "THREE" to 3), uppercases)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun mapMapValues() {
|
|
||||||
val inputs = mapOf("one" to 1, "two" to 2, "three" to 3)
|
|
||||||
|
|
||||||
val squares = inputs.mapValues { it.value * it.value }
|
|
||||||
assertEquals(mapOf("one" to 1, "two" to 4, "three" to 9), squares)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
package com.baeldung.collections.transformations
|
|
||||||
|
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class ReduceUnitTest {
|
|
||||||
@Test
|
|
||||||
fun testJoinToStringAsReduce() {
|
|
||||||
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
|
|
||||||
|
|
||||||
val result = inputs.reduce { acc, next -> "$acc, $next" }
|
|
||||||
assertEquals("Jan, Feb, Mar, Apr, May", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testFoldToLength() {
|
|
||||||
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
|
|
||||||
|
|
||||||
val result = inputs.fold(0) { acc, next -> acc + next.length }
|
|
||||||
assertEquals(15, result)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package com.baeldung.collections.transformations
|
|
||||||
|
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class ZipUnitTest {
|
|
||||||
@Test
|
|
||||||
fun testZip() {
|
|
||||||
val left = listOf("one", "two", "three")
|
|
||||||
val right = listOf(1, 2, 3)
|
|
||||||
val zipped = left.zip(right)
|
|
||||||
assertEquals (listOf(Pair("one", 1), Pair("two", 2), Pair("three", 3)), zipped)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testZipShort() {
|
|
||||||
val left = listOf("one", "two")
|
|
||||||
val right = listOf(1, 2, 3)
|
|
||||||
val zipped = left.zip(right)
|
|
||||||
assertEquals (listOf(Pair("one", 1), Pair("two", 2)), zipped)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testUnzip() {
|
|
||||||
val left = listOf("one", "two", "three")
|
|
||||||
val right = listOf(1, 2, 3)
|
|
||||||
val zipped = left.zip(right)
|
|
||||||
|
|
||||||
val (newLeft, newRight) = zipped.unzip()
|
|
||||||
assertEquals(left, newLeft)
|
|
||||||
assertEquals(right, newRight)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
package com.baeldung.filter
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
|
|
||||||
internal class ChunkedTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenDNAFragmentString_whenChunking_thenProduceListOfChunks() {
|
|
||||||
val dnaFragment = "ATTCGCGGCCGCCAA"
|
|
||||||
|
|
||||||
val fragments = dnaFragment.chunked(3)
|
|
||||||
|
|
||||||
assertIterableEquals(listOf("ATT", "CGC", "GGC", "CGC", "CAA"), fragments)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenDNAString_whenChunkingWithTransformer_thenProduceTransformedList() {
|
|
||||||
val codonTable = mapOf("ATT" to "Isoleucine", "CAA" to "Glutamine", "CGC" to "Arginine", "GGC" to "Glycine")
|
|
||||||
val dnaFragment = "ATTCGCGGCCGCCAA"
|
|
||||||
|
|
||||||
val proteins = dnaFragment.chunked(3) { codon ->
|
|
||||||
codonTable[codon.toString()] ?: error("Unknown codon")
|
|
||||||
}
|
|
||||||
|
|
||||||
assertIterableEquals(listOf("Isoleucine", "Arginine", "Glycine", "Arginine", "Glutamine"), proteins)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenListOfValues_whenChunking_thenProduceListOfArrays() {
|
|
||||||
val whole = listOf(1, 4, 7, 4753, 2, 34, 62, 76, 5868, 0)
|
|
||||||
val chunks = whole.chunked(6)
|
|
||||||
|
|
||||||
val expected = listOf(listOf(1, 4, 7, 4753, 2, 34), listOf(62, 76, 5868, 0))
|
|
||||||
|
|
||||||
assertIterableEquals(expected, chunks)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,71 +0,0 @@
|
|||||||
package com.baeldung.filter
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
|
|
||||||
internal class DistinctTest {
|
|
||||||
data class SmallClass(val key: String, val num: Int)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenApplyingDistinct_thenReturnListOfNoDuplicateValues() {
|
|
||||||
val array = arrayOf(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9)
|
|
||||||
val result = array.distinct()
|
|
||||||
val expected = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
|
||||||
|
|
||||||
assertIterableEquals(expected, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenArrayOfClassObjects_whenApplyingDistinctOnClassProperty_thenReturnListDistinctOnThatValue() {
|
|
||||||
|
|
||||||
val original = arrayOf(
|
|
||||||
SmallClass("key1", 1),
|
|
||||||
SmallClass("key2", 2),
|
|
||||||
SmallClass("key3", 3),
|
|
||||||
SmallClass("key4", 3),
|
|
||||||
SmallClass("er", 9),
|
|
||||||
SmallClass("er", 10),
|
|
||||||
SmallClass("er", 11))
|
|
||||||
|
|
||||||
val actual = original.distinctBy { it.key }
|
|
||||||
|
|
||||||
val expected = listOf(
|
|
||||||
SmallClass("key1", 1),
|
|
||||||
SmallClass("key2", 2),
|
|
||||||
SmallClass("key3", 3),
|
|
||||||
SmallClass("key4", 3),
|
|
||||||
SmallClass("er", 9))
|
|
||||||
|
|
||||||
|
|
||||||
assertIterableEquals(expected, actual)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenArrayOfClassObjects_whenApplyingComplicatedSelector_thenReturnFirstElementToMatchEachSelectorValue() {
|
|
||||||
val array = arrayOf(
|
|
||||||
SmallClass("key1", 1),
|
|
||||||
SmallClass("key2", 2),
|
|
||||||
SmallClass("key3", 3),
|
|
||||||
SmallClass("key4", 3),
|
|
||||||
SmallClass("er", 9),
|
|
||||||
SmallClass("er", 10),
|
|
||||||
SmallClass("er", 11),
|
|
||||||
SmallClass("er", 11),
|
|
||||||
SmallClass("er", 91),
|
|
||||||
SmallClass("blob", 22),
|
|
||||||
SmallClass("dob", 27),
|
|
||||||
SmallClass("high", 201_434_314))
|
|
||||||
|
|
||||||
val actual = array.distinctBy { Math.floor(it.num / 10.0) }
|
|
||||||
|
|
||||||
val expected = listOf(
|
|
||||||
SmallClass("key1", 1),
|
|
||||||
SmallClass("er", 10),
|
|
||||||
SmallClass("er", 91),
|
|
||||||
SmallClass("blob", 22),
|
|
||||||
SmallClass("high", 201_434_314))
|
|
||||||
|
|
||||||
assertIterableEquals(expected, actual)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,53 +0,0 @@
|
|||||||
package com.baeldung.filter
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
|
|
||||||
internal class DropTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenDroppingFirstTwoItemsOfArray_thenTwoLess() {
|
|
||||||
val array = arrayOf(1, 2, 3, 4)
|
|
||||||
val result = array.drop(2)
|
|
||||||
val expected = listOf(3, 4)
|
|
||||||
|
|
||||||
assertIterableEquals(expected, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenDroppingMoreItemsOfArray_thenEmptyList() {
|
|
||||||
val array = arrayOf(1, 2, 3, 4)
|
|
||||||
val result = array.drop(5)
|
|
||||||
val expected = listOf<Int>()
|
|
||||||
|
|
||||||
assertIterableEquals(expected, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenArray_whenDroppingLastElement_thenReturnListWithoutLastElement() {
|
|
||||||
val array = arrayOf("1", "2", "3", "4")
|
|
||||||
val result = array.dropLast(1)
|
|
||||||
val expected = listOf("1", "2", "3")
|
|
||||||
|
|
||||||
assertIterableEquals(expected, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenArrayOfFloats_whenDroppingLastUntilPredicateIsFalse_thenReturnSubsetListOfFloats() {
|
|
||||||
val array = arrayOf(1f, 1f, 1f, 1f, 1f, 2f, 1f, 1f, 1f)
|
|
||||||
val result = array.dropLastWhile { it == 1f }
|
|
||||||
val expected = listOf(1f, 1f, 1f, 1f, 1f, 2f)
|
|
||||||
|
|
||||||
assertIterableEquals(expected, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenList_whenDroppingMoreThanAvailable_thenThrowException() {
|
|
||||||
val list = listOf('a', 'e', 'i', 'o', 'u')
|
|
||||||
val result = list.drop(6)
|
|
||||||
val expected: List<String> = listOf()
|
|
||||||
|
|
||||||
assertIterableEquals(expected, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
package com.baeldung.filter
|
|
||||||
|
|
||||||
import org.apache.commons.math3.primes.Primes
|
|
||||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
internal class FilterTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenAscendingValueMap_whenFilteringOnValue_ThenReturnSubsetOfMap() {
|
|
||||||
val originalMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3)
|
|
||||||
val filteredMap = originalMap.filter { it.value < 2 }
|
|
||||||
val expectedMap = mapOf("key1" to 1)
|
|
||||||
|
|
||||||
assertTrue { expectedMap == filteredMap }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenSeveralCollections_whenFilteringToAccumulativeList_thenListContainsAllContents() {
|
|
||||||
val array1 = arrayOf(90, 92, 93, 94, 92, 95, 93)
|
|
||||||
val array2 = sequenceOf(51, 31, 83, 674_506_111, 256_203_161, 15_485_863)
|
|
||||||
val list1 = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
|
||||||
val primes = mutableListOf<Int>()
|
|
||||||
|
|
||||||
val expected = listOf(2, 3, 5, 7, 31, 83, 15_485_863, 256_203_161, 674_506_111)
|
|
||||||
|
|
||||||
val primeCheck = { num: Int -> Primes.isPrime(num) }
|
|
||||||
|
|
||||||
array1.filterTo(primes, primeCheck)
|
|
||||||
list1.filterTo(primes, primeCheck)
|
|
||||||
array2.filterTo(primes, primeCheck)
|
|
||||||
|
|
||||||
primes.sort()
|
|
||||||
|
|
||||||
assertIterableEquals(expected, primes)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package com.baeldung.filter
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
|
||||||
import org.junit.jupiter.api.Assertions.assertThrows
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
|
|
||||||
internal class SliceTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenSlicingAnArrayWithDotRange_ThenListEqualsTheSlice() {
|
|
||||||
val original = arrayOf(1, 2, 3, 2, 1)
|
|
||||||
val actual = original.slice(1..3)
|
|
||||||
val expected = listOf(2, 3, 2)
|
|
||||||
|
|
||||||
assertIterableEquals(expected, actual)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenSlicingAnArrayWithDownToRange_thenListMadeUpOfReverseSlice() {
|
|
||||||
val original = arrayOf(1, 2, 3, 2, 1)
|
|
||||||
val actual = original.slice(3 downTo 0)
|
|
||||||
val expected = listOf(2, 3, 2, 1)
|
|
||||||
|
|
||||||
assertIterableEquals(expected, actual)
|
|
||||||
}
|
|
||||||
|
|
||||||
// From the 1.3 version of Kotlin APIs, slice doesn't return array of nulls but throw IndexOutOfBoundsException
|
|
||||||
// @Test
|
|
||||||
// fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() {
|
|
||||||
// val original = arrayOf(12, 3, 34, 4)
|
|
||||||
// val actual = original.slice(3..8)
|
|
||||||
// val expected = listOf(4, null, null, null, null, null)
|
|
||||||
//
|
|
||||||
// assertIterableEquals(expected, actual)
|
|
||||||
// }
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenSlicingBeyondRangeOfArrayWithStep_thenOutOfBoundsException() {
|
|
||||||
assertThrows(ArrayIndexOutOfBoundsException::class.java) {
|
|
||||||
val original = arrayOf(12, 3, 34, 4)
|
|
||||||
original.slice(3..8 step 2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package com.baeldung.filter
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
|
|
||||||
internal class TakeTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `given array of alternating types, when predicating on 'is String', then produce list of array up until predicate is false`() {
|
|
||||||
val originalArray = arrayOf("val1", 2, "val3", 4, "val5", 6)
|
|
||||||
val actualList = originalArray.takeWhile { it is String }
|
|
||||||
val expectedList = listOf("val1")
|
|
||||||
|
|
||||||
assertIterableEquals(expectedList, actualList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `given array of alternating types, when taking 4 items, then produce list of first 4 items`() {
|
|
||||||
val originalArray = arrayOf("val1", 2, "val3", 4, "val5", 6)
|
|
||||||
val actualList = originalArray.take(4)
|
|
||||||
val expectedList = listOf("val1", 2, "val3", 4)
|
|
||||||
|
|
||||||
println(originalArray.drop(4))
|
|
||||||
println(actualList)
|
|
||||||
|
|
||||||
assertIterableEquals(expectedList, actualList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when taking more items than available, then return all elements`() {
|
|
||||||
val originalArray = arrayOf(1, 2)
|
|
||||||
val actual = originalArray.take(10)
|
|
||||||
val expected = listOf(1, 2)
|
|
||||||
|
|
||||||
assertIterableEquals(expected, actual)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package com.baeldung.findelement
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertFalse
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
class FindAnElementInAListUnitTest {
|
|
||||||
|
|
||||||
var batmans: List<String> = listOf("Christian Bale", "Michael Keaton", "Ben Affleck", "George Clooney")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenFindASpecificItem_thenItemIsReturned() {
|
|
||||||
//Returns the first element matching the given predicate, or null if no such element was found.
|
|
||||||
val theFirstBatman = batmans.find { actor -> "Michael Keaton".equals(actor) }
|
|
||||||
assertEquals(theFirstBatman, "Michael Keaton")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenFilterWithPredicate_thenMatchingItemsAreReturned() {
|
|
||||||
//Returns a list containing only elements matching the given predicate.
|
|
||||||
val theCoolestBatmans = batmans.filter { actor -> actor.contains("a") }
|
|
||||||
assertTrue(theCoolestBatmans.contains("Christian Bale") && theCoolestBatmans.contains("Michael Keaton"))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenFilterNotWithPredicate_thenMatchingItemsAreReturned() {
|
|
||||||
//Returns a list containing only elements not matching the given predicate.
|
|
||||||
val theMehBatmans = batmans.filterNot { actor -> actor.contains("a") }
|
|
||||||
assertFalse(theMehBatmans.contains("Christian Bale") && theMehBatmans.contains("Michael Keaton"))
|
|
||||||
assertTrue(theMehBatmans.contains("Ben Affleck") && theMehBatmans.contains("George Clooney"))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
package com.baeldung.foldvsreduce
|
|
||||||
|
|
||||||
import org.junit.Test
|
|
||||||
import org.junit.jupiter.api.assertThrows
|
|
||||||
import java.lang.RuntimeException
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
class FoldAndReduceTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testReduceLimitations() {
|
|
||||||
val numbers: List<Int> = listOf(1, 2, 3)
|
|
||||||
val sum: Number = numbers.reduce { acc, next -> acc + next }
|
|
||||||
assertEquals(6, sum)
|
|
||||||
|
|
||||||
val emptyList = listOf<Int>()
|
|
||||||
assertThrows<RuntimeException> { emptyList.reduce { acc, next -> acc + next } }
|
|
||||||
|
|
||||||
// doesn't compile
|
|
||||||
// val sum = numbers.reduce { acc, next -> acc.toLong() + next.toLong()}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testFold() {
|
|
||||||
|
|
||||||
val numbers: List<Int> = listOf(1, 2, 3)
|
|
||||||
val sum: Int = numbers.fold(0, { acc, next -> acc + next })
|
|
||||||
assertEquals(6, sum)
|
|
||||||
|
|
||||||
//change result type
|
|
||||||
val sumLong: Long = numbers.fold(0L, { acc, next -> acc + next.toLong() })
|
|
||||||
assertEquals(6L, sumLong)
|
|
||||||
|
|
||||||
val emptyList = listOf<Int>()
|
|
||||||
val emptySum = emptyList.fold(0, { acc, next -> acc + next })
|
|
||||||
assertEquals(0, emptySum)
|
|
||||||
|
|
||||||
//power of changing result type
|
|
||||||
val (even, odd) = numbers.fold(Pair(listOf<Int>(), listOf<Int>()), { acc, next ->
|
|
||||||
if (next % 2 == 0) Pair(acc.first + next, acc.second)
|
|
||||||
else Pair(acc.first, acc.second + next)
|
|
||||||
})
|
|
||||||
|
|
||||||
assertEquals(listOf(2), even)
|
|
||||||
assertEquals(listOf(1, 3), odd)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testVariationsOfFold() {
|
|
||||||
val numbers = listOf(1, 2, 3)
|
|
||||||
val reversed = numbers.foldRight(listOf<Int>(), { next, acc -> acc + next})
|
|
||||||
assertEquals(listOf(3,2,1), reversed)
|
|
||||||
|
|
||||||
val reversedIndexes = numbers.foldRightIndexed(listOf<Int>(), { i, _, acc -> acc + i })
|
|
||||||
assertEquals(listOf(2,1,0), reversedIndexes)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,126 +0,0 @@
|
|||||||
package com.baeldung.kotlin.collections
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
|
||||||
import kotlin.test.assertFalse
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
class ListExampleUnitTest {
|
|
||||||
|
|
||||||
private val classUnderTest: ListExample = ListExample()
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenListIsCreated_thenContainsElements() {
|
|
||||||
assertTrue(classUnderTest.createList().contains("India"))
|
|
||||||
assertTrue(classUnderTest.createMutableList().contains("Seoul"))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenIterateUsingForEachLoop_thenSuccess() {
|
|
||||||
assertEquals(7, classUnderTest.iterateUsingForEachLoop()[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenIterateUsingForLoop_thenSuccess() {
|
|
||||||
assertEquals(5, classUnderTest.iterateUsingForLoop()[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenIterateUsingForLoopRange_thenSuccess() {
|
|
||||||
assertEquals(6, classUnderTest.iterateUsingForLoopRange()[3])
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenIterateUsingForEachIndexedLoop_thenSuccess() {
|
|
||||||
assertEquals(9, classUnderTest.iterateUsingForEachIndexedLoop()[4])
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenRetrieveElementsInList_thenSuccess() {
|
|
||||||
assertEquals("Japan", classUnderTest.retrieveElementsInList())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenRetrieveElementsUsingGet_thenSuccess() {
|
|
||||||
assertEquals("Brazil", classUnderTest.retrieveElementsUsingGet())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenRetrieveElementsFirstAndLast_thenSuccess() {
|
|
||||||
assertEquals("Australia", classUnderTest.retrieveElementsFirstAndLast())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenRetrieveSubList_thenSuccess() {
|
|
||||||
assertEquals(3, classUnderTest.retrieveSubList().size)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenRetrieveListSliceUsingIndices_thenSuccess() {
|
|
||||||
assertEquals(4, classUnderTest.retrieveListSliceUsingIndices().size)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenRetrieveListSliceUsingIndicesList_thenSuccess() {
|
|
||||||
assertEquals(2, classUnderTest.retrieveListSliceUsingIndicesList().size)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenCountList_thenSuccess() {
|
|
||||||
assertEquals(5, classUnderTest.countList())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenCountListUsingPredicate_thenSuccess() {
|
|
||||||
assertEquals(3, classUnderTest.countListUsingPredicate())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenCountListUsingProperty_thenSuccess() {
|
|
||||||
assertEquals(5, classUnderTest.countListUsingProperty())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenAddToList_thenSuccess() {
|
|
||||||
assertEquals(11, classUnderTest.addToList().count())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenRemoveFromList_thenSuccess() {
|
|
||||||
val list = classUnderTest.removeFromList()
|
|
||||||
assertEquals(3, list.size)
|
|
||||||
assertEquals("Sao Paulo", list[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenReplaceFromList_thenSuccess() {
|
|
||||||
val list = classUnderTest.replaceFromList()
|
|
||||||
assertEquals(5, list.size)
|
|
||||||
assertEquals("Barcelona", list[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenSortMutableList_thenSuccess() {
|
|
||||||
assertEquals("Sydney", classUnderTest.sortMutableList()[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenSortList_thenSuccess() {
|
|
||||||
assertEquals("India", classUnderTest.sortList()[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenCheckOneElementInList_thenSuccess() {
|
|
||||||
assertTrue(classUnderTest.checkOneElementInList())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenCheckOneElementInListUsingOperator_thenSuccess() {
|
|
||||||
assertFalse(classUnderTest.checkOneElementInListUsingOperator())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenCheckElementsInList_thenSuccess() {
|
|
||||||
assertTrue(classUnderTest.checkElementsInList())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,74 +0,0 @@
|
|||||||
package com.baeldung.listtomap
|
|
||||||
|
|
||||||
import org.junit.Test
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
class ListToMapTest {
|
|
||||||
|
|
||||||
val user1 = User("John", 18, listOf("Hiking, Swimming"))
|
|
||||||
val user2 = User("Sara", 25, listOf("Chess, Board Games"))
|
|
||||||
val user3 = User("Dave", 34, listOf("Games, Racing sports"))
|
|
||||||
val user4 = User("John", 30, listOf("Reading, Poker"))
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenList_whenConvertToMap_thenResult() {
|
|
||||||
val myList = listOf(user1, user2, user3)
|
|
||||||
val myMap = myList.map { it.name to it.age }.toMap()
|
|
||||||
|
|
||||||
assertTrue(myMap.get("John") == 18)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenList_whenAssociatedBy_thenResult() {
|
|
||||||
val myList = listOf(user1, user2, user3)
|
|
||||||
val myMap = myList.associateBy({ it.name }, { it.hobbies })
|
|
||||||
|
|
||||||
assertTrue(myMap.get("John")!!.contains("Hiking, Swimming"))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenStringList_whenConvertToMap_thenResult() {
|
|
||||||
val myList = listOf("a", "b", "c")
|
|
||||||
val myMap = myList.map { it to it }.toMap()
|
|
||||||
|
|
||||||
assertTrue(myMap.get("a") == "a")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenStringList_whenAssociate_thenResult() {
|
|
||||||
val myList = listOf("a", "b", "c", "c", "b")
|
|
||||||
val myMap = myList.associate{ it to it }
|
|
||||||
|
|
||||||
assertTrue(myMap.get("a") == "a")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenStringList_whenAssociateTo_thenResult() {
|
|
||||||
val myList = listOf("a", "b", "c", "c", "b")
|
|
||||||
val myMap = mutableMapOf<String, String>()
|
|
||||||
|
|
||||||
myList.associateTo(myMap) {it to it}
|
|
||||||
|
|
||||||
assertTrue(myMap.get("a") == "a")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenStringList_whenAssociateByTo_thenResult() {
|
|
||||||
val myList = listOf(user1, user2, user3, user4)
|
|
||||||
val myMap = mutableMapOf<String, Int>()
|
|
||||||
|
|
||||||
myList.associateByTo(myMap, {it.name}, {it.age})
|
|
||||||
|
|
||||||
assertTrue(myMap.get("Dave") == 34)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun givenStringList_whenAssociateByToUser_thenResult() {
|
|
||||||
val myList = listOf(user1, user2, user3, user4)
|
|
||||||
val myMap = mutableMapOf<String, User>()
|
|
||||||
|
|
||||||
myList.associateByTo(myMap) {it.name}
|
|
||||||
|
|
||||||
assertTrue(myMap.get("Dave")!!.age == 34)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
package com.baeldung.listtomap
|
|
||||||
|
|
||||||
data class User(val name: String, val age: Int, val hobbies: List<String>)
|
|
@ -1,13 +0,0 @@
|
|||||||
package com.baeldung.sorting
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions.assertTrue
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
|
|
||||||
class SortingExampleKtTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun naturalOrderComparator_ShouldBeAscendingTest() {
|
|
||||||
val resultingList = listOf(1, 5, 6, 6, 2, 3, 4).sortedWith(getSimpleComparator())
|
|
||||||
assertTrue(listOf(1, 2, 3, 4, 5, 6, 6) == resultingList)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,99 +0,0 @@
|
|||||||
package com.baeldung.splitlist
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
class SplitListIntoPartsTest {
|
|
||||||
private val evenList = listOf(0, "a", 1, "b", 2, "c");
|
|
||||||
|
|
||||||
private val unevenList = listOf(0, "a", 1, "b", 2, "c", 3);
|
|
||||||
|
|
||||||
private fun verifyList(resultList: List<List<Any>>) {
|
|
||||||
assertEquals("[[0, a], [1, b], [2, c]]", resultList.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun verifyPartialList(resultList: List<List<Any>>) {
|
|
||||||
assertEquals("[[0, a], [1, b], [2, c], [3]]", resultList.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenChunked_thenListIsSplit() {
|
|
||||||
val resultList = evenList.chunked(2)
|
|
||||||
verifyList(resultList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenUnevenChunked_thenListIsSplit() {
|
|
||||||
val resultList = unevenList.chunked(2)
|
|
||||||
verifyPartialList(resultList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenWindowed_thenListIsSplit() {
|
|
||||||
val resultList = evenList.windowed(2, 2)
|
|
||||||
verifyList(resultList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenUnevenPartialWindowed_thenListIsSplit() {
|
|
||||||
val resultList = unevenList.windowed(2, 2, partialWindows = true)
|
|
||||||
verifyPartialList(resultList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenUnevenWindowed_thenListIsSplit() {
|
|
||||||
val resultList = unevenList.windowed(2, 2, partialWindows = false)
|
|
||||||
verifyList(resultList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenGroupByWithAscendingNumbers_thenListIsSplit() {
|
|
||||||
val numberList = listOf(1, 2, 3, 4, 5, 6);
|
|
||||||
val resultList = numberList.groupBy { (it + 1) / 2 }
|
|
||||||
assertEquals("[[1, 2], [3, 4], [5, 6]]", resultList.values.toString())
|
|
||||||
assertEquals("[1, 2, 3]", resultList.keys.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenGroupByWithAscendingNumbersUneven_thenListIsSplit() {
|
|
||||||
val numberList = listOf(1, 2, 3, 4, 5, 6, 7);
|
|
||||||
val resultList = numberList.groupBy { (it + 1) / 2 }.values
|
|
||||||
assertEquals("[[1, 2], [3, 4], [5, 6], [7]]", resultList.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenGroupByWithRandomNumbers_thenListIsSplitInWrongWay() {
|
|
||||||
val numberList = listOf(1, 3, 8, 20, 23, 30);
|
|
||||||
val resultList = numberList.groupBy { (it + 1) / 2 }
|
|
||||||
assertEquals("[[1], [3], [8], [20], [23], [30]]", resultList.values.toString())
|
|
||||||
assertEquals("[1, 2, 4, 10, 12, 15]", resultList.keys.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenWithIndexGroupBy_thenListIsSplit() {
|
|
||||||
val resultList = evenList.withIndex()
|
|
||||||
.groupBy { it.index / 2 }
|
|
||||||
.map { it.value.map { it.value } }
|
|
||||||
verifyList(resultList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenWithIndexGroupByUneven_thenListIsSplit() {
|
|
||||||
val resultList = unevenList.withIndex()
|
|
||||||
.groupBy { it.index / 2 }
|
|
||||||
.map { it.value.map { it.value } }
|
|
||||||
verifyPartialList(resultList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenFoldIndexed_thenListIsSplit() {
|
|
||||||
val resultList = evenList.foldIndexed(ArrayList<ArrayList<Any>>(evenList.size / 2)) { index, acc, item ->
|
|
||||||
if (index % 2 == 0) {
|
|
||||||
acc.add(ArrayList(2))
|
|
||||||
}
|
|
||||||
acc.last().add(item)
|
|
||||||
acc
|
|
||||||
}
|
|
||||||
verifyList(resultList)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
## Core Kotlin Concurrency
|
|
||||||
|
|
||||||
This module contains articles about concurrency in Kotlin.
|
|
||||||
|
|
||||||
### Relevant articles:
|
|
||||||
- [Threads vs Coroutines in Kotlin](https://www.baeldung.com/kotlin-threads-coroutines)
|
|
||||||
- [Introduction to Kotlin Coroutines](https://www.baeldung.com/kotlin-coroutines)
|
|
@ -1,41 +0,0 @@
|
|||||||
<?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-concurrency</artifactId>
|
|
||||||
<name>core-kotlin-concurrency</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.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>
|
|
||||||
<assertj.version>3.10.0</assertj.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
|
@ -1,29 +0,0 @@
|
|||||||
package com.baeldung.channels
|
|
||||||
|
|
||||||
import kotlinx.coroutines.cancelChildren
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
|
|
||||||
fun main() = runBlocking {
|
|
||||||
val basket = Channel<String>(1)
|
|
||||||
|
|
||||||
launch { // coroutine1
|
|
||||||
val fruits = listOf("Apple", "Orange", "Banana")
|
|
||||||
for (fruit in fruits) {
|
|
||||||
println("coroutine1: Sending $fruit")
|
|
||||||
basket.send(fruit)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
launch { // coroutine2
|
|
||||||
repeat(3) {
|
|
||||||
delay(100)
|
|
||||||
println("coroutine2: Received ${basket.receive()}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(2000)
|
|
||||||
coroutineContext.cancelChildren()
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
package com.baeldung.channels
|
|
||||||
|
|
||||||
import kotlinx.coroutines.cancelChildren
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
import kotlinx.coroutines.channels.Channel.Factory.CONFLATED
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
|
|
||||||
fun main() = runBlocking {
|
|
||||||
val basket = Channel<String>(CONFLATED)
|
|
||||||
|
|
||||||
launch { // coroutine1
|
|
||||||
val fruits = listOf("Apple", "Orange", "Banana")
|
|
||||||
for (fruit in fruits) {
|
|
||||||
println("coroutine1: Sending $fruit")
|
|
||||||
basket.send(fruit)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
launch { // coroutine2
|
|
||||||
println("coroutine2: Received ${basket.receive()}")
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(2000)
|
|
||||||
coroutineContext.cancelChildren()
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
package com.baeldung.channels
|
|
||||||
|
|
||||||
import com.baeldung.channels.OrderStatus.*
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
import kotlinx.coroutines.channels.ReceiveChannel
|
|
||||||
import kotlinx.coroutines.channels.produce
|
|
||||||
|
|
||||||
enum class OrderStatus { ORDERED, BAKED, TOPPED, SERVED }
|
|
||||||
|
|
||||||
data class PizzaOrder(val orderNumber: Int, val orderStatus: OrderStatus = ORDERED)
|
|
||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
|
||||||
fun CoroutineScope.baking(orders: ReceiveChannel<PizzaOrder>) = produce {
|
|
||||||
for (order in orders) {
|
|
||||||
delay(200)
|
|
||||||
println("Baking ${order.orderNumber}")
|
|
||||||
send(order.copy(orderStatus = BAKED))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
|
||||||
fun CoroutineScope.topping(orders: ReceiveChannel<PizzaOrder>) = produce {
|
|
||||||
for (order in orders) {
|
|
||||||
delay(50)
|
|
||||||
println("Topping ${order.orderNumber}")
|
|
||||||
send(order.copy(orderStatus = TOPPED))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
|
||||||
fun CoroutineScope.produceOrders(count: Int) = produce {
|
|
||||||
repeat(count) {
|
|
||||||
delay(50)
|
|
||||||
send(PizzaOrder(orderNumber = it + 1))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ObsoleteCoroutinesApi
|
|
||||||
@ExperimentalCoroutinesApi
|
|
||||||
fun main() = runBlocking {
|
|
||||||
val orders = produceOrders(3)
|
|
||||||
|
|
||||||
val readyOrders = topping(baking(orders))
|
|
||||||
|
|
||||||
for (order in readyOrders) {
|
|
||||||
println("Serving ${order.orderNumber}")
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(3000)
|
|
||||||
println("End!")
|
|
||||||
coroutineContext.cancelChildren()
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
package com.baeldung.channels
|
|
||||||
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
||||||
import kotlinx.coroutines.channels.ReceiveChannel
|
|
||||||
import kotlinx.coroutines.channels.produce
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
|
||||||
fun CoroutineScope.produceFruits(): ReceiveChannel<String> = produce {
|
|
||||||
val fruits = listOf("Apple", "Orange", "Apple")
|
|
||||||
for (fruit in fruits) send(fruit)
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
|
||||||
fun main() = runBlocking {
|
|
||||||
val fruitChannel = produceFruits()
|
|
||||||
for (fruit in fruitChannel) {
|
|
||||||
println(fruit)
|
|
||||||
}
|
|
||||||
println("End!")
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package com.baeldung.channels
|
|
||||||
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
|
|
||||||
fun main() = runBlocking {
|
|
||||||
val basket = Channel<String>()
|
|
||||||
|
|
||||||
launch { // coroutine1
|
|
||||||
val fruits = listOf("Apple", "Orange", "Banana")
|
|
||||||
for (fruit in fruits) {
|
|
||||||
println("coroutine1: Sending $fruit")
|
|
||||||
basket.send(fruit)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
launch { // coroutine2
|
|
||||||
repeat(3) {
|
|
||||||
delay(100)
|
|
||||||
println("coroutine2: Received ${basket.receive()}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(2000)
|
|
||||||
coroutineContext.cancelChildren()
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
package com.baeldung.channels
|
|
||||||
|
|
||||||
import kotlinx.coroutines.cancelChildren
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
import kotlinx.coroutines.channels.SendChannel
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
|
|
||||||
suspend fun fetchYoutubeVideos(channel: SendChannel<String>) {
|
|
||||||
val videos = listOf("cat video", "food video")
|
|
||||||
for (video in videos) {
|
|
||||||
delay(100)
|
|
||||||
channel.send(video)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun fetchTweets(channel: SendChannel<String>) {
|
|
||||||
val tweets = listOf("tweet: Earth is round", "tweet: Coroutines and channels are cool")
|
|
||||||
for (tweet in tweets) {
|
|
||||||
delay(100)
|
|
||||||
channel.send(tweet)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun main() = runBlocking {
|
|
||||||
val aggregate = Channel<String>()
|
|
||||||
launch { fetchYoutubeVideos(aggregate) }
|
|
||||||
launch { fetchTweets(aggregate) }
|
|
||||||
|
|
||||||
repeat(4) {
|
|
||||||
println(aggregate.receive())
|
|
||||||
}
|
|
||||||
|
|
||||||
coroutineContext.cancelChildren()
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
package com.baeldung.channels
|
|
||||||
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
import kotlinx.coroutines.channels.ReceiveChannel
|
|
||||||
import kotlinx.coroutines.channels.produce
|
|
||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
|
||||||
fun CoroutineScope.producePizzaOrders(): ReceiveChannel<String> = produce {
|
|
||||||
var x = 1
|
|
||||||
while (true) {
|
|
||||||
send("Pizza Order No. ${x++}")
|
|
||||||
delay(100)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun CoroutineScope.pizzaOrderProcessor(id: Int, orders: ReceiveChannel<String>) = launch {
|
|
||||||
for (order in orders) {
|
|
||||||
println("Processor #$id is processing $order")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
|
||||||
fun main() = runBlocking {
|
|
||||||
val pizzaOrders = producePizzaOrders()
|
|
||||||
repeat(3) {
|
|
||||||
pizzaOrderProcessor(it + 1, pizzaOrders)
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(1000)
|
|
||||||
pizzaOrders.cancel()
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
package com.baeldung.channels
|
|
||||||
|
|
||||||
import kotlinx.coroutines.channels.ticker
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import java.time.Duration
|
|
||||||
import kotlin.random.Random
|
|
||||||
|
|
||||||
fun stockPrice(stock: String): Double {
|
|
||||||
log("Fetching stock price of $stock")
|
|
||||||
return Random.nextDouble(2.0, 3.0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun main() = runBlocking {
|
|
||||||
val tickerChannel = ticker(Duration.ofSeconds(5).toMillis())
|
|
||||||
|
|
||||||
repeat(3) {
|
|
||||||
tickerChannel.receive()
|
|
||||||
log(stockPrice("TESLA"))
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(Duration.ofSeconds(11).toMillis())
|
|
||||||
tickerChannel.cancel()
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
package com.baeldung.channels
|
|
||||||
|
|
||||||
import kotlinx.coroutines.cancelChildren
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
|
|
||||||
fun main() = runBlocking {
|
|
||||||
val channel = Channel<Int>(UNLIMITED)
|
|
||||||
|
|
||||||
launch { // coroutine1
|
|
||||||
repeat(100) {
|
|
||||||
println("coroutine1: Sending $it")
|
|
||||||
channel.send(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
launch { // coroutine2
|
|
||||||
repeat(100) {
|
|
||||||
println("coroutine2: Received ${channel.receive()}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(2000)
|
|
||||||
coroutineContext.cancelChildren()
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user