diff --git a/apache-poi/pom.xml b/apache-poi/pom.xml
index 1a0f77b025..546eedec5b 100644
--- a/apache-poi/pom.xml
+++ b/apache-poi/pom.xml
@@ -32,9 +32,27 @@
+
+
+
+ org.apache.maven.plugins
+ maven-resources-plugin
+ ${maven.resources.plugin.version}
+
+ UTF-8
+
+ xlsx
+ xls
+
+
+
+
+
+
5.2.0
1.0.6
+ 3.2.0
\ No newline at end of file
diff --git a/apache-poi/src/main/resources/cellstyle/CellStyleBorderHandlerTest.xlsx b/apache-poi/src/main/resources/cellstyle/CellStyleBorderHandlerTest.xlsx
new file mode 100644
index 0000000000..29f128211b
Binary files /dev/null and b/apache-poi/src/main/resources/cellstyle/CellStyleBorderHandlerTest.xlsx differ
diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellBorderHandlerUnitTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellBorderHandlerUnitTest.java
index 3cabff2dff..2a36a6b1b2 100644
--- a/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellBorderHandlerUnitTest.java
+++ b/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellBorderHandlerUnitTest.java
@@ -12,7 +12,7 @@ import java.nio.file.Paths;
import static org.junit.Assert.assertEquals;
public class CellBorderHandlerUnitTest {
- private static final String FILE_NAME = "cellstyle/CellStyleHandlerTest.xlsx";
+ private static final String FILE_NAME = "cellstyle/CellStyleBorderHandlerTest.xlsx";
private static final int SHEET_INDEX = 0;
private static CellBordersHandler cellBordersHandler;
diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/game/RockPaperScissorsGame.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/game/RockPaperScissorsGame.java
new file mode 100644
index 0000000000..fc9299f12d
--- /dev/null
+++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/game/RockPaperScissorsGame.java
@@ -0,0 +1,66 @@
+package com.baeldung.game;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+import java.util.Scanner;
+
+class RockPaperScissorsGame {
+
+ private static Map movesMap = new HashMap() {{
+ put(0, "rock");
+ put(1, "paper");
+ put(2, "scissors");
+ }};
+
+ public static void main(String[] args) {
+ Scanner scanner = new Scanner(System.in);
+ int wins = 0;
+ int losses = 0;
+
+ System.out.println("Welcome to Rock-Paper-Scissors! Please enter \"rock\", \"paper\", \"scissors\", or \"quit\" to exit.");
+
+ while (true) {
+ System.out.println("-------------------------");
+ System.out.print("Enter your move: ");
+ String playerMove = scanner.nextLine();
+
+ if (playerMove.equals("quit")) {
+ System.out.println("You won " + wins + " times and lost " + losses + " times.");
+ System.out.println("Thanks for playing! See you again.");
+ break;
+ }
+
+ if (!movesMap.containsValue(playerMove)) {
+ System.out.println("Your move isn't valid!");
+ continue;
+ }
+
+ String computerMove = getComputerMove();
+
+ if (playerMove.equals(computerMove)) {
+ System.out.println("It's a tie!");
+ } else if (isPlayerWin(playerMove, computerMove)) {
+ System.out.println("You won!");
+ wins++;
+ } else {
+ System.out.println("You lost!");
+ losses++;
+ }
+ }
+ }
+
+ private static boolean isPlayerWin(String playerMove, String computerMove) {
+ return playerMove.equals("rock") && computerMove.equals("scissors")
+ || (playerMove.equals("scissors") && computerMove.equals("paper"))
+ || (playerMove.equals("paper") && computerMove.equals("rock"));
+ }
+
+ private static String getComputerMove() {
+ Random random = new Random();
+ int randomNumber = random.nextInt(3);
+ String computerMove = movesMap.get(randomNumber);
+ System.out.println("Computer move: " + computerMove);
+ return computerMove;
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/uuid/UuidHelper.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/uuid/UuidHelper.java
new file mode 100644
index 0000000000..5807262094
--- /dev/null
+++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/uuid/UuidHelper.java
@@ -0,0 +1,33 @@
+package com.baeldung.uuid;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.UUID;
+
+public class UuidHelper {
+
+ public static byte[] convertUUIDToBytes(UUID uuid) {
+ ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
+ bb.putLong(uuid.getMostSignificantBits());
+ bb.putLong(uuid.getLeastSignificantBits());
+ return bb.array();
+ }
+
+ public static UUID convertBytesToUUID(byte[] bytes) {
+ ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
+ long high = byteBuffer.getLong();
+ long low = byteBuffer.getLong();
+ return new UUID(high, low);
+ }
+
+ public static void main(String[] args) {
+ UUID uuid = UUID.randomUUID();
+ System.out.println("Original UUID: " + uuid);
+
+ byte[] bytes = convertUUIDToBytes(uuid);
+ System.out.println("Converted byte array: " + Arrays.toString(bytes));
+
+ UUID uuidNew = convertBytesToUUID(bytes);
+ System.out.println("Converted UUID: " + uuidNew);
+ }
+}
diff --git a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/maps/initialize/EmptyMapInitializer.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/maps/initialize/EmptyMapInitializer.java
new file mode 100644
index 0000000000..3dc644f1af
--- /dev/null
+++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/maps/initialize/EmptyMapInitializer.java
@@ -0,0 +1,56 @@
+package com.baeldung.maps.initialize;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+public class EmptyMapInitializer {
+
+ public static Map articleMap;
+ static {
+ articleMap = new HashMap<>();
+ }
+
+ public static Map createEmptyMap() {
+ return Collections.emptyMap();
+ }
+
+ public void createMapUsingConstructors() {
+ Map hashMap = new HashMap();
+ Map linkedHashMap = new LinkedHashMap();
+ Map treeMap = new TreeMap();
+ }
+
+ public Map createEmptyMapUsingMapsObject() {
+ Map emptyMap = Maps.newHashMap();
+ return emptyMap;
+ }
+
+ public Map createGenericEmptyMapUsingMapsObject() {
+ Map genericEmptyMap = Maps.newHashMap();
+ return genericEmptyMap;
+ }
+
+ public static Map createMapUsingGuava() {
+ Map emptyMapUsingGuava =
+ Maps.newHashMap(ImmutableMap.of());
+ return emptyMapUsingGuava;
+ }
+
+ public SortedMap createEmptySortedMap() {
+ SortedMap sortedMap = Collections.emptySortedMap();
+ return sortedMap;
+ }
+
+ public NavigableMap createEmptyNavigableMap() {
+ NavigableMap navigableMap = Collections.emptyNavigableMap();
+ return navigableMap;
+ }
+
+}
diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/maps/initialize/EmptyMapInitializerUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/maps/initialize/EmptyMapInitializerUnitTest.java
new file mode 100644
index 0000000000..cc25205ba7
--- /dev/null
+++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/maps/initialize/EmptyMapInitializerUnitTest.java
@@ -0,0 +1,31 @@
+package com.baeldung.maps.initialize;
+
+import java.util.Map;
+import org.junit.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class EmptyMapInitializerUnitTest {
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void givenEmptyMap_whenAddingEntries_throwsException() {
+ Map map = EmptyMapInitializer.createEmptyMap();
+ map.put("key", "value");
+ }
+
+ @Test
+ public void givenEmptyMap_whenChecked_returnsTrue() {
+ assertTrue(EmptyMapInitializer.articleMap.isEmpty());
+ }
+
+ @Test
+ public void givenEmptyMap_whenCreatedUsingGuava_returnsEmptyOrNot() {
+ Map emptyMapUsingGuava =
+ EmptyMapInitializer.createMapUsingGuava();
+ assertTrue(emptyMapUsingGuava.isEmpty());
+ emptyMapUsingGuava.put("key", "value");
+ assertFalse(emptyMapUsingGuava.isEmpty());
+ }
+
+}
diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/Main.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/Main.java
new file mode 100644
index 0000000000..1024399a98
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/Main.java
@@ -0,0 +1,25 @@
+package com.baeldung.mapandhashmap;
+
+import com.baeldung.mapandhashmap.printer.HashMapPrinter;
+import com.baeldung.mapandhashmap.printer.MapPrinter;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.TreeMap;
+
+public class Main {
+ public static void main(String[] args) {
+ Map map = new HashMap<>();
+ HashMap hashMap = new HashMap<>();
+ TreeMap treeMap = new TreeMap<>();
+
+ HashMapPrinter hashMapPrinter = new HashMapPrinter();
+ hashMapPrinter.printMap(hashMap);
+// hashMapPrinter.printMap(treeMap); Compile time error
+// hashMapPrinter.printMap(map); Compile time error
+
+ MapPrinter mapPrinter = new MapPrinter();
+ mapPrinter.printMap(hashMap);
+ mapPrinter.printMap(treeMap);
+ mapPrinter.printMap(map);
+ }
+}
diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/HashMapPrinter.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/HashMapPrinter.java
new file mode 100644
index 0000000000..53c78bfc55
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/HashMapPrinter.java
@@ -0,0 +1,13 @@
+package com.baeldung.mapandhashmap.printer;
+
+import java.util.HashMap;
+import java.util.Map.Entry;
+
+public class HashMapPrinter {
+
+ public void printMap(final HashMap, ?> map) {
+ for (final Entry, ?> entry : map.entrySet()) {
+ System.out.println(entry.getKey() + " " + entry.getValue());
+ }
+ }
+}
diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapPrinter.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapPrinter.java
new file mode 100644
index 0000000000..e5c0ab49cd
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapPrinter.java
@@ -0,0 +1,13 @@
+package com.baeldung.mapandhashmap.printer;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+public class MapPrinter {
+
+ public void printMap(final Map, ?> map) {
+ for (final Entry, ?> entry : map.entrySet()) {
+ System.out.println(entry.getKey() + " " + entry.getValue());
+ }
+ }
+}
diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapReporter.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapReporter.java
new file mode 100644
index 0000000000..fd7347c2d1
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapReporter.java
@@ -0,0 +1,19 @@
+package com.baeldung.mapandhashmap.printer;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+public class MapReporter {
+
+ private final Map, ?> map;
+
+ public MapReporter(final Map, ?> map) {
+ this.map = map;
+ }
+
+ public void printMap() {
+ for (final Entry, ?> entry : this.map.entrySet()) {
+ System.out.println(entry.getKey() + " " + entry.getValue());
+ }
+ }
+}
diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/HashMapPrinterUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/HashMapPrinterUnitTest.java
new file mode 100644
index 0000000000..e1147f6e00
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/HashMapPrinterUnitTest.java
@@ -0,0 +1,56 @@
+package com.baeldung.mapandhashmap.printer;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+class HashMapPrinterUnitTest {
+
+ private final HashMapPrinter mapPrinter = new HashMapPrinter();
+ private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
+
+ @BeforeEach
+ public void setUp() {
+ System.setOut(new PrintStream(outputStreamCaptor));
+ }
+
+ @Test
+ @DisplayName("Test hash map printer with HashMap")
+ void testPrintHashMap() {
+ // given
+ String key = "HashMap";
+ String value = "Main default implementation for the Map interface";
+ String expected = key + " " + value;
+ HashMap map = new HashMap<>();
+ map.put(key, value);
+ // when
+ mapPrinter.printMap(map);
+ // then
+ String actual = outputStreamCaptor.toString().trim();
+ assertThat(actual).isEqualTo(expected);
+
+ }
+
+ @Test
+ @DisplayName("Test hash map printer with LinkedHash")
+ void testPrintLinkedHashMap() {
+ // given
+ String key = "LinkedHashMap";
+ String value = "Use this implementation if you need keep the order of elements";
+ String expected = key + " " + value;
+ LinkedHashMap map = new LinkedHashMap<>();
+ map.put(key, value);
+ // when
+ mapPrinter.printMap(map);
+ // then
+ String actual = outputStreamCaptor.toString().trim();
+ assertThat(actual).isEqualTo(expected);
+
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapPrinterUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapPrinterUnitTest.java
new file mode 100644
index 0000000000..8c45758bdf
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapPrinterUnitTest.java
@@ -0,0 +1,71 @@
+package com.baeldung.mapandhashmap.printer;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.TreeMap;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+class MapPrinterUnitTest {
+
+ private final MapPrinter mapPrinter = new MapPrinter();
+ private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
+
+ @BeforeEach
+ public void setUp() {
+ System.setOut(new PrintStream(outputStreamCaptor));
+ }
+
+ @Test
+ @DisplayName("Test printer with TreeMap")
+ void testPrintTreeMap() {
+ // given
+ String key = "TreeMap";
+ String value = "Used when sorting is needed";
+ String expected = key + " " + value;
+ TreeMap map = new TreeMap<>();
+ map.put(key, value);
+ // when
+ mapPrinter.printMap(map);
+ // then
+ String actual = outputStreamCaptor.toString().trim();
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @Test
+ @DisplayName("Test printer with HashMap")
+ void testPrintHashMap() {
+ // given
+ String key = "HashMap";
+ String value = "Main default implementation for the Map interface";
+ String expected = key + " " + value;
+ HashMap map = new HashMap<>();
+ map.put(key, value);
+ // when
+ mapPrinter.printMap(map);
+ // then
+ String actual = outputStreamCaptor.toString().trim();
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @Test
+ @DisplayName("Test printer with LinkedHash")
+ void testPrintLinkedHashMap() {
+ // given
+ String key = "LinkedHashMap";
+ String value = "Use this implementation if you need keep the order of elements";
+ String expected = key + " " + value;
+ LinkedHashMap map = new LinkedHashMap<>();
+ map.put(key, value);
+ // when
+ mapPrinter.printMap(map);
+ // then
+ String actual = outputStreamCaptor.toString().trim();
+ assertThat(actual).isEqualTo(expected);
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapReporterUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapReporterUnitTest.java
new file mode 100644
index 0000000000..8f858a75c4
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapReporterUnitTest.java
@@ -0,0 +1,74 @@
+package com.baeldung.mapandhashmap.printer;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.TreeMap;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+class MapReporterUnitTest {
+
+ private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
+
+ @BeforeEach
+ public void setUp() {
+ System.setOut(new PrintStream(outputStreamCaptor));
+ }
+
+ @Test
+ @DisplayName("Test reporter with TreeMap")
+ void testPrintTreeMap() {
+ // given
+ String key = "TreeMap";
+ String value = "Used when sorting is needed";
+ String expected = key + " " + value;
+ TreeMap map = new TreeMap<>();
+ map.put(key, value);
+ // when
+ MapReporter mapReporter = new MapReporter(map);
+ mapReporter.printMap();
+ // then
+ String actual = outputStreamCaptor.toString().trim();
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @Test
+ @DisplayName("Test reporter with HashMap")
+ void testPrintHashMap() {
+ // given
+ String key = "HashMap";
+ String value = "Main default implementation for the Map interface";
+ String expected = key + " " + value;
+ HashMap map = new HashMap<>();
+ map.put(key, value);
+ // when
+ MapReporter mapReporter = new MapReporter(map);
+ mapReporter.printMap();
+ // then
+ String actual = outputStreamCaptor.toString().trim();
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @Test
+ @DisplayName("Test reporter with LinkedHash")
+ void testPrintLinkedHashMap() {
+ // given
+ String key = "LinkedHashMap";
+ String value = "Use this implementation if you need keep the order of elements";
+ String expected = key + " " + value;
+ LinkedHashMap map = new LinkedHashMap<>();
+ map.put(key, value);
+ // when
+ MapReporter mapReporter = new MapReporter(map);
+ mapReporter.printMap();
+ // then
+ String actual = outputStreamCaptor.toString().trim();
+ assertThat(actual).isEqualTo(expected);
+ }
+
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/atomicstampedreference/StampedAccount.java b/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/atomicstampedreference/StampedAccount.java
index 415b24738a..69aed0b979 100644
--- a/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/atomicstampedreference/StampedAccount.java
+++ b/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/atomicstampedreference/StampedAccount.java
@@ -1,25 +1,22 @@
package com.baeldung.atomicstampedreference;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;
public class StampedAccount {
- private AtomicInteger stamp = new AtomicInteger(0);
- private AtomicStampedReference account = new AtomicStampedReference<>(0, 0);
-
- public int getBalance() {
- return account.getReference();
- }
-
- public int getStamp() {
- return account.getStamp();
- }
+ private final AtomicInteger stamp = new AtomicInteger(0);
+ private final AtomicStampedReference account = new AtomicStampedReference<>(0, 0);
public boolean deposit(int funds) {
int[] stamps = new int[1];
int current = this.account.get(stamps);
int newStamp = this.stamp.incrementAndGet();
+
+ // Thread is paused here to allow other threads to update the stamp and amount (for testing only)
+ sleep();
+
return this.account.compareAndSet(current, current + funds, stamps[0], newStamp);
}
@@ -29,4 +26,19 @@ public class StampedAccount {
int newStamp = this.stamp.incrementAndGet();
return this.account.compareAndSet(current, current - funds, stamps[0], newStamp);
}
+
+ public int getBalance() {
+ return account.getReference();
+ }
+
+ public int getStamp() {
+ return account.getStamp();
+ }
+
+ private static void sleep() {
+ try {
+ TimeUnit.SECONDS.sleep(1);
+ } catch (InterruptedException ignored) {
+ }
+ }
}
diff --git a/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/atomicstampedreference/ThreadStampedAccountUnitTest.java b/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/atomicstampedreference/ThreadStampedAccountUnitTest.java
index ce83355073..2840c25cf6 100644
--- a/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/atomicstampedreference/ThreadStampedAccountUnitTest.java
+++ b/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/atomicstampedreference/ThreadStampedAccountUnitTest.java
@@ -1,21 +1,38 @@
package com.baeldung.atomicstampedreference;
-import org.junit.Assert;
import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
public class ThreadStampedAccountUnitTest {
@Test
public void givenMultiThread_whenStampedAccount_thenSetBalance() throws InterruptedException {
StampedAccount account = new StampedAccount();
+
Thread t = new Thread(() -> {
- while (!account.withdrawal(100))
+ while (!account.deposit(100)) {
Thread.yield();
+ }
});
t.start();
- Assert.assertTrue(account.deposit(100));
- t.join(1_000);
- Assert.assertFalse(t.isAlive());
- Assert.assertSame(0, account.getBalance());
+
+ Thread t2 = new Thread(() -> {
+ while (!account.withdrawal(100)) {
+ Thread.yield();
+ }
+ });
+ t2.start();
+
+ t.join(10_000);
+ t2.join(10_000);
+
+ assertFalse(t.isAlive());
+ assertFalse(t2.isAlive());
+
+ assertEquals(0, account.getBalance());
+ assertTrue(account.getStamp() > 0);
}
}
diff --git a/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java b/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java
index dd1487b5cb..c022c02085 100644
--- a/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java
+++ b/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java
@@ -16,12 +16,17 @@ class LongRunningAction implements Runnable {
public void run() {
System.out.println("This is phase " + ph.getPhase());
System.out.println("Thread " + threadName + " before long running action");
- ph.arriveAndAwaitAdvance();
+
try {
- Thread.sleep(20);
+ Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
+
+ System.out.println("Thread " + threadName + " action completed and waiting for others");
+ ph.arriveAndAwaitAdvance();
+ System.out.println("Thread " + threadName + " proceeding in phase " + ph.getPhase());
+
ph.arriveAndDeregister();
}
}
\ No newline at end of file
diff --git a/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java b/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java
index a7b56e1151..88c18b5149 100644
--- a/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java
+++ b/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java
@@ -26,13 +26,20 @@ public class PhaserUnitTest {
executorService.submit(new LongRunningAction("thread-3", ph));
//then
+ System.out.println("Thread " + Thread.currentThread().getName() + " waiting for others");
ph.arriveAndAwaitAdvance();
+ System.out.println("Thread " + Thread.currentThread().getName() + " proceeding in phase " + ph.getPhase());
+
assertEquals(1, ph.getPhase());
//and
executorService.submit(new LongRunningAction("thread-4", ph));
executorService.submit(new LongRunningAction("thread-5", ph));
+
+ System.out.println("Thread " + Thread.currentThread().getName() + " waiting for others");
ph.arriveAndAwaitAdvance();
+ System.out.println("Thread " + Thread.currentThread().getName() + " proceeding in phase " + ph.getPhase());
+
assertEquals(2, ph.getPhase());
diff --git a/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatement.java b/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatement.java
new file mode 100644
index 0000000000..d76ae76a0e
--- /dev/null
+++ b/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatement.java
@@ -0,0 +1,41 @@
+package com.baeldung.exception.missingreturnstatement;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class MissingReturnStatement {
+ public static void main(String[] args) {
+ int a = -12;
+ int result = pow(a);
+ System.out.println(result);
+ Map dictionary = createDictionary();
+ dictionary.forEach((s, integer) -> System.out.println(s + " " + integer));
+ }
+
+ public static int pow(int number) {
+ int pow = number * number;
+ return pow;
+ }
+
+ public static String checkNumber(int number) {
+ if (number == 0) {
+ return "It's equals to zero";
+ }
+
+ for (int i = 0; i < number; i++) {
+ if (i > 100) {
+ return "It's a big number";
+ }
+ }
+ return "It's a negative number";
+ }
+
+ public static Map createDictionary() {
+ List words = Arrays.asList("Hello", "World");
+ return words.stream()
+ .collect(Collectors.toMap(s -> s, s -> 1));
+ }
+
+}
diff --git a/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatementUnitTest.java b/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatementUnitTest.java
new file mode 100644
index 0000000000..97a050a3f0
--- /dev/null
+++ b/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatementUnitTest.java
@@ -0,0 +1,38 @@
+package com.baeldung.exception.missingreturnstatement;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.junit.Test;
+
+public class MissingReturnStatementUnitTest {
+
+ @Test
+ public void givenANumber_thenReturnItsPow() {
+ int number = 10;
+ int pow = MissingReturnStatement.pow(number);
+ assertEquals(100, pow);
+ }
+
+ @Test
+ public void givenABigNumber_thenReturnItsType() {
+ int number = 200;
+ String type = MissingReturnStatement.checkNumber(number);
+ assertEquals("It's a big number", type);
+ }
+
+ @Test
+ public void givenANegativeNumber_thenReturnItsType() {
+ int number = -10;
+ String type = MissingReturnStatement.checkNumber(number);
+ assertEquals("It's a negative number", type);
+ }
+
+ @Test
+ public void getStringDictionary_thenPrintValues() {
+ Map dictionary = MissingReturnStatement.createDictionary();
+ assertEquals(2, dictionary.size());
+ dictionary.forEach((s, integer) -> System.out.println(s + " - " + integer));
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-jar/pom.xml b/core-java-modules/core-java-jar/pom.xml
index da107c745f..714a370287 100644
--- a/core-java-modules/core-java-jar/pom.xml
+++ b/core-java-modules/core-java-jar/pom.xml
@@ -259,110 +259,7 @@
-
-
- buildAgentLoader
-
-
-
- org.apache.maven.plugins
- maven-jar-plugin
-
-
- package
-
- jar
-
-
- agentLoader
- target/classes
-
-
- true
-
- ${project.build.outputDirectory}/META-INF/MANIFEST.MF
-
-
-
- com/baeldung/instrumentation/application/AgentLoader.class
- com/baeldung/instrumentation/application/Launcher.class
-
-
-
-
-
-
-
-
-
- buildApplication
-
-
-
- org.apache.maven.plugins
- maven-jar-plugin
-
-
- package
-
- jar
-
-
- application
- target/classes
-
-
- true
-
- ${project.build.outputDirectory}/META-INF/MANIFEST.MF
-
-
-
- com/baeldung/instrumentation/application/MyAtm.class
- com/baeldung/instrumentation/application/MyAtmApplication.class
- com/baeldung/instrumentation/application/Launcher.class
-
-
-
-
-
-
-
-
-
- buildAgent
-
-
-
- org.apache.maven.plugins
- maven-jar-plugin
-
-
- package
-
- jar
-
-
- agent
- target/classes
-
-
- true
-
- ${project.build.outputDirectory}/META-INF/MANIFEST.MF
-
-
-
- com/baeldung/instrumentation/agent/AtmTransformer.class
- com/baeldung/instrumentation/agent/MyInstrumentationAgent.class
-
-
-
-
-
-
-
-
+
diff --git a/core-java-modules/core-java-jvm/README.md b/core-java-modules/core-java-jvm/README.md
index 431cf021c8..e9266dea3b 100644
--- a/core-java-modules/core-java-jvm/README.md
+++ b/core-java-modules/core-java-jvm/README.md
@@ -15,3 +15,13 @@ This module contains articles about working with the Java Virtual Machine (JVM).
- [What Causes java.lang.OutOfMemoryError: unable to create new native thread](https://www.baeldung.com/java-outofmemoryerror-unable-to-create-new-native-thread)
- [View Bytecode of a Class File in Java](https://www.baeldung.com/java-class-view-bytecode)
- More articles: [[next -->]](/core-java-modules/core-java-jvm-2)
+
+
+To run the code for the Instrumentation: https://www.baeldung.com/java-instrumentation article:
+1- build the module
+2- run the module 3 times to build the 3 jars:
+ mvn install -PbuildAgentLoader
+ mvn install -PbuildApplication
+ mvn install -PbuildAgent
+3- update the commands in the article with the exact names of the jars generated in the target folder
+4- update the path in the AgentLoader class with the path of the agent on your system
\ No newline at end of file
diff --git a/core-java-modules/core-java-jvm/pom.xml b/core-java-modules/core-java-jvm/pom.xml
index c47765e43d..e4c0f949c2 100644
--- a/core-java-modules/core-java-jvm/pom.xml
+++ b/core-java-modules/core-java-jvm/pom.xml
@@ -57,6 +57,11 @@
jol-core
${jol-core.version}
+
+ org.slf4j
+ log4j-over-slf4j
+ ${org.slf4j.version}
+
@@ -66,5 +71,114 @@
8.0.1
6.5.0
+
+
+
+ buildAgentLoader
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+ package
+
+ jar
+
+
+ agentLoader
+ target/classes
+
+
+ true
+ com.baeldung.instrumentation.application.Launcher
+
+ ${project.build.outputDirectory}/META-INF/MANIFEST.MF
+
+
+
+ com/baeldung/instrumentation/application/AgentLoader.class
+ com/baeldung/instrumentation/application/Launcher.class
+
+
+
+
+
+
+
+
+
+ buildApplication
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+ package
+
+ jar
+
+
+ application
+ target/classes
+
+
+ true
+ com.baeldung.instrumentation.application.Launcher
+
+ ${project.build.outputDirectory}/META-INF/MANIFEST.MF
+
+
+
+ com/baeldung/instrumentation/application/MyAtm.class
+ com/baeldung/instrumentation/application/MyAtmApplication.class
+ com/baeldung/instrumentation/application/Launcher.class
+
+
+
+
+
+
+
+
+
+ buildAgent
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+ package
+
+ jar
+
+
+ agent
+ target/classes
+
+
+ true
+ com.baeldung.instrumentation.application.Launcher
+
+ ${project.build.outputDirectory}/META-INF/MANIFEST.MF
+
+
+
+ com/baeldung/instrumentation/agent/AtmTransformer.class
+ com/baeldung/instrumentation/agent/MyInstrumentationAgent.class
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/core-java-modules/core-java-jvm/src/main/resources/META-INF/MANIFEST.MF b/core-java-modules/core-java-jvm/src/main/resources/META-INF/MANIFEST.MF
new file mode 100644
index 0000000000..d16a93ec83
--- /dev/null
+++ b/core-java-modules/core-java-jvm/src/main/resources/META-INF/MANIFEST.MF
@@ -0,0 +1,5 @@
+Agent-Class: com.baeldung.instrumentation.agent.MyInstrumentationAgent
+Can-Redefine-Classes: true
+Can-Retransform-Classes: true
+Premain-Class: com.baeldung.instrumentation.agent.MyInstrumentationAgent
+Main-Class: com.baeldung.instrumentation.application.Launcher
\ No newline at end of file
diff --git a/core-java-modules/core-java-security-3/src/test/java/com/baeldung/truststore/TrustStoreUnitTest.java b/core-java-modules/core-java-security-3/src/test/java/com/baeldung/truststore/TrustStoreUnitTest.java
new file mode 100644
index 0000000000..2c3d4c8959
--- /dev/null
+++ b/core-java-modules/core-java-security-3/src/test/java/com/baeldung/truststore/TrustStoreUnitTest.java
@@ -0,0 +1,29 @@
+package com.baeldung.truststore;
+
+import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+
+import java.io.IOException;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.security.cert.PKIXParameters;
+
+public class TrustStoreUnitTest {
+
+ @Test
+ public void whenOpeningTrustStore_thenExceptionIsThrown() throws Exception {
+ KeyStore keyStore = getKeyStore();
+ InvalidAlgorithmParameterException invalidAlgorithmParameterException =
+ Assertions.assertThrows(InvalidAlgorithmParameterException.class, () -> new PKIXParameters(keyStore));
+ Assertions.assertEquals("the trustAnchors parameter must be non-empty", invalidAlgorithmParameterException.getMessage());
+ }
+
+ private KeyStore getKeyStore() throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException {
+ KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
+ ks.load(null, "changeIt".toCharArray());
+ return ks;
+ }
+}
\ No newline at end of file
diff --git a/docker/docker-include-outside-build-context/projects/config/Dockerfile b/docker/docker-include-outside-build-context/projects/config/Dockerfile
new file mode 100644
index 0000000000..754f9f9be3
--- /dev/null
+++ b/docker/docker-include-outside-build-context/projects/config/Dockerfile
@@ -0,0 +1,2 @@
+FROM nginx:latest
+COPY nginx.conf /etc/nginx/nginx.conf
diff --git a/docker/docker-include-outside-build-context/projects/config/nginx.conf b/docker/docker-include-outside-build-context/projects/config/nginx.conf
new file mode 100644
index 0000000000..944da2f112
--- /dev/null
+++ b/docker/docker-include-outside-build-context/projects/config/nginx.conf
@@ -0,0 +1,8 @@
+events {}
+
+http {
+ server {
+ listen 80;
+ index index.html;
+ }
+}
diff --git a/docker/docker-include-outside-build-context/projects/sample-site/docker/Dockerfile b/docker/docker-include-outside-build-context/projects/sample-site/docker/Dockerfile
new file mode 100644
index 0000000000..bda0bbe257
--- /dev/null
+++ b/docker/docker-include-outside-build-context/projects/sample-site/docker/Dockerfile
@@ -0,0 +1,3 @@
+FROM nginx:latest
+COPY sample-site/html/* /etc/nginx/html/
+COPY config/nginx.conf /etc/nginx/nginx.conf
diff --git a/docker/docker-include-outside-build-context/projects/sample-site/docker/Dockerfile-from-base b/docker/docker-include-outside-build-context/projects/sample-site/docker/Dockerfile-from-base
new file mode 100644
index 0000000000..1b72414bc0
--- /dev/null
+++ b/docker/docker-include-outside-build-context/projects/sample-site/docker/Dockerfile-from-base
@@ -0,0 +1,2 @@
+FROM sample-site-base:latest
+COPY html/* /etc/nginx/html/
diff --git a/docker/docker-include-outside-build-context/projects/sample-site/docker/Dockerfile-script b/docker/docker-include-outside-build-context/projects/sample-site/docker/Dockerfile-script
new file mode 100644
index 0000000000..572028f19f
--- /dev/null
+++ b/docker/docker-include-outside-build-context/projects/sample-site/docker/Dockerfile-script
@@ -0,0 +1,3 @@
+FROM nginx:latest
+COPY html/* /etc/nginx/html/
+COPY config/nginx.conf /etc/nginx/nginx.conf
diff --git a/docker/docker-include-outside-build-context/projects/sample-site/docker/build-docker.sh b/docker/docker-include-outside-build-context/projects/sample-site/docker/build-docker.sh
new file mode 100755
index 0000000000..e84687b7b7
--- /dev/null
+++ b/docker/docker-include-outside-build-context/projects/sample-site/docker/build-docker.sh
@@ -0,0 +1,6 @@
+mkdir tmp-context
+cp -R ../html tmp-context/
+cp -R ../../config tmp-context/
+cp Dockerfile-script tmp-context/Dockerfile
+docker build -t sample-site:latest tmp-context
+rm -rf tmp-context
diff --git a/docker/docker-include-outside-build-context/projects/sample-site/html/index.html b/docker/docker-include-outside-build-context/projects/sample-site/html/index.html
new file mode 100644
index 0000000000..85f287fad7
--- /dev/null
+++ b/docker/docker-include-outside-build-context/projects/sample-site/html/index.html
@@ -0,0 +1,10 @@
+
+
+
+
+ Sample Site
+
+
+ Welcome to the first page of the site
+
+
diff --git a/gradle/gradle-source-vs-target-compatibility/build.gradle b/gradle/gradle-source-vs-target-compatibility/build.gradle
new file mode 100644
index 0000000000..8cc50c15c6
--- /dev/null
+++ b/gradle/gradle-source-vs-target-compatibility/build.gradle
@@ -0,0 +1,12 @@
+plugins {
+ id 'java'
+}
+
+description = 'gradle-source-vs-target-compatibility'
+
+group 'com.baeldung'
+
+java {
+ sourceCompatibility = "1.6"
+ targetCompatibility = "1.8"
+}
diff --git a/gradle/gradle-source-vs-target-compatibility/src/main/java/com/baeldung/helloworld/HelloWorldApp.java b/gradle/gradle-source-vs-target-compatibility/src/main/java/com/baeldung/helloworld/HelloWorldApp.java
new file mode 100644
index 0000000000..a951a72dd6
--- /dev/null
+++ b/gradle/gradle-source-vs-target-compatibility/src/main/java/com/baeldung/helloworld/HelloWorldApp.java
@@ -0,0 +1,9 @@
+package com.baeldung.helloworld;
+
+public class HelloWorldApp {
+
+ public static void main(String[] args) {
+ System.out.println("Hello World!");
+ }
+
+}
diff --git a/gradle/settings.gradle b/gradle/settings.gradle
index 59300f9281..d1eb10f4d2 100644
--- a/gradle/settings.gradle
+++ b/gradle/settings.gradle
@@ -6,5 +6,6 @@ include 'greeter'
include 'gradletaskdemo'
include 'junit5'
include 'gradle-employee-app'
+include 'gradle-source-vs-target-compatibility'
println 'This will be executed during the initialization phase.'
diff --git a/java-collections-maps-3/src/main/java/com/baeldung/map/invert/InvertHashMapExample.java b/java-collections-maps-3/src/main/java/com/baeldung/map/invert/InvertHashMapExample.java
new file mode 100644
index 0000000000..a203fd17c4
--- /dev/null
+++ b/java-collections-maps-3/src/main/java/com/baeldung/map/invert/InvertHashMapExample.java
@@ -0,0 +1,59 @@
+package com.baeldung.map.invert;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+
+public class InvertHashMapExample {
+
+ public static void main(String[] args) {
+
+ Map map = new HashMap<>();
+ map.put("first", 1);
+ map.put("second", 2);
+ System.out.println(map);
+
+ invertMapUsingForLoop(map);
+ invertMapUsingStreams(map);
+ invertMapUsingMapper(map);
+
+ map.put("two", 2);
+ invertMapUsingGroupingBy(map);
+ }
+
+ public static Map invertMapUsingForLoop(Map map) {
+ Map inversedMap = new HashMap();
+ for (Entry entry : map.entrySet()) {
+ inversedMap.put(entry.getValue(), entry.getKey());
+ }
+ System.out.println(inversedMap);
+ return inversedMap;
+ }
+
+ public static Map invertMapUsingStreams(Map map) {
+ Map inversedMap = map.entrySet()
+ .stream()
+ .collect(Collectors.toMap(Entry::getValue, Entry::getKey));
+ System.out.println(inversedMap);
+ return inversedMap;
+ }
+
+ public static Map invertMapUsingMapper(Map sourceMap) {
+ Map inversedMap = sourceMap.entrySet()
+ .stream()
+ .collect(Collectors.toMap(Entry::getValue, Entry::getKey, (oldValue, newValue) -> oldValue));
+ System.out.println(inversedMap);
+ return inversedMap;
+ }
+
+ public static Map> invertMapUsingGroupingBy(Map map) {
+ Map> inversedMap = map.entrySet()
+ .stream()
+ .collect(Collectors.groupingBy(Map.Entry::getValue, Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
+ System.out.println(inversedMap);
+ return inversedMap;
+ }
+
+}
diff --git a/java-collections-maps-3/src/test/java/com/baeldung/map/invert/InvertHashMapUnitTest.java b/java-collections-maps-3/src/test/java/com/baeldung/map/invert/InvertHashMapUnitTest.java
new file mode 100644
index 0000000000..6870cdef23
--- /dev/null
+++ b/java-collections-maps-3/src/test/java/com/baeldung/map/invert/InvertHashMapUnitTest.java
@@ -0,0 +1,72 @@
+package com.baeldung.map.invert;
+
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+public class InvertHashMapUnitTest {
+
+ Map sourceMap;
+
+ @BeforeEach
+ void setup() {
+ sourceMap = new HashMap<>();
+ sourceMap.put("Sunday", 0);
+ sourceMap.put("Monday", 1);
+ sourceMap.put("Tuesday", 2);
+ sourceMap.put("Wednesday", 3);
+ sourceMap.put("Thursday", 4);
+ sourceMap.put("Friday", 5);
+ sourceMap.put("Saturday", 6);
+ }
+
+ @Test
+ void givenSourceMap_whenUsingForLoop_returnsInvertedMap() {
+ Map inversedMap = InvertHashMapExample.invertMapUsingForLoop(sourceMap);
+
+ assertNotNull(inversedMap);
+ assertEquals(sourceMap.size(), inversedMap.size());
+ assertEquals("Monday", inversedMap.get(1));
+ }
+
+ @Test
+ void givenSourceMap_whenUsingStreams_returnsInvertedMap() {
+ Map inversedMap = InvertHashMapExample.invertMapUsingStreams(sourceMap);
+
+ assertNotNull(inversedMap);
+ assertEquals(sourceMap.size(), inversedMap.size());
+ assertEquals("Monday", inversedMap.get(1));
+ }
+
+ @Test
+ void givenSourceMap_whenUsingMapper_returnsInvertedMap() {
+ Map inversedMap = InvertHashMapExample.invertMapUsingMapper(sourceMap);
+
+ assertNotNull(inversedMap);
+ assertEquals(sourceMap.size(), inversedMap.size());
+ assertEquals("Monday", inversedMap.get(1));
+ }
+
+ @Test
+ void givenSourceMapWithDuplicateValues_whenUsingGroupBy_returnsInvertedMap() {
+ sourceMap.put("MONDAY", 1);
+ Map> inversedMap = InvertHashMapExample.invertMapUsingGroupingBy(sourceMap);
+
+ assertNotNull(inversedMap);
+ assertNotEquals(sourceMap.size(), inversedMap.size()); // duplicate keys are merged now
+ assertEquals(2, inversedMap.get(1).size());
+ assertTrue(inversedMap.get(1).contains("Monday"));
+ assertTrue(inversedMap.get(1).contains("MONDAY"));
+ }
+
+}
diff --git a/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp b/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp
new file mode 100644
index 0000000000..37adb35333
--- /dev/null
+++ b/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp
@@ -0,0 +1,21 @@
+#include "com_baeldung_jni_RegisterNativesHelloWorldJNI.h"
+#include
+
+
+JNIEXPORT jstring JNICALL hello (JNIEnv* env, jobject thisObject) {
+ std::string hello = "Hello from registered native C++ !!";
+ std::cout << hello << std::endl;
+ return env->NewStringUTF(hello.c_str());
+}
+
+static JNINativeMethod methods[] = {
+ {"sayHello", "()Ljava/lang/String;", (void*) &hello },
+};
+
+
+JNIEXPORT void JNICALL Java_com_baeldung_jni_RegisterNativesHelloWorldJNI_register (JNIEnv* env, jobject thsObject) {
+ jclass clazz = env->FindClass("com/baeldung/jni/RegisterNativesHelloWorldJNI");
+
+ (env)->RegisterNatives(clazz, methods, sizeof(methods)/sizeof(methods[0]));
+}
+
diff --git a/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.h b/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.h
new file mode 100644
index 0000000000..bcca780208
--- /dev/null
+++ b/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.h
@@ -0,0 +1,29 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include
+/* Header for class com_baeldung_jni_RegisterNativesHelloWorldJNI */
+
+#ifndef _Included_com_baeldung_jni_RegisterNativesHelloWorldJNI
+#define _Included_com_baeldung_jni_RegisterNativesHelloWorldJNI
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class: com_baeldung_jni_RegisterNativesHelloWorldJNI
+ * Method: sayHello
+ * Signature: ()Ljava/lang/String;
+ */
+JNIEXPORT jstring JNICALL Java_com_baeldung_jni_RegisterNativesHelloWorldJNI_sayHello
+ (JNIEnv *, jobject);
+
+/*
+ * Class: com_baeldung_jni_RegisterNativesHelloWorldJNI
+ * Method: register
+ * Signature: ()V
+ */
+JNIEXPORT void JNICALL Java_com_baeldung_jni_RegisterNativesHelloWorldJNI_register
+ (JNIEnv *, jobject);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/java-native/src/main/cpp/generateNativeLibMac.sh b/java-native/src/main/cpp/generateNativeLibMac.sh
old mode 100644
new mode 100755
index d11dcc7c01..834a07acc8
--- a/java-native/src/main/cpp/generateNativeLibMac.sh
+++ b/java-native/src/main/cpp/generateNativeLibMac.sh
@@ -3,4 +3,5 @@
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_HelloWorldJNI.cpp -o com_baeldung_jni_HelloWorldJNI.o
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_ExampleParametersJNI.cpp -o com_baeldung_jni_ExampleParametersJNI.o
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_ExampleObjectsJNI.cpp -o com_baeldung_jni_ExampleObjectsJNI.o
-g++ -dynamiclib -o ../../../native/macos/libnative.dylib com_baeldung_jni_HelloWorldJNI.o com_baeldung_jni_ExampleParametersJNI.o com_baeldung_jni_ExampleObjectsJNI.o -lc
\ No newline at end of file
+g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp -o com_baeldung_jni_RegisterNativesHelloWorldJNI.o
+g++ -dynamiclib -o ../../../native/macos/libnative.dylib com_baeldung_jni_HelloWorldJNI.o com_baeldung_jni_ExampleParametersJNI.o com_baeldung_jni_ExampleObjectsJNI.o com_baeldung_jni_RegisterNativesHelloWorldJNI.o -lc
\ No newline at end of file
diff --git a/java-native/src/main/java/com/baeldung/jni/RegisterNativesHelloWorldJNI.java b/java-native/src/main/java/com/baeldung/jni/RegisterNativesHelloWorldJNI.java
new file mode 100644
index 0000000000..c5461aafef
--- /dev/null
+++ b/java-native/src/main/java/com/baeldung/jni/RegisterNativesHelloWorldJNI.java
@@ -0,0 +1,18 @@
+package com.baeldung.jni;
+
+public class RegisterNativesHelloWorldJNI {
+
+ static {
+ System.loadLibrary("native");
+ }
+
+ public static void main(String[] args) {
+ RegisterNativesHelloWorldJNI helloWorldJNI = new RegisterNativesHelloWorldJNI();
+ helloWorldJNI.register();
+ helloWorldJNI.sayHello();
+ }
+
+ public native String sayHello();
+
+ public native void register();
+}
diff --git a/java-native/src/test/java/com/baeldung/jni/registernatives/JNIRegisterNativesManualTest.java b/java-native/src/test/java/com/baeldung/jni/registernatives/JNIRegisterNativesManualTest.java
new file mode 100644
index 0000000000..96e15bd822
--- /dev/null
+++ b/java-native/src/test/java/com/baeldung/jni/registernatives/JNIRegisterNativesManualTest.java
@@ -0,0 +1,26 @@
+package com.baeldung.jni.registernatives;
+
+import com.baeldung.jni.RegisterNativesHelloWorldJNI;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class JNIRegisterNativesManualTest {
+ @Before
+ public void setup() {
+ System.loadLibrary("native");
+ }
+
+ @Test
+ public void whenRegisteredNativeHelloWorld_thenOutputIsAsExpected() {
+ RegisterNativesHelloWorldJNI helloWorld = new RegisterNativesHelloWorldJNI();
+ helloWorld.register();
+
+ String helloFromNative = helloWorld.sayHello();
+
+ assertNotNull(helloFromNative);
+ assertTrue(helloFromNative.equals("Hello from registered native C++ !!"));
+ }
+}
diff --git a/parent-spring-5/pom.xml b/parent-spring-5/pom.xml
index 175e8d58f9..b2d581c9c1 100644
--- a/parent-spring-5/pom.xml
+++ b/parent-spring-5/pom.xml
@@ -24,7 +24,7 @@
- 5.3.13
+ 5.3.16
5.6.0
1.5.10.RELEASE
diff --git a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/scalarmethod/HibernateScalarExample.java b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/scalarmethod/HibernateScalarExample.java
new file mode 100644
index 0000000000..69aaaae19d
--- /dev/null
+++ b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/scalarmethod/HibernateScalarExample.java
@@ -0,0 +1,47 @@
+package com.baeldung.hibernate.scalarmethod;
+
+import java.util.List;
+
+import org.hibernate.Session;
+import org.hibernate.type.StandardBasicTypes;
+
+public class HibernateScalarExample {
+
+ private Session session;
+
+ public HibernateScalarExample(Session session) {
+ this.session = session;
+ }
+
+ public List