Merge branch 'master' of https://github.com/eugenp/tutorials into future-vavr
This commit is contained in:
commit
31a0f7b817
|
@ -1,258 +1,276 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>core-java-8</artifactId>
|
<artifactId>core-java-8</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>core-java-8</name>
|
<name>core-java-8</name>
|
||||||
|
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<!-- utils -->
|
<!-- utils -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.guava</groupId>
|
<groupId>com.google.guava</groupId>
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>${guava.version}</version>
|
<version>${guava.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-collections4</artifactId>
|
<artifactId>commons-collections4</artifactId>
|
||||||
<version>${commons-collections4.version}</version>
|
<version>${commons-collections4.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>${commons-io.version}</version>
|
<version>${commons-io.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${commons-lang3.version}</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-math3</artifactId>
|
<artifactId>commons-math3</artifactId>
|
||||||
<version>${commons-math3.version}</version>
|
<version>${commons-math3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>log4j</groupId>
|
<groupId>log4j</groupId>
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<version>1.2.17</version>
|
<version>1.2.17</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-codec</groupId>
|
<groupId>commons-codec</groupId>
|
||||||
<artifactId>commons-codec</artifactId>
|
<artifactId>commons-codec</artifactId>
|
||||||
<version>${commons-codec.version}</version>
|
<version>${commons-codec.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>${lombok.version}</version>
|
<version>${lombok.version}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.jayway.awaitility</groupId>
|
<groupId>com.jayway.awaitility</groupId>
|
||||||
<artifactId>awaitility</artifactId>
|
<artifactId>awaitility</artifactId>
|
||||||
<version>${avaitility.version}</version>
|
<version>${avaitility.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
<build>
|
<artifactId>jmh-core</artifactId>
|
||||||
<finalName>core-java-8</finalName>
|
<version>1.19</version>
|
||||||
<resources>
|
</dependency>
|
||||||
<resource>
|
|
||||||
<directory>src/main/resources</directory>
|
<dependency>
|
||||||
<filtering>true</filtering>
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
</resource>
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
</resources>
|
<version>1.19</version>
|
||||||
|
</dependency>
|
||||||
<plugins>
|
|
||||||
<plugin>
|
<dependency>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
<artifactId>maven-dependency-plugin</artifactId>
|
<artifactId>jmh-generator-bytecode</artifactId>
|
||||||
<executions>
|
<version>1.19</version>
|
||||||
<execution>
|
</dependency>
|
||||||
<id>copy-dependencies</id>
|
|
||||||
<phase>prepare-package</phase>
|
</dependencies>
|
||||||
<goals>
|
|
||||||
<goal>copy-dependencies</goal>
|
<build>
|
||||||
</goals>
|
<finalName>core-java-8</finalName>
|
||||||
<configuration>
|
<resources>
|
||||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
<resource>
|
||||||
</configuration>
|
<directory>src/main/resources</directory>
|
||||||
</execution>
|
<filtering>true</filtering>
|
||||||
</executions>
|
</resource>
|
||||||
</plugin>
|
</resources>
|
||||||
|
|
||||||
<plugin>
|
<plugins>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<plugin>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<configuration>
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
<archive>
|
<executions>
|
||||||
<manifest>
|
<execution>
|
||||||
<addClasspath>true</addClasspath>
|
<id>copy-dependencies</id>
|
||||||
<classpathPrefix>libs/</classpathPrefix>
|
<phase>prepare-package</phase>
|
||||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
<goals>
|
||||||
</manifest>
|
<goal>copy-dependencies</goal>
|
||||||
</archive>
|
</goals>
|
||||||
</configuration>
|
<configuration>
|
||||||
</plugin>
|
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||||
<plugin>
|
</configuration>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
</execution>
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
</executions>
|
||||||
<executions>
|
</plugin>
|
||||||
<execution>
|
|
||||||
<phase>package</phase>
|
<plugin>
|
||||||
<goals>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<goal>single</goal>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
</goals>
|
<configuration>
|
||||||
<configuration>
|
<archive>
|
||||||
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
|
<manifest>
|
||||||
<archive>
|
<addClasspath>true</addClasspath>
|
||||||
<manifest>
|
<classpathPrefix>libs/</classpathPrefix>
|
||||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
</manifest>
|
</manifest>
|
||||||
</archive>
|
</archive>
|
||||||
<descriptorRefs>
|
</configuration>
|
||||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
</plugin>
|
||||||
</descriptorRefs>
|
<plugin>
|
||||||
</configuration>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
</execution>
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
</executions>
|
<executions>
|
||||||
</plugin>
|
<execution>
|
||||||
<plugin>
|
<phase>package</phase>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<goals>
|
||||||
<artifactId>maven-shade-plugin</artifactId>
|
<goal>single</goal>
|
||||||
<executions>
|
</goals>
|
||||||
<execution>
|
<configuration>
|
||||||
<goals>
|
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
|
||||||
<goal>shade</goal>
|
<archive>
|
||||||
</goals>
|
<manifest>
|
||||||
<configuration>
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
</manifest>
|
||||||
<transformers>
|
</archive>
|
||||||
<transformer
|
<descriptorRefs>
|
||||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
</descriptorRefs>
|
||||||
</transformer>
|
</configuration>
|
||||||
</transformers>
|
</execution>
|
||||||
</configuration>
|
</executions>
|
||||||
</execution>
|
</plugin>
|
||||||
</executions>
|
<plugin>
|
||||||
</plugin>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<plugin>
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
<groupId>com.jolira</groupId>
|
<executions>
|
||||||
<artifactId>onejar-maven-plugin</artifactId>
|
<execution>
|
||||||
<executions>
|
<goals>
|
||||||
<execution>
|
<goal>shade</goal>
|
||||||
<configuration>
|
</goals>
|
||||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
<configuration>
|
||||||
<attachToBuild>true</attachToBuild>
|
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||||
<filename>${project.build.finalName}-onejar.${project.packaging}</filename>
|
<transformers>
|
||||||
</configuration>
|
<transformer
|
||||||
<goals>
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
<goal>one-jar</goal>
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
</goals>
|
</transformer>
|
||||||
</execution>
|
</transformers>
|
||||||
</executions>
|
</configuration>
|
||||||
</plugin>
|
</execution>
|
||||||
<plugin>
|
</executions>
|
||||||
<groupId>org.springframework.boot</groupId>
|
</plugin>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<plugin>
|
||||||
<executions>
|
<groupId>com.jolira</groupId>
|
||||||
<execution>
|
<artifactId>onejar-maven-plugin</artifactId>
|
||||||
<goals>
|
<executions>
|
||||||
<goal>repackage</goal>
|
<execution>
|
||||||
</goals>
|
<configuration>
|
||||||
<configuration>
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
<classifier>spring-boot</classifier>
|
<attachToBuild>true</attachToBuild>
|
||||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
<filename>${project.build.finalName}-onejar.${project.packaging}</filename>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
<goals>
|
||||||
</executions>
|
<goal>one-jar</goal>
|
||||||
</plugin>
|
</goals>
|
||||||
</plugins>
|
</execution>
|
||||||
|
</executions>
|
||||||
</build>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
<profiles>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<profile>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
<id>integration</id>
|
<executions>
|
||||||
<build>
|
<execution>
|
||||||
<plugins>
|
<goals>
|
||||||
<plugin>
|
<goal>repackage</goal>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
</goals>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<configuration>
|
||||||
<executions>
|
<classifier>spring-boot</classifier>
|
||||||
<execution>
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
<phase>integration-test</phase>
|
</configuration>
|
||||||
<goals>
|
</execution>
|
||||||
<goal>test</goal>
|
</executions>
|
||||||
</goals>
|
</plugin>
|
||||||
<configuration>
|
</plugins>
|
||||||
<excludes>
|
|
||||||
<exclude>**/*ManualTest.java</exclude>
|
</build>
|
||||||
</excludes>
|
|
||||||
<includes>
|
<profiles>
|
||||||
<include>**/*IntegrationTest.java</include>
|
<profile>
|
||||||
</includes>
|
<id>integration</id>
|
||||||
</configuration>
|
<build>
|
||||||
</execution>
|
<plugins>
|
||||||
</executions>
|
<plugin>
|
||||||
<configuration>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<systemPropertyVariables>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<test.mime>json</test.mime>
|
<executions>
|
||||||
</systemPropertyVariables>
|
<execution>
|
||||||
</configuration>
|
<phase>integration-test</phase>
|
||||||
</plugin>
|
<goals>
|
||||||
</plugins>
|
<goal>test</goal>
|
||||||
</build>
|
</goals>
|
||||||
</profile>
|
<configuration>
|
||||||
</profiles>
|
<excludes>
|
||||||
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
<properties>
|
</excludes>
|
||||||
|
<includes>
|
||||||
<!-- util -->
|
<include>**/*IntegrationTest.java</include>
|
||||||
<guava.version>21.0</guava.version>
|
</includes>
|
||||||
<commons-lang3.version>3.5</commons-lang3.version>
|
</configuration>
|
||||||
<commons-math3.version>3.6.1</commons-math3.version>
|
</execution>
|
||||||
<commons-io.version>2.5</commons-io.version>
|
</executions>
|
||||||
<commons-collections4.version>4.1</commons-collections4.version>
|
<configuration>
|
||||||
<collections-generic.version>4.01</collections-generic.version>
|
<systemPropertyVariables>
|
||||||
<commons-codec.version>1.10</commons-codec.version>
|
<test.mime>json</test.mime>
|
||||||
<lombok.version>1.16.12</lombok.version>
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
<!-- testing -->
|
</plugin>
|
||||||
<assertj.version>3.6.1</assertj.version>
|
</plugins>
|
||||||
<avaitility.version>1.7.0</avaitility.version>
|
</build>
|
||||||
|
</profile>
|
||||||
</properties>
|
</profiles>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
|
||||||
|
<!-- util -->
|
||||||
|
<guava.version>21.0</guava.version>
|
||||||
|
<commons-lang3.version>3.5</commons-lang3.version>
|
||||||
|
<commons-math3.version>3.6.1</commons-math3.version>
|
||||||
|
<commons-io.version>2.5</commons-io.version>
|
||||||
|
<commons-collections4.version>4.1</commons-collections4.version>
|
||||||
|
<collections-generic.version>4.01</collections-generic.version>
|
||||||
|
<commons-codec.version>1.10</commons-codec.version>
|
||||||
|
<lombok.version>1.16.12</lombok.version>
|
||||||
|
|
||||||
|
<!-- testing -->
|
||||||
|
<assertj.version>3.6.1</assertj.version>
|
||||||
|
<avaitility.version>1.7.0</avaitility.version>
|
||||||
|
|
||||||
|
</properties>
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.baeldung.counter;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
|
import org.openjdk.jmh.annotations.Fork;
|
||||||
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
|
|
||||||
|
import com.baeldung.counter.CounterUtil.MutableInteger;
|
||||||
|
|
||||||
|
@Fork(value = 1, warmups = 3)
|
||||||
|
@BenchmarkMode(Mode.All)
|
||||||
|
public class CounterStatistics {
|
||||||
|
|
||||||
|
private static final Map<String, Integer> counterMap = new HashMap<>();
|
||||||
|
private static final Map<String, MutableInteger> counterWithMutableIntMap = new HashMap<>();
|
||||||
|
private static final Map<String, int[]> counterWithIntArrayMap = new HashMap<>();
|
||||||
|
private static final Map<String, Long> counterWithLongWrapperMap = new HashMap<>();
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void wrapperAsCounter() {
|
||||||
|
CounterUtil.counterWithWrapperObject(counterMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void lambdaExpressionWithWrapper() {
|
||||||
|
CounterUtil.counterWithLambdaAndWrapper(counterWithLongWrapperMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void mutableIntegerAsCounter() {
|
||||||
|
CounterUtil.counterWithMutableInteger(counterWithMutableIntMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void primitiveArrayAsCounter() {
|
||||||
|
CounterUtil.counterWithPrimitiveArray(counterWithIntArrayMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
org.openjdk.jmh.Main.main(args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.baeldung.counter;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.counter.CounterUtil.MutableInteger;
|
||||||
|
|
||||||
|
public class CounterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMapWithWrapperAsCounter_runsSuccessfully() {
|
||||||
|
Map<String, Integer> counterMap = new HashMap<>();
|
||||||
|
CounterUtil.counterWithWrapperObject(counterMap);
|
||||||
|
|
||||||
|
assertEquals(3, counterMap.get("China")
|
||||||
|
.intValue());
|
||||||
|
assertEquals(2, counterMap.get("India")
|
||||||
|
.intValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMapWithLambdaAndWrapperCounter_runsSuccessfully() {
|
||||||
|
Map<String, Long> counterMap = new HashMap<>();
|
||||||
|
CounterUtil.counterWithLambdaAndWrapper(counterMap);
|
||||||
|
|
||||||
|
assertEquals(3l, counterMap.get("China")
|
||||||
|
.longValue());
|
||||||
|
assertEquals(2l, counterMap.get("India")
|
||||||
|
.longValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMapWithMutableIntegerCounter_runsSuccessfully() {
|
||||||
|
Map<String, MutableInteger> counterMap = new HashMap<>();
|
||||||
|
CounterUtil.counterWithMutableInteger(counterMap);
|
||||||
|
assertEquals(3, counterMap.get("China")
|
||||||
|
.getCount());
|
||||||
|
assertEquals(2, counterMap.get("India")
|
||||||
|
.getCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMapWithPrimitiveArray_runsSuccessfully() {
|
||||||
|
Map<String, int[]> counterMap = new HashMap<>();
|
||||||
|
CounterUtil.counterWithPrimitiveArray(counterMap);
|
||||||
|
assertEquals(3, counterMap.get("China")[0]);
|
||||||
|
assertEquals(2, counterMap.get("India")[0]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
package com.baeldung.counter;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class CounterUtil {
|
||||||
|
|
||||||
|
private final static String[] COUNTRY_NAMES = { "China", "Australia", "India", "USA", "USSR", "UK", "China", "France", "Poland", "Austria", "India", "USA", "Egypt", "China" };
|
||||||
|
|
||||||
|
public static void counterWithWrapperObject(Map<String, Integer> counterMap) {
|
||||||
|
for (String country : COUNTRY_NAMES) {
|
||||||
|
counterMap.compute(country, (k, v) -> v == null ? 1 : v + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void counterWithLambdaAndWrapper(Map<String, Long> counterMap) {
|
||||||
|
counterMap.putAll(Stream.of(COUNTRY_NAMES)
|
||||||
|
.parallel()
|
||||||
|
.collect(Collectors.groupingBy(k -> k, Collectors.counting())));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MutableInteger {
|
||||||
|
int count;
|
||||||
|
|
||||||
|
public MutableInteger(int count) {
|
||||||
|
this.count = count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void increment() {
|
||||||
|
this.count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCount() {
|
||||||
|
return this.count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void counterWithMutableInteger(Map<String, MutableInteger> counterMap) {
|
||||||
|
for (String country : COUNTRY_NAMES) {
|
||||||
|
MutableInteger oldValue = counterMap.get(country);
|
||||||
|
if (oldValue != null) {
|
||||||
|
oldValue.increment();
|
||||||
|
} else {
|
||||||
|
counterMap.put(country, new MutableInteger(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void counterWithPrimitiveArray(Map<String, int[]> counterMap) {
|
||||||
|
for (String country : COUNTRY_NAMES) {
|
||||||
|
int[] oldCounter = counterMap.get(country);
|
||||||
|
if (oldCounter != null) {
|
||||||
|
oldCounter[0] += 1;
|
||||||
|
} else {
|
||||||
|
counterMap.put(country, new int[] { 1 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.baeldung.kotlin
|
||||||
|
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class ExtensionMethods {
|
||||||
|
@Test
|
||||||
|
fun simpleExtensionMethod() {
|
||||||
|
fun String.escapeForXml() : String {
|
||||||
|
return this
|
||||||
|
.replace("&", "&")
|
||||||
|
.replace("<", "<")
|
||||||
|
.replace(">", ">")
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals("Nothing", "Nothing".escapeForXml())
|
||||||
|
Assert.assertEquals("<Tag>", "<Tag>".escapeForXml())
|
||||||
|
Assert.assertEquals("a&b", "a&b".escapeForXml())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun genericExtensionMethod() {
|
||||||
|
fun <T> T.concatAsString(b: T) : String {
|
||||||
|
return this.toString() + b.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals("12", "1".concatAsString("2"))
|
||||||
|
Assert.assertEquals("12", 1.concatAsString(2))
|
||||||
|
// This doesn't compile
|
||||||
|
// Assert.assertEquals("12", 1.concatAsString(2.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun infixExtensionMethod() {
|
||||||
|
infix fun Number.toPowerOf(exponent: Number): Double {
|
||||||
|
return Math.pow(this.toDouble(), exponent.toDouble())
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals(9.0, 3 toPowerOf 2, 0.1)
|
||||||
|
Assert.assertEquals(3.0, 9 toPowerOf 0.5, 0.1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun operatorExtensionMethod() {
|
||||||
|
operator fun List<Int>.times(by: Int): List<Int> {
|
||||||
|
return this.map { it * by }
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals(listOf(2, 4, 6), listOf(1, 2, 3) * 2)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,104 @@
|
||||||
|
package org.baeldung.bddmockito;
|
||||||
|
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
import static org.mockito.BDDMockito.*;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
|
||||||
|
|
||||||
|
public class BDDMockitoTest {
|
||||||
|
|
||||||
|
PhoneBookService phoneBookService;
|
||||||
|
PhoneBookRepository phoneBookRepository;
|
||||||
|
|
||||||
|
String momContactName = "Mom";
|
||||||
|
String momPhoneNumber = "01234";
|
||||||
|
String xContactName = "x";
|
||||||
|
String tooLongPhoneNumber = "01111111111111";
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
phoneBookRepository = Mockito.mock(PhoneBookRepository.class);
|
||||||
|
phoneBookService = new PhoneBookService(phoneBookRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidContactName_whenSearchInPhoneBook_thenRetunPhoneNumber() {
|
||||||
|
given(phoneBookRepository.contains(momContactName)).willReturn(true);
|
||||||
|
given(phoneBookRepository.getPhoneNumberByContactName(momContactName))
|
||||||
|
.will((InvocationOnMock invocation) -> {
|
||||||
|
if(invocation.getArgument(0).equals(momContactName)) {
|
||||||
|
return momPhoneNumber;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
String phoneNumber = phoneBookService.search(momContactName);
|
||||||
|
|
||||||
|
then(phoneBookRepository).should().contains(momContactName);
|
||||||
|
then(phoneBookRepository).should().getPhoneNumberByContactName(momContactName);
|
||||||
|
Assert.assertEquals(phoneNumber, momPhoneNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidContactName_whenSearch_thenRetunNull() {
|
||||||
|
given(phoneBookRepository.contains(xContactName)).willReturn(false);
|
||||||
|
|
||||||
|
String phoneNumber = phoneBookService.search(xContactName);
|
||||||
|
|
||||||
|
then(phoneBookRepository).should().contains(xContactName);
|
||||||
|
then(phoneBookRepository).should(never()).getPhoneNumberByContactName(xContactName);
|
||||||
|
Assert.assertEquals(phoneNumber, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidContactNameAndPhoneNumber_whenRegister_thenSucceed() {
|
||||||
|
given(phoneBookRepository.contains(momContactName)).willReturn(false);
|
||||||
|
|
||||||
|
phoneBookService.register(momContactName, momPhoneNumber);
|
||||||
|
|
||||||
|
verify(phoneBookRepository).insert(momContactName, momPhoneNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEmptyPhoneNumber_whenRegister_thenFail() {
|
||||||
|
given(phoneBookRepository.contains(momContactName)).willReturn(false);
|
||||||
|
|
||||||
|
phoneBookService.register(xContactName, "");
|
||||||
|
|
||||||
|
then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenLongPhoneNumber_whenRegister_thenFail() {
|
||||||
|
given(phoneBookRepository.contains(xContactName)).willReturn(false);
|
||||||
|
willThrow(new RuntimeException())
|
||||||
|
.given(phoneBookRepository).insert(any(String.class), eq(tooLongPhoneNumber));
|
||||||
|
|
||||||
|
try {
|
||||||
|
phoneBookService.register(xContactName, tooLongPhoneNumber);
|
||||||
|
fail("Should throw exception");
|
||||||
|
} catch (RuntimeException ex) { }
|
||||||
|
|
||||||
|
then(phoneBookRepository).should(never()).insert(momContactName, tooLongPhoneNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenExistentContactName_whenRegister_thenFail() {
|
||||||
|
given(phoneBookRepository.contains(momContactName))
|
||||||
|
.willThrow(new RuntimeException("Name already exist"));
|
||||||
|
|
||||||
|
try {
|
||||||
|
phoneBookService.register(momContactName, momPhoneNumber);
|
||||||
|
fail("Should throw exception");
|
||||||
|
} catch(Exception ex) { }
|
||||||
|
|
||||||
|
then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package org.baeldung.bddmockito;
|
||||||
|
|
||||||
|
public interface PhoneBookRepository {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert phone record
|
||||||
|
* @param name Contact name
|
||||||
|
* @param phone Phone number
|
||||||
|
*/
|
||||||
|
void insert(String name, String phone);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for contact phone number
|
||||||
|
* @param name Contact name
|
||||||
|
* @return phone number
|
||||||
|
*/
|
||||||
|
String getPhoneNumberByContactName(String name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the phonebook contains this contact
|
||||||
|
* @param name Contact name
|
||||||
|
* @return true if this contact name exists
|
||||||
|
*/
|
||||||
|
boolean contains(String name);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package org.baeldung.bddmockito;
|
||||||
|
|
||||||
|
public class PhoneBookService {
|
||||||
|
|
||||||
|
private PhoneBookRepository phoneBookRepository;
|
||||||
|
|
||||||
|
public PhoneBookService(PhoneBookRepository phoneBookRepository) {
|
||||||
|
this.phoneBookRepository = phoneBookRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a contact
|
||||||
|
* @param name Contact name
|
||||||
|
* @param phone Phone number
|
||||||
|
*/
|
||||||
|
public void register(String name, String phone) {
|
||||||
|
if(!name.isEmpty() && !phone.isEmpty() && !phoneBookRepository.contains(name)) {
|
||||||
|
phoneBookRepository.insert(name, phone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for a phone number by contact name
|
||||||
|
* @param name Contact name
|
||||||
|
* @return Phone number
|
||||||
|
*/
|
||||||
|
public String search(String name) {
|
||||||
|
if(!name.isEmpty() && phoneBookRepository.contains(name)) {
|
||||||
|
return phoneBookRepository.getPhoneNumberByContactName(name);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue