+1
-2
@@ -1,7 +1,6 @@
|
||||
## Atomikos
|
||||
|
||||
This module contains articles about Atomikos
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Guide Transactions Using Atomikos]()
|
||||
- [A Guide to Atomikos](https://www.baeldung.com/java-atomikos)
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Building Java Applications with Bazel](https://www.baeldung.com/bazel-build-tool)
|
||||
@@ -12,4 +12,5 @@ This module contains articles about core Groovy concepts
|
||||
- [Closures in Groovy](https://www.baeldung.com/groovy-closures)
|
||||
- [Converting a String to a Date in Groovy](https://www.baeldung.com/groovy-string-to-date)
|
||||
- [Guide to I/O in Groovy](https://www.baeldung.com/groovy-io)
|
||||
- [[More -->]](/core-groovy-2)
|
||||
- [Convert String to Integer in Groovy](https://www.baeldung.com/groovy-convert-string-to-integer)
|
||||
- [[More -->]](/core-groovy-2)
|
||||
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package com.baeldung.java14.foreign.api;
|
||||
|
||||
import jdk.incubator.foreign.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
public class ForeignMemoryUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenAValueIsSet_thenAccessTheValue() {
|
||||
long value = 10;
|
||||
MemoryAddress memoryAddress =
|
||||
MemorySegment.allocateNative(8).baseAddress();
|
||||
VarHandle varHandle = MemoryHandles.varHandle(long.class,
|
||||
ByteOrder.nativeOrder());
|
||||
varHandle.set(memoryAddress, value);
|
||||
assertThat(varHandle.get(memoryAddress), is(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMultipleValuesAreSet_thenAccessAll() {
|
||||
VarHandle varHandle = MemoryHandles.varHandle(int.class,
|
||||
ByteOrder.nativeOrder());
|
||||
|
||||
try(MemorySegment memorySegment =
|
||||
MemorySegment.allocateNative(100)) {
|
||||
MemoryAddress base = memorySegment.baseAddress();
|
||||
for(int i=0; i<25; i++) {
|
||||
varHandle.set(base.addOffset((i*4)), i);
|
||||
}
|
||||
for(int i=0; i<25; i++) {
|
||||
assertThat(varHandle.get(base.addOffset((i*4))), is(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSetValuesWithMemoryLayout_thenTheyCanBeRetrieved() {
|
||||
SequenceLayout sequenceLayout =
|
||||
MemoryLayout.ofSequence(25,
|
||||
MemoryLayout.ofValueBits(64, ByteOrder.nativeOrder()));
|
||||
VarHandle varHandle =
|
||||
sequenceLayout.varHandle(long.class,
|
||||
MemoryLayout.PathElement.sequenceElement());
|
||||
|
||||
try(MemorySegment memorySegment =
|
||||
MemorySegment.allocateNative(sequenceLayout)) {
|
||||
MemoryAddress base = memorySegment.baseAddress();
|
||||
for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) {
|
||||
varHandle.set(base, i, i);
|
||||
}
|
||||
for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) {
|
||||
assertThat(varHandle.get(base, i), is(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSlicingMemorySegment_thenTheyCanBeAccessedIndividually() {
|
||||
MemoryAddress memoryAddress =
|
||||
MemorySegment.allocateNative(12).baseAddress();
|
||||
MemoryAddress memoryAddress1 =
|
||||
memoryAddress.segment().asSlice(0,4).baseAddress();
|
||||
MemoryAddress memoryAddress2 =
|
||||
memoryAddress.segment().asSlice(4,4).baseAddress();
|
||||
MemoryAddress memoryAddress3 =
|
||||
memoryAddress.segment().asSlice(8,4).baseAddress();
|
||||
|
||||
VarHandle intHandle =
|
||||
MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
|
||||
intHandle.set(memoryAddress1, Integer.MIN_VALUE);
|
||||
intHandle.set(memoryAddress2, 0);
|
||||
intHandle.set(memoryAddress3, Integer.MAX_VALUE);
|
||||
|
||||
assertThat(intHandle.get(memoryAddress1), is(Integer.MIN_VALUE));
|
||||
assertThat(intHandle.get(memoryAddress2), is(0));
|
||||
assertThat(intHandle.get(memoryAddress3), is(Integer.MAX_VALUE));
|
||||
}
|
||||
}
|
||||
@@ -5,4 +5,5 @@ This module contains articles about Map data structures in Java.
|
||||
### Relevant Articles:
|
||||
- [Java TreeMap vs HashMap](https://www.baeldung.com/java-treemap-vs-hashmap)
|
||||
- [Comparing Two HashMaps in Java](https://www.baeldung.com/java-compare-hashmaps)
|
||||
- [The Map.computeIfAbsent() Method](https://www.baeldung.com/java-map-computeifabsent)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2)
|
||||
|
||||
@@ -4,5 +4,5 @@
|
||||
|
||||
### Relevant Articles:
|
||||
- [Using a Mutex Object in Java](https://www.baeldung.com/java-mutex)
|
||||
- [Testing Multi-Threaded Code in Java] (https://www.baeldung.com/java-testing-multithreaded)
|
||||
- [Testing Multi-Threaded Code in Java](https://www.baeldung.com/java-testing-multithreaded)
|
||||
|
||||
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.baeldung.lockfree;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class NonBlockingQueue<T> {
|
||||
|
||||
private final AtomicReference<Node<T>> head, tail;
|
||||
private final AtomicInteger size;
|
||||
|
||||
public NonBlockingQueue() {
|
||||
head = new AtomicReference<>(null);
|
||||
tail = new AtomicReference<>(null);
|
||||
size = new AtomicInteger();
|
||||
size.set(0);
|
||||
}
|
||||
|
||||
public void add(T element) {
|
||||
if (element == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
Node<T> node = new Node<>(element);
|
||||
Node<T> currentTail;
|
||||
do {
|
||||
currentTail = tail.get();
|
||||
node.setPrevious(currentTail);
|
||||
} while(!tail.compareAndSet(currentTail, node));
|
||||
|
||||
if(node.previous != null) {
|
||||
node.previous.next = node;
|
||||
}
|
||||
|
||||
head.compareAndSet(null, node); //if we are inserting the first element
|
||||
size.incrementAndGet();
|
||||
}
|
||||
|
||||
public T get() {
|
||||
if(head.get() == null) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
Node<T> currentHead;
|
||||
Node<T> nextNode;
|
||||
do {
|
||||
currentHead = head.get();
|
||||
nextNode = currentHead.getNext();
|
||||
} while(!head.compareAndSet(currentHead, nextNode));
|
||||
|
||||
size.decrementAndGet();
|
||||
return currentHead.getValue();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.size.get();
|
||||
}
|
||||
|
||||
private class Node<T> {
|
||||
private final T value;
|
||||
private volatile Node<T> next;
|
||||
private volatile Node<T> previous;
|
||||
|
||||
public Node(T value) {
|
||||
this.value = value;
|
||||
this.next = null;
|
||||
}
|
||||
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public Node<T> getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setPrevious(Node<T> previous) {
|
||||
this.previous = previous;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,5 @@ This module contains articles about networking in Java
|
||||
- [Sending Emails with Java](https://www.baeldung.com/java-email)
|
||||
- [Authentication with HttpUrlConnection](https://www.baeldung.com/java-http-url-connection)
|
||||
- [Download a File from an URL in Java](https://www.baeldung.com/java-download-file)
|
||||
- [Handling java.net.ConnectException](https://www.baeldung.com/java-net-connectexception)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-networking)
|
||||
|
||||
@@ -26,7 +26,6 @@ public class RegexUnitTest {
|
||||
while (matcher.find())
|
||||
matches++;
|
||||
assertEquals(matches, 2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -452,7 +451,6 @@ public class RegexUnitTest {
|
||||
Matcher matcher = pattern.matcher("dogs are friendly");
|
||||
assertTrue(matcher.lookingAt());
|
||||
assertFalse(matcher.matches());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -460,7 +458,6 @@ public class RegexUnitTest {
|
||||
Pattern pattern = Pattern.compile("dog");
|
||||
Matcher matcher = pattern.matcher("dog");
|
||||
assertTrue(matcher.matches());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -469,7 +466,6 @@ public class RegexUnitTest {
|
||||
Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly");
|
||||
String newStr = matcher.replaceFirst("cat");
|
||||
assertEquals("cats are domestic animals, dogs are friendly", newStr);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -478,7 +474,6 @@ public class RegexUnitTest {
|
||||
Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly");
|
||||
String newStr = matcher.replaceAll("cat");
|
||||
assertEquals("cats are domestic animals, cats are friendly", newStr);
|
||||
|
||||
}
|
||||
|
||||
public synchronized static int runTest(String regex, String text) {
|
||||
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
package com.baeldung.regex.phonenumbers;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PhoneNumbersRegexUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenMatchesTenDigitsNumber_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("^\\d{10}$");
|
||||
Matcher matcher = pattern.matcher("2055550125");
|
||||
assertTrue(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMOreThanTenDigits_thenNotCorrect() {
|
||||
Pattern pattern = Pattern.compile("^\\d{10}$");
|
||||
Matcher matcher = pattern.matcher("20555501251");
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchesTenDigitsNumberWhitespacesDotHyphen_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$");
|
||||
Matcher matcher = pattern.matcher("202 555 0125");
|
||||
assertTrue(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIncludesBracket_thenNotCorrect() {
|
||||
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$");
|
||||
Matcher matcher = pattern.matcher("202]555 0125");
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotStartsWithBatchesOfThreeDigits_thenNotCorrect() {
|
||||
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$");
|
||||
Matcher matcher = pattern.matcher("2021 555 0125");
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLastPartWithNoFourDigits_thenNotCorrect() {
|
||||
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$");
|
||||
Matcher matcher = pattern.matcher("202 555 012");
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchesTenDigitsNumberParenthesis_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
|
||||
Matcher matcher = pattern.matcher("(202) 555-0125");
|
||||
assertTrue(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJustOpeningParenthesis_thenNotCorrect() {
|
||||
Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
|
||||
Matcher matcher = pattern.matcher("(202 555-0125");
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJustClosingParenthesis_thenNotCorrect() {
|
||||
Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
|
||||
Matcher matcher = pattern.matcher("202) 555-0125");
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchesTenDigitsNumberPrefix_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
|
||||
Matcher matcher = pattern.matcher("+111 (202) 555-0125");
|
||||
assertTrue(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIncorrectPrefix_thenNotCorrect() {
|
||||
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
|
||||
Matcher matcher = pattern.matcher("-111 (202) 555-0125");
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTooLongPrefix_thenNotCorrect() {
|
||||
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
|
||||
Matcher matcher = pattern.matcher("+1111 (202) 555-0125");
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchesPhoneNumber_thenCorrect() {
|
||||
String patterns
|
||||
= "^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"
|
||||
+ "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$"
|
||||
+ "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?)(\\d{2}[ ]?){2}\\d{2}$";
|
||||
|
||||
String[] validPhoneNumbers
|
||||
= {"2055550125","202 555 0125", "(202) 555-0125", "+111 (202) 555-0125", "636 856 789", "+111 636 856 789", "636 85 67 89", "+111 636 85 67 89"};
|
||||
|
||||
Pattern pattern = Pattern.compile(patterns);
|
||||
for(String phoneNumber : validPhoneNumbers) {
|
||||
Matcher matcher = pattern.matcher(phoneNumber);
|
||||
assertTrue(matcher.matches());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotMatchesPhoneNumber_thenNotCorrect() {
|
||||
String patterns
|
||||
= "^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"
|
||||
+ "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$"
|
||||
+ "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?)(\\d{2}[ ]?){2}\\d{2}$";
|
||||
|
||||
String[] invalidPhoneNumbers
|
||||
= {"20555501251","202]555 0125", "2021 555 012", "(202 555-0125", "202) 555-0125", "-111 (202) 555-0125", "+1111 (202) 555-0125", "636 85 789", "636 85 67 893"};
|
||||
|
||||
Pattern pattern = Pattern.compile(patterns);
|
||||
for(String phoneNumber : invalidPhoneNumbers) {
|
||||
Matcher matcher = pattern.matcher(phoneNumber);
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,5 @@ This module contains articles about the Stream API in Java.
|
||||
- [Primitive Type Streams in Java 8](https://www.baeldung.com/java-8-primitive-streams)
|
||||
- [Debugging Java 8 Streams with IntelliJ](https://www.baeldung.com/intellij-debugging-java-streams)
|
||||
- [Add BigDecimals using the Stream API](https://www.baeldung.com/java-stream-add-bigdecimals)
|
||||
- [Should We Close a Java Stream?](https://www.baeldung.com/java-stream-close)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-2)
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.late
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class LateInitUnitTest {
|
||||
|
||||
private lateinit var answer: String
|
||||
|
||||
@Test(expected = UninitializedPropertyAccessException::class)
|
||||
fun givenLateInit_WhenNotInitialized_ShouldThrowAnException() {
|
||||
answer.length
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenLateInit_TheIsInitialized_ReturnsTheInitializationStatus() {
|
||||
assertFalse { this::answer.isInitialized }
|
||||
answer = "42"
|
||||
assertTrue { this::answer.isInitialized }
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -6,6 +6,7 @@ import org.apache.commons.collections4.MapUtils;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ConvertListToMapService {
|
||||
@@ -21,7 +22,7 @@ public class ConvertListToMapService {
|
||||
}
|
||||
|
||||
public Map<Integer, Animal> convertListAfterJava8(List<Animal> list) {
|
||||
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, animal -> animal));
|
||||
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, Function.identity()));
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -53,14 +53,14 @@ public class CollectionToArrayListUnitTest {
|
||||
|
||||
verifyShallowCopy(srcCollection, newList);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Section 5. Deep Copy
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingDeepCopy_thenVerifyDeepCopy() {
|
||||
ArrayList<Foo> newList = srcCollection.stream()
|
||||
.map(foo -> foo.deepCopy())
|
||||
.map(Foo::deepCopy)
|
||||
.collect(toCollection(ArrayList::new));
|
||||
|
||||
verifyDeepCopy(srcCollection, newList);
|
||||
@@ -83,13 +83,13 @@ public class CollectionToArrayListUnitTest {
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
private void verifyShallowCopy(Collection a, Collection b) {
|
||||
private void verifyShallowCopy(Collection<Foo> a, Collection<Foo> b) {
|
||||
assertEquals("Collections have different lengths", a.size(), b.size());
|
||||
Iterator<Foo> iterA = a.iterator();
|
||||
Iterator<Foo> iterB = b.iterator();
|
||||
while (iterA.hasNext()) {
|
||||
// use '==' to test instance identity
|
||||
assertTrue("Foo instances differ!", iterA.next() == iterB.next());
|
||||
assertSame("Foo instances differ!", iterA.next(), iterB.next());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ public class CollectionToArrayListUnitTest {
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
private void verifyDeepCopy(Collection a, Collection b) {
|
||||
private void verifyDeepCopy(Collection<Foo> a, Collection<Foo> b) {
|
||||
assertEquals("Collections have different lengths", a.size(), b.size());
|
||||
Iterator<Foo> iterA = a.iterator();
|
||||
Iterator<Foo> iterB = b.iterator();
|
||||
@@ -106,7 +106,7 @@ public class CollectionToArrayListUnitTest {
|
||||
Foo nextA = iterA.next();
|
||||
Foo nextB = iterB.next();
|
||||
// should not be same instance
|
||||
assertFalse("Foo instances are the same!", nextA == nextB);
|
||||
assertNotSame("Foo instances are the same!", nextA, nextB);
|
||||
// but should have same content
|
||||
assertFalse("Foo instances have different content!", fooDiff(nextA, nextB));
|
||||
}
|
||||
|
||||
+3
-3
@@ -23,7 +23,7 @@ public class ConvertIteratorToListServiceUnitTest {
|
||||
Iterator<Integer> iterator;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
public void setUp() {
|
||||
iterator = Arrays.asList(1, 2, 3)
|
||||
.iterator();
|
||||
}
|
||||
@@ -31,7 +31,7 @@ public class ConvertIteratorToListServiceUnitTest {
|
||||
@Test
|
||||
public void givenAnIterator_whenConvertIteratorToListUsingWhileLoop_thenReturnAList() {
|
||||
|
||||
List<Integer> actualList = new ArrayList<Integer>();
|
||||
List<Integer> actualList = new ArrayList<>();
|
||||
|
||||
// Convert Iterator to List using while loop dsf
|
||||
while (iterator.hasNext()) {
|
||||
@@ -44,7 +44,7 @@ public class ConvertIteratorToListServiceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnIterator_whenConvertIteratorToListAfterJava8_thenReturnAList() {
|
||||
List<Integer> actualList = new ArrayList<Integer>();
|
||||
List<Integer> actualList = new ArrayList<>();
|
||||
|
||||
// Convert Iterator to List using Java 8
|
||||
iterator.forEachRemaining(actualList::add);
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java Map With Case-Insensitive Keys](https://www.baeldung.com/java-map-with-case-insensitive-keys)
|
||||
@@ -17,4 +17,4 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m
|
||||
- [Using NullAway to Avoid NullPointerExceptions](https://www.baeldung.com/java-nullaway)
|
||||
- [Introduction to Alibaba Arthas](https://www.baeldung.com/java-alibaba-arthas-intro)
|
||||
- [Quick Guide to Spring Cloud Circuit Breaker](https://www.baeldung.com/spring-cloud-circuit-breaker)
|
||||
- More articles [[<-- prev]](/libraries-2) [[next -->]](/libraries-4)
|
||||
- More articles [[<-- prev]](/libraries-2) [[next -->]](/libraries-4)
|
||||
|
||||
@@ -11,4 +11,4 @@ This module contains articles about test libraries.
|
||||
- [Introduction to Awaitlity](https://www.baeldung.com/awaitlity-testing)
|
||||
- [Introduction to Hoverfly in Java](https://www.baeldung.com/hoverfly)
|
||||
- [Testing with Hamcrest](https://www.baeldung.com/java-junit-hamcrest-guide)
|
||||
- [Introduction To DBUnit](https://www.baeldung.com/dbunit)
|
||||
- [Introduction To DBUnit](https://www.baeldung.com/java-dbunit)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [HTTP/2 in Netty](https://www.baeldung.com/netty-http2)
|
||||
@@ -1,3 +0,0 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Implementing The OAuth 2.0 Authorization Framework Using Jakarta EE](https://www.baeldung.com/java-ee-oauth2-implementation)
|
||||
@@ -1,3 +0,0 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Implementing The OAuth 2.0 Authorization Framework Using Jakarta EE](https://www.baeldung.com/java-ee-oauth2-implementation)
|
||||
@@ -14,3 +14,4 @@ This module contains articles specific to use of Hibernate as a JPA implementati
|
||||
- [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception)
|
||||
- [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context)
|
||||
- [Quick Guide to EntityManager#getReference()](https://www.baeldung.com/jpa-entity-manager-get-reference)
|
||||
- [Hibernate Error “No Persistence Provider for EntityManager”](https://www.baeldung.com/hibernate-no-persistence-provider)
|
||||
|
||||
@@ -11,3 +11,4 @@ This module contains articles about MongoDB in Java.
|
||||
- [Introduction to Morphia – Java ODM for MongoDB](https://www.baeldung.com/mongodb-morphia)
|
||||
- [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations)
|
||||
- [MongoDB BSON to JSON](https://www.baeldung.com/bson-to-json)
|
||||
- [BSON to JSON Document Conversion in Java](https://www.baeldung.com/java-convert-bson-to-json)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi)
|
||||
- [Oracle Connection Pooling With Spring](https://www.baeldung.com/spring-oracle-connection-pooling)
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.spring.transactional;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
class TransactionalCompareApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TransactionalCompareApplication.class, args);
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.spring.transactional.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Car {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String make;
|
||||
|
||||
private String model;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(Long id, String make, String model) {
|
||||
this.id = id;
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Car{" +
|
||||
"id=" + id +
|
||||
", make='" + make + '\'' +
|
||||
", model='" + model + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.spring.transactional.repository;
|
||||
|
||||
import com.baeldung.spring.transactional.entity.Car;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface CarRepository extends JpaRepository<Car, Long> {
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.spring.transactional.service;
|
||||
|
||||
import com.baeldung.spring.transactional.entity.Car;
|
||||
import com.baeldung.spring.transactional.repository.CarRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Isolation;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.persistence.EntityExistsException;
|
||||
|
||||
@Service
|
||||
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.SUPPORTS, readOnly = false, timeout = 30)
|
||||
public class CarService {
|
||||
|
||||
@Autowired
|
||||
private CarRepository carRepository;
|
||||
|
||||
@Transactional(rollbackFor = IllegalArgumentException.class, noRollbackFor = EntityExistsException.class,
|
||||
rollbackForClassName = "IllegalArgumentException", noRollbackForClassName = "EntityExistsException")
|
||||
public Car save(Car car) {
|
||||
return carRepository.save(car);
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.spring.transactional.service;
|
||||
|
||||
import com.baeldung.spring.transactional.entity.Car;
|
||||
import com.baeldung.spring.transactional.repository.CarRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.persistence.EntityExistsException;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional(Transactional.TxType.SUPPORTS)
|
||||
public class RentalService {
|
||||
|
||||
@Autowired
|
||||
private CarRepository carRepository;
|
||||
|
||||
@Transactional(rollbackOn = IllegalArgumentException.class, dontRollbackOn = EntityExistsException.class)
|
||||
public Car rent(Car car) {
|
||||
return carRepository.save(car);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
- [JPA Entity Lifecycle Events](https://www.baeldung.com/jpa-entity-lifecycle-events)
|
||||
- [Working with Lazy Element Collections in JPA](https://www.baeldung.com/java-jpa-lazy-collections)
|
||||
- [Calling Stored Procedures from Spring Data JPA Repositories](https://www.baeldung.com/spring-data-jpa-stored-procedures)
|
||||
- [Custom Naming Convention with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-custom-naming)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user