commit
f11fe7f346
@ -95,7 +95,6 @@
|
||||
<gson.version>2.8.2</gson.version>
|
||||
<aws-java-sdk.version>1.11.241</aws-java-sdk.version>
|
||||
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
21
aws/pom.xml
21
aws/pom.xml
@ -100,26 +100,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>${maven-dependency-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includeScope>test</includeScope>
|
||||
<includeTypes>so,dll,dylib</includeTypes>
|
||||
<outputDirectory>${project.basedir}/native-libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@ -144,7 +124,6 @@
|
||||
<commons-codec-version>1.10.L001</commons-codec-version>
|
||||
<jets3t-version>0.9.4.0006L</jets3t-version>
|
||||
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -41,27 +41,6 @@
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<!-- This plugin will set properties values using dependency information -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<!--
|
||||
Goal that sets a property pointing to the artifact file for each project dependency.
|
||||
For each dependency (direct and transitive) a project property will be set which
|
||||
follows the:
|
||||
|
||||
groupId:artifactId:type:[classifier]
|
||||
|
||||
form and contains the path to the resolved artifact. -->
|
||||
<goal>properties</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
|
@ -116,23 +116,6 @@
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
@ -57,26 +57,6 @@
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
|
@ -57,26 +57,6 @@
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
|
@ -166,22 +166,6 @@
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.decimalformat;
|
||||
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
public class DoubletoString {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
double doubleValue = 345.56;
|
||||
|
||||
System.out.println(String.valueOf((int) doubleValue));
|
||||
|
||||
System.out.println(String.format("%.0f", doubleValue));
|
||||
|
||||
doubleValue = Math.floor(doubleValue);
|
||||
DecimalFormat df = new DecimalFormat("#");
|
||||
df.setRoundingMode(RoundingMode.FLOOR);
|
||||
System.out.println(df.format(doubleValue));
|
||||
|
||||
Locale enlocale = new Locale("en", "US");
|
||||
String pattern = "###,##";
|
||||
df = (DecimalFormat) NumberFormat.getNumberInstance(enlocale);
|
||||
df.applyPattern(pattern);
|
||||
String format = df.format(doubleValue);
|
||||
System.out.println(format);
|
||||
|
||||
Locale dalocale = new Locale("da", "DK");
|
||||
df = (DecimalFormat) NumberFormat.getNumberInstance(dalocale);
|
||||
df.applyPattern(pattern);
|
||||
System.out.println(df.format(doubleValue));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -8,7 +8,6 @@
|
||||
- [Generics in Kotlin](http://www.baeldung.com/kotlin-generics)
|
||||
- [Introduction to Kotlin Coroutines](http://www.baeldung.com/kotlin-coroutines)
|
||||
- [Destructuring Declarations in Kotlin](http://www.baeldung.com/kotlin-destructuring-declarations)
|
||||
- [Kotlin with Mockito](http://www.baeldung.com/kotlin-mockito)
|
||||
- [Lazy Initialization in Kotlin](http://www.baeldung.com/kotlin-lazy-initialization)
|
||||
- [Overview of Kotlin Collections API](http://www.baeldung.com/kotlin-collections-api)
|
||||
- [Converting a List to Map in Kotlin](http://www.baeldung.com/kotlin-list-to-map)
|
||||
@ -19,8 +18,6 @@
|
||||
- [Extension Methods in Kotlin](http://www.baeldung.com/kotlin-extension-methods)
|
||||
- [Infix Functions in Kotlin](http://www.baeldung.com/kotlin-infix-functions)
|
||||
- [Try-with-resources in Kotlin](http://www.baeldung.com/kotlin-try-with-resources)
|
||||
- [HTTP Requests with Kotlin and khttp](http://www.baeldung.com/kotlin-khttp)
|
||||
- [Kotlin Dependency Injection with Kodein](http://www.baeldung.com/kotlin-kodein-dependency-injection)
|
||||
- [Regular Expressions in Kotlin](http://www.baeldung.com/kotlin-regular-expressions)
|
||||
- [Objects in Kotlin](http://www.baeldung.com/kotlin-objects)
|
||||
- [Reading from a File in Kotlin](http://www.baeldung.com/kotlin-read-file)
|
||||
@ -28,11 +25,7 @@
|
||||
- [Filtering Kotlin Collections](http://www.baeldung.com/kotlin-filter-collection)
|
||||
- [Writing to a File in Kotlin](http://www.baeldung.com/kotlin-write-file)
|
||||
- [Lambda Expressions in Kotlin](http://www.baeldung.com/kotlin-lambda-expressions)
|
||||
- [Writing Specifications with Kotlin and Spek](http://www.baeldung.com/kotlin-spek)
|
||||
- [Processing JSON with Kotlin and Klaxson](http://www.baeldung.com/kotlin-json-klaxson)
|
||||
- [Kotlin String Templates](http://www.baeldung.com/kotlin-string-template)
|
||||
- [Java EE 8 Security API](http://www.baeldung.com/java-ee-8-security)
|
||||
- [Kotlin with Ktor](http://www.baeldung.com/kotlin-ktor)
|
||||
- [Working with Enums in Kotlin](http://www.baeldung.com/kotlin-enum)
|
||||
- [Create a Java and Kotlin Project with Maven](http://www.baeldung.com/kotlin-maven-java-project)
|
||||
- [Reflection with Kotlin](http://www.baeldung.com/kotlin-reflection)
|
||||
@ -41,4 +34,3 @@
|
||||
- [Kotlin Constructors](https://www.baeldung.com/kotlin-constructors)
|
||||
- [Creational Design Patterns in Kotlin: Builder](https://www.baeldung.com/kotlin-builder-pattern)
|
||||
- [Kotlin Nested and Inner Classes](https://www.baeldung.com/kotlin-inner-classes)
|
||||
- [Guide to the Kotlin Exposed Framework](https://www.baeldung.com/kotlin-exposed-persistence)
|
||||
|
@ -6,7 +6,6 @@ version '1.0-SNAPSHOT'
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.2.41'
|
||||
ext.ktor_version = '0.9.2'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@ -44,14 +43,6 @@ sourceSets {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "io.ktor:ktor-server-netty:$ktor_version"
|
||||
compile "ch.qos.logback:logback-classic:1.2.1"
|
||||
compile "io.ktor:ktor-gson:$ktor_version"
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
implementation 'com.beust:klaxon:3.0.1'
|
||||
|
||||
}
|
||||
task runServer(type: JavaExec) {
|
||||
main = 'APIServer'
|
||||
classpath = sourceSets.main.runtimeClasspath
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
||||
|
||||
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
version="3.0">
|
||||
<!-- path to application.conf file, required -->
|
||||
<!-- note that this file is always loaded as an absolute path from the classpath -->
|
||||
<context-param>
|
||||
<param-name>io.ktor.ktor.config</param-name>
|
||||
<param-value>application.conf</param-value>
|
||||
</context-param>
|
||||
|
||||
<servlet>
|
||||
<display-name>KtorServlet</display-name>
|
||||
<servlet-name>KtorServlet</servlet-name>
|
||||
<servlet-class>io.ktor.server.servlet.ServletApplicationEngine</servlet-class>
|
||||
|
||||
<!-- required! -->
|
||||
<async-supported>true</async-supported>
|
||||
|
||||
<!-- 100mb max file upload, optional -->
|
||||
<multipart-config>
|
||||
<max-file-size>304857600</max-file-size>
|
||||
<max-request-size>304857600</max-request-size>
|
||||
<file-size-threshold>0</file-size-threshold>
|
||||
</multipart-config>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>KtorServlet</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
</web-app>
|
@ -12,33 +12,7 @@
|
||||
<relativePath>../parent-kotlin</relativePath>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>exposed</id>
|
||||
<name>exposed</name>
|
||||
<url>https://dl.bintray.com/kotlin/exposed</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-api</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-subject-extension</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-junit-platform-engine</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
@ -50,38 +24,12 @@
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>khttp</groupId>
|
||||
<artifactId>khttp</artifactId>
|
||||
<version>${khttp.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.nhaarman</groupId>
|
||||
<artifactId>mockito-kotlin</artifactId>
|
||||
<version>${mockito-kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.salomonbrys.kodein</groupId>
|
||||
<artifactId>kodein</artifactId>
|
||||
<version>${kodein.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.beust</groupId>
|
||||
<artifactId>klaxon</artifactId>
|
||||
<version>${klaxon.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.exposed</groupId>
|
||||
<artifactId>exposed</artifactId>
|
||||
<version>${exposed.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
@ -110,16 +58,11 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
<kodein.version>4.1.0</kodein.version>
|
||||
<klaxon.version>3.0.4</klaxon.version>
|
||||
<khttp.version>0.1.0</khttp.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<junit.platform.version>1.1.1</junit.platform.version>
|
||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<exposed.version>0.10.4</exposed.version>
|
||||
<fuel.version>1.15.0</fuel.version>
|
||||
</properties>
|
||||
|
||||
|
@ -1,73 +0,0 @@
|
||||
@file:JvmName("APIServer")
|
||||
|
||||
|
||||
import io.ktor.application.call
|
||||
import io.ktor.application.install
|
||||
import io.ktor.features.CallLogging
|
||||
import io.ktor.features.ContentNegotiation
|
||||
import io.ktor.features.DefaultHeaders
|
||||
import io.ktor.gson.gson
|
||||
import io.ktor.request.path
|
||||
import io.ktor.request.receive
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.*
|
||||
import io.ktor.server.engine.embeddedServer
|
||||
import io.ktor.server.netty.Netty
|
||||
import org.slf4j.event.Level
|
||||
|
||||
data class Author(val name: String, val website: String)
|
||||
data class ToDo(var id: Int, val name: String, val description: String, val completed: Boolean)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
val toDoList = ArrayList<ToDo>();
|
||||
val jsonResponse = """{
|
||||
"id": 1,
|
||||
"task": "Pay waterbill",
|
||||
"description": "Pay water bill today",
|
||||
}"""
|
||||
|
||||
|
||||
embeddedServer(Netty, 8080) {
|
||||
install(DefaultHeaders) {
|
||||
header("X-Developer", "Baeldung")
|
||||
}
|
||||
install(CallLogging) {
|
||||
level = Level.DEBUG
|
||||
filter { call -> call.request.path().startsWith("/todo") }
|
||||
filter { call -> call.request.path().startsWith("/author") }
|
||||
}
|
||||
install(ContentNegotiation) {
|
||||
gson {
|
||||
setPrettyPrinting()
|
||||
}
|
||||
}
|
||||
routing() {
|
||||
route("/todo") {
|
||||
post {
|
||||
var toDo = call.receive<ToDo>();
|
||||
toDo.id = toDoList.size;
|
||||
toDoList.add(toDo);
|
||||
call.respond("Added")
|
||||
|
||||
}
|
||||
delete("/{id}") {
|
||||
call.respond(toDoList.removeAt(call.parameters["id"]!!.toInt()));
|
||||
}
|
||||
get("/{id}") {
|
||||
|
||||
call.respond(toDoList[call.parameters["id"]!!.toInt()]);
|
||||
}
|
||||
get {
|
||||
call.respond(toDoList);
|
||||
}
|
||||
}
|
||||
get("/author"){
|
||||
call.respond(Author("Baeldung","baeldung.com"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}.start(wait = true)
|
||||
}
|
@ -36,22 +36,6 @@
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
|
@ -61,23 +61,6 @@
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
@ -83,23 +83,6 @@
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
|
@ -0,0 +1,29 @@
|
||||
package com.baeldung.maths;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class BigDecimalDemo {
|
||||
|
||||
/** Calculate total amount to be paid for an item rounded to cents..
|
||||
* @param quantity
|
||||
* @param unitPrice
|
||||
* @param discountRate
|
||||
* @param taxRate
|
||||
* @return
|
||||
*/
|
||||
public static BigDecimal calculateTotalAmount(BigDecimal quantity,
|
||||
BigDecimal unitPrice, BigDecimal discountRate, BigDecimal taxRate) {
|
||||
BigDecimal amount = quantity.multiply(unitPrice);
|
||||
BigDecimal discount = amount.multiply(discountRate);
|
||||
BigDecimal discountedAmount = amount.subtract(discount);
|
||||
BigDecimal tax = discountedAmount.multiply(taxRate);
|
||||
BigDecimal total = discountedAmount.add(tax);
|
||||
|
||||
// round to 2 decimal places using HALF_EVEN
|
||||
BigDecimal roundedTotal = total.setScale(2, RoundingMode.HALF_EVEN);
|
||||
|
||||
return roundedTotal;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
package com.baeldung.maths;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BigDecimalDemoUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenBigDecimalCreated_thenValueMatches() {
|
||||
BigDecimal bdFromString = new BigDecimal("0.1");
|
||||
BigDecimal bdFromCharArray = new BigDecimal(
|
||||
new char[] { '3', '.', '1', '6', '1', '5' });
|
||||
BigDecimal bdlFromInt = new BigDecimal(42);
|
||||
BigDecimal bdFromLong = new BigDecimal(123412345678901L);
|
||||
BigInteger bigInteger = BigInteger.probablePrime(100, new Random());
|
||||
BigDecimal bdFromBigInteger = new BigDecimal(bigInteger);
|
||||
|
||||
assertEquals("0.1", bdFromString.toString());
|
||||
assertEquals("3.1615", bdFromCharArray.toString());
|
||||
assertEquals("42", bdlFromInt.toString());
|
||||
assertEquals("123412345678901", bdFromLong.toString());
|
||||
assertEquals(bigInteger.toString(), bdFromBigInteger.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBigDecimalCreatedFromDouble_thenValueMayNotMatch() {
|
||||
BigDecimal bdFromDouble = new BigDecimal(0.1d);
|
||||
assertNotEquals("0.1", bdFromDouble.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBigDecimalCreatedUsingValueOf_thenValueMatches() {
|
||||
BigDecimal bdFromLong1 = BigDecimal.valueOf(123412345678901L);
|
||||
BigDecimal bdFromLong2 = BigDecimal.valueOf(123412345678901L, 2);
|
||||
BigDecimal bdFromDouble = BigDecimal.valueOf(0.1d);
|
||||
|
||||
assertEquals("123412345678901", bdFromLong1.toString());
|
||||
assertEquals("1234123456789.01", bdFromLong2.toString());
|
||||
assertEquals("0.1", bdFromDouble.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEqualsCalled_thenSizeAndScaleMatched() {
|
||||
BigDecimal bd1 = new BigDecimal("1.0");
|
||||
BigDecimal bd2 = new BigDecimal("1.00");
|
||||
|
||||
assertFalse(bd1.equals(bd2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingBigDecimals_thenExpectedResult() {
|
||||
BigDecimal bd1 = new BigDecimal("1.0");
|
||||
BigDecimal bd2 = new BigDecimal("1.00");
|
||||
BigDecimal bd3 = new BigDecimal("2.0");
|
||||
|
||||
assertTrue(bd1.compareTo(bd3) < 0);
|
||||
assertTrue(bd3.compareTo(bd1) > 0);
|
||||
assertTrue(bd1.compareTo(bd2) == 0);
|
||||
assertTrue(bd1.compareTo(bd3) <= 0);
|
||||
assertTrue(bd1.compareTo(bd2) >= 0);
|
||||
assertTrue(bd1.compareTo(bd3) != 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPerformingArithmetic_thenExpectedResult() {
|
||||
BigDecimal bd1 = new BigDecimal("4.0");
|
||||
BigDecimal bd2 = new BigDecimal("2.0");
|
||||
|
||||
BigDecimal sum = bd1.add(bd2);
|
||||
BigDecimal difference = bd1.subtract(bd2);
|
||||
BigDecimal quotient = bd1.divide(bd2);
|
||||
BigDecimal product = bd1.multiply(bd2);
|
||||
|
||||
assertTrue(sum.compareTo(new BigDecimal("6.0")) == 0);
|
||||
assertTrue(difference.compareTo(new BigDecimal("2.0")) == 0);
|
||||
assertTrue(quotient.compareTo(new BigDecimal("2.0")) == 0);
|
||||
assertTrue(product.compareTo(new BigDecimal("8.0")) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGettingAttributes_thenExpectedResult() {
|
||||
BigDecimal bd = new BigDecimal("-12345.6789");
|
||||
|
||||
assertEquals(9, bd.precision());
|
||||
assertEquals(4, bd.scale());
|
||||
assertEquals(-1, bd.signum());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRoundingDecimal_thenExpectedResult() {
|
||||
BigDecimal bd = new BigDecimal("2.5");
|
||||
// Round to 1 digit using HALF_EVEN
|
||||
BigDecimal rounded = bd
|
||||
.round(new MathContext(1, RoundingMode.HALF_EVEN));
|
||||
|
||||
assertEquals("2", rounded.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPurchaseTxn_whenCalculatingTotalAmount_thenExpectedResult() {
|
||||
BigDecimal quantity = new BigDecimal("4.5");
|
||||
BigDecimal unitPrice = new BigDecimal("2.69");
|
||||
BigDecimal discountRate = new BigDecimal("0.10");
|
||||
BigDecimal taxRate = new BigDecimal("0.0725");
|
||||
|
||||
BigDecimal amountToBePaid = BigDecimalDemo
|
||||
.calculateTotalAmount(quantity, unitPrice, discountRate, taxRate);
|
||||
assertEquals("11.68", amountToBePaid.toString());
|
||||
}
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
package com.baeldung.maths;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BigIntegerDemoUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenBigIntegerCreatedFromConstructor_thenExpectedResult() {
|
||||
BigInteger biFromString = new BigInteger("1234567890987654321");
|
||||
BigInteger biFromByteArray = new BigInteger(
|
||||
new byte[] { 64, 64, 64, 64, 64, 64 });
|
||||
BigInteger biFromSignMagnitude = new BigInteger(-1,
|
||||
new byte[] { 64, 64, 64, 64, 64, 64 });
|
||||
|
||||
assertEquals("1234567890987654321", biFromString.toString());
|
||||
assertEquals("70644700037184", biFromByteArray.toString());
|
||||
assertEquals("-70644700037184", biFromSignMagnitude.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLongConvertedToBigInteger_thenValueMatches() {
|
||||
BigInteger bi = BigInteger.valueOf(2305843009213693951L);
|
||||
|
||||
assertEquals("2305843009213693951", bi.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigIntegers_whentCompared_thenExpectedResult() {
|
||||
BigInteger i = new BigInteger("123456789012345678901234567890");
|
||||
BigInteger j = new BigInteger("123456789012345678901234567891");
|
||||
BigInteger k = new BigInteger("123456789012345678901234567892");
|
||||
|
||||
assertTrue(i.compareTo(i) == 0);
|
||||
assertTrue(j.compareTo(i) > 0);
|
||||
assertTrue(j.compareTo(k) < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigIntegers_whenPerformingArithmetic_thenExpectedResult() {
|
||||
BigInteger i = new BigInteger("4");
|
||||
BigInteger j = new BigInteger("2");
|
||||
|
||||
BigInteger sum = i.add(j);
|
||||
BigInteger difference = i.subtract(j);
|
||||
BigInteger quotient = i.divide(j);
|
||||
BigInteger product = i.multiply(j);
|
||||
|
||||
assertEquals(new BigInteger("6"), sum);
|
||||
assertEquals(new BigInteger("2"), difference);
|
||||
assertEquals(new BigInteger("2"), quotient);
|
||||
assertEquals(new BigInteger("8"), product);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigIntegers_whenPerformingBitOperations_thenExpectedResult() {
|
||||
BigInteger i = new BigInteger("17");
|
||||
BigInteger j = new BigInteger("7");
|
||||
|
||||
BigInteger and = i.and(j);
|
||||
BigInteger or = i.or(j);
|
||||
BigInteger not = j.not();
|
||||
BigInteger xor = i.xor(j);
|
||||
BigInteger andNot = i.andNot(j);
|
||||
BigInteger shiftLeft = i.shiftLeft(1);
|
||||
BigInteger shiftRight = i.shiftRight(1);
|
||||
|
||||
assertEquals(new BigInteger("1"), and);
|
||||
assertEquals(new BigInteger("23"), or);
|
||||
assertEquals(new BigInteger("-8"), not);
|
||||
assertEquals(new BigInteger("22"), xor);
|
||||
assertEquals(new BigInteger("16"), andNot);
|
||||
assertEquals(new BigInteger("34"), shiftLeft);
|
||||
assertEquals(new BigInteger("8"), shiftRight);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigIntegers_whenPerformingBitManipulations_thenExpectedResult() {
|
||||
BigInteger i = new BigInteger("1018");
|
||||
|
||||
int bitCount = i.bitCount();
|
||||
int bitLength = i.bitLength();
|
||||
int getLowestSetBit = i.getLowestSetBit();
|
||||
boolean testBit3 = i.testBit(3);
|
||||
BigInteger setBit12 = i.setBit(12);
|
||||
BigInteger flipBit0 = i.flipBit(0);
|
||||
BigInteger clearBit3 = i.clearBit(3);
|
||||
|
||||
assertEquals(8, bitCount);
|
||||
assertEquals(10, bitLength);
|
||||
assertEquals(1, getLowestSetBit);
|
||||
assertEquals(true, testBit3);
|
||||
assertEquals(new BigInteger("5114"), setBit12);
|
||||
assertEquals(new BigInteger("1019"), flipBit0);
|
||||
assertEquals(new BigInteger("1010"), clearBit3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigIntegers_whenModularCalculation_thenExpectedResult() {
|
||||
BigInteger i = new BigInteger("31");
|
||||
BigInteger j = new BigInteger("24");
|
||||
BigInteger k = new BigInteger("16");
|
||||
|
||||
BigInteger gcd = j.gcd(k);
|
||||
BigInteger multiplyAndmod = j.multiply(k)
|
||||
.mod(i);
|
||||
BigInteger modInverse = j.modInverse(i);
|
||||
BigInteger modPow = j.modPow(k, i);
|
||||
|
||||
assertEquals(new BigInteger("8"), gcd);
|
||||
assertEquals(new BigInteger("12"), multiplyAndmod);
|
||||
assertEquals(new BigInteger("22"), modInverse);
|
||||
assertEquals(new BigInteger("7"), modPow);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigIntegers_whenPrimeOperations_thenExpectedResult() {
|
||||
BigInteger i = BigInteger.probablePrime(100, new Random());
|
||||
|
||||
boolean isProbablePrime = i.isProbablePrime(1000);
|
||||
assertEquals(true, isProbablePrime);
|
||||
}
|
||||
}
|
@ -86,23 +86,6 @@
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
@ -26,3 +26,4 @@
|
||||
- [Convert a String to Title Case](http://www.baeldung.com/java-string-title-case)
|
||||
- [Compact Strings in Java 9](http://www.baeldung.com/java-9-compact-string)
|
||||
- [Java Check a String for Lowercase/Uppercase Letter, Special Character and Digit](https://www.baeldung.com/java-lowercase-uppercase-special-character-digit-regex)
|
||||
- [Convert java.util.Date to String](https://www.baeldung.com/java-util-date-to-string)
|
||||
|
@ -71,23 +71,6 @@
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
10
kotlin-libraries/README.md
Normal file
10
kotlin-libraries/README.md
Normal file
@ -0,0 +1,10 @@
|
||||
## Relevant articles:
|
||||
|
||||
- [Kotlin with Mockito](http://www.baeldung.com/kotlin-mockito)
|
||||
- [HTTP Requests with Kotlin and khttp](http://www.baeldung.com/kotlin-khttp)
|
||||
- [Kotlin Dependency Injection with Kodein](http://www.baeldung.com/kotlin-kodein-dependency-injection)
|
||||
- [Writing Specifications with Kotlin and Spek](http://www.baeldung.com/kotlin-spek)
|
||||
- [Processing JSON with Kotlin and Klaxson](http://www.baeldung.com/kotlin-json-klaxson)
|
||||
- [Kotlin with Ktor](http://www.baeldung.com/kotlin-ktor)
|
||||
- [Idiomatic Logging in Kotlin](http://www.baeldung.com/kotlin-logging)
|
||||
- [Guide to the Kotlin Exposed Framework](https://www.baeldung.com/kotlin-exposed-persistence)
|
10
core-kotlin/kotlin-ktor/build.gradle → kotlin-libraries/build.gradle
Executable file → Normal file
10
core-kotlin/kotlin-ktor/build.gradle → kotlin-libraries/build.gradle
Executable file → Normal file
@ -12,6 +12,7 @@ buildscript {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
@ -33,12 +34,21 @@ repositories {
|
||||
jcenter()
|
||||
maven { url "https://dl.bintray.com/kotlin/ktor" }
|
||||
}
|
||||
sourceSets {
|
||||
main{
|
||||
kotlin{
|
||||
srcDirs 'com/baeldung/ktor'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "io.ktor:ktor-server-netty:$ktor_version"
|
||||
compile "ch.qos.logback:logback-classic:1.2.1"
|
||||
compile "io.ktor:ktor-gson:$ktor_version"
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
implementation 'com.beust:klaxon:3.0.1'
|
||||
|
||||
}
|
||||
task runServer(type: JavaExec) {
|
0
core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.jar → kotlin-libraries/gradle/wrapper/gradle-wrapper.jar
vendored
Executable file → Normal file
0
core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.jar → kotlin-libraries/gradle/wrapper/gradle-wrapper.jar
vendored
Executable file → Normal file
0
core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.properties → kotlin-libraries/gradle/wrapper/gradle-wrapper.properties
vendored
Executable file → Normal file
0
core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.properties → kotlin-libraries/gradle/wrapper/gradle-wrapper.properties
vendored
Executable file → Normal file
0
core-kotlin/kotlin-ktor/gradlew → kotlin-libraries/gradlew
vendored
Executable file → Normal file
0
core-kotlin/kotlin-ktor/gradlew → kotlin-libraries/gradlew
vendored
Executable file → Normal file
0
core-kotlin/kotlin-ktor/gradlew.bat → kotlin-libraries/gradlew.bat
vendored
Executable file → Normal file
0
core-kotlin/kotlin-ktor/gradlew.bat → kotlin-libraries/gradlew.bat
vendored
Executable file → Normal file
105
kotlin-libraries/pom.xml
Normal file
105
kotlin-libraries/pom.xml
Normal file
@ -0,0 +1,105 @@
|
||||
<?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>kotlin-libraries</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-kotlin</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../parent-kotlin</relativePath>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>exposed</id>
|
||||
<name>exposed</name>
|
||||
<url>https://dl.bintray.com/kotlin/exposed</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-api</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-subject-extension</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-junit-platform-engine</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>khttp</groupId>
|
||||
<artifactId>khttp</artifactId>
|
||||
<version>${khttp.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.nhaarman</groupId>
|
||||
<artifactId>mockito-kotlin</artifactId>
|
||||
<version>${mockito-kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.salomonbrys.kodein</groupId>
|
||||
<artifactId>kodein</artifactId>
|
||||
<version>${kodein.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.beust</groupId>
|
||||
<artifactId>klaxon</artifactId>
|
||||
<version>${klaxon.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.exposed</groupId>
|
||||
<artifactId>exposed</artifactId>
|
||||
<version>${exposed.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
<kodein.version>4.1.0</kodein.version>
|
||||
<klaxon.version>3.0.4</klaxon.version>
|
||||
<khttp.version>0.1.0</khttp.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<junit.platform.version>1.1.1</junit.platform.version>
|
||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<exposed.version>0.10.4</exposed.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
0
core-kotlin/kotlin-ktor/resources/logback.xml → kotlin-libraries/resources/logback.xml
Executable file → Normal file
0
core-kotlin/kotlin-ktor/resources/logback.xml → kotlin-libraries/resources/logback.xml
Executable file → Normal file
0
core-kotlin/kotlin-ktor/settings.gradle → kotlin-libraries/settings.gradle
Executable file → Normal file
0
core-kotlin/kotlin-ktor/settings.gradle → kotlin-libraries/settings.gradle
Executable file → Normal file
@ -0,0 +1,17 @@
|
||||
package com.baeldung.kotlin.junit5
|
||||
|
||||
class Calculator {
|
||||
fun add(a: Int, b: Int) = a + b
|
||||
|
||||
fun divide(a: Int, b: Int) = if (b == 0) {
|
||||
throw DivideByZeroException(a)
|
||||
} else {
|
||||
a / b
|
||||
}
|
||||
|
||||
fun square(a: Int) = a * a
|
||||
|
||||
fun squareRoot(a: Int) = Math.sqrt(a.toDouble())
|
||||
|
||||
fun log(base: Int, value: Int) = Math.log(value.toDouble()) / Math.log(base.toDouble())
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package com.baeldung.kotlin.junit5
|
||||
|
||||
class DivideByZeroException(val numerator: Int) : Exception()
|
@ -777,19 +777,6 @@
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<version>${maven-bundle-plugin.version}</version>
|
||||
<type>maven-plugin</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<extensions>true</extensions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>${maven-failsafe-plugin.version}</version>
|
||||
|
19
lombok/src/main/java/com/baeldung/lombok/builder/Child.java
Normal file
19
lombok/src/main/java/com/baeldung/lombok/builder/Child.java
Normal file
@ -0,0 +1,19 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Child extends Parent {
|
||||
|
||||
private final String childName;
|
||||
private final int childAge;
|
||||
|
||||
@Builder(builderMethodName = "childBuilder")
|
||||
public Child(String parentName, int parentAge, String childName, int childAge) {
|
||||
super(parentName, parentAge);
|
||||
this.childName = childName;
|
||||
this.childAge = childAge;
|
||||
}
|
||||
|
||||
}
|
11
lombok/src/main/java/com/baeldung/lombok/builder/Parent.java
Normal file
11
lombok/src/main/java/com/baeldung/lombok/builder/Parent.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class Parent {
|
||||
private final String parentName;
|
||||
private final int parentAge;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Related Article Sections:
|
||||
* 4. Using @Getter on a Boolean Field
|
||||
*
|
||||
*/
|
||||
public class GetterBoolean {
|
||||
|
||||
@Getter
|
||||
private Boolean running = true;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Related Article Sections:
|
||||
* 3. Using @Getter on a boolean Field
|
||||
*
|
||||
*/
|
||||
public class GetterBooleanPrimitive {
|
||||
|
||||
@Getter
|
||||
private boolean running;
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Related Article Sections:
|
||||
* 3.2. Two boolean Fields With the Same Accessor Name
|
||||
*
|
||||
*/
|
||||
public class GetterBooleanPrimitiveSameAccessor {
|
||||
|
||||
@Getter
|
||||
boolean running = true;
|
||||
|
||||
@Getter
|
||||
boolean isRunning = false;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Related Article Sections:
|
||||
* 3.1. A boolean Field Having the Same Name With Its Accessor
|
||||
*
|
||||
*/
|
||||
public class GetterBooleanSameAccessor {
|
||||
@Getter
|
||||
private boolean isRunning = true;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Related Article Sections:
|
||||
* 4. Using @Getter on a Boolean Field
|
||||
*
|
||||
*/
|
||||
public class GetterBooleanType {
|
||||
|
||||
@Getter
|
||||
private Boolean running = true;
|
||||
}
|
@ -1,42 +1,59 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class BuilderUnitTest
|
||||
{
|
||||
public class BuilderUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBuilder_WidgetIsBuilt() {
|
||||
Widget testWidget = Widget.builder().name("foo").id(1).build();
|
||||
assertThat(testWidget.getName())
|
||||
.isEqualTo("foo");
|
||||
assertThat(testWidget.getId())
|
||||
.isEqualTo(1);
|
||||
Widget testWidget = Widget.builder()
|
||||
.name("foo")
|
||||
.id(1)
|
||||
.build();
|
||||
assertThat(testWidget.getName()).isEqualTo("foo");
|
||||
assertThat(testWidget.getId()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenToBuilder_whenToBuilder_BuilderIsCreated() {
|
||||
|
||||
Widget testWidget = Widget.builder().name("foo").id(1).build();
|
||||
Widget testWidget = Widget.builder()
|
||||
.name("foo")
|
||||
.id(1)
|
||||
.build();
|
||||
Widget.WidgetBuilder widgetBuilder = testWidget.toBuilder();
|
||||
|
||||
Widget newWidget = widgetBuilder.id(2).build();
|
||||
assertThat(newWidget.getName())
|
||||
.isEqualTo("foo");
|
||||
assertThat(newWidget.getId())
|
||||
.isEqualTo(2);
|
||||
Widget newWidget = widgetBuilder.id(2)
|
||||
.build();
|
||||
assertThat(newWidget.getName()).isEqualTo("foo");
|
||||
assertThat(newWidget.getId()).isEqualTo(2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void givenBuilderMethod_ClientIsBuilt() {
|
||||
ImmutableClient testImmutableClient = ClientBuilder.builder().name("foo").id(1).build();
|
||||
assertThat(testImmutableClient.getName())
|
||||
.isEqualTo("foo");
|
||||
assertThat(testImmutableClient.getId())
|
||||
.isEqualTo(1);
|
||||
ImmutableClient testImmutableClient = ClientBuilder.builder()
|
||||
.name("foo")
|
||||
.id(1)
|
||||
.build();
|
||||
assertThat(testImmutableClient.getName()).isEqualTo("foo");
|
||||
assertThat(testImmutableClient.getId()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBuilderAtMethodLevel_ChildInheritingParentIsBuilt() {
|
||||
Child child = Child.childBuilder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.build();
|
||||
|
||||
assertThat(child.getChildName()).isEqualTo("Emma");
|
||||
assertThat(child.getChildAge()).isEqualTo(6);
|
||||
assertThat(child.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(child.getParentAge()).isEqualTo(38);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class GetterBooleanUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenBasicBooleanField_thenMethodNamePrefixedWithIsFollowedByFieldName() {
|
||||
GetterBooleanPrimitive lombokExamples = new GetterBooleanPrimitive();
|
||||
assertFalse(lombokExamples.isRunning());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBooleanFieldPrefixedWithIs_thenMethodNameIsSameAsFieldName() {
|
||||
GetterBooleanSameAccessor lombokExamples = new GetterBooleanSameAccessor();
|
||||
assertTrue(lombokExamples.isRunning());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTwoBooleanFieldsCauseNamingConflict_thenLombokMapsToFirstDeclaredField() {
|
||||
GetterBooleanPrimitiveSameAccessor lombokExamples = new GetterBooleanPrimitiveSameAccessor();
|
||||
assertTrue(lombokExamples.isRunning() == lombokExamples.running);
|
||||
assertFalse(lombokExamples.isRunning() == lombokExamples.isRunning);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFieldOfBooleanType_thenLombokPrefixesMethodWithGetInsteadOfIs() {
|
||||
GetterBooleanType lombokExamples = new GetterBooleanType();
|
||||
assertTrue(lombokExamples.getRunning());
|
||||
}
|
||||
}
|
@ -154,25 +154,6 @@
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>${maven-dependency-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includeScope>test</includeScope>
|
||||
<includeTypes>so,dll,dylib</includeTypes>
|
||||
<outputDirectory>${project.basedir}/native-libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@ -196,7 +177,6 @@
|
||||
<dynamodb.version>1.11.106</dynamodb.version>
|
||||
<dynamodblocal.version>1.11.86</dynamodblocal.version>
|
||||
<dynamodblocal.repository.url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</dynamodblocal.repository.url>
|
||||
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
2
pom.xml
2
pom.xml
@ -352,6 +352,7 @@
|
||||
<module>java-streams</module>
|
||||
<module>core-java-persistence</module>
|
||||
<module>core-kotlin</module>
|
||||
<module>kotlin-libraries</module>
|
||||
<module>core-groovy</module>
|
||||
<module>core-java-concurrency</module>
|
||||
<module>core-java-concurrency-collections</module>
|
||||
@ -1240,6 +1241,7 @@
|
||||
<module>spring-boot-ops</module>
|
||||
<module>spring-5</module>
|
||||
<module>core-kotlin</module>
|
||||
<module>kotlin-libraries</module>
|
||||
<module>core-java</module>
|
||||
<module>google-web-toolkit</module>
|
||||
<module>spring-security-mvc-custom</module>
|
||||
|
@ -1,2 +1,3 @@
|
||||
### Relevant Articles:
|
||||
- [Logging in Spring Boot](http://www.baeldung.com/spring-boot-logging)
|
||||
- [How to Disable Console Logging in Spring Boot](https://www.baeldung.com/spring-boot-disable-console-logging)
|
||||
|
@ -2,3 +2,9 @@
|
||||
|
||||
- [Guide to the Favicon in Spring Boot](http://www.baeldung.com/spring-boot-favicon)
|
||||
- [Custom Validation MessageSource in Spring Boot](https://www.baeldung.com/spring-custom-validation-message-source)
|
||||
- [Spring Boot Annotations](http://www.baeldung.com/spring-boot-annotations)
|
||||
- [Spring Scheduling Annotations](http://www.baeldung.com/spring-scheduling-annotations)
|
||||
- [Spring Web Annotations](http://www.baeldung.com/spring-mvc-annotations)
|
||||
- [Spring Core Annotations](http://www.baeldung.com/spring-core-annotations)
|
||||
- [Display RSS Feed with Spring MVC](http://www.baeldung.com/spring-mvc-rss-feed)
|
||||
|
||||
|
@ -32,6 +32,13 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- ROME for RSS -->
|
||||
<dependency>
|
||||
<groupId>com.rometools</groupId>
|
||||
<artifactId>rome</artifactId>
|
||||
<version>${rome.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -47,6 +54,8 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<!-- ROME for RSS -->
|
||||
<rome.version>1.10.0</rome.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
@ -3,3 +3,4 @@
|
||||
- [Spring Boot with Multiple SQL Import Files](http://www.baeldung.com/spring-boot-sql-import-files)
|
||||
- [Configuring Separate Spring DataSource for Tests](http://www.baeldung.com/spring-testing-separate-data-source)
|
||||
- [Quick Guide on data.sql and schema.sql Files in Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql)
|
||||
- [Configuring a Tomcat Connection Pool in Spring Boot](https://www.baeldung.com/spring-boot-tomcat-connection-pool)
|
||||
|
@ -41,6 +41,26 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>21.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- JUnit Jupiter dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- JUnit platform launcher -->
|
||||
<!-- To be able to run tests from IDE directly -->
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-launcher</artifactId>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,11 +1,11 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
|
||||
public class Application {
|
||||
|
@ -0,0 +1,46 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
||||
@Entity
|
||||
class Aggregate {
|
||||
@Transient
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private Aggregate() {
|
||||
}
|
||||
|
||||
Aggregate(long id, ApplicationEventPublisher eventPublisher) {
|
||||
this.id = id;
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DomainEntity [id=" + id + "]";
|
||||
}
|
||||
|
||||
void domainOperation() {
|
||||
// some business logic
|
||||
if (eventPublisher != null) {
|
||||
eventPublisher.publishEvent(new DomainEvent());
|
||||
}
|
||||
}
|
||||
|
||||
long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import org.springframework.data.domain.AfterDomainEventPublication;
|
||||
import org.springframework.data.domain.DomainEvents;
|
||||
|
||||
@Entity
|
||||
public class Aggregate2 {
|
||||
@Transient
|
||||
private final Collection<DomainEvent> domainEvents;
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
public Aggregate2() {
|
||||
domainEvents = new ArrayList<>();
|
||||
}
|
||||
|
||||
@AfterDomainEventPublication
|
||||
public void clearEvents() {
|
||||
domainEvents.clear();
|
||||
}
|
||||
|
||||
public void domainOperation() {
|
||||
// some domain operation
|
||||
domainEvents.add(new DomainEvent());
|
||||
}
|
||||
|
||||
@DomainEvents
|
||||
public Collection<DomainEvent> events() {
|
||||
return domainEvents;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface Aggregate2Repository extends CrudRepository<Aggregate2, Long> {
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.springframework.data.domain.AbstractAggregateRoot;
|
||||
|
||||
@Entity
|
||||
public class Aggregate3 extends AbstractAggregateRoot<Aggregate3> {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
public void domainOperation() {
|
||||
// some domain operation
|
||||
registerEvent(new DomainEvent());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
* @author goobar
|
||||
*
|
||||
*/
|
||||
public interface Aggregate3Repository extends CrudRepository<Aggregate3, Long> {
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface AggregateRepository extends CrudRepository<Aggregate, Long> {
|
||||
|
||||
}
|
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