Merge remote-tracking branch 'baeldung/master' into BAEL-3841#declarative-caching-testing

This commit is contained in:
Adrian Maghear 2020-06-10 19:41:38 +02:00
commit e1a6218c1b
207 changed files with 2042 additions and 406 deletions

View File

@ -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 {}

View File

@ -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)
}
}

View File

@ -4,19 +4,19 @@ import java.util.Objects;
public record Person (String name, String address) {
public static String UNKWOWN_ADDRESS = "Unknown";
public static String UNNAMED = "Unnamed";
public static String UNKNOWN_ADDRESS = "Unknown";
public static String UNNAMED = "Unnamed";
public Person {
Objects.requireNonNull(name);
Objects.requireNonNull(address);
}
public Person {
Objects.requireNonNull(name);
Objects.requireNonNull(address);
}
public Person(String name) {
this(name, UNKWOWN_ADDRESS);
}
public Person(String name) {
this(name, UNKNOWN_ADDRESS);
}
public static Person unnamed(String address) {
return new Person(UNNAMED, address);
}
}
public static Person unnamed(String address) {
return new Person(UNNAMED, address);
}
}

View File

@ -134,7 +134,7 @@ public class PersonTest {
Person person = new Person(name);
assertEquals(name, person.name());
assertEquals(Person.UNKWOWN_ADDRESS, person.address());
assertEquals(Person.UNKNOWN_ADDRESS, person.address());
}
@Test
@ -147,4 +147,4 @@ public class PersonTest {
assertEquals(Person.UNNAMED, person.name());
assertEquals(address, person.address());
}
}
}

View File

@ -4,7 +4,7 @@
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-8-datetime</artifactId>
<artifactId>core-java-8-datetime-2</artifactId>
<version>${project.parent.version}</version>
<name>core-java-8-datetime</name>
<packaging>jar</packaging>
@ -41,7 +41,6 @@
</dependencies>
<build>
<finalName>core-java-datetime-java8</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>

View File

@ -5,7 +5,7 @@ import org.junit.Test;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
public class VariableHandlesUnitTest {
@ -18,25 +18,23 @@ public class VariableHandlesUnitTest {
@Test
public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
VarHandle publicIntHandle = MethodHandles
VarHandle PUBLIC_TEST_VARIABLE = MethodHandles
.lookup()
.in(VariableHandlesUnitTest.class)
.findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class);
assertThat(publicIntHandle.coordinateTypes().size() == 1);
assertThat(publicIntHandle.coordinateTypes().get(0) == VariableHandlesUnitTest.class);
assertEquals(1, PUBLIC_TEST_VARIABLE.coordinateTypes().size());
assertEquals(VariableHandlesUnitTest.class, PUBLIC_TEST_VARIABLE.coordinateTypes().get(0));
}
@Test
public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
VarHandle privateIntHandle = MethodHandles
VarHandle PRIVATE_TEST_VARIABLE = MethodHandles
.privateLookupIn(VariableHandlesUnitTest.class, MethodHandles.lookup())
.findVarHandle(VariableHandlesUnitTest.class, "privateTestVariable", int.class);
assertThat(privateIntHandle.coordinateTypes().size() == 1);
assertThat(privateIntHandle.coordinateTypes().get(0) == VariableHandlesUnitTest.class);
assertEquals(1, PRIVATE_TEST_VARIABLE.coordinateTypes().size());
assertEquals(VariableHandlesUnitTest.class, PRIVATE_TEST_VARIABLE.coordinateTypes().get(0));
}
@Test
@ -44,63 +42,64 @@ public class VariableHandlesUnitTest {
VarHandle arrayVarHandle = MethodHandles
.arrayElementVarHandle(int[].class);
assertThat(arrayVarHandle.coordinateTypes().size() == 2);
assertThat(arrayVarHandle.coordinateTypes().get(0) == int[].class);
assertEquals(2, arrayVarHandle.coordinateTypes().size());
assertEquals(int[].class, arrayVarHandle.coordinateTypes().get(0));
}
@Test
public void givenVarHandle_whenGetIsInvoked_ThenValueOfVariableIsReturned() throws NoSuchFieldException, IllegalAccessException {
VarHandle publicIntHandle = MethodHandles
VarHandle PUBLIC_TEST_VARIABLE = MethodHandles
.lookup()
.in(VariableHandlesUnitTest.class)
.findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class);
assertThat((int) publicIntHandle.get(this) == 1);
assertEquals(1, (int) PUBLIC_TEST_VARIABLE.get(this));
}
@Test
public void givenVarHandle_whenSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
VarHandle publicIntHandle = MethodHandles
VarHandle VARIABLE_TO_SET = MethodHandles
.lookup()
.in(VariableHandlesUnitTest.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
public void givenVarHandle_whenCompareAndSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
VarHandle publicIntHandle = MethodHandles
VarHandle VARIABLE_TO_COMPARE_AND_SET = MethodHandles
.lookup()
.in(VariableHandlesUnitTest.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
public void givenVarHandle_whenGetAndAddIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
VarHandle publicIntHandle = MethodHandles
VarHandle VARIABLE_TO_GET_AND_ADD = MethodHandles
.lookup()
.in(VariableHandlesUnitTest.class)
.findVarHandle(VariableHandlesUnitTest.class, "variableToGetAndAdd", int.class);
int before = (int) publicIntHandle.getAndAdd(this, 200);
assertThat(before == 0);
assertThat((int) publicIntHandle.get(this) == 200);
int before = (int) VARIABLE_TO_GET_AND_ADD.getAndAdd(this, 200);
assertEquals(0, before);
assertEquals(200, (int) VARIABLE_TO_GET_AND_ADD.get(this));
}
@Test
public void givenVarHandle_whenGetAndBitwiseOrIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
VarHandle publicIntHandle = MethodHandles
VarHandle VARIABLE_TO_BITWISE_OR = MethodHandles
.lookup()
.in(VariableHandlesUnitTest.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);
assertThat(variableToBitwiseOr == 127);
assertEquals(0, before);
assertEquals(127, (byte) VARIABLE_TO_BITWISE_OR.get(this));
}
}

View File

@ -9,3 +9,4 @@
- [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)
- [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)

View File

@ -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;
}
}

View File

@ -9,13 +9,11 @@ public class StampedAccount {
private AtomicStampedReference<Integer> account = new AtomicStampedReference<>(0, 0);
public int getBalance() {
return this.account.get(new int[1]);
return account.getReference();
}
public int getStamp() {
int[] stamps = new int[1];
this.account.get(stamps);
return stamps[0];
return account.getStamp();
}
public boolean deposit(int funds) {

View File

@ -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());
}
}

View File

@ -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)

View File

@ -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>

View File

@ -1,10 +1,9 @@
package com.baeldung.asciiart;
import java.awt.Font;
import com.baeldung.asciiart.AsciiArt.Settings;
import org.junit.Test;
import com.baeldung.asciiart.AsciiArt.Settings;
import java.awt.*;
public class AsciiArtIntegrationTest {
@ -16,5 +15,4 @@ public class AsciiArtIntegrationTest {
asciiArt.drawString(text, "*", settings);
}
}

View File

@ -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)
- [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)
- [Finding Leap Years in Java](https://www.baeldung.com/java-leap-year)
- [[<-- Prev]](/core-java-modules/core-java-date-operations-1)

View File

@ -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)
- [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)
- [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object)

View File

@ -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);
}
}

View File

@ -8,3 +8,5 @@
- [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)
- [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)

View File

@ -200,7 +200,7 @@ public class ReflectionUnitTest {
@Test
public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
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");
field.setAccessible(true);
@ -266,7 +266,7 @@ public class ReflectionUnitTest {
@Test
public void givenMethod_whenInvokes_thenCorrect() throws Exception {
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 walksMethod = birdClass.getDeclaredMethod("walks");
final boolean walks = (boolean) walksMethod.invoke(bird);

View File

@ -26,6 +26,17 @@ public class StringToIntOrIntegerUnitTest {
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
public void givenString_whenCallingIntegerConstructor_shouldConvertToInt() {
String givenString = "42";

View File

@ -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)
- [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)
- [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)

View File

@ -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)
- [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)
- [Java Timer](http://www.baeldung.com/java-timer-and-timertask)

View File

@ -1,4 +1,4 @@
package com.baeldung.java.clock;
package com.baeldung.clock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

View File

@ -1,35 +1,13 @@
## Core Java Cookbooks and Examples
### Relevant Articles:
- [Java Timer](http://www.baeldung.com/java-timer-and-timertask)
- [Getting Started with Java Properties](http://www.baeldung.com/java-properties)
- [Introduction to Nashorn](http://www.baeldung.com/java-nashorn)
- [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency)
- [JVM Log Forging](http://www.baeldung.com/jvm-log-forging)
- [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)
- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization)
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin)
- [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 Exceptions 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)
[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 Java Serialization](http://www.baeldung.com/java-serialization)
[Guide to UUID in Java](http://www.baeldung.com/java-uuid)
[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)
[What is the serialVersionUID?](http://www.baeldung.com/java-serial-version-uid)
[A Guide to the ResourceBundle](http://www.baeldung.com/java-resourcebundle)
[Merging java.util.Properties Objects](https://www.baeldung.com/java-merging-properties)

View File

@ -3,30 +3,57 @@ package com.baeldung.uuid;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
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();
public static void main(String[] args) {
try {
System.out.println("Type 3 : " + generateType3UUID(NAMESPACE_DNS, "google.com"));
System.out.println("Type 4 : " + generateType4UUID());
System.out.println("Type 5 : " + generateType5UUID(NAMESPACE_URL, "google.com"));
System.out.println("Unique key : " + generateUniqueKeysWithUUIDAndMessageDigest());
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
e.printStackTrace();
}
/**
* Type 1 UUID Generation
*/
public static UUID generateType1UUID() {
long most64SigBits = get64MostSignificantBitsForVersion1();
long least64SigBits = get64LeastSignificantBitsForVersion1();
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;
}
/**
* 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
*
* @throws UnsupportedEncodingException
*
* @throws UnsupportedEncodingException
*/
public static UUID generateType5UUID(String namespace, String name) throws UnsupportedEncodingException {
String source = namespace + name;
byte[] bytes = source.getBytes("UTF-8");
UUID uuid = type5UUIDFromBytes(bytes);
return uuid;
byte[] nameSpaceBytes = bytesFromUUID(namespace);
byte[] nameBytes = name.getBytes("UTF-8");
byte[] result = joinBytes(nameSpaceBytes, nameBytes);
return type5UUIDFromBytes(result);
}
public static UUID type5UUIDFromBytes(byte[] name) {
@ -91,20 +108,20 @@ public class UUIDGenerator {
/**
* Unique Keys Generation Using Message Digest and Type 4 UUID
*
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static String generateUniqueKeysWithUUIDAndMessageDigest() throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest salt = MessageDigest.getInstance("SHA-256");
salt.update(UUID.randomUUID()
.toString()
.getBytes("UTF-8"));
.toString()
.getBytes("UTF-8"));
String digest = bytesToHex(salt.digest());
return digest;
}
public static String bytesToHex(byte[] bytes) {
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
@ -114,4 +131,37 @@ public class UUIDGenerator {
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;
}
}

View File

@ -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());
}
}

View File

@ -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)

View File

@ -10,3 +10,4 @@ This module contains articles about core Kotlin collections.
- [Filtering Kotlin Collections](https://www.baeldung.com/kotlin-filter-collection)
- [Collection Transformations in Kotlin](https://www.baeldung.com/kotlin-collection-transformations)
- [Difference between fold and reduce in Kotlin](https://www.baeldung.com/kotlin/fold-vs-reduce)
- [Guide to Sorting in Kotlin](https://www.baeldung.com/kotlin-sort)

View File

@ -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)

View File

@ -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>

View File

@ -1,4 +1,4 @@
package com.baeldung.binarytree
package com.binarytree
import org.junit.After
import org.junit.Assert.assertEquals

View File

@ -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)

View File

@ -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>

View File

@ -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)

View File

@ -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>

View File

@ -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)
- [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)
- [Kotlin Scope Functions](https://www.baeldung.com/kotlin-scope-functions)
- [[<-- Prev]](/core-kotlin-modules/core-kotlin-lang)

View File

@ -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)

View File

@ -3,8 +3,8 @@
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-2</artifactId>
<name>core-kotlin-2</name>
<artifactId>core-kotlin-testing</artifactId>
<name>core-kotlin-testing</name>
<packaging>jar</packaging>
<parent>
@ -15,11 +15,15 @@
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<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>

View File

@ -7,13 +7,5 @@ This module contains articles about Kotlin core features.
- [Kotlin Java Interoperability](https://www.baeldung.com/kotlin-java-interoperability)
- [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)
- [Guide to Sorting in Kotlin](https://www.baeldung.com/kotlin-sort)
- [Creational Design Patterns in Kotlin: Builder](https://www.baeldung.com/kotlin-builder-pattern)
- [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)
- [Kotlin Ternary Conditional Operator](https://www.baeldung.com/kotlin-ternary-operator)
- [Sequences in Kotlin](https://www.baeldung.com/kotlin/sequences)

View File

@ -21,4 +21,12 @@ class TernaryOperatorTest {
}
assertEquals("yes", result)
}
@Test
fun `using elvis`() {
val a: String? = null
val result = a ?: "Default"
assertEquals("Default", result)
}
}

View File

@ -18,17 +18,19 @@
<modules>
<module>core-kotlin</module>
<module>core-kotlin-2</module>
<module>core-kotlin-advanced</module>
<module>core-kotlin-annotations</module>
<module>core-kotlin-collections</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-lang</module>
<module>core-kotlin-lang-2</module>
<module>core-kotlin-strings</module>
<module>core-kotlin-lang-oop</module>
<module>core-kotlin-lang-oop-2</module>
<module>core-kotlin-strings</module>
<module>core-kotlin-testing</module>
</modules>
<build>

View File

@ -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)
- [Introduction to Big Queue](https://www.baeldung.com/java-big-queue)
- [Guide to AVL Trees in Java](https://www.baeldung.com/java-avl-trees)
- [Graphs in Java](https://www.baeldung.com/java-graphs)

View File

@ -12,8 +12,9 @@
<parent>
<groupId>com.baeldung.dddmodules</groupId>
<artifactId>dddmodules</artifactId>
<artifactId>ddd-modules</artifactId>
<version>1.0</version>
<relativePath>../</relativePath>
</parent>
<dependencies>

View File

@ -11,8 +11,9 @@
<parent>
<groupId>com.baeldung.dddmodules</groupId>
<artifactId>dddmodules</artifactId>
<artifactId>ddd-modules</artifactId>
<version>1.0</version>
<relativePath>../</relativePath>
</parent>
<dependencies>

View File

@ -11,8 +11,9 @@
<parent>
<groupId>com.baeldung.dddmodules</groupId>
<artifactId>dddmodules</artifactId>
<artifactId>ddd-modules</artifactId>
<version>1.0</version>
<relativePath>../</relativePath>
</parent>
<dependencies>

Some files were not shown because too many files have changed in this diff Show More