Merge branch 'master' into BAEL-19881

This commit is contained in:
Loredana Crusoveanu 2019-12-21 17:37:33 +02:00 committed by GitHub
commit 537d6d83b0
196 changed files with 1656 additions and 326 deletions

View File

@ -1,3 +1,4 @@
**UPDATE**: The price of "Learn Spring Security OAuth" will permanently change on the 11th of December, along with the upcoming OAuth2 material: http://bit.ly/github-lss
The Courses
==============================

View File

@ -10,4 +10,5 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
- [Knapsack Problem Implementation in Java](https://www.baeldung.com/java-knapsack)
- [How to Determine if a Binary Tree is Balanced](https://www.baeldung.com/java-balanced-binary-tree)
- [The Caesar Cipher in Java](https://www.baeldung.com/java-caesar-cipher)
- More articles: [[<-- prev]](/../algorithms-miscellaneous-4)

View File

@ -0,0 +1,33 @@
package com.baeldung.algorithms.mergesortedarrays;
public class SortedArrays {
public static int[] merge(int[] foo, int[] bar) {
int fooLength = foo.length;
int barLength = bar.length;
int[] merged = new int[fooLength + barLength];
int fooPosition, barPosition, mergedPosition;
fooPosition = barPosition = mergedPosition = 0;
while (fooPosition < fooLength && barPosition < barLength) {
if (foo[fooPosition] < bar[barPosition]) {
merged[mergedPosition++] = foo[fooPosition++];
} else {
merged[mergedPosition++] = bar[barPosition++];
}
}
while (fooPosition < fooLength) {
merged[mergedPosition++] = foo[fooPosition++];
}
while (barPosition < barLength) {
merged[mergedPosition++] = bar[barPosition++];
}
return merged;
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.algorithms.mergesortedarrays;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.mergesortedarrays.SortedArrays;
public class SortedArraysUnitTest {
@Test
public void givenTwoSortedArrays_whenMerged_thenReturnMergedSortedArray() {
int[] foo = { 3, 7 };
int[] bar = { 4, 8, 11 };
int[] merged = { 3, 4, 7, 8, 11 };
assertArrayEquals(merged, SortedArrays.merge(foo, bar));
}
@Test
public void givenTwoSortedArraysWithDuplicates_whenMerged_thenReturnMergedSortedArray() {
int[] foo = { 3, 3, 7 };
int[] bar = { 4, 8, 8, 11 };
int[] merged = { 3, 3, 4, 7, 8, 8, 11 };
assertArrayEquals(merged, SortedArrays.merge(foo, bar));
}
}

View File

@ -12,3 +12,4 @@ This module contains articles about Java 11 core features
- [An Introduction to Epsilon GC: A No-Op Experimental Garbage Collector](https://www.baeldung.com/jvm-epsilon-gc-garbage-collector)
- [Guide to jlink](https://www.baeldung.com/jlink)
- [Negate a Predicate Method Reference with Java 11](https://www.baeldung.com/java-negate-predicate-method-reference)
- [Benchmark JDK Collections vs Eclipse Collections](https://www.baeldung.com/jdk-collections-vs-eclipse-collections)

View File

@ -0,0 +1,29 @@
package com.baeldung.patternreuse;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PatternJava11UnitTest {
@Test
public void givenPreCompiledPattern_whenCallAsMatchPredicate_thenReturnMatchPredicateToMatchesThePatternInTheListElements() {
List<String> namesToValidate = Arrays.asList("Fabio Silva", "Fabio Luis Silva");
Pattern firstLastNamePreCompiledPattern = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}");
Predicate<String> patternAsMatchPredicate = firstLastNamePreCompiledPattern.asMatchPredicate();
List<String> validatedNames = namesToValidate.stream()
.filter(patternAsMatchPredicate)
.collect(Collectors.toList());
assertTrue(validatedNames.contains("Fabio Silva"));
assertFalse(validatedNames.contains("Fabio Luis Silva"));
}
}

View File

@ -0,0 +1,185 @@
package com.baeldung.arraysort;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import java.util.Arrays;
import java.util.Random;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
* Time taken by JUnit test cases can be seen in JUnit Runner
* @author rchaudhary23
*
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SortComparisonUnitTest {
private int[] sizeOfArrays = { 1000, 10000, 100000, 1000000 };
private int[] _1000_elements_array;
private int[] _10000_elements_array;
private int[] _100000_elements_array;
private int[] _1000000_elements_array;
@Before
public void setUp() throws Exception {
_1000_elements_array = new int[sizeOfArrays[0]];
_10000_elements_array = new int[sizeOfArrays[1]];
_100000_elements_array = new int[sizeOfArrays[2]];
_1000000_elements_array = new int[sizeOfArrays[3]];
Random random = new Random();
for (int i = 0; i < sizeOfArrays[0]; i++) {
_1000_elements_array[i] = random.nextInt(sizeOfArrays[0]) + random.nextInt(sizeOfArrays[0]);
}
for (int i = 0; i < sizeOfArrays[1]; i++) {
_10000_elements_array[i] = random.nextInt(sizeOfArrays[1]) + random.nextInt(sizeOfArrays[1]);
}
for (int i = 0; i < sizeOfArrays[2]; i++) {
_100000_elements_array[i] = random.nextInt(sizeOfArrays[2]) + random.nextInt(sizeOfArrays[2]);
}
for (int i = 0; i < sizeOfArrays[3]; i++) {
_1000000_elements_array[i] = random.nextInt(sizeOfArrays[3]) + random.nextInt(sizeOfArrays[3]);
}
}
@Test
public void givenArrayOfIntegers_whenUsingArraysSortMethod_thenSortFullArrayInAscendingOrder() {
int[] array = { 10, 4, 6, 2, 1, 9, 7, 8, 3, 5 };
int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Arrays.sort(array);
assertArrayEquals(expected, array);
}
@Test
public void givenArrayOfIntegers_whenUsingArraysSortMethodWithRange_thenSortRangeOfArrayInAscendingOrder() {
int[] array = { 10, 4, 6, 2, 1, 9, 7, 8, 3, 5 };
int[] expected = { 10, 4, 1, 2, 6, 7, 8, 9, 3, 5 };
Arrays.sort(array, 2, 8);
assertArrayEquals(expected, array);
}
@Test
public void givenArrayOfIntegers_whenUsingArraysParallelSortMethod_thenSortFullArrayInAscendingOrder() {
int[] array = { 10, 4, 6, 2, 1, 9, 7, 8, 3, 5 };
int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Arrays.parallelSort(array);
assertArrayEquals(expected, array);
}
@Test
public void givenArrayOfIntegers_whenUsingArraysParallelSortMethodWithRange_thenSortRangeOfArrayInAscendingOrder() {
int[] array = { 10, 4, 6, 2, 1, 9, 7, 8, 3, 5 };
int[] expected = { 10, 4, 1, 2, 6, 7, 8, 9, 3, 5 };
Arrays.parallelSort(array, 2, 8);
assertArrayEquals(expected, array);
}
@Test
public void givenIntegerArrayOf1000Elements_whenUsingArraysSortMethod_thenSortFullArrayInAscendingOrder() {
int[] sequentialDataSet = Arrays.copyOf(_1000_elements_array, _1000_elements_array.length);
Arrays.sort(sequentialDataSet);
assertNotNull(sequentialDataSet);
assertNotSame(Arrays.copyOf(_1000_elements_array, _1000_elements_array.length), sequentialDataSet);
}
@Test
public void givenIntegerArrayOf1000Elements_whenUsingArraysParallelSortMethod_thenSortFullArrayInAscendingOrder() {
int[] parallelDataSet = Arrays.copyOf(_1000_elements_array, _1000_elements_array.length);
Arrays.parallelSort(parallelDataSet);
assertNotNull(parallelDataSet);
assertNotSame(Arrays.copyOf(_1000_elements_array, _1000_elements_array.length), parallelDataSet);
}
@Test
public void givenIntegerArrayOf10000Elements_whenUsingArraysSortMethod_thenSortFullArrayInAscendingOrder() {
int[] sequentialDataSet = Arrays.copyOf(_10000_elements_array, _10000_elements_array.length);
Arrays.sort(sequentialDataSet);
assertNotNull(sequentialDataSet);
assertNotSame(Arrays.copyOf(_10000_elements_array, _10000_elements_array.length), sequentialDataSet);
}
@Test
public void givenIntegerArrayOf10000Elements_whenUsingArraysParallelSortMethod_thenSortFullArrayInAscendingOrder() {
int[] parallelDataSet = Arrays.copyOf(_10000_elements_array, _10000_elements_array.length);
Arrays.parallelSort(parallelDataSet);
assertNotNull(parallelDataSet);
assertNotSame(Arrays.copyOf(_10000_elements_array, _10000_elements_array.length), parallelDataSet);
}
@Test
public void givenIntegerArrayOf100000Elements_whenUsingArraysSortMethod_thenSortFullArrayInAscendingOrder() {
int[] sequentialDataSet = Arrays.copyOf(_100000_elements_array, _100000_elements_array.length);
Arrays.sort(sequentialDataSet);
assertNotNull(sequentialDataSet);
assertNotSame(Arrays.copyOf(_100000_elements_array, _100000_elements_array.length), sequentialDataSet);
}
@Test
public void givenIntegerArrayOf100000Elements_whenUsingArraysParallelSortMethod_thenSortFullArrayInAscendingOrder() {
int[] parallelDataSet = Arrays.copyOf(_100000_elements_array, _100000_elements_array.length);
Arrays.parallelSort(parallelDataSet);
assertNotNull(parallelDataSet);
assertNotSame(Arrays.copyOf(_100000_elements_array, _100000_elements_array.length), parallelDataSet);
}
@Test
public void givenIntegerArrayOf1000000Elements_whenUsingArraysSortMethod_thenSortFullArrayInAscendingOrder() {
int[] sequentialDataSet = Arrays.copyOf(_1000000_elements_array, _1000000_elements_array.length);
Arrays.sort(sequentialDataSet);
assertNotNull(sequentialDataSet);
assertNotSame(Arrays.copyOf(_1000000_elements_array, _1000000_elements_array.length), sequentialDataSet);
}
@Test
public void givenIntegerArrayOf1000000Elements_whenUsingArraysParallelSortMethod_thenSortFullArrayInAscendingOrder() {
int[] parallelDataSet = Arrays.copyOf(_1000000_elements_array, _1000000_elements_array.length);
Arrays.parallelSort(parallelDataSet);
assertNotNull(parallelDataSet);
assertNotSame(Arrays.copyOf(_1000000_elements_array, _1000000_elements_array.length), parallelDataSet);
}
@After
public void tearDown() throws Exception {
sizeOfArrays = null;
_1000_elements_array = null;
_10000_elements_array = null;
_100000_elements_array = null;
_1000000_elements_array = null;
}
}

View File

@ -7,3 +7,5 @@
- [Java 8 Unsigned Arithmetic Support](https://www.baeldung.com/java-unsigned-arithmetic)
- [How to Separate Double into Integer and Decimal Parts](https://www.baeldung.com/java-separate-double-into-integer-decimal-parts)
- [The strictfp Keyword in Java](https://www.baeldung.com/java-strictfp)
- [Basic Calculator in Java](https://www.baeldung.com/java-basic-calculator)
- [Overflow and Underflow in Java](https://www.baeldung.com/java-overflow-underflow)

View File

@ -14,6 +14,19 @@
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-core.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-text</finalName>
<resources>

View File

@ -0,0 +1,92 @@
package com.baeldung.patternreuse;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.RunnerException;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(value = 1, warmups = 1)
@Warmup(iterations = 5)
@State(Scope.Benchmark)
public class PatternPerformanceComparison {
private static final String PATTERN = "\\d*[02468]";
private static List<String> values;
private static Matcher matcherFromPreCompiledPattern;
private static Pattern preCompiledPattern;
public static void main(String[] args) throws IOException, RunnerException {
org.openjdk.jmh.Main.main(args);
}
@Benchmark
public void matcherFromPreCompiledPatternResetMatches(Blackhole bh) {
//With pre-compiled pattern and reusing the matcher
// 1 Pattern object created
// 1 Matcher objects created
for (String value : values) {
bh.consume(matcherFromPreCompiledPattern.reset(value).matches());
}
}
@Benchmark
public void preCompiledPatternMatcherMatches(Blackhole bh) {
// With pre-compiled pattern
// 1 Pattern object created
// 5_000_000 Matcher objects created
for (String value : values) {
bh.consume(preCompiledPattern.matcher(value).matches());
}
}
@Benchmark
public void patternCompileMatcherMatches(Blackhole bh) {
// Above approach "Pattern.matches(PATTERN, value)" makes this internally
// 5_000_000 Pattern objects created
// 5_000_000 Matcher objects created
for (String value : values) {
bh.consume(Pattern.compile(PATTERN).matcher(value).matches());
}
}
@Benchmark
public void patternMatches(Blackhole bh) {
// Above approach "value.matches(PATTERN)" makes this internally
// 5_000_000 Pattern objects created
// 5_000_000 Matcher objects created
for (String value : values) {
bh.consume(Pattern.matches(PATTERN, value));
}
}
@Benchmark
public void stringMatchs(Blackhole bh) {
// 5_000_000 Pattern objects created
// 5_000_000 Matcher objects created
Instant start = Instant.now();
for (String value : values) {
bh.consume(value.matches(PATTERN));
}
}
@Setup()
public void setUp() {
preCompiledPattern = Pattern.compile(PATTERN);
matcherFromPreCompiledPattern = preCompiledPattern.matcher("");
values = new ArrayList<>();
for (int x = 1; x <= 5_000_000; x++) {
values.add(String.valueOf(x));
}
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.patternreuse;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.*;
public class PatternUnitTest {
private static final Pattern FIRST_LAST_NAME_PRE_COMPILED_PATTERN = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}");
private static final Pattern SPLIT_PRE_COMPILED_PATTERN = Pattern.compile("__");
@Test
public void givenPreCompiledPattern_whenCallMatcher_thenReturnAMatcherToMatches() {
Matcher matcherName1 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher("Fabio Silva");
Matcher matcherName2 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher("Mr. Silva");
boolean matchesName1 = matcherName1.matches();
boolean matchesName2 = matcherName2.matches();
assertTrue(matchesName1);
assertFalse(matchesName2);
}
@Test
public void givenPreCompiledPattern_whenCallAsPredicate_thenReturnPredicateToFindThePatternInTheListElements() {
List<String> namesToValidate = Arrays.asList("Fabio Silva", "Mr. Silva");
Predicate<String> patternsAsPredicate = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.asPredicate();
List<String> validNames = namesToValidate.stream()
.filter(patternsAsPredicate)
.collect(Collectors.toList());
assertEquals(1, validNames.size());
assertTrue(validNames.contains("Fabio Silva"));
}
@Test
public void givenPreCompiledPattern_whenCallSplit_thenReturnArrayWithValuesSplitByThePattern() {
String[] textSplit = SPLIT_PRE_COMPILED_PATTERN.split("My_Name__is__Fabio_Silva");
assertEquals("My_Name", textSplit[0]);
assertEquals("is", textSplit[1]);
assertEquals("Fabio_Silva", textSplit[2]);
}
@Test
public void givenPreCompiledPattern_whenCallSplitAsStream_thenReturnArrayWithValuesSplitByThePattern() {
Stream<String> textSplitAsStream = SPLIT_PRE_COMPILED_PATTERN.splitAsStream("My_Name__is__Fabio_Silva");
String[] textSplit = textSplitAsStream.toArray(String[]::new);
assertEquals("My_Name", textSplit[0]);
assertEquals("is", textSplit[1]);
assertEquals("Fabio_Silva", textSplit[2]);
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
public abstract class Animal {
public String type = "Animal";

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
public class Cow extends Animal {
private String breed;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
public class Dog extends Animal {
private String petName;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
public class Employee {
private int id;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
import java.util.List;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
import com.google.gson.annotations.Expose;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
import java.util.Objects;

View File

@ -1,19 +1,19 @@
package org.baeldung.gson.entities;
public class User {
private int id;
private String name;
private transient String nationality;
public User(int id, String name, String nationality) {
this.id = id;
this.name = name;
this.nationality = nationality;
}
public User(int id, String name) {
this(id, name, null);
}
}
package com.baeldung.gson.entities;
public class User {
private int id;
private String name;
private transient String nationality;
public User(int id, String name, String nationality) {
this.id = id;
this.name = name;
this.nationality = nationality;
}
public User(int id, String name) {
this(id, name, null);
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
import com.google.gson.annotations.SerializedName;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class BooleanExample {
public boolean value;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class ByteExample {
public byte value = (byte) 1;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class CharExample {
public char value;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class DoubleExample {
public double value;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class FloatExample {
public float value;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class InfinityValuesExample {
public float negativeInfinity;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class LongExample {
public long value = 1;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class PrimitiveBundle {
public byte byteValue;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class PrimitiveBundleInitialized {
// @formatter:off

View File

@ -1,11 +1,11 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import org.baeldung.gson.entities.ActorGson;
import com.baeldung.gson.entities.ActorGson;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;

View File

@ -1,10 +1,10 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.baeldung.gson.entities.ActorGson;
import com.baeldung.gson.entities.ActorGson;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
@ -8,7 +8,7 @@ import com.google.gson.JsonObject;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import org.baeldung.gson.entities.Animal;
import com.baeldung.gson.entities.Animal;
public class AnimalDeserializer implements JsonDeserializer<Animal> {
private String animalTypeElementName;

View File

@ -1,12 +1,11 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.baeldung.gson.entities.Employee;
import com.baeldung.gson.entities.Employee;
import com.google.gson.*;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import java.lang.reflect.Type;
import java.text.ParseException;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import com.google.gson.annotations.Expose;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import com.google.gson.annotations.Expose;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,8 +1,9 @@
package org.baeldung.gson.advance;
package com.baeldung.gson.advance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.baeldung.gson.entities.Dog;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
@ -10,11 +11,10 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.baeldung.gson.entities.Animal;
import org.baeldung.gson.entities.Cow;
import org.baeldung.gson.entities.Dog;
import org.baeldung.gson.entities.MyClass;
import org.baeldung.gson.serialization.AnimalDeserializer;
import com.baeldung.gson.entities.Animal;
import com.baeldung.gson.entities.Cow;
import com.baeldung.gson.entities.MyClass;
import com.baeldung.gson.serialization.AnimalDeserializer;
import org.junit.Test;
public class GsonAdvanceUnitTest {

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.advance;
package com.baeldung.gson.advance;
/*
* Copyright (C) 2011 Google Inc.

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.conversion;
package com.baeldung.gson.conversion;
import com.google.gson.*;
import org.junit.Assert;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
public class Foo {
public int intValue;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
public class FooWithInner {
public int intValue;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
public class GenericFoo<T> {

View File

@ -1,8 +1,8 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import static org.junit.Assert.assertEquals;
import org.baeldung.gson.entities.Weather;
import com.baeldung.gson.entities.Weather;
import org.junit.Test;
import com.google.gson.Gson;

View File

@ -1,10 +1,10 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import java.text.ParseException;
import org.baeldung.gson.entities.ActorGson;
import org.baeldung.gson.entities.Movie;
import org.baeldung.gson.serialization.ActorGsonDeserializer;
import com.baeldung.gson.entities.Movie;
import com.baeldung.gson.serialization.ActorGsonDeserializer;
import com.baeldung.gson.entities.ActorGson;
import org.junit.Assert;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import java.lang.reflect.Type;
import java.text.ParseException;
@ -6,9 +6,9 @@ import java.util.Date;
import java.util.Map;
import org.apache.commons.lang3.time.DateUtils;
import org.baeldung.gson.entities.Employee;
import org.baeldung.gson.serialization.MapDeserializer;
import org.baeldung.gson.serialization.StringDateMapDeserializer;
import com.baeldung.gson.entities.Employee;
import com.baeldung.gson.serialization.MapDeserializer;
import com.baeldung.gson.serialization.StringDateMapDeserializer;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization.test;
package com.baeldung.gson.deserialization.test;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
@ -10,11 +10,11 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import org.baeldung.gson.deserialization.Foo;
import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields;
import org.baeldung.gson.deserialization.FooInstanceCreator;
import org.baeldung.gson.deserialization.FooWithInner;
import org.baeldung.gson.deserialization.GenericFoo;
import com.baeldung.gson.deserialization.Foo;
import com.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields;
import com.baeldung.gson.deserialization.FooInstanceCreator;
import com.baeldung.gson.deserialization.FooWithInner;
import com.baeldung.gson.deserialization.GenericFoo;
import org.junit.Test;
import com.google.common.collect.Lists;

View File

@ -1,7 +1,7 @@
package org.baeldung.gson.primitives;
package com.baeldung.gson.primitives;
import com.baeldung.gson.primitives.models.*;
import com.google.gson.*;
import org.baeldung.gson.primitives.models.*;
import org.junit.Test;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import java.lang.reflect.Type;

View File

@ -1,11 +1,11 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import com.baeldung.gson.entities.Movie;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import org.baeldung.gson.entities.ActorGson;
import org.baeldung.gson.entities.Movie;
import org.baeldung.gson.entities.MovieWithNullValue;
import com.baeldung.gson.entities.ActorGson;
import com.baeldung.gson.entities.MovieWithNullValue;
import org.junit.Assert;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
public class SourceClass {
private int intValue;

View File

@ -1,18 +1,16 @@
package org.baeldung.gson.serialization.test;
package com.baeldung.gson.serialization.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Type;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import org.baeldung.gson.serialization.DifferentNameSerializer;
import org.baeldung.gson.serialization.IgnoringFieldsNotMatchingCriteriaSerializer;
import org.baeldung.gson.serialization.IgnoringFieldsSerializer;
import org.baeldung.gson.serialization.SourceClass;
import com.baeldung.gson.serialization.DifferentNameSerializer;
import com.baeldung.gson.serialization.IgnoringFieldsNotMatchingCriteriaSerializer;
import com.baeldung.gson.serialization.IgnoringFieldsSerializer;
import com.baeldung.gson.serialization.SourceClass;
import org.joda.time.DateTime;
import org.junit.Test;

View File

@ -1,43 +1,43 @@
package org.baeldung.gson.serialization.test;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.baeldung.gson.entities.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@RunWith(Parameterized.class)
public class JsonFileUnitTest {
@Parameter
public Object object;
@Parameters
public static Object[] data() {
return new Object[] { 123.45, new User(1, "Tom", "American") };
}
@Test
public void givenProperData_whenStoredInFile_shouldSaveJsonSuccessfully() {
String filePath = "target/output.json";
try (Writer writer = new FileWriter(filePath)) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(object, writer);
Assert.assertTrue(Files.exists(Paths.get(filePath)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.baeldung.gson.serialization.test;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.baeldung.gson.entities.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@RunWith(Parameterized.class)
public class JsonFileUnitTest {
@Parameter
public Object object;
@Parameters
public static Object[] data() {
return new Object[] { 123.45, new User(1, "Tom", "American") };
}
@Test
public void givenProperData_whenStoredInFile_shouldSaveJsonSuccessfully() {
String filePath = "target/output.json";
try (Writer writer = new FileWriter(filePath)) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(object, writer);
Assert.assertTrue(Files.exists(Paths.get(filePath)));
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import static org.junit.jupiter.api.Assertions.assertEquals;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
public class CustomEvent {
private String action;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import com.google.common.eventbus.DeadEvent;
import com.google.common.eventbus.Subscribe;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava.memoizer;
package com.baeldung.guava.memoizer;
import java.math.BigInteger;
import java.util.Random;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava.memoizer;
package com.baeldung.guava.memoizer;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;

View File

@ -1,11 +1,10 @@
package org.baeldung.guava.memoizer;
package com.baeldung.guava.memoizer;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.math.BigInteger;
import java.util.concurrent.TimeUnit;
public class FibonacciSequence {

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import com.google.common.hash.BloomFilter;

View File

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

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import static org.junit.Assert.*;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import com.google.common.eventbus.EventBus;
import org.junit.After;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import static org.junit.Assert.*;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import static org.junit.Assert.*;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;

View File

@ -1,9 +1,9 @@
package org.baeldung.guava;
package com.baeldung.guava;
import com.google.common.base.Suppliers;
import org.baeldung.guava.memoizer.CostlySupplier;
import org.baeldung.guava.memoizer.Factorial;
import org.baeldung.guava.memoizer.FibonacciSequence;
import com.baeldung.guava.memoizer.CostlySupplier;
import com.baeldung.guava.memoizer.Factorial;
import com.baeldung.guava.memoizer.FibonacciSequence;
import org.junit.Test;
import java.math.BigInteger;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import com.google.common.collect.Lists;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava;
package com.baeldung.guava;
import com.google.common.util.concurrent.RateLimiter;

View File

@ -1,4 +1,4 @@
package org.baeldung.httpclient;
package com.baeldung.httpclient;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

View File

@ -1,4 +1,4 @@
package org.baeldung.httpclient;
package com.baeldung.httpclient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
@ -38,7 +38,7 @@ public class HttpClientMultipartLiveTest {
private static final String TEXTFILENAME = "temp.txt";
private static final String IMAGEFILENAME = "image.jpg";
private static final String ZIPFILENAME = "zipFile.zip";
private static final Logger LOGGER = Logger.getLogger("org.baeldung.httpclient.HttpClientMultipartLiveTest");
private static final Logger LOGGER = Logger.getLogger("com.baeldung.httpclient.HttpClientMultipartLiveTest");
private CloseableHttpClient client;
private HttpPost post;
private BufferedReader rd;

View File

@ -1,4 +1,4 @@
package org.baeldung.httpclient;
package com.baeldung.httpclient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;

View File

@ -1,4 +1,4 @@
package org.baeldung.httpclient;
package com.baeldung.httpclient;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;

View File

@ -1,4 +1,4 @@
package org.baeldung.httpclient.advancedconfig;
package com.baeldung.httpclient.advancedconfig;
import com.github.tomakehurst.wiremock.junit.WireMockRule;

View File

@ -1,5 +1,6 @@
package org.baeldung.httpclient.base;
package com.baeldung.httpclient.base;
import com.baeldung.httpclient.ResponseUtil;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.CloseableHttpResponse;
@ -8,7 +9,6 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.baeldung.httpclient.ResponseUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

View File

@ -1,5 +1,6 @@
package org.baeldung.httpclient.base;
package com.baeldung.httpclient.base;
import com.baeldung.httpclient.ResponseUtil;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
@ -11,7 +12,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.baeldung.httpclient.ResponseUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

View File

@ -1,5 +1,6 @@
package org.baeldung.httpclient.base;
package com.baeldung.httpclient.base;
import com.baeldung.httpclient.ResponseUtil;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
@ -8,7 +9,6 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.baeldung.httpclient.ResponseUtil;
import org.junit.Test;
import java.io.IOException;

View File

@ -1,4 +1,4 @@
package org.baeldung.httpclient.conn;
package com.baeldung.httpclient.conn;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;

View File

@ -1,4 +1,4 @@
package org.baeldung.httpclient.conn;
package com.baeldung.httpclient.conn;
import java.util.concurrent.TimeUnit;

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