Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
@@ -62,3 +62,5 @@ jmeter/src/main/resources/*-JMeter.csv
|
||||
**/dist
|
||||
**/tmp
|
||||
**/out-tsc
|
||||
**/nbproject/
|
||||
**/nb-configuration.xml
|
||||
@@ -21,3 +21,8 @@ In additional to Spring, the following technologies are in focus: `core Java`, `
|
||||
Building the project
|
||||
====================
|
||||
To do the full build, do: `mvn install -Pdefault -Dgib.enabled=false`
|
||||
|
||||
|
||||
Building a single module
|
||||
====================
|
||||
To build a specific module run the command: `mvn clean install -Dgib.enabled=false` in the module directory
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.algorithms.insertionsort;
|
||||
|
||||
public class InsertionSort {
|
||||
|
||||
public static void insertionSortImperative(int[] input) {
|
||||
for (int i = 1; i < input.length; i++) {
|
||||
int key = input[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && input[j] > key) {
|
||||
input[j + 1] = input[j];
|
||||
j = j - 1;
|
||||
}
|
||||
input[j + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
public static void insertionSortRecursive(int[] input) {
|
||||
insertionSortRecursive(input, input.length);
|
||||
}
|
||||
|
||||
private static void insertionSortRecursive(int[] input, int i) {
|
||||
// base case
|
||||
if (i <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// sort the first i - 1 elements of the array
|
||||
insertionSortRecursive(input, i - 1);
|
||||
|
||||
// then find the correct position of the element at position i
|
||||
int key = input[i - 1];
|
||||
int j = i - 2;
|
||||
// shifting the elements from their position by 1
|
||||
while (j >= 0 && input[j] > key) {
|
||||
input[j + 1] = input[j];
|
||||
j = j - 1;
|
||||
}
|
||||
// inserting the key at the appropriate position
|
||||
input[j + 1] = key;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.algorithms.insertionsort;
|
||||
|
||||
import com.baeldung.algorithms.insertionsort.InsertionSort;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class InsertionSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenInsertionSortImperative_thenSortedAsc() {
|
||||
int[] input = {6, 2, 3, 4, 5, 1};
|
||||
InsertionSort.insertionSortImperative(input);
|
||||
int[] expected = {1, 2, 3, 4, 5, 6};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenInsertionSortRecursive_thenSortedAsc() {
|
||||
int[] input = {6, 4, 5, 2, 3, 1};
|
||||
InsertionSort.insertionSortRecursive(input);
|
||||
int[] expected = {1, 2, 3, 4, 5, 6};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
}
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.internationalization;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.FormatStyle;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateTimeFormatterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDefaultUsLocaleAndDateTimeAndPattern_whenFormatWithDifferentLocales_thenGettingLocalizedDateTimes() {
|
||||
Locale.setDefault(Locale.US);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 1, 10, 15, 50, 500);
|
||||
String pattern = "dd-MMMM-yyyy HH:mm:ss.SSS";
|
||||
|
||||
DateTimeFormatter defaultTimeFormatter = DateTimeFormatter.ofPattern(pattern);
|
||||
DateTimeFormatter plTimeFormatter = DateTimeFormatter.ofPattern(pattern, new Locale("pl", "PL"));
|
||||
DateTimeFormatter deTimeFormatter = DateTimeFormatter.ofPattern(pattern).withLocale(Locale.GERMANY);
|
||||
|
||||
Assert.assertEquals("01-January-2018 10:15:50.000", defaultTimeFormatter.format(localDateTime));
|
||||
Assert.assertEquals("01-stycznia-2018 10:15:50.000", plTimeFormatter.format(localDateTime));
|
||||
Assert.assertEquals("01-Januar-2018 10:15:50.000", deTimeFormatter.format(localDateTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateTimeAndTimeZone_whenFormatWithDifferentLocales_thenGettingLocalizedZonedDateTimes() {
|
||||
Locale.setDefault(Locale.US);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 1, 10, 15, 50, 500);
|
||||
ZoneId losAngelesTimeZone = TimeZone.getTimeZone("America/Los_Angeles").toZoneId();
|
||||
|
||||
DateTimeFormatter localizedFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
|
||||
DateTimeFormatter frLocalizedFormatter =
|
||||
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.FRANCE);
|
||||
String formattedDateTime = localizedFormatter.format(ZonedDateTime.of(localDateTime, losAngelesTimeZone));
|
||||
String frFormattedDateTime = frLocalizedFormatter.format(ZonedDateTime.of(localDateTime, losAngelesTimeZone));
|
||||
|
||||
Assert.assertEquals("Monday, January 1, 2018 10:15:50 AM PST", formattedDateTime);
|
||||
Assert.assertEquals("lundi 1 janvier 2018 10 h 15 PST", frFormattedDateTime);
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@
|
||||
- [Thread Safe LIFO Data Structure Implementations](https://www.baeldung.com/java-lifo-thread-safe)
|
||||
- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list)
|
||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||
- [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)
|
||||
- [Get the Key for a Value from a Java Map](https://www.baeldung.com/java-map-key-from-value)
|
||||
- [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity)
|
||||
|
||||
@@ -1,90 +1,88 @@
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-collections</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-collections</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>${collections-generic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.collections</groupId>
|
||||
<artifactId>eclipse-collections</artifactId>
|
||||
<version>${eclipse.collections.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.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>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${openjdk.jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${openjdk.jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-exec</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>one.util</groupId>
|
||||
<artifactId>streamex</artifactId>
|
||||
<version>0.6.5</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
<properties>
|
||||
<openjdk.jmh.version>1.19</openjdk.jmh.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<eclipse.collections.version>7.1.0</eclipse.collections.version>
|
||||
</properties>
|
||||
</project>
|
||||
<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-collections</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-collections</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>${collections-generic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.collections</groupId>
|
||||
<artifactId>eclipse-collections</artifactId>
|
||||
<version>${eclipse.collections.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.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>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${openjdk.jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${openjdk.jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-exec</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>one.util</groupId>
|
||||
<artifactId>streamex</artifactId>
|
||||
<version>0.6.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<openjdk.jmh.version>1.19</openjdk.jmh.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<eclipse.collections.version>7.1.0</eclipse.collections.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.convertcollectiontoarraylist;
|
||||
|
||||
/**
|
||||
* This POJO is the element type of our collection. It has a deepCopy() method.
|
||||
*
|
||||
* @author chris
|
||||
*/
|
||||
public class Foo {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private Foo parent;
|
||||
|
||||
public Foo() {
|
||||
}
|
||||
|
||||
public Foo(int id, String name, Foo parent) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Foo getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(Foo parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public Foo deepCopy() {
|
||||
return new Foo(
|
||||
this.id, this.name, this.parent != null ? this.parent.deepCopy() : null);
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.synchronizedcollections.application;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Application {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(Application.class.getName());
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
List<Integer> syncCollection = Collections.synchronizedList(Arrays.asList(1, 2, 3, 4, 5, 6));
|
||||
synchronized (syncCollection) {
|
||||
syncCollection.forEach((e) -> {LOGGER.info(e.toString());});
|
||||
}
|
||||
}
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package com.baeldung.convertcollectiontoarraylist;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import static java.util.stream.Collectors.toCollection;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chris
|
||||
*/
|
||||
public class FooUnitTest {
|
||||
private static Collection<Foo> srcCollection = new HashSet<>();
|
||||
|
||||
public FooUnitTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
int i = 0;
|
||||
Foo john = new Foo(i++, "John", null);
|
||||
Foo mary = new Foo(i++, "Mary", null);
|
||||
Foo sam = new Foo(i++, "Sam", john);
|
||||
Foo alice = new Foo(i++, "Alice", john);
|
||||
Foo buffy = new Foo(i++, "Buffy", sam);
|
||||
srcCollection.add(john);
|
||||
srcCollection.add(mary);
|
||||
srcCollection.add(sam);
|
||||
srcCollection.add(alice);
|
||||
srcCollection.add(buffy);
|
||||
|
||||
// make sure the collection isn't sorted accidentally
|
||||
assertFalse("Oops: source collection is already sorted!", isSorted(srcCollection));
|
||||
}
|
||||
|
||||
/**
|
||||
* Section 3. Using the ArrayList Constructor
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingConstructor_thenVerifyShallowCopy() {
|
||||
ArrayList<Foo> newList = new ArrayList<>(srcCollection);
|
||||
verifyShallowCopy(srcCollection, newList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Section 4. Using the Streams API
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingStream_thenVerifyShallowCopy() {
|
||||
ArrayList<Foo> newList = srcCollection.stream().collect(toCollection(ArrayList::new));
|
||||
|
||||
verifyShallowCopy(srcCollection, newList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Section 5. Deep Copy
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingDeepCopy_thenVerifyDeepCopy() {
|
||||
ArrayList<Foo> newList = srcCollection.stream()
|
||||
.map(foo -> foo.deepCopy())
|
||||
.collect(toCollection(ArrayList::new));
|
||||
|
||||
verifyDeepCopy(srcCollection, newList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Section 6. Controlling the List Order
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingSortedStream_thenVerifySortOrder() {
|
||||
ArrayList<Foo> newList = srcCollection.stream()
|
||||
.sorted(Comparator.comparing(Foo::getName))
|
||||
.collect(toCollection(ArrayList::new));
|
||||
|
||||
assertTrue("ArrayList is not sorted by name", isSorted(newList));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the contents of the two collections are the same
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
private void verifyShallowCopy(Collection a, Collection 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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the contents of the two collections are the same
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
private void verifyDeepCopy(Collection a, Collection b) {
|
||||
assertEquals("Collections have different lengths", a.size(), b.size());
|
||||
Iterator<Foo> iterA = a.iterator();
|
||||
Iterator<Foo> iterB = b.iterator();
|
||||
while (iterA.hasNext()) {
|
||||
Foo nextA = iterA.next();
|
||||
Foo nextB = iterB.next();
|
||||
// should not be same instance
|
||||
assertFalse("Foo instances are the same!", nextA == nextB);
|
||||
// but should have same content
|
||||
assertFalse("Foo instances have different content!", fooDiff(nextA, nextB));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the contents of a and b differ. Test parent recursively
|
||||
* @param a
|
||||
* @param b
|
||||
* @return False if the two items are the same
|
||||
*/
|
||||
private boolean fooDiff(Foo a, Foo b) {
|
||||
if (a != null && b != null) {
|
||||
return a.getId() != b.getId()
|
||||
|| !a.getName().equals(b.getName())
|
||||
|| fooDiff(a.getParent(), b.getParent());
|
||||
}
|
||||
return !(a == null && b == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param c collection of Foo
|
||||
* @return true if the collection is sorted by name
|
||||
*/
|
||||
private static boolean isSorted(Collection<Foo> c) {
|
||||
String prevName = null;
|
||||
for (Foo foo : c) {
|
||||
if (prevName == null || foo.getName().compareTo(prevName) > 0) {
|
||||
prevName = foo.getName();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.java.map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections4.MultiMap;
|
||||
import org.apache.commons.collections4.MultiMapUtils;
|
||||
import org.apache.commons.collections4.MultiValuedMap;
|
||||
import org.apache.commons.collections4.map.MultiValueMap;
|
||||
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
|
||||
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.LinkedHashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.TreeMultimap;
|
||||
|
||||
|
||||
public class KeyCheckTest {
|
||||
|
||||
@Test
|
||||
public void whenKeyIsPresent_thenContainsKeyReturnsTrue() {
|
||||
Map<String, String> map = Collections.singletonMap("key", "value");
|
||||
|
||||
assertTrue(map.containsKey("key"));
|
||||
assertFalse(map.containsKey("missing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenKeyHasNullValue_thenGetStillWorks() {
|
||||
Map<String, String> map = Collections.singletonMap("nothing", null);
|
||||
|
||||
assertTrue(map.containsKey("nothing"));
|
||||
assertNull(map.get("nothing"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.java.map;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class KeyCheckUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenKeyIsPresent_thenContainsKeyReturnsTrue() {
|
||||
Map<String, String> map = Collections.singletonMap("key", "value");
|
||||
|
||||
assertTrue(map.containsKey("key"));
|
||||
assertFalse(map.containsKey("missing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenKeyHasNullValue_thenGetStillWorks() {
|
||||
Map<String, String> map = Collections.singletonMap("nothing", null);
|
||||
|
||||
assertTrue(map.containsKey("nothing"));
|
||||
assertNull(map.get("nothing"));
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SynchronizedCollectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedCollection_whenTwoThreadsAddElements_thenCorrectCollectionSize() throws InterruptedException {
|
||||
Collection<Integer> syncCollection = Collections.synchronizedCollection(new ArrayList<>());
|
||||
|
||||
Runnable listOperations = () -> {
|
||||
syncCollection.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
|
||||
};
|
||||
Thread thread1 = new Thread(listOperations);
|
||||
Thread thread2 = new Thread(listOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncCollection.size()).isEqualTo(12);
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class SynchronizedListUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedList_whenTwoThreadsAddElements_thenCorrectListSize() throws InterruptedException {
|
||||
List<Integer> syncList = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
Runnable listOperations = () -> {
|
||||
syncList.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
|
||||
};
|
||||
Thread thread1 = new Thread(listOperations);
|
||||
Thread thread2 = new Thread(listOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncList.size()).isEqualTo(12);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringList_whenTwoThreadsIterateOnSynchronizedList_thenCorrectResult() throws InterruptedException {
|
||||
List<String> syncCollection = Collections.synchronizedList(Arrays.asList("a", "b", "c"));
|
||||
List<String> uppercasedCollection = new ArrayList<>();
|
||||
|
||||
Runnable listOperations = () -> {
|
||||
synchronized (syncCollection) {
|
||||
syncCollection.forEach((e) -> {
|
||||
uppercasedCollection.add(e.toUpperCase());
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Thread thread1 = new Thread(listOperations);
|
||||
Thread thread2 = new Thread(listOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(uppercasedCollection.get(0)).isEqualTo("A");
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class SynchronizedMapUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedMap_whenTwoThreadsAddElements_thenCorrectMapSize() throws InterruptedException {
|
||||
Map<Integer, String> syncMap = Collections.synchronizedMap(new HashMap<>());
|
||||
|
||||
Runnable mapOperations = () -> {
|
||||
syncMap.put(1, "one");
|
||||
syncMap.put(2, "two");
|
||||
syncMap.put(3, "three");
|
||||
|
||||
};
|
||||
Thread thread1 = new Thread(mapOperations);
|
||||
Thread thread2 = new Thread(mapOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncMap.size()).isEqualTo(3);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class SynchronizedSetUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedSet_whenTwoThreadsAddElements_thenCorrectSetSize() throws InterruptedException {
|
||||
Set<Integer> syncSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
Runnable setOperations = () -> {syncSet.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));};
|
||||
Thread thread1 = new Thread(setOperations);
|
||||
Thread thread2 = new Thread(setOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncSet.size()).isEqualTo(6);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SynchronizedSortedMapUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedSorteMap_whenTwoThreadsAddElements_thenCorrectSortedMapSize() throws InterruptedException {
|
||||
Map<Integer, String> syncSortedMap = Collections.synchronizedSortedMap(new TreeMap<>());
|
||||
|
||||
Runnable sortedMapOperations = () -> {
|
||||
syncSortedMap.put(1, "One");
|
||||
syncSortedMap.put(2, "Two");
|
||||
syncSortedMap.put(3, "Three");
|
||||
};
|
||||
Thread thread1 = new Thread(sortedMapOperations);
|
||||
Thread thread2 = new Thread(sortedMapOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncSortedMap.size()).isEqualTo(3);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.synchronizedcollections.test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SynchronizedSortedSetUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedSortedSet_whenTwoThreadsAddElements_thenCorrectSortedSetSize() throws InterruptedException {
|
||||
SortedSet<Integer> syncSortedSet = Collections.synchronizedSortedSet(new TreeSet<>());
|
||||
|
||||
Runnable sortedSetOperations = () -> {syncSortedSet.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));};
|
||||
sortedSetOperations.run();
|
||||
sortedSetOperations.run();
|
||||
Thread thread1 = new Thread(sortedSetOperations);
|
||||
Thread thread2 = new Thread(sortedSetOperations);
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
assertThat(syncSortedSet.size()).isEqualTo(6);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
public class OutputStreamExamples {
|
||||
|
||||
public void fileOutputStreamByteSequence(String file, String data) throws IOException {
|
||||
byte[] bytes = data.getBytes();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
out.write(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public void fileOutputStreamByteSubSequence(String file, String data) throws IOException {
|
||||
byte[] bytes = data.getBytes();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
out.write(bytes, 6, 5);
|
||||
}
|
||||
}
|
||||
|
||||
public void fileOutputStreamByteSingle(String file, String data) throws IOException {
|
||||
byte[] bytes = data.getBytes();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
out.write(bytes[6]);
|
||||
}
|
||||
}
|
||||
|
||||
public void bufferedOutputStream(String file, String... data) throws IOException {
|
||||
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
|
||||
for (String s : data) {
|
||||
out.write(s.getBytes());
|
||||
out.write(" ".getBytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void outputStreamWriter(String file, String data) throws IOException {
|
||||
try (OutputStream out = new FileOutputStream(file); Writer writer = new OutputStreamWriter(out, "UTF-8")) {
|
||||
writer.write(data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class OutputStreamExamplesUnitTest {
|
||||
|
||||
StringBuilder filePath = new StringBuilder();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
filePath.append("src");
|
||||
filePath.append(File.separator);
|
||||
filePath.append("test");
|
||||
filePath.append(File.separator);
|
||||
filePath.append("resources");
|
||||
filePath.append(File.separator);
|
||||
filePath.append("output_file.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStream_whenWriteSingleByteCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.fileOutputStreamByteSingle(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStream_whenWriteByteSequenceCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.fileOutputStreamByteSequence(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStream_whenWriteByteSubSequenceCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.fileOutputStreamByteSubSequence(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBufferedOutputStream_whenCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.bufferedOutputStream(filePath.toString(), "Hello", "World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStreamWriter_whenCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.outputStreamWriter(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
package com.baeldung.classloader;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class CustomClassLoader extends ClassLoader {
|
||||
|
||||
|
||||
public Class getClass(String name) throws ClassNotFoundException {
|
||||
@Override
|
||||
public Class findClass(String name) throws ClassNotFoundException {
|
||||
byte[] b = loadClassFromFile(name);
|
||||
return defineClass(name, b, 0, b.length);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ public class CustomClassLoaderUnitTest {
|
||||
public void customLoader() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
|
||||
|
||||
CustomClassLoader customClassLoader = new CustomClassLoader();
|
||||
Class<?> c = customClassLoader.getClass(PrintClassLoader.class.getName());
|
||||
Class<?> c = customClassLoader.findClass(PrintClassLoader.class.getName());
|
||||
|
||||
Object ob = c.newInstance();
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.datastructures
|
||||
|
||||
/**
|
||||
* Example of how to use the {@link Node} class.
|
||||
*
|
||||
*/
|
||||
fun main(args: Array<String>) {
|
||||
val tree = Node(4)
|
||||
val keys = arrayOf(8, 15, 21, 3, 7, 2, 5, 10, 2, 3, 4, 6, 11)
|
||||
for (key in keys) {
|
||||
tree.insert(key)
|
||||
}
|
||||
val node = tree.find(4)!!
|
||||
println("Node with value ${node.key} [left = ${node.left?.key}, right = ${node.right?.key}]")
|
||||
println("Delete node with key = 3")
|
||||
node.delete(3)
|
||||
print("Tree content after the node elimination: ")
|
||||
println(tree.visit().joinToString { it.toString() })
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package com.baeldung.datastructures
|
||||
|
||||
/**
|
||||
* An ADT for a binary search tree.
|
||||
* Note that this data type is neither immutable nor thread safe.
|
||||
*/
|
||||
class Node(
|
||||
var key: Int,
|
||||
var left: Node? = null,
|
||||
var right: Node? = null) {
|
||||
|
||||
/**
|
||||
* Return a node with given value. If no such node exists, return null.
|
||||
* @param value
|
||||
*/
|
||||
fun find(value: Int): Node? = when {
|
||||
this.key > value -> left?.find(value)
|
||||
this.key < value -> right?.find(value)
|
||||
else -> this
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a given value into the tree.
|
||||
* After insertion, the tree should contain a node with the given value.
|
||||
* If the tree already contains the given value, nothing is performed.
|
||||
* @param value
|
||||
*/
|
||||
fun insert(value: Int) {
|
||||
if (value > this.key) {
|
||||
if (this.right == null) {
|
||||
this.right = Node(value)
|
||||
} else {
|
||||
this.right?.insert(value)
|
||||
}
|
||||
} else if (value < this.key) {
|
||||
if (this.left == null) {
|
||||
this.left = Node(value)
|
||||
} else {
|
||||
this.left?.insert(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the value from the given tree. If the tree does not contain the value, the tree remains unchanged.
|
||||
* @param value
|
||||
*/
|
||||
fun delete(value: Int) {
|
||||
when {
|
||||
value > key -> scan(value, this.right, this)
|
||||
value < key -> scan(value, this.left, this)
|
||||
else -> removeNode(this, null)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan the tree in the search of the given value.
|
||||
* @param value
|
||||
* @param node sub-tree that potentially might contain the sought value
|
||||
* @param parent node's parent
|
||||
*/
|
||||
private fun scan(value: Int, node: Node?, parent: Node?) {
|
||||
if (node == null) {
|
||||
System.out.println("value " + value
|
||||
+ " seems not present in the tree.")
|
||||
return
|
||||
}
|
||||
when {
|
||||
value > node.key -> scan(value, node.right, node)
|
||||
value < node.key -> scan(value, node.left, node)
|
||||
value == node.key -> removeNode(node, parent)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the node.
|
||||
*
|
||||
* Removal process depends on how many children the node has.
|
||||
*
|
||||
* @param node node that is to be removed
|
||||
* @param parent parent of the node to be removed
|
||||
*/
|
||||
private fun removeNode(node: Node, parent: Node?) {
|
||||
node.left?.let { leftChild ->
|
||||
run {
|
||||
node.right?.let {
|
||||
removeTwoChildNode(node)
|
||||
} ?: removeSingleChildNode(node, leftChild)
|
||||
}
|
||||
} ?: run {
|
||||
node.right?.let { rightChild -> removeSingleChildNode(node, rightChild) } ?: removeNoChildNode(node, parent)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the node without children.
|
||||
* @param node
|
||||
* @param parent
|
||||
*/
|
||||
private fun removeNoChildNode(node: Node, parent: Node?) {
|
||||
parent?.let { p ->
|
||||
if (node == p.left) {
|
||||
p.left = null
|
||||
} else if (node == p.right) {
|
||||
p.right = null
|
||||
}
|
||||
} ?: throw IllegalStateException(
|
||||
"Can not remove the root node without child nodes")
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a node that has two children.
|
||||
*
|
||||
* The process of elimination is to find the biggest key in the left sub-tree and replace the key of the
|
||||
* node that is to be deleted with that key.
|
||||
*/
|
||||
private fun removeTwoChildNode(node: Node) {
|
||||
val leftChild = node.left!!
|
||||
leftChild.right?.let {
|
||||
val maxParent = findParentOfMaxChild(leftChild)
|
||||
maxParent.right?.let {
|
||||
node.key = it.key
|
||||
maxParent.right = null
|
||||
} ?: throw IllegalStateException("Node with max child must have the right child!")
|
||||
|
||||
} ?: run {
|
||||
node.key = leftChild.key
|
||||
node.left = leftChild.left
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a node whose right child contains the biggest value in the given sub-tree.
|
||||
* Assume that the node n has a non-null right child.
|
||||
*
|
||||
* @param n
|
||||
*/
|
||||
private fun findParentOfMaxChild(n: Node): Node {
|
||||
return n.right?.let { r -> r.right?.let { findParentOfMaxChild(r) } ?: n }
|
||||
?: throw IllegalArgumentException("Right child must be non-null")
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a parent that has only one child.
|
||||
* Removal is effectively is just coping the data from the child parent to the parent parent.
|
||||
* @param parent Node to be deleted. Assume that it has just one child
|
||||
* @param child Assume it is a child of the parent
|
||||
*/
|
||||
private fun removeSingleChildNode(parent: Node, child: Node) {
|
||||
parent.key = child.key
|
||||
parent.left = child.left
|
||||
parent.right = child.right
|
||||
}
|
||||
|
||||
fun visit(): Array<Int> {
|
||||
val a = left?.visit() ?: emptyArray()
|
||||
val b = right?.visit() ?: emptyArray()
|
||||
return a + arrayOf(key) + b
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,3 @@
|
||||
### Relevant articles
|
||||
|
||||
- [CDI Portable Extension and Flyway](https://www.baeldung.com/cdi-portable-extension)
|
||||
@@ -14,4 +14,5 @@
|
||||
- [Bootstrapping JPA Programmatically in Java](http://www.baeldung.com/java-bootstrap-jpa)
|
||||
- [Optimistic Locking in JPA](http://www.baeldung.com/jpa-optimistic-locking)
|
||||
- [Hibernate Entity Lifecycle](https://www.baeldung.com/hibernate-entity-lifecycle)
|
||||
- [Mapping A Hibernate Query to a Custom Class](https://www.baeldung.com/hibernate-query-to-custom-class)
|
||||
- [@JoinColumn Annotation Explained](https://www.baeldung.com/jpa-join-column)
|
||||
|
||||
@@ -4,13 +4,23 @@ import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.SequenceGenerator;
|
||||
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.Parameter;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence-generator")
|
||||
@SequenceGenerator(name = "sequence-generator", sequenceName = "user_sequence", initialValue = 4)
|
||||
@GeneratedValue(generator = "sequence-generator")
|
||||
@GenericGenerator(
|
||||
name = "sequence-generator",
|
||||
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
|
||||
parameters = {
|
||||
@Parameter(name = "sequence_name", value = "user_sequence"),
|
||||
@Parameter(name = "initial_value", value = "4"),
|
||||
@Parameter(name = "increment_size", value = "1")
|
||||
}
|
||||
)
|
||||
private long userId;
|
||||
|
||||
public long getUserId() {
|
||||
|
||||
+2
-2
@@ -80,7 +80,7 @@
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<maven.compiler.source>9</maven.compiler.source>
|
||||
<maven.compiler.target>9</maven.compiler.target>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
@@ -5,7 +5,9 @@ import org.junit.Test;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Period;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
@@ -26,6 +28,17 @@ public class DateDiffUnitTest {
|
||||
|
||||
assertEquals(diff, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
|
||||
LocalDate now = LocalDate.now();
|
||||
LocalDate sixDaysBehind = now.minusDays(6);
|
||||
|
||||
Period period = Period.between(now, sixDaysBehind);
|
||||
int diff = period.getDays();
|
||||
|
||||
assertEquals(diff, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user