Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -45,4 +45,4 @@
|
||||
<providermodule.version>1.0</providermodule.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -27,6 +27,17 @@
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
@@ -44,11 +55,30 @@
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<lombok.version>1.18.20</lombok.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<jmh.version>1.29</jmh.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
public class BenchmarkRunner {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class DifferentSourceSplitting {
|
||||
|
||||
private static final List<Integer> arrayListOfNumbers = new ArrayList<>();
|
||||
private static final List<Integer> linkedListOfNumbers = new LinkedList<>();
|
||||
|
||||
static {
|
||||
IntStream.rangeClosed(1, 1_000_000).forEach(i -> {
|
||||
arrayListOfNumbers.add(i);
|
||||
linkedListOfNumbers.add(i);
|
||||
});
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void differentSourceArrayListSequential() {
|
||||
arrayListOfNumbers.stream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void differentSourceArrayListParallel() {
|
||||
arrayListOfNumbers.parallelStream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void differentSourceLinkedListSequential() {
|
||||
linkedListOfNumbers.stream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void differentSourceLinkedListParallel() {
|
||||
linkedListOfNumbers.parallelStream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class MemoryLocalityCosts {
|
||||
|
||||
private static final int[] intArray = new int[1_000_000];
|
||||
private static final Integer[] integerArray = new Integer[1_000_000];
|
||||
|
||||
static {
|
||||
IntStream.rangeClosed(1, 1_000_000).forEach(i -> {
|
||||
intArray[i-1] = i;
|
||||
integerArray[i-1] = i;
|
||||
});
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void localityIntArraySequential() {
|
||||
Arrays.stream(intArray).reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void localityIntArrayParallel() {
|
||||
Arrays.stream(intArray).parallel().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void localityIntegerArraySequential() {
|
||||
Arrays.stream(integerArray).reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void localityIntegerArrayParallel() {
|
||||
Arrays.stream(integerArray).parallel().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class MergingCosts {
|
||||
|
||||
private static final List<Integer> arrayListOfNumbers = new ArrayList<>();
|
||||
|
||||
static {
|
||||
IntStream.rangeClosed(1, 1_000_000).forEach(i -> {
|
||||
arrayListOfNumbers.add(i);
|
||||
});
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void mergingCostsSumSequential() {
|
||||
arrayListOfNumbers.stream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void mergingCostsSumParallel() {
|
||||
arrayListOfNumbers.stream().parallel().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void mergingCostsGroupingSequential() {
|
||||
arrayListOfNumbers.stream().collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void mergingCostsGroupingParallel() {
|
||||
arrayListOfNumbers.stream().parallel().collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ParallelStream {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Integer> listOfNumbers = Arrays.asList(1, 2, 3, 4);
|
||||
listOfNumbers.parallelStream().forEach(number ->
|
||||
System.out.println(number + " " + Thread.currentThread().getName())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class SequentialStream {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Integer> listOfNumbers = Arrays.asList(1, 2, 3, 4);
|
||||
listOfNumbers.stream().forEach(number ->
|
||||
System.out.println(number + " " + Thread.currentThread().getName())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class SplittingCosts {
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void sourceSplittingIntStreamSequential() {
|
||||
IntStream.rangeClosed(1, 100).reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void sourceSplittingIntStreamParallel() {
|
||||
IntStream.rangeClosed(1, 100).parallel().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ForkJoinUnitTest {
|
||||
|
||||
@Test
|
||||
void givenSequentialStreamOfNumbers_whenReducingSumWithIdentityFive_thenResultIsCorrect() {
|
||||
List<Integer> listOfNumbers = Arrays.asList(1, 2, 3, 4);
|
||||
int sum = listOfNumbers.stream().reduce(5, Integer::sum);
|
||||
assertThat(sum).isEqualTo(15);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenParallelStreamOfNumbers_whenReducingSumWithIdentityFive_thenResultIsNotCorrect() {
|
||||
List<Integer> listOfNumbers = Arrays.asList(1, 2, 3, 4);
|
||||
int sum = listOfNumbers.parallelStream().reduce(5, Integer::sum);
|
||||
assertThat(sum).isNotEqualTo(15);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenParallelStreamOfNumbers_whenReducingSumWithIdentityZero_thenResultIsCorrect() {
|
||||
List<Integer> listOfNumbers = Arrays.asList(1, 2, 3, 4);
|
||||
int sum = listOfNumbers.parallelStream().reduce(0, Integer::sum) + 5;
|
||||
assertThat(sum).isEqualTo(15);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParallelStreamOfNumbers_whenUsingCustomThreadPool_thenResultIsCorrect()
|
||||
throws InterruptedException, ExecutionException {
|
||||
List<Integer> listOfNumbers = Arrays.asList(1, 2, 3, 4);
|
||||
ForkJoinPool customThreadPool = new ForkJoinPool(4);
|
||||
int sum = customThreadPool.submit(
|
||||
() -> listOfNumbers.parallelStream().reduce(0, Integer::sum)).get();
|
||||
customThreadPool.shutdown();
|
||||
assertThat(sum).isEqualTo(10);
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.stringtobigdecimal;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.text.ParseException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringToBigDecimalConversionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenBigDecimalObjectWithStringParameter_ThenResultIsDecimalObject() {
|
||||
BigDecimal bigDecimal = new BigDecimal(123);
|
||||
assertEquals(bigDecimal, new BigDecimal("123"));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenBigDecimalObjectWithStringParameter_ThenNullPointerExceptionIsThrown() {
|
||||
String bigDecimal = null;
|
||||
new BigDecimal(bigDecimal);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void givenInalidString_WhenBigDecimalObjectWithStringParameter_ThenNumberFormatExceptionIsThrown() {
|
||||
new BigDecimal("&");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenValueOfDoubleFromString_ThenResultIsDecimalObject() {
|
||||
BigDecimal bigDecimal = new BigDecimal(123.42).setScale(2, BigDecimal.ROUND_HALF_UP);
|
||||
assertEquals(bigDecimal, BigDecimal.valueOf(Double.valueOf("123.42")));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenValueOfDoubleFromString_ThenNullPointerExceptionIsThrown() {
|
||||
BigDecimal.valueOf(Double.valueOf(null));
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void givenInalidString_WhenValueOfDoubleFromString_ThenNumberFormatExceptionIsThrown() {
|
||||
BigDecimal.valueOf(Double.valueOf("&"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenDecimalFormatOfString_ThenResultIsDecimalObject() throws ParseException {
|
||||
BigDecimal bigDecimal = new BigDecimal(10692467440017.111).setScale(3, BigDecimal.ROUND_HALF_UP);
|
||||
|
||||
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
|
||||
symbols.setGroupingSeparator(',');
|
||||
symbols.setDecimalSeparator('.');
|
||||
String pattern = "#,##0.0#";
|
||||
DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
|
||||
decimalFormat.setParseBigDecimal(true);
|
||||
|
||||
// parse the string value
|
||||
BigDecimal parsedStringValue = (BigDecimal) decimalFormat.parse("10,692,467,440,017.111");
|
||||
|
||||
assertEquals(bigDecimal, parsedStringValue);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenDecimalFormatOfString_ThenNullPointerExceptionIsThrown() throws ParseException {
|
||||
new DecimalFormat("#").parse(null);
|
||||
}
|
||||
|
||||
@Test(expected = ParseException.class)
|
||||
public void givenInalidString_WhenDecimalFormatOfString_ThenNumberFormatExceptionIsThrown() throws ParseException {
|
||||
new DecimalFormat("#").parse("&");
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.splitkeepdelimiters;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
|
||||
public class SplitAndKeepDelimitersUnitTest {
|
||||
|
||||
private final String positivelookAheadRegex = "((?=@))";
|
||||
private final String positivelookBehindRegex = "((?<=@))";
|
||||
private final String positivelookAroundRegex = "((?=@)|(?<=@))";
|
||||
private final String positiveLookAroundMultiDelimiterRegex = "((?=:|#|@)|(?<=:|#|@))";
|
||||
|
||||
private String text = "Hello@World@This@Is@A@Java@Program";
|
||||
private String textMixed = "@HelloWorld@This:Is@A#Java#Program";
|
||||
private String textMixed2 = "pg@no;10@hello;world@this;is@a#10words;Java#Program";
|
||||
|
||||
@Test
|
||||
public void givenString_splitAndKeepDelimiters_using_javaLangString() {
|
||||
|
||||
assertThat(text.split(positivelookAheadRegex)).containsExactly("Hello", "@World", "@This", "@Is", "@A", "@Java", "@Program");
|
||||
|
||||
assertThat(text.split(positivelookBehindRegex)).containsExactly("Hello@", "World@", "This@", "Is@", "A@", "Java@", "Program");
|
||||
|
||||
assertThat(text.split(positivelookAroundRegex)).containsExactly("Hello", "@", "World", "@", "This", "@", "Is", "@", "A", "@", "Java", "@", "Program");
|
||||
|
||||
assertThat(textMixed.split(positiveLookAroundMultiDelimiterRegex)).containsExactly("@", "HelloWorld", "@", "This", ":", "Is", "@", "A", "#", "Java", "#", "Program");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_splitAndKeepDelimiters_using_ApacheCommonsLang3StringUtils() {
|
||||
|
||||
assertThat(StringUtils.splitByCharacterType(textMixed2)).containsExactly("pg", "@", "no", ";", "10", "@", "hello", ";", "world", "@", "this", ";", "is", "@", "a", "#", "10", "words", ";", "J", "ava", "#", "P", "rogram");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_splitAndKeepDelimiters_using_GuavaSplitter() {
|
||||
|
||||
assertThat(Splitter.onPattern(positivelookAroundRegex)
|
||||
.splitToList(text)).containsExactly("Hello", "@", "World", "@", "This", "@", "Is", "@", "A", "@", "Java", "@", "Program");
|
||||
|
||||
assertThat(Splitter.on(Pattern.compile(positivelookAroundRegex))
|
||||
.splitToList(text)).containsExactly("Hello", "@", "World", "@", "This", "@", "Is", "@", "A", "@", "Java", "@", "Program");
|
||||
|
||||
assertThat(Splitter.onPattern(positiveLookAroundMultiDelimiterRegex)
|
||||
.splitToList(textMixed)).containsExactly("@", "HelloWorld", "@", "This", ":", "Is", "@", "A", "#", "Java", "#", "Program");
|
||||
|
||||
assertThat(Splitter.on(Pattern.compile(positiveLookAroundMultiDelimiterRegex))
|
||||
.splitToList(textMixed)).containsExactly("@", "HelloWorld", "@", "This", ":", "Is", "@", "A", "#", "Java", "#", "Program");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>2.1.3.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.mantisrx</groupId>
|
||||
@@ -35,36 +34,31 @@
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.10.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.andreinc.mockneat</groupId>
|
||||
<artifactId>mockneat</artifactId>
|
||||
<version>0.3.8</version>
|
||||
<version>0.4.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webflux</artifactId>
|
||||
<version>5.0.9.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor.netty</groupId>
|
||||
<artifactId>reactor-netty</artifactId>
|
||||
<version>0.9.12.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>SpringLibReleaseRepo</id>
|
||||
<url>https://repo.spring.io/libs-release/</url>
|
||||
<id>jcenter</id>
|
||||
<url>https://jcenter.bintray.com/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
|
||||
@@ -26,6 +26,11 @@
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.findbugs</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>${findbugs.annotations.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
@@ -41,6 +46,7 @@
|
||||
|
||||
<properties>
|
||||
<intellij.annotations.version>16.0.2</intellij.annotations.version>
|
||||
<findbugs.annotations.version>3.0.1</findbugs.annotations.version>
|
||||
<assertj-core.version>3.9.1</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
|
||||
+4
-4
@@ -1,12 +1,12 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import edu.umd.cs.findbugs.annotations.NonNull;
|
||||
import edu.umd.cs.findbugs.annotations.Nullable;
|
||||
|
||||
public class FindBugsAnnotations {
|
||||
|
||||
public void accept(@NotNull Object param) {
|
||||
public void accept(@NonNull Object param) {
|
||||
System.out.println(param.toString());
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ public class FindBugsAnnotations {
|
||||
System.out.println("Printing " + param);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@NonNull
|
||||
public Object process() throws Exception {
|
||||
Object result = doSomething();
|
||||
if (result == null) {
|
||||
|
||||
@@ -68,6 +68,12 @@
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.jpa.IdGeneration;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
// @GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.jpa.IdGeneration;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
public class UserService {
|
||||
|
||||
EntityManager entityManager;
|
||||
|
||||
public UserService(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public long saveUser(User user){
|
||||
entityManager.persist(user);
|
||||
return user.getId();
|
||||
}
|
||||
}
|
||||
@@ -97,4 +97,20 @@
|
||||
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="jpa-h2-id-generation">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.IdGeneration.User</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:idGen"/>
|
||||
<property name="javax.persistence.jdbc.user" value="sa"/>
|
||||
<property name="javax.persistence.jdbc.password" value=""/>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
|
||||
<property name="hibernate.format_sql" value="true"/>
|
||||
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.jpa.IdGeneration;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import java.util.UUID;
|
||||
|
||||
public class IdGenerationIntegrationTest {
|
||||
|
||||
private static EntityManager entityManager;
|
||||
private static UserService service;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2-id-generation");
|
||||
entityManager = factory.createEntityManager();
|
||||
service = new UserService(entityManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNewUserIsPersisted_thenEntityHasNoId() {
|
||||
User user = new User();
|
||||
user.setUsername("test");
|
||||
user.setPassword(UUID.randomUUID().toString());
|
||||
|
||||
long index = service.saveUser(user);
|
||||
Assert.assertEquals(0L, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTransactionIsControlled_thenEntityHasId() {
|
||||
User user = new User();
|
||||
user.setUsername("test");
|
||||
user.setPassword(UUID.randomUUID().toString());
|
||||
|
||||
entityManager.getTransaction().begin();
|
||||
long index = service.saveUser(user);
|
||||
entityManager.getTransaction().commit();
|
||||
|
||||
Assert.assertEquals(2L, index);
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.attribute.override.entity;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Address {
|
||||
private String name;
|
||||
private String city;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.attribute.override.entity;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Embedded;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Embeddable
|
||||
public class Brand {
|
||||
private String name;
|
||||
private LocalDate foundationDate;
|
||||
@Embedded
|
||||
private Address address;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public LocalDate getFoundationDate() {
|
||||
return foundationDate;
|
||||
}
|
||||
|
||||
public void setFoundationDate(LocalDate foundationDate) {
|
||||
this.foundationDate = foundationDate;
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.attribute.override.entity;
|
||||
|
||||
import javax.persistence.AttributeOverride;
|
||||
import javax.persistence.AttributeOverrides;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Embedded;
|
||||
import javax.persistence.Entity;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
@AttributeOverride(name = "identifier", column = @Column(name = "VIN"))
|
||||
public class Car extends Vehicle {
|
||||
|
||||
private String model;
|
||||
private String name;
|
||||
@Embedded
|
||||
@AttributeOverrides({
|
||||
@AttributeOverride(name = "name", column = @Column(name = "BRAND_NAME", length = 5)),
|
||||
@AttributeOverride(name = "address.name", column = @Column(name = "ADDRESS_NAME"))
|
||||
})
|
||||
private Brand brand;
|
||||
@ElementCollection
|
||||
@AttributeOverrides({
|
||||
@AttributeOverride(name = "key.name", column = @Column(name = "OWNER_NAME")),
|
||||
@AttributeOverride(name = "key.surname", column = @Column(name = "OWNER_SURNAME")),
|
||||
@AttributeOverride(name = "value.name", column = @Column(name = "ADDRESS_NAME")),
|
||||
})
|
||||
Map<Owner, Address> owners;
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Brand getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(Brand brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.attribute.override.entity;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Owner {
|
||||
private String name;
|
||||
private String surname;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public void setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.attribute.override.entity;
|
||||
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
public class Vehicle {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String identifier;
|
||||
private Integer numberOfWheels;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public void setIdentifier(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public Integer getNumberOfWheels() {
|
||||
return numberOfWheels;
|
||||
}
|
||||
|
||||
public void setNumberOfWheels(Integer numberOfWheels) {
|
||||
this.numberOfWheels = numberOfWheels;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.attribute.override.repository;
|
||||
|
||||
import com.baeldung.attribute.override.entity.Car;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface CarRepository extends JpaRepository<Car, Integer> {
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.attribute.override;
|
||||
|
||||
import com.baeldung.Application;
|
||||
import com.baeldung.attribute.override.entity.Address;
|
||||
import com.baeldung.attribute.override.entity.Brand;
|
||||
import com.baeldung.attribute.override.entity.Car;
|
||||
import com.baeldung.attribute.override.repository.CarRepository;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = { Application.class })
|
||||
public class AttributeOverrideIntegrationTest {
|
||||
|
||||
private static final LocalDate FORD_FOUNDATION_DATE = LocalDate.parse("1903-06-16");
|
||||
@Autowired
|
||||
CarRepository carRepository;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void whenInsertingCar_thenEmbeddedAndMappedFieldsArePopulated() {
|
||||
|
||||
Car fordMustang = createMustang();
|
||||
|
||||
carRepository.save(fordMustang);
|
||||
Car actualCar = carRepository.getOne(fordMustang.getId());
|
||||
|
||||
Assertions.assertThat(actualCar).isEqualTo(fordMustang);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Car createMustang() {
|
||||
Address address = new Address();
|
||||
address.setName("Ford United States");
|
||||
address.setCity("Dearborn");
|
||||
|
||||
Brand ford = new Brand();
|
||||
ford.setName("Ford");
|
||||
ford.setFoundationDate(FORD_FOUNDATION_DATE);
|
||||
|
||||
Car fordMustang = new Car();
|
||||
fordMustang.setIdentifier("WP1AB29P88LA47599");
|
||||
fordMustang.setModel("Ford");
|
||||
fordMustang.setName("My car");
|
||||
fordMustang.setBrand(ford);
|
||||
return fordMustang;
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.softdelete;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Filter;
|
||||
import org.hibernate.annotations.FilterDef;
|
||||
import org.hibernate.annotations.ParamDef;
|
||||
import org.hibernate.annotations.SQLDelete;
|
||||
|
||||
@Entity
|
||||
@Table(name = "tbl_products")
|
||||
@SQLDelete(sql = "UPDATE tbl_products SET deleted = true WHERE id=?")
|
||||
@FilterDef(name = "deletedProductFilter", parameters = @ParamDef(name = "isDeleted", type = "boolean"))
|
||||
@Filter(name = "deletedProductFilter", condition = "deleted = :isDeleted")
|
||||
public class Product implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private double price;
|
||||
|
||||
private boolean deleted = Boolean.FALSE;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public boolean isDeleted() {
|
||||
return deleted;
|
||||
}
|
||||
|
||||
public void setDeleted(boolean deleted) {
|
||||
this.deleted = deleted;
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.softdelete;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/products")
|
||||
public class ProductController {
|
||||
|
||||
@Autowired
|
||||
private ProductService productService;
|
||||
|
||||
@PostMapping
|
||||
public Product createOne(@RequestBody Product product) {
|
||||
return productService.create(product);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void removeOne(@PathVariable("id") Long id) {
|
||||
productService.remove(id);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Iterable<Product> findAll(@RequestParam(value = "isDeleted", required = false, defaultValue = "false") boolean isDeleted) {
|
||||
return productService.findAll(isDeleted);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.softdelete;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface ProductRepository extends CrudRepository<Product, Long>{
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user