Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
commit
4ffa5f1892
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
================================
|
||||
- [Building a Microservice with Apache Meecrowave](http://www.baeldung.com/apache-meecrowave)
|
|
@ -46,7 +46,6 @@
|
|||
- [Overview of Java Built-in Annotations](http://www.baeldung.com/java-default-annotations)
|
||||
- [Finding Min/Max in an Array with Java](http://www.baeldung.com/java-array-min-max)
|
||||
- [Internationalization and Localization in Java 8](http://www.baeldung.com/java-8-localization)
|
||||
- [Filtering Kotlin Collections](http://www.baeldung.com/kotlin-filter-collection)
|
||||
- [How to Find an Element in a List with Java](http://www.baeldung.com/find-list-element-java)
|
||||
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
|
||||
- [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get)
|
||||
|
@ -55,3 +54,5 @@
|
|||
- [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic)
|
||||
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
|
||||
- [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference)
|
||||
- [Image to Base64 String Conversion](http://www.baeldung.com/java-base64-image-string)
|
||||
- [Calculate Age in Java](http://www.baeldung.com/java-get-age)
|
||||
|
|
|
@ -0,0 +1,111 @@
|
|||
package com.baeldung.list;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RemoveAll {
|
||||
|
||||
static void removeWithWhileLoopPrimitiveElement(List<Integer> list, int element) {
|
||||
while (list.contains(element)) {
|
||||
list.remove(element);
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithWhileLoopNonPrimitiveElement(List<Integer> list, Integer element) {
|
||||
while (list.contains(element)) {
|
||||
list.remove(element);
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithWhileLoopStoringFirstOccurrenceIndex(List<Integer> list, Integer element) {
|
||||
int index;
|
||||
while ((index = list.indexOf(element)) >= 0) {
|
||||
list.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithCallingRemoveUntilModifies(List<Integer> list, Integer element) {
|
||||
while (list.remove(element))
|
||||
;
|
||||
}
|
||||
|
||||
static void removeWithStandardForLoopUsingIndex(List<Integer> list, int element) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (Objects.equals(element, list.get(i))) {
|
||||
list.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithForLoopDecrementOnRemove(List<Integer> list, int element) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (Objects.equals(element, list.get(i))) {
|
||||
list.remove(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithForLoopIncrementIfRemains(List<Integer> list, int element) {
|
||||
for (int i = 0; i < list.size();) {
|
||||
if (Objects.equals(element, list.get(i))) {
|
||||
list.remove(i);
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithForEachLoop(List<Integer> list, int element) {
|
||||
for (Integer number : list) {
|
||||
if (Objects.equals(number, element)) {
|
||||
list.remove(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithIterator(List<Integer> list, int element) {
|
||||
for (Iterator<Integer> i = list.iterator(); i.hasNext();) {
|
||||
Integer number = i.next();
|
||||
if (Objects.equals(number, element)) {
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static List<Integer> removeWithCollectingAndReturningRemainingElements(List<Integer> list, int element) {
|
||||
List<Integer> remainingElements = new ArrayList<>();
|
||||
for (Integer number : list) {
|
||||
if (!Objects.equals(number, element)) {
|
||||
remainingElements.add(number);
|
||||
}
|
||||
}
|
||||
return remainingElements;
|
||||
}
|
||||
|
||||
static void removeWithCollectingRemainingElementsAndAddingToOriginalList(List<Integer> list, int element) {
|
||||
List<Integer> remainingElements = new ArrayList<>();
|
||||
for (Integer number : list) {
|
||||
if (!Objects.equals(number, element)) {
|
||||
remainingElements.add(number);
|
||||
}
|
||||
}
|
||||
|
||||
list.clear();
|
||||
list.addAll(remainingElements);
|
||||
}
|
||||
|
||||
static List<Integer> removeWithStreamFilter(List<Integer> list, Integer element) {
|
||||
return list.stream()
|
||||
.filter(e -> !Objects.equals(e, element))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
static void removeWithRemoveIf(List<Integer> list, Integer element) {
|
||||
list.removeIf(n -> Objects.equals(n, element));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Stream;
|
||||
import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNull {
|
||||
|
||||
/**
|
||||
* This method shows how to make a null safe stream from a collection through the use of
|
||||
* emptyIfNull() method from Apache Commons CollectionUtils library
|
||||
*
|
||||
* @param collection The collection that is to be converted into a stream
|
||||
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||
*/
|
||||
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||
return emptyIfNull(collection).stream();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingJava8OptionalContainer {
|
||||
|
||||
/**
|
||||
* This method shows how to make a null safe stream from a collection through the use of
|
||||
* Java SE 8’s Optional Container
|
||||
*
|
||||
* @param collection The collection that is to be converted into a stream
|
||||
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||
*/
|
||||
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||
return Optional.ofNullable(collection)
|
||||
.map(Collection::stream)
|
||||
.orElseGet(Stream::empty);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingNullDereferenceCheck {
|
||||
|
||||
/**
|
||||
* This method shows how to make a null safe stream from a collection through the use of a check
|
||||
* to prevent null dereferences
|
||||
*
|
||||
* @param collection The collection that is to be converted into a stream
|
||||
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||
*/
|
||||
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||
return collection == null ? Stream.empty() : collection.stream();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
package com.baeldung.list;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class AddElementsUnitTest {
|
||||
|
||||
List<Flower> flowers;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.flowers = new ArrayList<>(Arrays.asList(
|
||||
new Flower("Poppy", 12),
|
||||
new Flower("Anemone", 8),
|
||||
new Flower("Catmint", 12)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithNewItems() {
|
||||
List<Flower> anotherList = new ArrayList<>();
|
||||
anotherList.addAll(flowers);
|
||||
|
||||
assertEquals(anotherList.size(), flowers.size());
|
||||
Assert.assertTrue(anotherList.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithOneModifiedElementByConstructor() {
|
||||
List<Flower> anotherList = new ArrayList<>();
|
||||
anotherList.addAll(flowers);
|
||||
|
||||
Flower flower = anotherList.get(0);
|
||||
flower.setPetals(flowers.get(0).getPetals() * 3);
|
||||
|
||||
assertEquals(anotherList.size(), flowers.size());
|
||||
Assert.assertTrue(anotherList.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListAndElements_whenUseCollectionsAddAll_thenAddElementsToTargetList() {
|
||||
List<Flower> target = new ArrayList<>();
|
||||
|
||||
Collections.addAll(target, flowers.get(0), flowers.get(1), flowers.get(2), flowers.get(0));
|
||||
|
||||
assertEquals(target.size(), 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListSkipFirstElementByStreamProcess() {
|
||||
List<Flower> flowerVase = new ArrayList<>();
|
||||
|
||||
flowers.stream()
|
||||
.skip(1)
|
||||
.forEachOrdered(flowerVase::add);
|
||||
|
||||
assertEquals(flowerVase.size() + 1, flowers.size());
|
||||
assertFalse(flowerVase.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListFilteringElementsByStreamProcess() {
|
||||
List<Flower> flowerVase = new ArrayList<>();
|
||||
|
||||
flowers.stream()
|
||||
.filter(f -> f.getPetals() > 10)
|
||||
.forEachOrdered(flowerVase::add);
|
||||
|
||||
assertEquals(flowerVase.size() + 1, flowers.size());
|
||||
assertFalse(flowerVase.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListIsNotNull_thenAddElementsToListByStreamProcessWihtOptional() {
|
||||
List<Flower> target = new ArrayList<>();
|
||||
|
||||
Optional.ofNullable(flowers)
|
||||
.ifPresent(target::addAll);
|
||||
|
||||
assertNotNull(target);
|
||||
assertEquals(target.size(), 3);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,208 @@
|
|||
package com.baeldung.list;
|
||||
|
||||
import static com.baeldung.list.RemoveAll.removeWithCallingRemoveUntilModifies;
|
||||
import static com.baeldung.list.RemoveAll.removeWithCollectingAndReturningRemainingElements;
|
||||
import static com.baeldung.list.RemoveAll.removeWithCollectingRemainingElementsAndAddingToOriginalList;
|
||||
import static com.baeldung.list.RemoveAll.removeWithForEachLoop;
|
||||
import static com.baeldung.list.RemoveAll.removeWithForLoopDecrementOnRemove;
|
||||
import static com.baeldung.list.RemoveAll.removeWithForLoopIncrementIfRemains;
|
||||
import static com.baeldung.list.RemoveAll.removeWithIterator;
|
||||
import static com.baeldung.list.RemoveAll.removeWithRemoveIf;
|
||||
import static com.baeldung.list.RemoveAll.removeWithStandardForLoopUsingIndex;
|
||||
import static com.baeldung.list.RemoveAll.removeWithStreamFilter;
|
||||
import static com.baeldung.list.RemoveAll.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RemoveAllUnitTest {
|
||||
|
||||
private List<Integer> list(Integer... elements) {
|
||||
return new ArrayList<>(Arrays.asList(elements));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithWhileLoopUsingPrimitiveElement_thenTheResultCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
assertThatThrownBy(() -> removeWithWhileLoopPrimitiveElement(list, valueToRemove))
|
||||
.isInstanceOf(IndexOutOfBoundsException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithWhileLoopUsingNonPrimitiveElement_thenTheResultCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithWhileLoopNonPrimitiveElement(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithWhileLoopStoringFirstOccurrenceIndex_thenTheResultCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithWhileLoopStoringFirstOccurrenceIndex(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCallingRemoveUntilModifies_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithCallingRemoveUntilModifies(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithoutDuplication_whenRemovingElementsWithStandardForLoopUsingIndex_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithStandardForLoopUsingIndex(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithAdjacentElements_whenRemovingElementsWithStandardForLoop_thenTheResultIsInCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithStandardForLoopUsingIndex(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(1, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithAdjacentElements_whenRemovingElementsWithForLoopAndDecrementOnRemove_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithForLoopDecrementOnRemove(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithAdjacentElements_whenRemovingElementsWithForLoopAndIncrementIfRemains_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithForLoopIncrementIfRemains(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithForEachLoop_thenExceptionIsThrown() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
assertThatThrownBy(() -> removeWithForEachLoop(list, valueToRemove))
|
||||
.isInstanceOf(ConcurrentModificationException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithIterator_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithIterator(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCollectingAndReturningRemainingElements_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
List<Integer> result = removeWithCollectingAndReturningRemainingElements(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCollectingRemainingAndAddingToOriginalList_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithCollectingRemainingElementsAndAddingToOriginalList(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithStreamFilter_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
List<Integer> result = removeWithStreamFilter(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCallingRemoveIf_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithRemoveIf(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest {
|
||||
|
||||
private final NullSafeCollectionStreamsUsingCommonsEmptyIfNull instance =
|
||||
new NullSafeCollectionStreamsUsingCommonsEmptyIfNull();
|
||||
|
||||
@Test
|
||||
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
|
||||
Collection<String> collection = null;
|
||||
Stream<String> expResult = Stream.empty();
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
|
||||
|
||||
Collection<String> collection = Arrays.asList("a", "b", "c");
|
||||
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
}
|
||||
|
||||
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
|
||||
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
|
||||
while (iter1.hasNext() && iter2.hasNext())
|
||||
assertEquals(iter1.next(), iter2.next());
|
||||
assert !iter1.hasNext() && !iter2.hasNext();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Kwaje Anthony <kwajeanthony@gmail.com>
|
||||
*/
|
||||
public class NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest {
|
||||
|
||||
private final NullSafeCollectionStreamsUsingJava8OptionalContainer instance =
|
||||
new NullSafeCollectionStreamsUsingJava8OptionalContainer();
|
||||
|
||||
@Test
|
||||
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
|
||||
Collection<String> collection = null;
|
||||
Stream<String> expResult = Stream.empty();
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
|
||||
|
||||
Collection<String> collection = Arrays.asList("a", "b", "c");
|
||||
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
}
|
||||
|
||||
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
|
||||
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
|
||||
while (iter1.hasNext() && iter2.hasNext())
|
||||
assertEquals(iter1.next(), iter2.next());
|
||||
assert !iter1.hasNext() && !iter2.hasNext();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
|
||||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Kwaje Anthony <kwajeanthony@gmail.com>
|
||||
*/
|
||||
public class NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest {
|
||||
|
||||
private final NullSafeCollectionStreamsUsingNullDereferenceCheck instance =
|
||||
new NullSafeCollectionStreamsUsingNullDereferenceCheck();
|
||||
|
||||
@Test
|
||||
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
|
||||
Collection<String> collection = null;
|
||||
Stream<String> expResult = Stream.empty();
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
|
||||
|
||||
Collection<String> collection = Arrays.asList("a", "b", "c");
|
||||
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
}
|
||||
|
||||
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
|
||||
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
|
||||
while (iter1.hasNext() && iter2.hasNext())
|
||||
assertEquals(iter1.next(), iter2.next());
|
||||
assert !iter1.hasNext() && !iter2.hasNext();
|
||||
}
|
||||
|
||||
}
|
|
@ -19,11 +19,23 @@
|
|||
<version>${awaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -50,6 +62,8 @@
|
|||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<awaitility.version>1.7.0</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.java9.language.stream;
|
||||
|
||||
import static java.util.stream.Collectors.filtering;
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class StreamsGroupingCollectionFilter {
|
||||
|
||||
static public Map<Integer, List<Integer>> findEvenNumbersAfterGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
|
||||
Function<Integer, Integer> getQuantityOfDigits = item -> (int) Math.log10(item) + 1;
|
||||
|
||||
return baseCollection.stream()
|
||||
.collect(groupingBy(getQuantityOfDigits, filtering(item -> item % 2 == 0, toList())));
|
||||
}
|
||||
|
||||
static public Map<Integer, List<Integer>> findEvenNumbersBeforeGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
|
||||
|
||||
return baseCollection.stream()
|
||||
.filter(item -> item % 2 == 0)
|
||||
.collect(groupingBy(item -> (int) Math.log10(item) + 1, toList()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.java9.maps.initialize;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapsInitializer {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void createMapWithMapOf() {
|
||||
Map<String, String> emptyMap = Map.of();
|
||||
Map<String, String> singletonMap = Map.of("key1", "value");
|
||||
Map<String, String> map = Map.of("key1","value1", "key2", "value2");
|
||||
}
|
||||
|
||||
public void createMapWithMapEntries() {
|
||||
Map<String, String> map = Map.ofEntries(
|
||||
new AbstractMap.SimpleEntry<String, String>("name", "John"),
|
||||
new AbstractMap.SimpleEntry<String, String>("city", "budapest"),
|
||||
new AbstractMap.SimpleEntry<String, String>("zip", "000000"),
|
||||
new AbstractMap.SimpleEntry<String, String>("home", "1231231231")
|
||||
);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void createMutableMaps() {
|
||||
Map<String, String> map = new HashMap<String, String> (Map.of("key1","value1", "key2", "value2"));
|
||||
Map<String, String> map2 = new HashMap<String, String> ( Map.ofEntries(
|
||||
new AbstractMap.SimpleEntry<String, String>("name", "John"),
|
||||
new AbstractMap.SimpleEntry<String, String>("city", "budapest")));
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.java9.language.stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class CollectionFilterUnitTest {
|
||||
|
||||
private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 12, 55, 56, 101, 115, 8002, 223, 2668, 19, 8);
|
||||
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP = createExpectedFilterAfterGroupingMap();
|
||||
private static Map<Integer, List<Integer>> createExpectedFilterAfterGroupingMap() {
|
||||
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
map.put(1, Arrays.asList(8));
|
||||
map.put(2, Arrays.asList(12, 56));
|
||||
map.put(3, Collections.emptyList());
|
||||
map.put(4, Arrays.asList(8002, 2668));
|
||||
return map;
|
||||
|
||||
}
|
||||
|
||||
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP = createExpectedFilterBeforeGroupingMap();
|
||||
private static Map<Integer, List<Integer>> createExpectedFilterBeforeGroupingMap() {
|
||||
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
map.put(1, Arrays.asList(8));
|
||||
map.put(2, Arrays.asList(12, 56));
|
||||
map.put(4, Arrays.asList(8002, 2668));
|
||||
return map;
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAStringCollection_whenFilteringFourLetterWords_thenObtainTheFilteredCollection() {
|
||||
Map<Integer, List<Integer>> filteredAfterGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersAfterGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION);
|
||||
Map<Integer, List<Integer>> filteredBeforeGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersBeforeGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION);
|
||||
|
||||
assertThat(filteredAfterGroupingMap).containsAllEntriesOf(EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP);
|
||||
assertThat(filteredBeforeGroupingMap).doesNotContainKey(3)
|
||||
.containsAllEntriesOf(EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP);
|
||||
}
|
||||
|
||||
}
|
|
@ -30,3 +30,4 @@
|
|||
- [How to TDD a List Implementation in Java](http://www.baeldung.com/java-test-driven-list)
|
||||
- [How to Store Duplicate Keys in a Map in Java?](http://www.baeldung.com/java-map-duplicate-keys)
|
||||
- [Getting the Size of an Iterable in Java](http://www.baeldung.com/java-iterable-size)
|
||||
- [Iterating Backward Through a List](http://www.baeldung.com/java-list-iterate-backwards)
|
||||
|
|
|
@ -36,19 +36,37 @@
|
|||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.collections</groupId>
|
||||
<artifactId>eclipse-collections-api</artifactId>
|
||||
<version>${eclipse.collections.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>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<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>9.2.0</eclipse.collections.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.java.filtering;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.Predicate;
|
||||
|
||||
public class CollectionUtilsCollectionFilter {
|
||||
|
||||
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||
Predicate<Integer> apacheEventNumberPredicate = item -> item % 2 == 0;
|
||||
|
||||
CollectionUtils.filter(baseCollection, apacheEventNumberPredicate);
|
||||
return baseCollection;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.java.filtering;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.eclipse.collections.api.block.predicate.Predicate;
|
||||
import org.eclipse.collections.impl.factory.Lists;
|
||||
import org.eclipse.collections.impl.utility.Iterate;
|
||||
|
||||
public class EclipseCollectionsCollectionFilter {
|
||||
|
||||
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||
Predicate<Integer> eclipsePredicate = item -> item % 2 == 0;
|
||||
Collection<Integer> filteredList = Lists.mutable.ofAll(baseCollection)
|
||||
.select(eclipsePredicate);
|
||||
|
||||
return filteredList;
|
||||
}
|
||||
|
||||
static public Collection<Integer> findEvenNumbersUsingIterate(Collection<Integer> baseCollection) {
|
||||
Predicate<Integer> eclipsePredicate = new Predicate<Integer>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean accept(Integer arg0) {
|
||||
return arg0 % 2 == 0;
|
||||
}
|
||||
};
|
||||
Collection<Integer> filteredList = Iterate.select(baseCollection, eclipsePredicate);
|
||||
|
||||
return filteredList;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.java.filtering;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Collections2;
|
||||
|
||||
public class GuavaCollectionFilter {
|
||||
|
||||
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||
Predicate<Integer> guavaPredicate = item -> item % 2 == 0;
|
||||
|
||||
Collection<Integer> filteredCollection = Collections2.filter(baseCollection, guavaPredicate);
|
||||
return filteredCollection;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.java.filtering;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StreamsCollectionFilter {
|
||||
|
||||
public static <T> Collection<T> filterCollectionHelperMethod(Collection<T> baseCollection, Predicate<T> predicate) {
|
||||
return baseCollection.stream()
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
static public Collection<Integer> findEvenNumbersUsingHelperMethod(Collection<Integer> baseCollection) {
|
||||
return filterCollectionHelperMethod(baseCollection, item -> item % 2 == 0);
|
||||
}
|
||||
|
||||
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||
Predicate<Integer> streamsPredicate = item -> item % 2 == 0;
|
||||
|
||||
return baseCollection.stream()
|
||||
.filter(streamsPredicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.java.list;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
private int petals;
|
||||
|
||||
public Flower(String name, int petals) {
|
||||
this.name = name;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getPetals() {
|
||||
return petals;
|
||||
}
|
||||
|
||||
public void setPetals(int petals) {
|
||||
this.petals = petals;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package com.baeldung.java.map.initialize;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class MapInitializer {
|
||||
|
||||
public static Map<String, String> articleMapOne;
|
||||
static {
|
||||
articleMapOne = new HashMap<>();
|
||||
articleMapOne.put("ar01", "Intro to Map");
|
||||
articleMapOne.put("ar02", "Some article");
|
||||
}
|
||||
|
||||
public static Map<String, String> createSingletonMap() {
|
||||
Map<String, String> passwordMap = Collections.singletonMap("username1", "password1");
|
||||
return passwordMap;
|
||||
|
||||
}
|
||||
|
||||
public Map<String, String> createEmptyMap() {
|
||||
Map<String, String> emptyMap = Collections.emptyMap();
|
||||
return emptyMap;
|
||||
}
|
||||
|
||||
public Map<String, String> createUsingDoubleBrace() {
|
||||
Map<String, String> doubleBraceMap = new HashMap<String, String>() {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
{
|
||||
put("key1", "value1");
|
||||
put("key2", "value2");
|
||||
}
|
||||
};
|
||||
return doubleBraceMap;
|
||||
}
|
||||
|
||||
public Map<String, String> createMapUsingStreamStringArray() {
|
||||
Map<String, String> map = Stream.of(new String[][] { { "Hello", "World" }, { "John", "Doe" }, })
|
||||
.collect(Collectors.toMap(data -> data[0], data -> data[1]));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String, Integer> createMapUsingStreamObjectArray() {
|
||||
Map<String, Integer> map = Stream.of(new Object[][] { { "data1", 1 }, { "data2", 2 }, })
|
||||
.collect(Collectors.toMap(data -> (String) data[0], data -> (Integer) data[1]));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String, Integer> createMapUsingStreamSimpleEntry() {
|
||||
Map<String, Integer> map = Stream.of(new AbstractMap.SimpleEntry<>("idea", 1), new AbstractMap.SimpleEntry<>("mobile", 2))
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String, Integer> createMapUsingStreamSimpleImmutableEntry() {
|
||||
Map<String, Integer> map = Stream.of(new AbstractMap.SimpleImmutableEntry<>("idea", 1), new AbstractMap.SimpleImmutableEntry<>("mobile", 2))
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String, String> createImmutableMapWithStreams() {
|
||||
Map<String, String> map = Stream.of(new String[][] { { "Hello", "World" }, { "John", "Doe" }, })
|
||||
.collect(Collectors.collectingAndThen(Collectors.toMap(data -> data[0], data -> data[1]), Collections::<String, String> unmodifiableMap));
|
||||
return map;
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.java.filtering;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class CollectionFiltersUnitTest {
|
||||
|
||||
private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 14, 2, 7, 1, 5, 8);
|
||||
private static final Collection<Integer> EXPECTED_EVEN_FILTERED_COLLECTION = Arrays.asList(14, 2, 8);
|
||||
|
||||
@Test
|
||||
public void givenAStringCollection_whenFilteringFourLetterWords_thenObtainTheFilteredCollection() {
|
||||
final Collection<String> baseStrings = Arrays.asList("java", "baeldung", "type", "example", "other");
|
||||
|
||||
Collection<String> filtered = StreamsCollectionFilter.filterCollectionHelperMethod(baseStrings, item -> item.length() == 4);
|
||||
|
||||
assertThat(filtered).containsExactlyInAnyOrder("java", "type");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnIntegerCollection_whenFilteringEvenValues_thenObtainTheFilteredCollectionForAllCases() {
|
||||
Collection<Integer> filteredWithStreams1 = StreamsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
|
||||
Collection<Integer> filteredWithCollectionUtils = CollectionUtilsCollectionFilter.findEvenNumbers(new ArrayList<>(BASE_INTEGER_COLLECTION));
|
||||
Collection<Integer> filteredWithEclipseCollections = EclipseCollectionsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
|
||||
Collection<Integer> filteredWithEclipseCollectionsUsingIterate = EclipseCollectionsCollectionFilter.findEvenNumbersUsingIterate(BASE_INTEGER_COLLECTION);
|
||||
Collection<Integer> filteredWithGuava = GuavaCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
|
||||
|
||||
assertThat(filteredWithStreams1).hasSameElementsAs(filteredWithCollectionUtils)
|
||||
.hasSameElementsAs(filteredWithEclipseCollections)
|
||||
.hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)
|
||||
.hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)
|
||||
.hasSameElementsAs(filteredWithGuava)
|
||||
.hasSameElementsAs(EXPECTED_EVEN_FILTERED_COLLECTION);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.java.map.initialize;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MapInitializerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenStaticMap_whenUpdated_thenCorrect() {
|
||||
|
||||
MapInitializer.articleMapOne.put("NewArticle1", "Convert array to List");
|
||||
|
||||
assertEquals(MapInitializer.articleMapOne.get("NewArticle1"), "Convert array to List");
|
||||
|
||||
}
|
||||
|
||||
@Test(expected=UnsupportedOperationException.class)
|
||||
public void givenSingleTonMap_whenEntriesAdded_throwsException() {
|
||||
|
||||
Map<String, String> map = MapInitializer.createSingletonMap();
|
||||
map.put("username2", "password2");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package com.baeldung.list.listoflist;
|
||||
|
||||
import com.baeldung.java.list.Flower;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import java.util.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class AddElementsToListUnitTest {
|
||||
|
||||
List<Flower> flowers;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.flowers = new ArrayList<>(Arrays.asList(
|
||||
new Flower("Poppy", 12),
|
||||
new Flower("Anemone", 8),
|
||||
new Flower("Catmint", 12)));
|
||||
}
|
||||
@Test
|
||||
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithNewItems() {
|
||||
List<Flower> anotherList = new ArrayList<>();
|
||||
anotherList.addAll(flowers);
|
||||
assertEquals(anotherList.size(), flowers.size());
|
||||
Assert.assertTrue(anotherList.containsAll(flowers));
|
||||
}
|
||||
@Test
|
||||
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithOneModifiedElementByConstructor() {
|
||||
List<Flower> anotherList = new ArrayList<>();
|
||||
anotherList.addAll(flowers);
|
||||
Flower flower = anotherList.get(0);
|
||||
flower.setPetals(flowers.get(0).getPetals() * 3);
|
||||
assertEquals(anotherList.size(), flowers.size());
|
||||
Assert.assertTrue(anotherList.containsAll(flowers));
|
||||
}
|
||||
@Test
|
||||
public void givenAListAndElements_whenUseCollectionsAddAll_thenAddElementsToTargetList() {
|
||||
List<Flower> target = new ArrayList<>();
|
||||
Collections.addAll(target, flowers.get(0), flowers.get(1), flowers.get(2), flowers.get(0));
|
||||
assertEquals(target.size(), 4);
|
||||
}
|
||||
@Test
|
||||
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListSkipFirstElementByStreamProcess() {
|
||||
List<Flower> flowerVase = new ArrayList<>();
|
||||
flowers.stream()
|
||||
.skip(1)
|
||||
.forEachOrdered(flowerVase::add);
|
||||
assertEquals(flowerVase.size() + 1, flowers.size());
|
||||
assertFalse(flowerVase.containsAll(flowers));
|
||||
}
|
||||
@Test
|
||||
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListFilteringElementsByStreamProcess() {
|
||||
List<Flower> flowerVase = new ArrayList<>();
|
||||
flowers.stream()
|
||||
.filter(f -> f.getPetals() > 10)
|
||||
.forEachOrdered(flowerVase::add);
|
||||
assertEquals(flowerVase.size() + 1, flowers.size());
|
||||
assertFalse(flowerVase.containsAll(flowers));
|
||||
}
|
||||
@Test
|
||||
public void givenAList_whenListIsNotNull_thenAddElementsToListByStreamProcessWihtOptional() {
|
||||
List<Flower> target = new ArrayList<>();
|
||||
Optional.ofNullable(flowers)
|
||||
.ifPresent(target::addAll);
|
||||
assertNotNull(target);
|
||||
assertEquals(target.size(), 3);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.list.removefirst;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RemoveFirstElementUnitTest {
|
||||
|
||||
private List<String> list = new ArrayList<>();
|
||||
private LinkedList<String> linkedList = new LinkedList<>();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
|
||||
list.add("cat");
|
||||
list.add("dog");
|
||||
list.add("pig");
|
||||
list.add("cow");
|
||||
list.add("goat");
|
||||
|
||||
linkedList.add("cat");
|
||||
linkedList.add("dog");
|
||||
linkedList.add("pig");
|
||||
linkedList.add("cow");
|
||||
linkedList.add("goat");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenRemoveFirst_thenRemoved() {
|
||||
list.remove(0);
|
||||
|
||||
assertThat(list, hasSize(4));
|
||||
assertThat(list, not(contains("cat")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedList_whenRemoveFirst_thenRemoved() {
|
||||
|
||||
linkedList.removeFirst();
|
||||
|
||||
assertThat(linkedList, hasSize(4));
|
||||
assertThat(linkedList, not(contains("cat")));
|
||||
}
|
||||
|
||||
}
|
|
@ -28,3 +28,5 @@
|
|||
- [Guide to Java NIO2 Asynchronous Channel APIs](http://www.baeldung.com/java-nio-2-async-channels)
|
||||
- [A Guide to NIO2 Asynchronous Socket Channel](http://www.baeldung.com/java-nio2-async-socket-channel)
|
||||
- [Download a File From an URL in Java](http://www.baeldung.com/java-download-file)
|
||||
- [Create a Symbolic Link with Java](http://www.baeldung.com/java-symlink)
|
||||
- [Quick Use of FilenameFilter](http://www.baeldung.com/java-filename-filter)
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
<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.core-java-persistence</groupId>
|
||||
<artifactId>core-java-persistence</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-persistence</name>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-dbcp2</artifactId>
|
||||
<version>${commons-dbcp2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>${HikariCP.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mchange</groupId>
|
||||
<artifactId>c3p0</artifactId>
|
||||
<version>${c3p0.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>core-java-persistence</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
<properties>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<commons-dbcp2.version>2.4.0</commons-dbcp2.version>
|
||||
<HikariCP.version>3.2.0</HikariCP.version>
|
||||
<c3p0.version>0.9.5.2</c3p0.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.connectionpool.connectionpools;
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
|
@ -56,10 +56,16 @@ public class BasicConnectionPool implements ConnectionPool {
|
|||
return DriverManager.getConnection(url, user, password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return connectionPool.size() + usedConnections.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Connection> getConnectionPool() {
|
||||
return connectionPool;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrl() {
|
||||
return url;
|
||||
|
@ -75,6 +81,7 @@ public class BasicConnectionPool implements ConnectionPool {
|
|||
return password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() throws SQLException {
|
||||
usedConnections.forEach(this::releaseConnection);
|
||||
for (Connection c : connectionPool) {
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.connectionpool.connectionpools;
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
import java.beans.PropertyVetoException;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.connectionpool.connectionpools;
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
@ -10,9 +10,15 @@ public interface ConnectionPool {
|
|||
|
||||
boolean releaseConnection(Connection connection);
|
||||
|
||||
List<Connection> getConnectionPool();
|
||||
|
||||
int getSize();
|
||||
|
||||
String getUrl();
|
||||
|
||||
String getUser();
|
||||
|
||||
String getPassword();
|
||||
|
||||
void shutdown() throws SQLException;;
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.connectionpool.connectionpools;
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.connectionpool.connectionpools;
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
|
@ -1,10 +1,8 @@
|
|||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.baeldung.connectionpool.connectionpools.BasicConnectionPool;
|
||||
import com.baeldung.connectionpool.connectionpools.ConnectionPool;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.baeldung.connectionpool.connectionpools.C3poDataSource;
|
||||
import java.sql.SQLException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.baeldung.connectionpool.connectionpools.DBCPDataSource;
|
||||
import java.sql.SQLException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.baeldung.connectionpool.connectionpools.HikariCPDataSource;
|
||||
import java.sql.SQLException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
|
@ -163,3 +163,8 @@
|
|||
- [Console I/O in Java](http://www.baeldung.com/java-console-input-output)
|
||||
- [Guide to the java.util.Arrays Class](http://www.baeldung.com/java-util-arrays)
|
||||
- [Create a Custom Exception in Java](http://www.baeldung.com/java-new-custom-exception)
|
||||
- [Guide to java.util.GregorianCalendar](http://www.baeldung.com/java-gregorian-calendar)
|
||||
- [Java Global Exception Handler](http://www.baeldung.com/java-global-exception-handler)
|
||||
- [Encrypting and Decrypting Files in Java](http://www.baeldung.com/java-cipher-input-output-stream)
|
||||
- [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object)
|
||||
- [Exception Handling in Java](http://www.baeldung.com/java-exceptions)
|
||||
|
|
|
@ -158,21 +158,6 @@
|
|||
<artifactId>jmimemagic</artifactId>
|
||||
<version>${jmime-magic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-dbcp2</artifactId>
|
||||
<version>${commons-dbcp2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>${HikariCP.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mchange</groupId>
|
||||
<artifactId>c3p0</artifactId>
|
||||
<version>${c3p0.version}</version>
|
||||
</dependency>
|
||||
<!-- instrumentation -->
|
||||
<dependency>
|
||||
<groupId>org.javassist</groupId>
|
||||
|
@ -544,11 +529,6 @@
|
|||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
|
||||
<!-- JDBC pooling frameworks -->
|
||||
<commons-dbcp2.version>2.4.0</commons-dbcp2.version>
|
||||
<HikariCP.version>3.2.0</HikariCP.version>
|
||||
<c3p0.version>0.9.5.2</c3p0.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<springframework.spring-web.version>4.3.4.RELEASE</springframework.spring-web.version>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.logging;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class LogUsingSlf4J {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Logger logger = LoggerFactory.getLogger(LogUsingSlf4J.class);
|
||||
|
||||
logger.error("An exception occurred!");
|
||||
logger.error("An exception occurred!", new Exception("Custom exception"));
|
||||
logger.error("{}, {}! An exception occurred!", "Hello", "World", new Exception("Custom exception"));
|
||||
}
|
||||
|
||||
}
|
|
@ -34,3 +34,5 @@
|
|||
- [Java EE 8 Security API](http://www.baeldung.com/java-ee-8-security)
|
||||
- [Kotlin with Ktor](http://www.baeldung.com/kotlin-ktor)
|
||||
- [Working with Enums in Kotlin](http://www.baeldung.com/kotlin-enum)
|
||||
- [Create a Java and Kotlin Project with Maven](http://www.baeldung.com/kotlin-maven-java-project)
|
||||
- [Reflection with Kotlin](http://www.baeldung.com/kotlin-reflection)
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Introduction to GWT](http://www.baeldung.com/gwt)
|
|
@ -6,10 +6,16 @@
|
|||
<!-- POM file generated with GWT webAppCreator -->
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>google-web-toolkit</artifactId>
|
||||
<artifactId>google_web_toolkit</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<!-- ensure all GWT deps use the same version (unless overridden) -->
|
||||
|
|
|
@ -22,6 +22,8 @@
|
|||
<!-- Specify the app entry point class. -->
|
||||
<entry-point class='com.baeldung.client.Google_web_toolkit'/>
|
||||
|
||||
<set-property name="user.agent" value="gecko1_8"/>
|
||||
|
||||
<!-- Specify the paths for translatable code -->
|
||||
<source path='client'/>
|
||||
<source path='shared'/>
|
||||
|
|
|
@ -6,3 +6,4 @@
|
|||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
/bin/
|
||||
|
|
|
@ -85,7 +85,7 @@
|
|||
- [Implementing a FTP-Client in Java](http://www.baeldung.com/java-ftp-client)
|
||||
- [Convert String to Date in Java](http://www.baeldung.com/java-string-to-date)
|
||||
- [Histograms with Apache Commons Frequency](http://www.baeldung.com/apache-commons-frequency)
|
||||
|
||||
- [Guide to Resilience4j](http://www.baeldung.com/resilience4j)
|
||||
|
||||
The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own.
|
||||
|
||||
|
|
|
@ -772,6 +772,19 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
@ -909,6 +922,7 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<snakeyaml.version>1.21</snakeyaml.version>
|
||||
<googleclient.version>1.23.0</googleclient.version>
|
||||
<crdt.version>0.1.0</crdt.version>
|
||||
<multiverse.version>0.7.0</multiverse.version>
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.mqtt;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.IMqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class EngineTemperatureSensor implements Callable<Void> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(EngineTemperatureSensor.class);
|
||||
public static final String TOPIC = "engine/temperature";
|
||||
|
||||
private IMqttClient client;
|
||||
private Random rnd = new Random();
|
||||
|
||||
public EngineTemperatureSensor(IMqttClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void call() throws Exception {
|
||||
|
||||
if ( !client.isConnected()) {
|
||||
log.info("[I31] Client not connected.");
|
||||
return null;
|
||||
}
|
||||
|
||||
MqttMessage msg = readEngineTemp();
|
||||
msg.setQos(0);
|
||||
msg.setRetained(true);
|
||||
client.publish(TOPIC,msg);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method simulates reading the engine temperature
|
||||
* @return
|
||||
*/
|
||||
private MqttMessage readEngineTemp() {
|
||||
double temp = 80 + rnd.nextDouble() * 20.0;
|
||||
byte[] payload = String.format("T:%04.2f",temp).getBytes();
|
||||
MqttMessage msg = new MqttMessage(payload);
|
||||
return msg;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.snakeyaml;
|
||||
|
||||
public class Address {
|
||||
private String line;
|
||||
private String city;
|
||||
private String state;
|
||||
private Integer zip;
|
||||
|
||||
public String getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public void setLine(String line) {
|
||||
this.line = line;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public Integer getZip() {
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(Integer zip) {
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.snakeyaml;
|
||||
|
||||
public class Contact {
|
||||
|
||||
private String type;
|
||||
|
||||
private int number;
|
||||
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.snakeyaml;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private int age;
|
||||
private List<Contact> contactDetails;
|
||||
private Address homeAddress;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public List<Contact> getContactDetails() {
|
||||
return contactDetails;
|
||||
}
|
||||
|
||||
public void setContactDetails(List<Contact> contactDetails) {
|
||||
this.contactDetails = contactDetails;
|
||||
}
|
||||
|
||||
public Address getHomeAddress() {
|
||||
return homeAddress;
|
||||
}
|
||||
|
||||
public void setHomeAddress(Address homeAddress) {
|
||||
this.homeAddress = homeAddress;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
package com.baeldung.mqtt;
|
||||
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class EngineTemperatureSensorLiveTest {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(EngineTemperatureSensorLiveTest.class);
|
||||
|
||||
@Test
|
||||
public void whenSendSingleMessage_thenSuccess() throws Exception {
|
||||
|
||||
String publisherId = UUID.randomUUID().toString();
|
||||
MqttClient publisher = new MqttClient("tcp://iot.eclipse.org:1883",publisherId);
|
||||
|
||||
String subscriberId = UUID.randomUUID().toString();
|
||||
MqttClient subscriber = new MqttClient("tcp://iot.eclipse.org:1883",subscriberId);
|
||||
|
||||
MqttConnectOptions options = new MqttConnectOptions();
|
||||
options.setAutomaticReconnect(true);
|
||||
options.setCleanSession(true);
|
||||
options.setConnectionTimeout(10);
|
||||
|
||||
|
||||
subscriber.connect(options);
|
||||
publisher.connect(options);
|
||||
|
||||
CountDownLatch receivedSignal = new CountDownLatch(1);
|
||||
|
||||
subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> {
|
||||
byte[] payload = msg.getPayload();
|
||||
log.info("[I46] Message received: topic={}, payload={}", topic, new String(payload));
|
||||
receivedSignal.countDown();
|
||||
});
|
||||
|
||||
|
||||
Callable<Void> target = new EngineTemperatureSensor(publisher);
|
||||
target.call();
|
||||
|
||||
receivedSignal.await(1, TimeUnit.MINUTES);
|
||||
|
||||
log.info("[I56] Success !");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSendMultipleMessages_thenSuccess() throws Exception {
|
||||
|
||||
String publisherId = UUID.randomUUID().toString();
|
||||
MqttClient publisher = new MqttClient("tcp://iot.eclipse.org:1883",publisherId);
|
||||
|
||||
String subscriberId = UUID.randomUUID().toString();
|
||||
MqttClient subscriber = new MqttClient("tcp://iot.eclipse.org:1883",subscriberId);
|
||||
|
||||
|
||||
MqttConnectOptions options = new MqttConnectOptions();
|
||||
options.setAutomaticReconnect(true);
|
||||
options.setCleanSession(true);
|
||||
options.setConnectionTimeout(10);
|
||||
|
||||
|
||||
publisher.connect(options);
|
||||
subscriber.connect(options);
|
||||
|
||||
CountDownLatch receivedSignal = new CountDownLatch(10);
|
||||
|
||||
subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> {
|
||||
byte[] payload = msg.getPayload();
|
||||
log.info("[I82] Message received: topic={}, payload={}", topic, new String(payload));
|
||||
receivedSignal.countDown();
|
||||
});
|
||||
|
||||
|
||||
Callable<Void> target = new EngineTemperatureSensor(publisher);
|
||||
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
target.call();
|
||||
}
|
||||
catch(Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}, 1, 1, TimeUnit.SECONDS);
|
||||
|
||||
|
||||
receivedSignal.await(1, TimeUnit.MINUTES);
|
||||
executor.shutdown();
|
||||
|
||||
assertTrue(receivedSignal.getCount() == 0 , "Countdown should be zero");
|
||||
|
||||
log.info("[I105] Success !");
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.snakeyaml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import com.baeldung.snakeyaml.Customer;
|
||||
|
||||
public class JavaToYAMLSerializationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenDumpMap_thenGenerateCorrectYAML() {
|
||||
Map<String, Object> data = new LinkedHashMap<String, Object>();
|
||||
data.put("name", "Silenthand Olleander");
|
||||
data.put("race", "Human");
|
||||
data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
|
||||
Yaml yaml = new Yaml();
|
||||
StringWriter writer = new StringWriter();
|
||||
yaml.dump(data, writer);
|
||||
String expectedYaml = "name: Silenthand Olleander\nrace: Human\ntraits: [ONE_HAND, ONE_EYE]\n";
|
||||
assertEquals(expectedYaml, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDumpACustomType_thenGenerateCorrectYAML() {
|
||||
Customer customer = new Customer();
|
||||
customer.setAge(45);
|
||||
customer.setFirstName("Greg");
|
||||
customer.setLastName("McDowell");
|
||||
Yaml yaml = new Yaml();
|
||||
StringWriter writer = new StringWriter();
|
||||
yaml.dump(customer, writer);
|
||||
String expectedYaml = "!!com.baeldung.snakeyaml.Customer {age: 45, contactDetails: null, firstName: Greg,\n homeAddress: null, lastName: McDowell}\n";
|
||||
assertEquals(expectedYaml, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDumpAsCustomType_thenGenerateCorrectYAML() {
|
||||
Customer customer = new Customer();
|
||||
customer.setAge(45);
|
||||
customer.setFirstName("Greg");
|
||||
customer.setLastName("McDowell");
|
||||
Yaml yaml = new Yaml();
|
||||
String expectedYaml = "{age: 45, contactDetails: null, firstName: Greg, homeAddress: null, lastName: McDowell}\n";
|
||||
assertEquals(expectedYaml, yaml.dumpAs(customer, Tag.MAP, null));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
package com.baeldung.snakeyaml;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.yaml.snakeyaml.TypeDescription;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class YAMLToJavaDeserialisationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenLoadYAMLDocument_thenLoadCorrectMap() {
|
||||
Yaml yaml = new Yaml();
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customer.yaml");
|
||||
Map<String, Object> obj = yaml.load(inputStream);
|
||||
assertEquals("John", obj.get("firstName"));
|
||||
assertEquals("Doe", obj.get("lastName"));
|
||||
assertEquals(20, obj.get("age"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAMLDocumentWithTopLevelClass_thenLoadCorrectJavaObject() {
|
||||
Yaml yaml = new Yaml(new Constructor(Customer.class));
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customer.yaml");
|
||||
Customer customer = yaml.load(inputStream);
|
||||
assertEquals("John", customer.getFirstName());
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
assertEquals(20, customer.getAge());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAMLDocumentWithAssumedClass_thenLoadCorrectJavaObject() {
|
||||
Yaml yaml = new Yaml();
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customer_with_type.yaml");
|
||||
Customer customer = yaml.load(inputStream);
|
||||
assertEquals("John", customer.getFirstName());
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
assertEquals(20, customer.getAge());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAML_thenLoadCorrectImplicitTypes() {
|
||||
Yaml yaml = new Yaml();
|
||||
Map<Object, Object> document = yaml.load("3.0: 2018-07-22");
|
||||
assertNotNull(document);
|
||||
assertEquals(1, document.size());
|
||||
assertTrue(document.containsKey(3.0d));
|
||||
assertTrue(document.get(3.0d) instanceof Date);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAMLDocumentWithTopLevelClass_thenLoadCorrectJavaObjectWithNestedObjects() {
|
||||
Yaml yaml = new Yaml(new Constructor(Customer.class));
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customer_with_contact_details_and_address.yaml");
|
||||
Customer customer = yaml.load(inputStream);
|
||||
assertNotNull(customer);
|
||||
assertEquals("John", customer.getFirstName());
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
assertEquals(31, customer.getAge());
|
||||
assertNotNull(customer.getContactDetails());
|
||||
assertEquals(2, customer.getContactDetails().size());
|
||||
assertEquals("mobile", customer.getContactDetails()
|
||||
.get(0)
|
||||
.getType());
|
||||
assertEquals(123456789,customer.getContactDetails()
|
||||
.get(0)
|
||||
.getNumber());
|
||||
assertEquals("landline", customer.getContactDetails()
|
||||
.get(1)
|
||||
.getType());
|
||||
assertEquals(456786868, customer.getContactDetails()
|
||||
.get(1)
|
||||
.getNumber());
|
||||
assertNotNull(customer.getHomeAddress());
|
||||
assertEquals("Xyz, DEF Street", customer.getHomeAddress()
|
||||
.getLine());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAMLDocumentWithTypeDescription_thenLoadCorrectJavaObjectWithCorrectGenericType() {
|
||||
Constructor constructor = new Constructor(Customer.class);
|
||||
TypeDescription customTypeDescription = new TypeDescription(Customer.class);
|
||||
customTypeDescription.addPropertyParameters("contactDetails", Contact.class);
|
||||
constructor.addTypeDescription(customTypeDescription);
|
||||
Yaml yaml = new Yaml(constructor);
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customer_with_contact_details.yaml");
|
||||
Customer customer = yaml.load(inputStream);
|
||||
assertNotNull(customer);
|
||||
assertEquals("John", customer.getFirstName());
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
assertEquals(31, customer.getAge());
|
||||
assertNotNull(customer.getContactDetails());
|
||||
assertEquals(2, customer.getContactDetails().size());
|
||||
assertEquals("mobile", customer.getContactDetails()
|
||||
.get(0)
|
||||
.getType());
|
||||
assertEquals("landline", customer.getContactDetails()
|
||||
.get(1)
|
||||
.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadMultipleYAMLDocuments_thenLoadCorrectJavaObjects() {
|
||||
Yaml yaml = new Yaml(new Constructor(Customer.class));
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customers.yaml");
|
||||
int count = 0;
|
||||
for (Object object : yaml.loadAll(inputStream)) {
|
||||
count++;
|
||||
assertTrue(object instanceof Customer);
|
||||
}
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 20
|
|
@ -0,0 +1,7 @@
|
|||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 31
|
||||
contactDetails:
|
||||
- { type: "mobile", number: 123456789}
|
||||
- { type: "landline", number: 456786868}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 31
|
||||
contactDetails:
|
||||
- type: "mobile"
|
||||
number: 123456789
|
||||
- type: "landline"
|
||||
number: 456786868
|
||||
homeAddress:
|
||||
line: "Xyz, DEF Street"
|
||||
city: "City Y"
|
||||
state: "State Y"
|
||||
zip: 345657
|
|
@ -0,0 +1,6 @@
|
|||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 31
|
||||
contactDetails:
|
||||
- !contact { type: "mobile", number: 123456789}
|
||||
- !contact { type: "landline", number: 456786868}
|
|
@ -0,0 +1,4 @@
|
|||
!!com.baeldung.snakeyaml.Customer
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 20
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 20
|
||||
---
|
||||
firstName: "Jack"
|
||||
lastName: "Jones"
|
||||
age: 25
|
|
@ -19,6 +19,13 @@
|
|||
<version>${log4j-core.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- This is the needed API component. -->
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>${log4j-core.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- This is used by JSONLayout. -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.logging.log4j2.appender;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.core.Appender;
|
||||
import org.apache.logging.log4j.core.Core;
|
||||
import org.apache.logging.log4j.core.Filter;
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.appender.AbstractAppender;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginElement;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
|
||||
|
||||
@Plugin(name = "MapAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
|
||||
public class MapAppender extends AbstractAppender {
|
||||
|
||||
private ConcurrentMap<String, LogEvent> eventMap = new ConcurrentHashMap<>();
|
||||
|
||||
protected MapAppender(String name, Filter filter) {
|
||||
super(name, filter, null);
|
||||
}
|
||||
|
||||
@PluginFactory
|
||||
public static MapAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Filter") final Filter filter) {
|
||||
return new MapAppender(name, filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void append(LogEvent event) {
|
||||
if (event.getLevel()
|
||||
.isLessSpecificThan(Level.WARN)) {
|
||||
error("Unable to log less than WARN level.");
|
||||
return;
|
||||
}
|
||||
eventMap.put(Instant.now()
|
||||
.toString(), event);
|
||||
}
|
||||
|
||||
public ConcurrentMap<String, LogEvent> getEventMap() {
|
||||
return eventMap;
|
||||
}
|
||||
|
||||
public void setEventMap(ConcurrentMap<String, LogEvent> eventMap) {
|
||||
this.eventMap = eventMap;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.logging.log4j2.appender;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.core.LoggerContext;
|
||||
import org.apache.logging.log4j.core.config.Configuration;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class MapAppenderIntegrationTest {
|
||||
|
||||
private Logger logger;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
logger = LogManager.getLogger(MapAppenderIntegrationTest.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoggerEmitsLoggingEvent_thenAppenderReceivesEvent() throws Exception {
|
||||
logger.info("Test from {}", this.getClass()
|
||||
.getSimpleName());
|
||||
LoggerContext context = LoggerContext.getContext(false);
|
||||
Configuration config = context.getConfiguration();
|
||||
MapAppender appender = config.getAppender("MapAppender");
|
||||
assertEquals(appender.getEventMap()
|
||||
.size(), 1);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
|
||||
status="WARN">
|
||||
packages="com.baeldung" status="WARN">
|
||||
<Appenders>
|
||||
<xi:include
|
||||
href="log4j2-includes/console-appender_pattern-layout_colored.xml" />
|
||||
|
@ -50,6 +50,7 @@
|
|||
size="17 kB" />
|
||||
</Policies>
|
||||
</RollingFile>
|
||||
<MapAppender name="MapAppender"/>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
|
||||
|
@ -82,6 +83,7 @@
|
|||
</Logger>
|
||||
<Root level="DEBUG">
|
||||
<AppenderRef ref="ConsoleAppender" />
|
||||
<AppenderRef ref="MapAppender" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
|
@ -10,3 +10,4 @@
|
|||
- [Composite Design Pattern in Java](http://www.baeldung.com/java-composite-pattern)
|
||||
- [Visitor Design Pattern in Java](http://www.baeldung.com/java-visitor-pattern)
|
||||
- [The DAO Pattern in Java](http://www.baeldung.com/java-dao-pattern)
|
||||
- [Interpreter Design Pattern in Java](http://www.baeldung.com/java-interpreter-pattern)
|
||||
|
|
37
pom.xml
37
pom.xml
|
@ -312,6 +312,7 @@
|
|||
<module>core-java-collections</module>
|
||||
<module>core-java-io</module>
|
||||
<module>core-java-8</module>
|
||||
<module>core-java-persistence</module>
|
||||
<module>core-kotlin</module>
|
||||
<module>core-groovy</module>
|
||||
<module>core-java-concurrency</module>
|
||||
|
@ -422,6 +423,7 @@
|
|||
<module>spring-boot-persistence</module>
|
||||
<module>spring-boot-security</module>
|
||||
<module>spring-boot-mvc</module>
|
||||
<module>spring-boot-vue</module>
|
||||
<module>spring-boot-logging-log4j2</module>
|
||||
<module>spring-cloud-data-flow</module>
|
||||
<module>spring-cloud</module>
|
||||
|
@ -857,7 +859,7 @@
|
|||
<module>core-java-io</module>
|
||||
<module>core-java-8</module>
|
||||
<module>core-groovy</module>
|
||||
<module>core-java-concurrency</module>
|
||||
|
||||
<module>couchbase</module>
|
||||
<module>deltaspike</module>
|
||||
<module>dozer</module>
|
||||
|
@ -876,7 +878,7 @@
|
|||
<module>spring-static-resources</module>
|
||||
<module>hazelcast</module>
|
||||
<module>hbase</module>
|
||||
<module>httpclient</module>
|
||||
|
||||
<module>hystrix</module>
|
||||
<module>image-processing</module>
|
||||
<module>immutables</module>
|
||||
|
@ -905,11 +907,11 @@
|
|||
<module>linkrest</module>
|
||||
<module>logging-modules/log-mdc</module>
|
||||
<module>logging-modules/log4j</module>
|
||||
<module>logging-modules/log4j2</module>
|
||||
|
||||
<module>logging-modules/logback</module>
|
||||
<module>lombok</module>
|
||||
<module>mapstruct</module>
|
||||
<module>metrics</module>
|
||||
|
||||
<module>maven</module>
|
||||
<module>mesos-marathon</module>
|
||||
<module>msf4j</module>
|
||||
|
@ -961,25 +963,25 @@
|
|||
<module>spring-cucumber</module>
|
||||
<module>spring-ejb</module>
|
||||
<module>spring-aop</module>
|
||||
<module>spring-data-couchbase-2</module>
|
||||
|
||||
<module>persistence-modules/spring-data-dynamodb</module>
|
||||
<module>spring-data-keyvalue</module>
|
||||
<module>spring-data-mongodb</module>
|
||||
<module>persistence-modules/spring-data-neo4j</module>
|
||||
<module>persistence-modules/spring-data-redis</module>
|
||||
|
||||
<module>spring-data-rest</module>
|
||||
<module>persistence-modules/spring-data-solr</module>
|
||||
<module>spring-dispatcher-servlet</module>
|
||||
<module>spring-exceptions</module>
|
||||
<module>spring-freemarker</module>
|
||||
<module>persistence-modules/spring-hibernate-3</module>
|
||||
<module>spring-hibernate4</module>
|
||||
|
||||
<module>persistence-modules/spring-hibernate-5</module>
|
||||
<module>persistence-modules/spring-data-eclipselink</module>
|
||||
<module>spring-integration</module>
|
||||
<module>spring-jenkins-pipeline</module>
|
||||
<module>spring-jersey</module>
|
||||
<module>jmeter</module>
|
||||
|
||||
<module>spring-jms</module>
|
||||
<module>spring-jooq</module>
|
||||
<module>persistence-modules/spring-jpa</module>
|
||||
|
@ -1040,13 +1042,13 @@
|
|||
<module>testing-modules/testing</module>
|
||||
<module>testing-modules/testng</module>
|
||||
<module>video-tutorials</module>
|
||||
<module>xml</module>
|
||||
|
||||
<module>xmlunit-2</module>
|
||||
<module>struts-2</module>
|
||||
<module>apache-velocity</module>
|
||||
<module>apache-solrj</module>
|
||||
<module>rabbitmq</module>
|
||||
<module>vertx</module>
|
||||
|
||||
<module>persistence-modules/spring-data-gemfire</module>
|
||||
<module>mybatis</module>
|
||||
<module>spring-drools</module>
|
||||
|
@ -1076,11 +1078,22 @@
|
|||
<module>apache-meecrowave</module>
|
||||
<module>testing-modules/junit-abstract</module>
|
||||
|
||||
<module>spring-hibernate4</module>
|
||||
<module>xml</module>
|
||||
<module>vertx</module>
|
||||
<module>metrics</module>
|
||||
<module>httpclient</module>
|
||||
|
||||
<!-- problematic -->
|
||||
<!--
|
||||
<module>ejb</module>
|
||||
<module>persistence-modules/java-cassandra</module>
|
||||
<module>persistence-modules/spring-data-cassandra</module>
|
||||
<module>logging-modules/log4j2</module>
|
||||
<module>spring-data-couchbase-2</module>
|
||||
<module>persistence-modules/spring-data-redis</module>
|
||||
|
||||
<module>jmeter</module>
|
||||
-->
|
||||
|
||||
<!-- heavy -->
|
||||
|
@ -1096,6 +1109,7 @@
|
|||
<module>core-java</module>
|
||||
<module>google-web-toolkit</module>
|
||||
<module>spring-security-mvc-custom</module>
|
||||
<module>core-java-concurrency</module>
|
||||
-->
|
||||
|
||||
<!-- 30:32 min -->
|
||||
|
@ -1158,7 +1172,7 @@
|
|||
<module>spring-security-mvc-custom</module>
|
||||
<module>hibernate5</module>
|
||||
<module>spring-data-elasticsearch</module>
|
||||
|
||||
<module>core-java-concurrency</module>
|
||||
</modules>
|
||||
|
||||
</profile>
|
||||
|
@ -1222,5 +1236,4 @@
|
|||
<!-- <maven-pmd-plugin.version>3.9.0</maven-pmd-plugin.version> -->
|
||||
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -15,3 +15,4 @@
|
|||
- [RxJava Maybe](http://www.baeldung.com/rxjava-maybe)
|
||||
- [Introduction to RxRelay for RxJava](http://www.baeldung.com/rx-relay)
|
||||
- [Filtering Observables in RxJava](http://www.baeldung.com/rxjava-filtering)
|
||||
- [RxJava One Observable, Multiple Subscribers](http://www.baeldung.com/rxjava-multiple-subscribers-observable)
|
||||
|
|
|
@ -13,11 +13,11 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter
|
|||
|
||||
@Controller
|
||||
public class ResponseBodyEmitterController {
|
||||
private ExecutorService nonBlockingService = Executors.newSingleThreadExecutor();
|
||||
|
||||
@GetMapping(Constants.API_RBE)
|
||||
public ResponseEntity<ResponseBodyEmitter> handleRbe() {
|
||||
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
|
||||
ExecutorService nonBlockingService = Executors.newSingleThreadExecutor();
|
||||
|
||||
nonBlockingService.execute(() -> {
|
||||
try {
|
||||
|
|
|
@ -10,12 +10,12 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|||
|
||||
@Controller
|
||||
public class SseEmitterController {
|
||||
private ExecutorService nonBlockingService = Executors.newSingleThreadExecutor();
|
||||
|
||||
@GetMapping(Constants.API_SSE)
|
||||
public SseEmitter handleSse() {
|
||||
SseEmitter emitter = new SseEmitter();
|
||||
|
||||
ExecutorService nonBlockingService = Executors.newSingleThreadExecutor();
|
||||
nonBlockingService.execute(() -> {
|
||||
try {
|
||||
emitter.send(Constants.API_SSE_MSG + " @ " + new Date());
|
||||
|
|
|
@ -5,16 +5,11 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
|
||||
### Relevant Articles
|
||||
|
||||
- [Concurrent Test Execution in Spring 5](http://www.baeldung.com/spring-5-concurrent-tests)
|
||||
- [Introduction to the Functional Web Framework in Spring 5](http://www.baeldung.com/spring-5-functional-web)
|
||||
- [Exploring the Spring 5 MVC URL Matching Improvements](http://www.baeldung.com/spring-5-mvc-url-matching)
|
||||
- [Spring 5 WebClient](http://www.baeldung.com/spring-5-webclient)
|
||||
- [Spring 5 Functional Bean Registration](http://www.baeldung.com/spring-5-functional-beans)
|
||||
- [The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5](http://www.baeldung.com/spring-5-junit-config)
|
||||
- [Spring Security 5 for Reactive Applications](http://www.baeldung.com/spring-security-5-reactive)
|
||||
- [Spring 5 Testing with @EnabledIf Annotation](https://github.com/eugenp/tutorials/tree/master/spring-5)
|
||||
- [Reactive WebSockets with Spring 5](http://www.baeldung.com/spring-5-reactive-websockets)
|
||||
- [Spring Boot Actuator](http://www.baeldung.com/spring-boot-actuators)
|
||||
- [Exploring the Spring 5 MVC URL Matching Improvements](http://www.baeldung.com/spring-5-mvc-url-matching)
|
||||
- [Spring Security 5 for Reactive Applications](http://www.baeldung.com/spring-security-5-reactive)
|
||||
- [Reactive WebSockets with Spring 5](http://www.baeldung.com/spring-5-reactive-websockets)
|
||||
- [Spring Webflux Filters](http://www.baeldung.com/spring-webflux-filters)
|
||||
- [Reactive Flow with MongoDB, Kotlin, and Spring WebFlux](http://www.baeldung.com/kotlin-mongodb-spring-webflux)
|
||||
- [Spring Data Reactive Repositories with MongoDB](http://www.baeldung.com/spring-data-mongodb-reactive)
|
||||
- [How to Set a Header on a Response with Spring 5](http://www.baeldung.com/spring-response-header)
|
||||
|
|
|
@ -127,15 +127,6 @@
|
|||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.reactivex.rxjava2</groupId>
|
||||
<artifactId>rxjava</artifactId>
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
class Actor {
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
|
||||
public Actor() {
|
||||
}
|
||||
|
||||
public Actor(String firstname, String lastname) {
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
}
|
||||
|
||||
public String getFirstname() {
|
||||
return firstname;
|
||||
}
|
||||
|
||||
public String getLastname() {
|
||||
return lastname;
|
||||
}
|
||||
|
||||
}
|
|
@ -34,8 +34,7 @@ public class FormHandler {
|
|||
|
||||
private AtomicLong extractData(List<DataBuffer> dataBuffers) {
|
||||
AtomicLong atomicLong = new AtomicLong(0);
|
||||
dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer()
|
||||
.array().length));
|
||||
dataBuffers.forEach(d -> atomicLong.addAndGet(d.readableByteCount()));
|
||||
return atomicLong;
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ import java.util.List;
|
|||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.Wrapper;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
||||
import org.springframework.boot.web.server.WebServer;
|
||||
|
@ -61,7 +62,8 @@ public class FunctionalWebApplication {
|
|||
tomcat.setPort(9090);
|
||||
Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
|
||||
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
|
||||
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
|
||||
Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
|
||||
servletWrapper.setAsyncSupported(true);
|
||||
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
|
||||
|
||||
TomcatWebServer server = new TomcatWebServer(tomcat);
|
|
@ -1,11 +1,9 @@
|
|||
package com.baeldung.reactive;
|
||||
|
||||
import com.mongodb.reactivestreams.client.MongoClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Spring5ReactiveApplication{
|
||||
|
@ -14,12 +12,4 @@ public class Spring5ReactiveApplication{
|
|||
SpringApplication.run(Spring5ReactiveApplication.class, args);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
MongoClient mongoClient;
|
||||
|
||||
@Bean
|
||||
public ReactiveMongoTemplate reactiveMongoTemplate() {
|
||||
return new ReactiveMongoTemplate(mongoClient, "test");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung;
|
||||
package com.baeldung.reactive;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
@ -11,7 +11,7 @@ import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
|||
import reactor.ipc.netty.NettyContext;
|
||||
import reactor.ipc.netty.http.server.HttpServer;
|
||||
|
||||
@ComponentScan(basePackages = {"com.baeldung.security"})
|
||||
@ComponentScan(basePackages = {"com.baeldung.reactive.security"})
|
||||
@EnableWebFlux
|
||||
public class SpringSecurity5Application {
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.reactive.actuator;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.security.reactive.EndpointRequest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebFluxSecurity
|
||||
public class WebSecurityConfig {
|
||||
|
||||
|
||||
@Bean
|
||||
public SecurityWebFilterChain securitygWebFilterChain(
|
||||
ServerHttpSecurity http) {
|
||||
return http
|
||||
|
||||
.authorizeExchange()
|
||||
.matchers(EndpointRequest.to(
|
||||
FeaturesEndpoint.class
|
||||
)).permitAll().and().csrf().disable().build();
|
||||
}
|
||||
|
||||
}
|
|
@ -4,16 +4,8 @@ import java.util.Collections;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
|
||||
|
||||
@SpringBootApplication(exclude = { MongoAutoConfiguration.class,
|
||||
MongoDataAutoConfiguration.class,
|
||||
MongoReactiveDataAutoConfiguration.class,
|
||||
MongoReactiveAutoConfiguration.class }
|
||||
)
|
||||
@SpringBootApplication
|
||||
public class CorsOnAnnotatedElementsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
@RestController("FurtherCorsConfigsController-cors-on-global-config-and-more")
|
||||
@RequestMapping("/cors-on-global-config-and-more")
|
||||
public class FurtherCorsConfigsController {
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
@RestController("RegularRestController-cors-on-global-config")
|
||||
@RequestMapping("/cors-on-global-config")
|
||||
public class RegularRestController {
|
||||
|
||||
|
|
|
@ -8,8 +8,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/web-filter-and-more-on-annotated")
|
||||
//@RestController
|
||||
//@RequestMapping("/web-filter-and-more-on-annotated")
|
||||
public class FurtherCorsConfigsController {
|
||||
|
||||
@DeleteMapping("/further-mixed-config-endpoint")
|
||||
|
|
|
@ -7,8 +7,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/web-filter-on-annotated")
|
||||
//@RestController
|
||||
//@RequestMapping("/web-filter-on-annotated")
|
||||
public class RegularRestController {
|
||||
|
||||
@PutMapping("/regular-put-endpoint")
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.security;
|
||||
package com.baeldung.reactive.security;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.security;
|
||||
package com.baeldung.reactive.security;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Service;
|
|
@ -1,5 +1,6 @@
|
|||
package com.baeldung.security;
|
||||
package com.baeldung.reactive.security;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.security.reactive.EndpointRequest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
|
@ -9,6 +10,8 @@ import org.springframework.security.core.userdetails.User;
|
|||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
|
||||
import com.baeldung.reactive.actuator.FeaturesEndpoint;
|
||||
|
||||
@EnableWebFluxSecurity
|
||||
@EnableReactiveMethodSecurity
|
||||
public class SecurityConfig {
|
||||
|
@ -16,10 +19,18 @@ public class SecurityConfig {
|
|||
@Bean
|
||||
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
|
||||
return http.authorizeExchange()
|
||||
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN")
|
||||
.anyExchange().authenticated()
|
||||
.and().formLogin()
|
||||
.and().build();
|
||||
.pathMatchers("/", "/admin")
|
||||
.hasAuthority("ROLE_ADMIN")
|
||||
.matchers(EndpointRequest.to(FeaturesEndpoint.class))
|
||||
.permitAll()
|
||||
.anyExchange()
|
||||
.permitAll()
|
||||
.and()
|
||||
.formLogin()
|
||||
.and()
|
||||
.csrf()
|
||||
.disable()
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
|
@ -7,6 +7,7 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.t
|
|||
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.Wrapper;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
||||
import org.springframework.boot.web.server.WebServer;
|
||||
|
@ -41,7 +42,8 @@ public class ExploreSpring5URLPatternUsingRouterFunctions {
|
|||
tomcat.setPort(9090);
|
||||
Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
|
||||
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
|
||||
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
|
||||
Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
|
||||
servletWrapper.setAsyncSupported(true);
|
||||
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
|
||||
|
||||
TomcatWebServer server = new TomcatWebServer(tomcat);
|
||||
|
|
|
@ -4,8 +4,12 @@ import lombok.AllArgsConstructor;
|
|||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class EmployeeCreationEvent {
|
||||
private String employeeId;
|
||||
private String creationTime;
|
||||
public EmployeeCreationEvent(String employeeId, String creationTime) {
|
||||
super();
|
||||
this.employeeId = employeeId;
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
@Component("EmployeeWebSocketHandler")
|
||||
public class EmployeeWebSocketHandler implements WebSocketHandler {
|
||||
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.reactive.websocket;
|
||||
package com.baeldung.websocket;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.reactive.websocket;
|
||||
package com.baeldung.websocket;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.reactive.websocket;
|
||||
package com.baeldung.websocket;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
@ -1,19 +1,22 @@
|
|||
package com.baeldung.reactive.websocket;
|
||||
package com.baeldung.websocket;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.HandlerMapping;
|
||||
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.reactive.socket.WebSocketHandler;
|
||||
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class ReactiveWebSocketConfiguration {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("ReactiveWebSocketHandler")
|
||||
private WebSocketHandler webSocketHandler;
|
||||
|
||||
@Bean
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.reactive.websocket;
|
||||
package com.baeldung.websocket;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
@ -14,7 +14,7 @@ import java.time.Duration;
|
|||
import static java.time.LocalDateTime.now;
|
||||
import static java.util.UUID.randomUUID;
|
||||
|
||||
@Component
|
||||
@Component("ReactiveWebSocketHandler")
|
||||
public class ReactiveWebSocketHandler implements WebSocketHandler {
|
||||
|
||||
private static final ObjectMapper json = new ObjectMapper();
|
|
@ -1,5 +1,8 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
||||
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
@ -12,9 +15,6 @@ import org.springframework.util.LinkedMultiValueMap;
|
|||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
|
||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
||||
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
|
||||
|
||||
public class FunctionalWebApplicationIntegrationTest {
|
||||
|
||||
private static WebTestClient client;
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue