Merge remote-tracking branch 'baeldung/master' into BAEL-3841#declarative-caching-testing
This commit is contained in:
commit
e1a6218c1b
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.determinedatatype
|
||||||
|
|
||||||
|
class Person {
|
||||||
|
|
||||||
|
private int ageAsInt
|
||||||
|
private Double ageAsDouble
|
||||||
|
private String ageAsString
|
||||||
|
|
||||||
|
Person() {}
|
||||||
|
Person(int ageAsInt) { this.ageAsInt = ageAsInt}
|
||||||
|
Person(Double ageAsDouble) { this.ageAsDouble = ageAsDouble}
|
||||||
|
Person(String ageAsString) { this.ageAsString = ageAsString}
|
||||||
|
}
|
||||||
|
class Student extends Person {}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.baeldung.determinedatatype
|
||||||
|
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
import com.baeldung.determinedatatype.Person
|
||||||
|
|
||||||
|
public class PersonTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenWhenParameterTypeIsInteger_thenReturnTrue() {
|
||||||
|
Person personObj = new Person(10)
|
||||||
|
Assert.assertTrue(personObj.ageAsInt instanceof Integer)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenWhenParameterTypeIsDouble_thenReturnTrue() {
|
||||||
|
Person personObj = new Person(10.0)
|
||||||
|
Assert.assertTrue((personObj.ageAsDouble).getClass() == Double)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenWhenParameterTypeIsString_thenReturnTrue() {
|
||||||
|
Person personObj = new Person("10 years")
|
||||||
|
Assert.assertTrue(personObj.ageAsString.class == String)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenClassName_WhenParameterIsInteger_thenReturnTrue() {
|
||||||
|
Assert.assertTrue(Person.class.getDeclaredField('ageAsInt').type == int.class)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenWhenObjectIsInstanceOfType_thenReturnTrue() {
|
||||||
|
Person personObj = new Person()
|
||||||
|
Assert.assertTrue(personObj instanceof Person)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenWhenInstanceIsOfSubtype_thenReturnTrue() {
|
||||||
|
Student studentObj = new Student()
|
||||||
|
Assert.assertTrue(studentObj in Person)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGroovyList_WhenFindClassName_thenReturnTrue() {
|
||||||
|
def ageList = ['ageAsString','ageAsDouble', 10]
|
||||||
|
Assert.assertTrue(ageList.class == ArrayList)
|
||||||
|
Assert.assertTrue(ageList.getClass() == ArrayList)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGrooyMap_WhenFindClassName_thenReturnTrue() {
|
||||||
|
def ageMap = [ageAsString: '10 years', ageAsDouble: 10.0]
|
||||||
|
Assert.assertFalse(ageMap.class == LinkedHashMap)
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,19 +4,19 @@ import java.util.Objects;
|
||||||
|
|
||||||
public record Person (String name, String address) {
|
public record Person (String name, String address) {
|
||||||
|
|
||||||
public static String UNKWOWN_ADDRESS = "Unknown";
|
public static String UNKNOWN_ADDRESS = "Unknown";
|
||||||
public static String UNNAMED = "Unnamed";
|
public static String UNNAMED = "Unnamed";
|
||||||
|
|
||||||
public Person {
|
public Person {
|
||||||
Objects.requireNonNull(name);
|
Objects.requireNonNull(name);
|
||||||
Objects.requireNonNull(address);
|
Objects.requireNonNull(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person(String name) {
|
public Person(String name) {
|
||||||
this(name, UNKWOWN_ADDRESS);
|
this(name, UNKNOWN_ADDRESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Person unnamed(String address) {
|
public static Person unnamed(String address) {
|
||||||
return new Person(UNNAMED, address);
|
return new Person(UNNAMED, address);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -134,7 +134,7 @@ public class PersonTest {
|
||||||
Person person = new Person(name);
|
Person person = new Person(name);
|
||||||
|
|
||||||
assertEquals(name, person.name());
|
assertEquals(name, person.name());
|
||||||
assertEquals(Person.UNKWOWN_ADDRESS, person.address());
|
assertEquals(Person.UNKNOWN_ADDRESS, person.address());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -147,4 +147,4 @@ public class PersonTest {
|
||||||
assertEquals(Person.UNNAMED, person.name());
|
assertEquals(Person.UNNAMED, person.name());
|
||||||
assertEquals(address, person.address());
|
assertEquals(address, person.address());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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>
|
||||||
<artifactId>core-java-8-datetime</artifactId>
|
<artifactId>core-java-8-datetime-2</artifactId>
|
||||||
<version>${project.parent.version}</version>
|
<version>${project.parent.version}</version>
|
||||||
<name>core-java-8-datetime</name>
|
<name>core-java-8-datetime</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
@ -41,7 +41,6 @@
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>core-java-datetime-java8</finalName>
|
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>src/main/resources</directory>
|
<directory>src/main/resources</directory>
|
||||||
|
|
|
@ -5,7 +5,7 @@ import org.junit.Test;
|
||||||
import java.lang.invoke.MethodHandles;
|
import java.lang.invoke.MethodHandles;
|
||||||
import java.lang.invoke.VarHandle;
|
import java.lang.invoke.VarHandle;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class VariableHandlesUnitTest {
|
public class VariableHandlesUnitTest {
|
||||||
|
|
||||||
|
@ -18,25 +18,23 @@ public class VariableHandlesUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
|
public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
|
||||||
VarHandle publicIntHandle = MethodHandles
|
VarHandle PUBLIC_TEST_VARIABLE = MethodHandles
|
||||||
.lookup()
|
.lookup()
|
||||||
.in(VariableHandlesUnitTest.class)
|
.in(VariableHandlesUnitTest.class)
|
||||||
.findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class);
|
.findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class);
|
||||||
|
|
||||||
assertThat(publicIntHandle.coordinateTypes().size() == 1);
|
assertEquals(1, PUBLIC_TEST_VARIABLE.coordinateTypes().size());
|
||||||
assertThat(publicIntHandle.coordinateTypes().get(0) == VariableHandlesUnitTest.class);
|
assertEquals(VariableHandlesUnitTest.class, PUBLIC_TEST_VARIABLE.coordinateTypes().get(0));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
|
public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
|
||||||
VarHandle privateIntHandle = MethodHandles
|
VarHandle PRIVATE_TEST_VARIABLE = MethodHandles
|
||||||
.privateLookupIn(VariableHandlesUnitTest.class, MethodHandles.lookup())
|
.privateLookupIn(VariableHandlesUnitTest.class, MethodHandles.lookup())
|
||||||
.findVarHandle(VariableHandlesUnitTest.class, "privateTestVariable", int.class);
|
.findVarHandle(VariableHandlesUnitTest.class, "privateTestVariable", int.class);
|
||||||
|
|
||||||
assertThat(privateIntHandle.coordinateTypes().size() == 1);
|
assertEquals(1, PRIVATE_TEST_VARIABLE.coordinateTypes().size());
|
||||||
assertThat(privateIntHandle.coordinateTypes().get(0) == VariableHandlesUnitTest.class);
|
assertEquals(VariableHandlesUnitTest.class, PRIVATE_TEST_VARIABLE.coordinateTypes().get(0));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -44,63 +42,64 @@ public class VariableHandlesUnitTest {
|
||||||
VarHandle arrayVarHandle = MethodHandles
|
VarHandle arrayVarHandle = MethodHandles
|
||||||
.arrayElementVarHandle(int[].class);
|
.arrayElementVarHandle(int[].class);
|
||||||
|
|
||||||
assertThat(arrayVarHandle.coordinateTypes().size() == 2);
|
assertEquals(2, arrayVarHandle.coordinateTypes().size());
|
||||||
assertThat(arrayVarHandle.coordinateTypes().get(0) == int[].class);
|
assertEquals(int[].class, arrayVarHandle.coordinateTypes().get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenVarHandle_whenGetIsInvoked_ThenValueOfVariableIsReturned() throws NoSuchFieldException, IllegalAccessException {
|
public void givenVarHandle_whenGetIsInvoked_ThenValueOfVariableIsReturned() throws NoSuchFieldException, IllegalAccessException {
|
||||||
VarHandle publicIntHandle = MethodHandles
|
VarHandle PUBLIC_TEST_VARIABLE = MethodHandles
|
||||||
.lookup()
|
.lookup()
|
||||||
.in(VariableHandlesUnitTest.class)
|
.in(VariableHandlesUnitTest.class)
|
||||||
.findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class);
|
.findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class);
|
||||||
|
|
||||||
assertThat((int) publicIntHandle.get(this) == 1);
|
assertEquals(1, (int) PUBLIC_TEST_VARIABLE.get(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenVarHandle_whenSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
public void givenVarHandle_whenSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
||||||
VarHandle publicIntHandle = MethodHandles
|
VarHandle VARIABLE_TO_SET = MethodHandles
|
||||||
.lookup()
|
.lookup()
|
||||||
.in(VariableHandlesUnitTest.class)
|
.in(VariableHandlesUnitTest.class)
|
||||||
.findVarHandle(VariableHandlesUnitTest.class, "variableToSet", int.class);
|
.findVarHandle(VariableHandlesUnitTest.class, "variableToSet", int.class);
|
||||||
publicIntHandle.set(this, 15);
|
|
||||||
|
|
||||||
assertThat((int) publicIntHandle.get(this) == 15);
|
VARIABLE_TO_SET.set(this, 15);
|
||||||
|
assertEquals(15, (int) VARIABLE_TO_SET.get(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenVarHandle_whenCompareAndSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
public void givenVarHandle_whenCompareAndSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
||||||
VarHandle publicIntHandle = MethodHandles
|
VarHandle VARIABLE_TO_COMPARE_AND_SET = MethodHandles
|
||||||
.lookup()
|
.lookup()
|
||||||
.in(VariableHandlesUnitTest.class)
|
.in(VariableHandlesUnitTest.class)
|
||||||
.findVarHandle(VariableHandlesUnitTest.class, "variableToCompareAndSet", int.class);
|
.findVarHandle(VariableHandlesUnitTest.class, "variableToCompareAndSet", int.class);
|
||||||
publicIntHandle.compareAndSet(this, 1, 100);
|
|
||||||
|
|
||||||
assertThat((int) publicIntHandle.get(this) == 100);
|
VARIABLE_TO_COMPARE_AND_SET.compareAndSet(this, 1, 100);
|
||||||
|
assertEquals(100, (int) VARIABLE_TO_COMPARE_AND_SET.get(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenVarHandle_whenGetAndAddIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
public void givenVarHandle_whenGetAndAddIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
||||||
VarHandle publicIntHandle = MethodHandles
|
VarHandle VARIABLE_TO_GET_AND_ADD = MethodHandles
|
||||||
.lookup()
|
.lookup()
|
||||||
.in(VariableHandlesUnitTest.class)
|
.in(VariableHandlesUnitTest.class)
|
||||||
.findVarHandle(VariableHandlesUnitTest.class, "variableToGetAndAdd", int.class);
|
.findVarHandle(VariableHandlesUnitTest.class, "variableToGetAndAdd", int.class);
|
||||||
int before = (int) publicIntHandle.getAndAdd(this, 200);
|
|
||||||
|
|
||||||
assertThat(before == 0);
|
int before = (int) VARIABLE_TO_GET_AND_ADD.getAndAdd(this, 200);
|
||||||
assertThat((int) publicIntHandle.get(this) == 200);
|
|
||||||
|
assertEquals(0, before);
|
||||||
|
assertEquals(200, (int) VARIABLE_TO_GET_AND_ADD.get(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenVarHandle_whenGetAndBitwiseOrIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
public void givenVarHandle_whenGetAndBitwiseOrIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
||||||
VarHandle publicIntHandle = MethodHandles
|
VarHandle VARIABLE_TO_BITWISE_OR = MethodHandles
|
||||||
.lookup()
|
.lookup()
|
||||||
.in(VariableHandlesUnitTest.class)
|
.in(VariableHandlesUnitTest.class)
|
||||||
.findVarHandle(VariableHandlesUnitTest.class, "variableToBitwiseOr", byte.class);
|
.findVarHandle(VariableHandlesUnitTest.class, "variableToBitwiseOr", byte.class);
|
||||||
byte before = (byte) publicIntHandle.getAndBitwiseOr(this, (byte) 127);
|
byte before = (byte) VARIABLE_TO_BITWISE_OR.getAndBitwiseOr(this, (byte) 127);
|
||||||
|
|
||||||
assertThat(before == 0);
|
assertEquals(0, before);
|
||||||
assertThat(variableToBitwiseOr == 127);
|
assertEquals(127, (byte) VARIABLE_TO_BITWISE_OR.get(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,3 +9,4 @@
|
||||||
- [Differences Between Collection.clear() and Collection.removeAll()](https://www.baeldung.com/java-collection-clear-vs-removeall)
|
- [Differences Between Collection.clear() and Collection.removeAll()](https://www.baeldung.com/java-collection-clear-vs-removeall)
|
||||||
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
|
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
|
||||||
- [Fail-Safe Iterator vs Fail-Fast Iterator](https://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator)
|
- [Fail-Safe Iterator vs Fail-Fast Iterator](https://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator)
|
||||||
|
- [Quick Guide to the Java Stack](https://www.baeldung.com/java-stack)
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.baeldung.abaproblem;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
public class Account {
|
||||||
|
|
||||||
|
private AtomicInteger balance;
|
||||||
|
private AtomicInteger transactionCount;
|
||||||
|
private ThreadLocal<Integer> currentThreadCASFailureCount;
|
||||||
|
|
||||||
|
public Account() {
|
||||||
|
this.balance = new AtomicInteger(0);
|
||||||
|
this.transactionCount = new AtomicInteger(0);
|
||||||
|
this.currentThreadCASFailureCount = new ThreadLocal<>();
|
||||||
|
this.currentThreadCASFailureCount.set(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getBalance() {
|
||||||
|
return balance.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTransactionCount() {
|
||||||
|
return transactionCount.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCurrentThreadCASFailureCount() {
|
||||||
|
return currentThreadCASFailureCount.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean withdraw(int amount) {
|
||||||
|
int current = getBalance();
|
||||||
|
maybeWait();
|
||||||
|
boolean result = balance.compareAndSet(current, current - amount);
|
||||||
|
if (result) {
|
||||||
|
transactionCount.incrementAndGet();
|
||||||
|
} else {
|
||||||
|
int currentCASFailureCount = currentThreadCASFailureCount.get();
|
||||||
|
currentThreadCASFailureCount.set(currentCASFailureCount + 1);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void maybeWait() {
|
||||||
|
if ("thread1".equals(Thread.currentThread().getName())) {
|
||||||
|
try {
|
||||||
|
TimeUnit.SECONDS.sleep(2);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean deposit(int amount) {
|
||||||
|
int current = balance.get();
|
||||||
|
boolean result = balance.compareAndSet(current, current + amount);
|
||||||
|
if (result) {
|
||||||
|
transactionCount.incrementAndGet();
|
||||||
|
} else {
|
||||||
|
int currentCASFailureCount = currentThreadCASFailureCount.get();
|
||||||
|
currentThreadCASFailureCount.set(currentCASFailureCount + 1);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -9,13 +9,11 @@ public class StampedAccount {
|
||||||
private AtomicStampedReference<Integer> account = new AtomicStampedReference<>(0, 0);
|
private AtomicStampedReference<Integer> account = new AtomicStampedReference<>(0, 0);
|
||||||
|
|
||||||
public int getBalance() {
|
public int getBalance() {
|
||||||
return this.account.get(new int[1]);
|
return account.getReference();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getStamp() {
|
public int getStamp() {
|
||||||
int[] stamps = new int[1];
|
return account.getStamp();
|
||||||
this.account.get(stamps);
|
|
||||||
return stamps[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean deposit(int funds) {
|
public boolean deposit(int funds) {
|
||||||
|
|
|
@ -0,0 +1,98 @@
|
||||||
|
package com.baeldung.abaproblem;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class AccountUnitTest {
|
||||||
|
|
||||||
|
private Account account;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
account = new Account();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void zeroBalanceInitializationTest() {
|
||||||
|
assertEquals(0, account.getBalance());
|
||||||
|
assertEquals(0, account.getTransactionCount());
|
||||||
|
assertEquals(0, account.getCurrentThreadCASFailureCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void depositTest() {
|
||||||
|
final int moneyToDeposit = 50;
|
||||||
|
|
||||||
|
assertTrue(account.deposit(moneyToDeposit));
|
||||||
|
|
||||||
|
assertEquals(moneyToDeposit, account.getBalance());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void withdrawTest() throws InterruptedException {
|
||||||
|
final int defaultBalance = 50;
|
||||||
|
final int moneyToWithdraw = 20;
|
||||||
|
|
||||||
|
account.deposit(defaultBalance);
|
||||||
|
|
||||||
|
assertTrue(account.withdraw(moneyToWithdraw));
|
||||||
|
|
||||||
|
assertEquals(defaultBalance - moneyToWithdraw, account.getBalance());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void abaProblemTest() throws InterruptedException {
|
||||||
|
final int defaultBalance = 50;
|
||||||
|
|
||||||
|
final int amountToWithdrawByThread1 = 20;
|
||||||
|
final int amountToWithdrawByThread2 = 10;
|
||||||
|
final int amountToDepositByThread2 = 10;
|
||||||
|
|
||||||
|
assertEquals(0, account.getTransactionCount());
|
||||||
|
assertEquals(0, account.getCurrentThreadCASFailureCount());
|
||||||
|
account.deposit(defaultBalance);
|
||||||
|
assertEquals(1, account.getTransactionCount());
|
||||||
|
|
||||||
|
Thread thread1 = new Thread(() -> {
|
||||||
|
|
||||||
|
// this will take longer due to the name of the thread
|
||||||
|
assertTrue(account.withdraw(amountToWithdrawByThread1));
|
||||||
|
|
||||||
|
// thread 1 fails to capture ABA problem
|
||||||
|
assertNotEquals(1, account.getCurrentThreadCASFailureCount());
|
||||||
|
|
||||||
|
}, "thread1");
|
||||||
|
|
||||||
|
Thread thread2 = new Thread(() -> {
|
||||||
|
|
||||||
|
assertTrue(account.deposit(amountToDepositByThread2));
|
||||||
|
assertEquals(defaultBalance + amountToDepositByThread2, account.getBalance());
|
||||||
|
|
||||||
|
// this will be fast due to the name of the thread
|
||||||
|
assertTrue(account.withdraw(amountToWithdrawByThread2));
|
||||||
|
|
||||||
|
// thread 1 didn't finish yet, so the original value will be in place for it
|
||||||
|
assertEquals(defaultBalance, account.getBalance());
|
||||||
|
|
||||||
|
assertEquals(0, account.getCurrentThreadCASFailureCount());
|
||||||
|
}, "thread2");
|
||||||
|
|
||||||
|
thread1.start();
|
||||||
|
thread2.start();
|
||||||
|
thread1.join();
|
||||||
|
thread2.join();
|
||||||
|
|
||||||
|
// compareAndSet operation succeeds for thread 1
|
||||||
|
assertEquals(defaultBalance - amountToWithdrawByThread1, account.getBalance());
|
||||||
|
|
||||||
|
//but there are other transactions
|
||||||
|
assertNotEquals(2, account.getTransactionCount());
|
||||||
|
|
||||||
|
// thread 2 did two modifications as well
|
||||||
|
assertEquals(4, account.getTransactionCount());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
#Core Java Console
|
||||||
|
|
||||||
|
[Read and Write User Input in Java](http://www.baeldung.com/java-console-input-output)
|
||||||
|
[Formatting with printf() in Java](https://www.baeldung.com/java-printstream-printf)
|
||||||
|
[ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java)
|
|
@ -0,0 +1,142 @@
|
||||||
|
<?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-console</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-console</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>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-console</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>${exec-maven-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<executable>java</executable>
|
||||||
|
<mainClass>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</mainClass>
|
||||||
|
<arguments>
|
||||||
|
<argument>-Xmx300m</argument>
|
||||||
|
<argument>-XX:+UseParallelGC</argument>
|
||||||
|
<argument>-classpath</argument>
|
||||||
|
<classpath />
|
||||||
|
<argument>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</argument>
|
||||||
|
</arguments>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>${maven-javadoc-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${source.version}</source>
|
||||||
|
<target>${target.version}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>integration</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
<includes>
|
||||||
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
<include>**/*IntTest.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<test.mime>json</test.mime>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>${exec-maven-plugin.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>run-benchmarks</id>
|
||||||
|
<!-- <phase>integration-test</phase> -->
|
||||||
|
<phase>none</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>exec</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<classpathScope>test</classpathScope>
|
||||||
|
<executable>java</executable>
|
||||||
|
<arguments>
|
||||||
|
<argument>-classpath</argument>
|
||||||
|
<classpath />
|
||||||
|
<argument>org.openjdk.jmh.Main</argument>
|
||||||
|
<argument>.*</argument>
|
||||||
|
</arguments>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||||
|
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||||
|
<source.version>1.8</source.version>
|
||||||
|
<target.version>1.8</target.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -1,10 +1,9 @@
|
||||||
package com.baeldung.asciiart;
|
package com.baeldung.asciiart;
|
||||||
|
|
||||||
import java.awt.Font;
|
import com.baeldung.asciiart.AsciiArt.Settings;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.asciiart.AsciiArt.Settings;
|
import java.awt.*;
|
||||||
|
|
||||||
public class AsciiArtIntegrationTest {
|
public class AsciiArtIntegrationTest {
|
||||||
|
|
||||||
|
@ -16,5 +15,4 @@ public class AsciiArtIntegrationTest {
|
||||||
|
|
||||||
asciiArt.drawString(text, "*", settings);
|
asciiArt.drawString(text, "*", settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -8,4 +8,5 @@ This module contains articles about date operations in Java.
|
||||||
- [Converting Java Date to OffsetDateTime](https://www.baeldung.com/java-convert-date-to-offsetdatetime)
|
- [Converting Java Date to OffsetDateTime](https://www.baeldung.com/java-convert-date-to-offsetdatetime)
|
||||||
- [How to Set the JVM Time Zone](https://www.baeldung.com/java-jvm-time-zone)
|
- [How to Set the JVM Time Zone](https://www.baeldung.com/java-jvm-time-zone)
|
||||||
- [How to determine day of week by passing specific date in Java?](https://www.baeldung.com/java-get-day-of-week)
|
- [How to determine day of week by passing specific date in Java?](https://www.baeldung.com/java-get-day-of-week)
|
||||||
|
- [Finding Leap Years in Java](https://www.baeldung.com/java-leap-year)
|
||||||
- [[<-- Prev]](/core-java-modules/core-java-date-operations-1)
|
- [[<-- Prev]](/core-java-modules/core-java-date-operations-1)
|
||||||
|
|
|
@ -12,3 +12,4 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
||||||
- [Guide to System.gc()](https://www.baeldung.com/java-system-gc)
|
- [Guide to System.gc()](https://www.baeldung.com/java-system-gc)
|
||||||
- [Runtime.getRuntime().halt() vs System.exit() in Java](https://www.baeldung.com/java-runtime-halt-vs-system-exit)
|
- [Runtime.getRuntime().halt() vs System.exit() in Java](https://www.baeldung.com/java-runtime-halt-vs-system-exit)
|
||||||
- [Adding Shutdown Hooks for JVM Applications](https://www.baeldung.com/jvm-shutdown-hooks)
|
- [Adding Shutdown Hooks for JVM Applications](https://www.baeldung.com/jvm-shutdown-hooks)
|
||||||
|
- [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object)
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.baeldung.varargs;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class HeapPollutionUnitTest {
|
||||||
|
|
||||||
|
@Test(expected = ClassCastException.class)
|
||||||
|
public void givenGenericVararg_whenUsedUnsafe_shouldThrowClassCastException() {
|
||||||
|
String one = firstOfFirst(Arrays.asList("one", "two"), Collections.emptyList());
|
||||||
|
|
||||||
|
assertEquals("one", one);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = ClassCastException.class)
|
||||||
|
public void givenGenericVararg_whenRefEscapes_mayCauseSubtleBugs() {
|
||||||
|
String[] args = returnAsIs("One", "Two");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String firstOfFirst(List<String>... strings) {
|
||||||
|
List<Integer> ints = Collections.singletonList(42);
|
||||||
|
Object[] objects = strings;
|
||||||
|
objects[0] = ints;
|
||||||
|
|
||||||
|
return strings[0].get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> T[] toArray(T... arguments) {
|
||||||
|
return arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> T[] returnAsIs(T a, T b) {
|
||||||
|
return toArray(a, b);
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,3 +8,5 @@
|
||||||
- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params)
|
- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params)
|
||||||
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
|
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
|
||||||
- [What Causes java.lang.reflect.InvocationTargetException?](https://www.baeldung.com/java-lang-reflect-invocationtargetexception)
|
- [What Causes java.lang.reflect.InvocationTargetException?](https://www.baeldung.com/java-lang-reflect-invocationtargetexception)
|
||||||
|
- [How to Find all Getters Returning Null](http://www.baeldung.com/java-getters-returning-null)
|
||||||
|
- [How to Get a Name of a Method Being Executed?](http://www.baeldung.com/java-name-of-executing-method)
|
||||||
|
|
|
@ -200,7 +200,7 @@ public class ReflectionUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
|
public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
|
||||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||||
final Bird bird = (Bird) birdClass.newInstance();
|
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
|
||||||
final Field field = birdClass.getDeclaredField("walks");
|
final Field field = birdClass.getDeclaredField("walks");
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
|
|
||||||
|
@ -266,7 +266,7 @@ public class ReflectionUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenMethod_whenInvokes_thenCorrect() throws Exception {
|
public void givenMethod_whenInvokes_thenCorrect() throws Exception {
|
||||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||||
final Bird bird = (Bird) birdClass.newInstance();
|
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
|
||||||
final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
|
final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
|
||||||
final Method walksMethod = birdClass.getDeclaredMethod("walks");
|
final Method walksMethod = birdClass.getDeclaredMethod("walks");
|
||||||
final boolean walks = (boolean) walksMethod.invoke(bird);
|
final boolean walks = (boolean) walksMethod.invoke(bird);
|
||||||
|
|
|
@ -26,6 +26,17 @@ public class StringToIntOrIntegerUnitTest {
|
||||||
assertThat(result).isEqualTo(new Integer(42));
|
assertThat(result).isEqualTo(new Integer(42));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenCallingValueOf_shouldCacheSomeValues() {
|
||||||
|
for (int i = -128; i <= 127; i++) {
|
||||||
|
String value = i + "";
|
||||||
|
Integer first = Integer.valueOf(value);
|
||||||
|
Integer second = Integer.valueOf(value);
|
||||||
|
|
||||||
|
assertThat(first).isSameAs(second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenString_whenCallingIntegerConstructor_shouldConvertToInt() {
|
public void givenString_whenCallingIntegerConstructor_shouldConvertToInt() {
|
||||||
String givenString = "42";
|
String givenString = "42";
|
||||||
|
|
|
@ -12,4 +12,6 @@ This module contains articles about string operations.
|
||||||
- [L-Trim and R-Trim Alternatives in Java](https://www.baeldung.com/java-trim-alternatives)
|
- [L-Trim and R-Trim Alternatives in Java](https://www.baeldung.com/java-trim-alternatives)
|
||||||
- [Java Convert PDF to Base64](https://www.baeldung.com/java-convert-pdf-to-base64)
|
- [Java Convert PDF to Base64](https://www.baeldung.com/java-convert-pdf-to-base64)
|
||||||
- [Encode a String to UTF-8 in Java](https://www.baeldung.com/java-string-encode-utf-8)
|
- [Encode a String to UTF-8 in Java](https://www.baeldung.com/java-string-encode-utf-8)
|
||||||
|
- [Guide to Character Encoding](https://www.baeldung.com/java-char-encoding)
|
||||||
|
- [Convert Hex to ASCII in Java](https://www.baeldung.com/java-convert-hex-to-ascii) #remove additional readme file
|
||||||
- More articles: [[<-- prev]](../core-java-string-operations)
|
- More articles: [[<-- prev]](../core-java-string-operations)
|
||||||
|
|
Binary file not shown.
|
@ -6,3 +6,4 @@ This module contains articles about the measurement of time in Java.
|
||||||
- [Guide to the Java Clock Class](http://www.baeldung.com/java-clock)
|
- [Guide to the Java Clock Class](http://www.baeldung.com/java-clock)
|
||||||
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
|
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
|
||||||
- [Overriding System Time for Testing in Java](https://www.baeldung.com/java-override-system-time)
|
- [Overriding System Time for Testing in Java](https://www.baeldung.com/java-override-system-time)
|
||||||
|
- [Java Timer](http://www.baeldung.com/java-timer-and-timertask)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.java.clock;
|
package com.baeldung.clock;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
|
@ -1,35 +1,13 @@
|
||||||
## Core Java Cookbooks and Examples
|
## Core Java Cookbooks and Examples
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Java Timer](http://www.baeldung.com/java-timer-and-timertask)
|
[Getting Started with Java Properties](http://www.baeldung.com/java-properties)
|
||||||
- [Getting Started with Java Properties](http://www.baeldung.com/java-properties)
|
[Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency)
|
||||||
- [Introduction to Nashorn](http://www.baeldung.com/java-nashorn)
|
[Introduction to Java Serialization](http://www.baeldung.com/java-serialization)
|
||||||
- [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency)
|
[Guide to UUID in Java](http://www.baeldung.com/java-uuid)
|
||||||
- [JVM Log Forging](http://www.baeldung.com/jvm-log-forging)
|
[Compiling Java *.class Files with javac](http://www.baeldung.com/javac)
|
||||||
- [How to Find all Getters Returning Null](http://www.baeldung.com/java-getters-returning-null)
|
[Introduction to Javadoc](http://www.baeldung.com/javadoc)
|
||||||
- [How to Get a Name of a Method Being Executed?](http://www.baeldung.com/java-name-of-executing-method)
|
[Guide to the Externalizable Interface in Java](http://www.baeldung.com/java-externalizable)
|
||||||
- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization)
|
[What is the serialVersionUID?](http://www.baeldung.com/java-serial-version-uid)
|
||||||
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
|
[A Guide to the ResourceBundle](http://www.baeldung.com/java-resourcebundle)
|
||||||
- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin)
|
[Merging java.util.Properties Objects](https://www.baeldung.com/java-merging-properties)
|
||||||
- [Quick Guide to the Java Stack](https://www.baeldung.com/java-stack)
|
|
||||||
- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac)
|
|
||||||
- [Introduction to Javadoc](http://www.baeldung.com/javadoc)
|
|
||||||
- [Guide to the Externalizable Interface in Java](http://www.baeldung.com/java-externalizable)
|
|
||||||
- [ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java)
|
|
||||||
- [What is the serialVersionUID?](http://www.baeldung.com/java-serial-version-uid)
|
|
||||||
- [A Guide to the ResourceBundle](http://www.baeldung.com/java-resourcebundle)
|
|
||||||
- [Java Global Exception Handler](http://www.baeldung.com/java-global-exception-handler)
|
|
||||||
- [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object)
|
|
||||||
- [Common Java Exceptions](http://www.baeldung.com/java-common-exceptions)
|
|
||||||
- [Merging java.util.Properties Objects](https://www.baeldung.com/java-merging-properties)
|
|
||||||
- [Java – Try with Resources](https://www.baeldung.com/java-try-with-resources)
|
|
||||||
- [Guide to Character Encoding](https://www.baeldung.com/java-char-encoding)
|
|
||||||
- [Graphs in Java](https://www.baeldung.com/java-graphs)
|
|
||||||
- [Read and Write User Input in Java](http://www.baeldung.com/java-console-input-output)
|
|
||||||
- [Formatting with printf() in Java](https://www.baeldung.com/java-printstream-printf)
|
|
||||||
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields)
|
|
||||||
- [Using Curl in Java](https://www.baeldung.com/java-curl)
|
|
||||||
- [Finding Leap Years in Java](https://www.baeldung.com/java-leap-year)
|
|
||||||
- [Making a JSON POST Request With HttpURLConnection](https://www.baeldung.com/httpurlconnection-post)
|
|
||||||
- [How to Find an Exception’s Root Cause in Java](https://www.baeldung.com/java-exception-root-cause)
|
|
||||||
- [Convert Hex to ASCII in Java](https://www.baeldung.com/java-convert-hex-to-ascii)
|
|
||||||
|
|
|
@ -3,30 +3,57 @@ package com.baeldung.uuid;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class UUIDGenerator {
|
public class UUIDGenerator {
|
||||||
|
|
||||||
/**
|
|
||||||
* These are predefined UUID for name spaces
|
|
||||||
*/
|
|
||||||
private static final String NAMESPACE_DNS = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
|
|
||||||
private static final String NAMESPACE_URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
|
|
||||||
private static final String NAMESPACE_OID = "6ba7b812-9dad-11d1-80b4-00c04fd430c8";
|
|
||||||
private static final String NAMESPACE_X500 = "6ba7b814-9dad-11d1-80b4-00c04fd430c8";
|
|
||||||
|
|
||||||
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
|
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
try {
|
* Type 1 UUID Generation
|
||||||
System.out.println("Type 3 : " + generateType3UUID(NAMESPACE_DNS, "google.com"));
|
*/
|
||||||
System.out.println("Type 4 : " + generateType4UUID());
|
public static UUID generateType1UUID() {
|
||||||
System.out.println("Type 5 : " + generateType5UUID(NAMESPACE_URL, "google.com"));
|
|
||||||
System.out.println("Unique key : " + generateUniqueKeysWithUUIDAndMessageDigest());
|
long most64SigBits = get64MostSignificantBitsForVersion1();
|
||||||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
|
long least64SigBits = get64LeastSignificantBitsForVersion1();
|
||||||
e.printStackTrace();
|
|
||||||
}
|
return new UUID(most64SigBits, least64SigBits);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long get64LeastSignificantBitsForVersion1() {
|
||||||
|
Random random = new Random();
|
||||||
|
long random63BitLong = random.nextLong() & 0x3FFFFFFFFFFFFFFFL;
|
||||||
|
long variant3BitFlag = 0x8000000000000000L;
|
||||||
|
return random63BitLong + variant3BitFlag;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long get64MostSignificantBitsForVersion1() {
|
||||||
|
LocalDateTime start = LocalDateTime.of(1582, 10, 15, 0, 0, 0);
|
||||||
|
Duration duration = Duration.between(start, LocalDateTime.now());
|
||||||
|
long seconds = duration.getSeconds();
|
||||||
|
long nanos = duration.getNano();
|
||||||
|
long timeForUuidIn100Nanos = seconds * 10000000 + nanos * 100;
|
||||||
|
long least12SignificatBitOfTime = (timeForUuidIn100Nanos & 0x000000000000FFFFL) >> 4;
|
||||||
|
long version = 1 << 12;
|
||||||
|
return (timeForUuidIn100Nanos & 0xFFFFFFFFFFFF0000L) + version + least12SignificatBitOfTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type 3 UUID Generation
|
||||||
|
*
|
||||||
|
* @throws UnsupportedEncodingException
|
||||||
|
*/
|
||||||
|
public static UUID generateType3UUID(String namespace, String name) throws UnsupportedEncodingException {
|
||||||
|
|
||||||
|
byte[] nameSpaceBytes = bytesFromUUID(namespace);
|
||||||
|
byte[] nameBytes = name.getBytes("UTF-8");
|
||||||
|
byte[] result = joinBytes(nameSpaceBytes, nameBytes);
|
||||||
|
|
||||||
|
return UUID.nameUUIDFromBytes(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,28 +64,18 @@ public class UUIDGenerator {
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Type 3 UUID Generation
|
|
||||||
*
|
|
||||||
* @throws UnsupportedEncodingException
|
|
||||||
*/
|
|
||||||
public static UUID generateType3UUID(String namespace, String name) throws UnsupportedEncodingException {
|
|
||||||
String source = namespace + name;
|
|
||||||
byte[] bytes = source.getBytes("UTF-8");
|
|
||||||
UUID uuid = UUID.nameUUIDFromBytes(bytes);
|
|
||||||
return uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Type 5 UUID Generation
|
* Type 5 UUID Generation
|
||||||
*
|
*
|
||||||
* @throws UnsupportedEncodingException
|
* @throws UnsupportedEncodingException
|
||||||
*/
|
*/
|
||||||
public static UUID generateType5UUID(String namespace, String name) throws UnsupportedEncodingException {
|
public static UUID generateType5UUID(String namespace, String name) throws UnsupportedEncodingException {
|
||||||
String source = namespace + name;
|
|
||||||
byte[] bytes = source.getBytes("UTF-8");
|
byte[] nameSpaceBytes = bytesFromUUID(namespace);
|
||||||
UUID uuid = type5UUIDFromBytes(bytes);
|
byte[] nameBytes = name.getBytes("UTF-8");
|
||||||
return uuid;
|
byte[] result = joinBytes(nameSpaceBytes, nameBytes);
|
||||||
|
|
||||||
|
return type5UUIDFromBytes(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UUID type5UUIDFromBytes(byte[] name) {
|
public static UUID type5UUIDFromBytes(byte[] name) {
|
||||||
|
@ -91,20 +108,20 @@ public class UUIDGenerator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unique Keys Generation Using Message Digest and Type 4 UUID
|
* Unique Keys Generation Using Message Digest and Type 4 UUID
|
||||||
*
|
*
|
||||||
* @throws NoSuchAlgorithmException
|
* @throws NoSuchAlgorithmException
|
||||||
* @throws UnsupportedEncodingException
|
* @throws UnsupportedEncodingException
|
||||||
*/
|
*/
|
||||||
public static String generateUniqueKeysWithUUIDAndMessageDigest() throws NoSuchAlgorithmException, UnsupportedEncodingException {
|
public static String generateUniqueKeysWithUUIDAndMessageDigest() throws NoSuchAlgorithmException, UnsupportedEncodingException {
|
||||||
MessageDigest salt = MessageDigest.getInstance("SHA-256");
|
MessageDigest salt = MessageDigest.getInstance("SHA-256");
|
||||||
salt.update(UUID.randomUUID()
|
salt.update(UUID.randomUUID()
|
||||||
.toString()
|
.toString()
|
||||||
.getBytes("UTF-8"));
|
.getBytes("UTF-8"));
|
||||||
String digest = bytesToHex(salt.digest());
|
String digest = bytesToHex(salt.digest());
|
||||||
return digest;
|
return digest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String bytesToHex(byte[] bytes) {
|
private static String bytesToHex(byte[] bytes) {
|
||||||
char[] hexChars = new char[bytes.length * 2];
|
char[] hexChars = new char[bytes.length * 2];
|
||||||
for (int j = 0; j < bytes.length; j++) {
|
for (int j = 0; j < bytes.length; j++) {
|
||||||
int v = bytes[j] & 0xFF;
|
int v = bytes[j] & 0xFF;
|
||||||
|
@ -114,4 +131,37 @@ public class UUIDGenerator {
|
||||||
return new String(hexChars);
|
return new String(hexChars);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
private static byte[] bytesFromUUID(String uuidHexString) {
|
||||||
|
String normalizedUUIDHexString = uuidHexString.replace("-","");
|
||||||
|
|
||||||
|
assert normalizedUUIDHexString.length() == 32;
|
||||||
|
|
||||||
|
byte[] bytes = new byte[16];
|
||||||
|
for (int i = 0; i < 16; i++) {
|
||||||
|
byte b = hexToByte(normalizedUUIDHexString.substring(i*2, i*2+2));
|
||||||
|
bytes[i] = b;
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte hexToByte(String hexString) {
|
||||||
|
int firstDigit = Character.digit(hexString.charAt(0),16);
|
||||||
|
int secondDigit = Character.digit(hexString.charAt(1),16);
|
||||||
|
return (byte) ((firstDigit << 4) + secondDigit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] joinBytes(byte[] byteArray1, byte[] byteArray2) {
|
||||||
|
int finalLength = byteArray1.length + byteArray2.length;
|
||||||
|
byte[] result = new byte[finalLength];
|
||||||
|
|
||||||
|
for(int i = 0; i < byteArray1.length; i++) {
|
||||||
|
result[i] = byteArray1[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < byteArray2.length; i++) {
|
||||||
|
result[byteArray1.length+i] = byteArray2[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package com.baeldung.uuid;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class UUIDGeneratorUnitTest {
|
||||||
|
|
||||||
|
private static final String NAMESPACE_URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
|
||||||
|
private static final String NAMESPACE_DNS = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void version_1_UUID_is_generated_with_correct_length_version_and_variant() {
|
||||||
|
|
||||||
|
UUID uuid = UUIDGenerator.generateType1UUID();
|
||||||
|
|
||||||
|
assertEquals(36, uuid.toString().length());
|
||||||
|
assertEquals(1, uuid.version());
|
||||||
|
assertEquals(2, uuid.variant());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void version_3_UUID_is_correctly_generated_for_domain_baeldung_com() throws UnsupportedEncodingException {
|
||||||
|
|
||||||
|
UUID uuid = UUIDGenerator.generateType3UUID(NAMESPACE_DNS, "baeldung.com");
|
||||||
|
|
||||||
|
assertEquals("23785b78-0132-3ac6-aff6-cfd5be162139", uuid.toString());
|
||||||
|
assertEquals(3, uuid.version());
|
||||||
|
assertEquals(2, uuid.variant());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void version_3_UUID_is_correctly_generated_for_domain_d() throws UnsupportedEncodingException {
|
||||||
|
|
||||||
|
UUID uuid = UUIDGenerator.generateType3UUID(NAMESPACE_DNS, "d");
|
||||||
|
|
||||||
|
assertEquals("dbd41ecb-f466-33de-b309-1468addfc63b", uuid.toString());
|
||||||
|
assertEquals(3, uuid.version());
|
||||||
|
assertEquals(2, uuid.variant());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void version_4_UUID_is_generated_with_correct_length_version_and_variant() {
|
||||||
|
|
||||||
|
UUID uuid = UUIDGenerator.generateType4UUID();
|
||||||
|
|
||||||
|
assertEquals(36, uuid.toString().length());
|
||||||
|
assertEquals(4, uuid.version());
|
||||||
|
assertEquals(2, uuid.variant());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void version_5_UUID_is_correctly_generated_for_domain_baeldung_com() throws UnsupportedEncodingException {
|
||||||
|
|
||||||
|
UUID uuid = UUIDGenerator.generateType5UUID(NAMESPACE_URL, "baeldung.com");
|
||||||
|
|
||||||
|
assertEquals("aeff44a5-8a61-52b6-bcbe-c8e5bd7d0300", uuid.toString());
|
||||||
|
assertEquals(5, uuid.version());
|
||||||
|
assertEquals(2, uuid.variant());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +0,0 @@
|
||||||
## Core Kotlin 2
|
|
||||||
|
|
||||||
This module contains articles about Kotlin core features.
|
|
||||||
|
|
||||||
### Relevant articles:
|
|
||||||
- [Working with Dates in Kotlin](https://www.baeldung.com/kotlin-dates)
|
|
||||||
- [Kotlin Ternary Conditional Operator](https://www.baeldung.com/kotlin-ternary-operator)
|
|
||||||
- [Sequences in Kotlin](https://www.baeldung.com/kotlin/sequences)
|
|
||||||
- [[<-- Prev]](/core-kotlin-modules/core-kotlin)
|
|
|
@ -10,3 +10,4 @@ This module contains articles about core Kotlin collections.
|
||||||
- [Filtering Kotlin Collections](https://www.baeldung.com/kotlin-filter-collection)
|
- [Filtering Kotlin Collections](https://www.baeldung.com/kotlin-filter-collection)
|
||||||
- [Collection Transformations in Kotlin](https://www.baeldung.com/kotlin-collection-transformations)
|
- [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)
|
- [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)
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
## Core Kotlin
|
||||||
|
|
||||||
|
This module contains articles about data structures in Kotlin
|
||||||
|
|
||||||
|
### Relevant articles:
|
||||||
|
[Implementing a Binary Tree in Kotlin](https://www.baeldung.com/kotlin-binary-tree)
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?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-datastructures</artifactId>
|
||||||
|
<name>core-kotlin-datastructures</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.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<junit.platform.version>1.1.1</junit.platform.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.binarytree
|
package com.binarytree
|
||||||
|
|
||||||
import org.junit.After
|
import org.junit.After
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
|
@ -0,0 +1,6 @@
|
||||||
|
## Core Kotlin Date and Time
|
||||||
|
|
||||||
|
This module contains articles about Kotlin core date/time features.
|
||||||
|
|
||||||
|
### Relevant articles:
|
||||||
|
[Working with Dates in Kotlin](https://www.baeldung.com/kotlin-dates)
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?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-date-time</artifactId>
|
||||||
|
<name>core-kotlin-date-time</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.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${org.assertj.core.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<junit.platform.version>1.1.1</junit.platform.version>
|
||||||
|
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
## Core Kotlin Design Patterns
|
||||||
|
|
||||||
|
This module contains articles about design patterns in Kotlin
|
||||||
|
|
||||||
|
### Relevant articles:
|
||||||
|
- [Creational Design Patterns in Kotlin: Builder](https://www.baeldung.com/kotlin-builder-pattern)
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?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-design-patterns</artifactId>
|
||||||
|
<name>core-kotlin-design-patterns</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.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<junit.platform.version>1.1.1</junit.platform.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -10,4 +10,5 @@ This module contains articles about core features in the Kotlin language.
|
||||||
- [Initializing Arrays in Kotlin](https://www.baeldung.com/kotlin-initialize-array)
|
- [Initializing Arrays in Kotlin](https://www.baeldung.com/kotlin-initialize-array)
|
||||||
- [Lazy Initialization in Kotlin](https://www.baeldung.com/kotlin-lazy-initialization)
|
- [Lazy Initialization in Kotlin](https://www.baeldung.com/kotlin-lazy-initialization)
|
||||||
- [Comprehensive Guide to Null Safety in Kotlin](https://www.baeldung.com/kotlin-null-safety)
|
- [Comprehensive Guide to Null Safety in Kotlin](https://www.baeldung.com/kotlin-null-safety)
|
||||||
|
- [Kotlin Scope Functions](https://www.baeldung.com/kotlin-scope-functions)
|
||||||
- [[<-- Prev]](/core-kotlin-modules/core-kotlin-lang)
|
- [[<-- Prev]](/core-kotlin-modules/core-kotlin-lang)
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
## Core Kotlin Testing
|
||||||
|
|
||||||
|
This module contains articles about testing in Kotlin
|
||||||
|
|
||||||
|
### Relevant articles:
|
||||||
|
- [JUnit 5 for Kotlin Developers](https://www.baeldung.com/junit-5-kotlin)
|
|
@ -3,8 +3,8 @@
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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>
|
||||||
<artifactId>core-kotlin-2</artifactId>
|
<artifactId>core-kotlin-testing</artifactId>
|
||||||
<name>core-kotlin-2</name>
|
<name>core-kotlin-testing</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
@ -15,11 +15,15 @@
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.junit.platform</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
<version>${assertj.version}</version>
|
<version>${junit.platform.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<junit.platform.version>1.1.1</junit.platform.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -7,13 +7,5 @@ This module contains articles about Kotlin core features.
|
||||||
- [Kotlin Java Interoperability](https://www.baeldung.com/kotlin-java-interoperability)
|
- [Kotlin Java Interoperability](https://www.baeldung.com/kotlin-java-interoperability)
|
||||||
- [Get a Random Number in Kotlin](https://www.baeldung.com/kotlin-random-number)
|
- [Get a Random Number in Kotlin](https://www.baeldung.com/kotlin-random-number)
|
||||||
- [Create a Java and Kotlin Project with Maven](https://www.baeldung.com/kotlin-maven-java-project)
|
- [Create a Java and Kotlin Project with Maven](https://www.baeldung.com/kotlin-maven-java-project)
|
||||||
- [Guide to Sorting in Kotlin](https://www.baeldung.com/kotlin-sort)
|
- [Kotlin Ternary Conditional Operator](https://www.baeldung.com/kotlin-ternary-operator)
|
||||||
- [Creational Design Patterns in Kotlin: Builder](https://www.baeldung.com/kotlin-builder-pattern)
|
- [Sequences in Kotlin](https://www.baeldung.com/kotlin/sequences)
|
||||||
- [Kotlin Scope Functions](https://www.baeldung.com/kotlin-scope-functions)
|
|
||||||
- [Implementing a Binary Tree in Kotlin](https://www.baeldung.com/kotlin-binary-tree)
|
|
||||||
- [JUnit 5 for Kotlin Developers](https://www.baeldung.com/junit-5-kotlin)
|
|
||||||
- [Converting Kotlin Data Class from JSON using GSON](https://www.baeldung.com/kotlin-json-convert-data-class)
|
|
||||||
- [Fuel HTTP Library with Kotlin](https://www.baeldung.com/kotlin-fuel)
|
|
||||||
- [Introduction to Kovenant Library for Kotlin](https://www.baeldung.com/kotlin-kovenant)
|
|
||||||
- [Dependency Injection for Kotlin with Injekt](https://www.baeldung.com/kotlin-dependency-injection-with-injekt)
|
|
||||||
- [[More --> ]](/core-kotlin-modules/core-kotlin-2)
|
|
||||||
|
|
|
@ -21,4 +21,12 @@ class TernaryOperatorTest {
|
||||||
}
|
}
|
||||||
assertEquals("yes", result)
|
assertEquals("yes", result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `using elvis`() {
|
||||||
|
val a: String? = null
|
||||||
|
val result = a ?: "Default"
|
||||||
|
|
||||||
|
assertEquals("Default", result)
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -18,17 +18,19 @@
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>core-kotlin</module>
|
<module>core-kotlin</module>
|
||||||
<module>core-kotlin-2</module>
|
|
||||||
<module>core-kotlin-advanced</module>
|
<module>core-kotlin-advanced</module>
|
||||||
<module>core-kotlin-annotations</module>
|
<module>core-kotlin-annotations</module>
|
||||||
<module>core-kotlin-collections</module>
|
<module>core-kotlin-collections</module>
|
||||||
<module>core-kotlin-concurrency</module>
|
<module>core-kotlin-concurrency</module>
|
||||||
|
<module>core-kotlin-date-time</module>
|
||||||
|
<module>core-kotlin-design-patterns</module>
|
||||||
<module>core-kotlin-io</module>
|
<module>core-kotlin-io</module>
|
||||||
<module>core-kotlin-lang</module>
|
<module>core-kotlin-lang</module>
|
||||||
<module>core-kotlin-lang-2</module>
|
<module>core-kotlin-lang-2</module>
|
||||||
<module>core-kotlin-strings</module>
|
|
||||||
<module>core-kotlin-lang-oop</module>
|
<module>core-kotlin-lang-oop</module>
|
||||||
<module>core-kotlin-lang-oop-2</module>
|
<module>core-kotlin-lang-oop-2</module>
|
||||||
|
<module>core-kotlin-strings</module>
|
||||||
|
<module>core-kotlin-testing</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -10,3 +10,4 @@ This module contains articles about data structures in Java
|
||||||
- [How to Print a Binary Tree Diagram](https://www.baeldung.com/java-print-binary-tree-diagram)
|
- [How to Print a Binary Tree Diagram](https://www.baeldung.com/java-print-binary-tree-diagram)
|
||||||
- [Introduction to Big Queue](https://www.baeldung.com/java-big-queue)
|
- [Introduction to Big Queue](https://www.baeldung.com/java-big-queue)
|
||||||
- [Guide to AVL Trees in Java](https://www.baeldung.com/java-avl-trees)
|
- [Guide to AVL Trees in Java](https://www.baeldung.com/java-avl-trees)
|
||||||
|
- [Graphs in Java](https://www.baeldung.com/java-graphs)
|
||||||
|
|
|
@ -12,8 +12,9 @@
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung.dddmodules</groupId>
|
<groupId>com.baeldung.dddmodules</groupId>
|
||||||
<artifactId>dddmodules</artifactId>
|
<artifactId>ddd-modules</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
|
<relativePath>../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
|
@ -11,8 +11,9 @@
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung.dddmodules</groupId>
|
<groupId>com.baeldung.dddmodules</groupId>
|
||||||
<artifactId>dddmodules</artifactId>
|
<artifactId>ddd-modules</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
|
<relativePath>../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
|
@ -11,8 +11,9 @@
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung.dddmodules</groupId>
|
<groupId>com.baeldung.dddmodules</groupId>
|
||||||
<artifactId>dddmodules</artifactId>
|
<artifactId>ddd-modules</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
|
<relativePath>../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue