Merge branch 'eugenp:master' into danielmcnally285_return_first_non_null

This commit is contained in:
danielmcnally285
2023-10-31 20:21:00 +00:00
committed by GitHub
414 changed files with 3612 additions and 381 deletions
+1 -1
View File
@@ -11,5 +11,5 @@ This module contains articles about Java 8 core features
- [Finding Min/Max in an Array with Java](https://www.baeldung.com/java-array-min-max)
- [Internationalization and Localization in Java 8](https://www.baeldung.com/java-8-localization)
- [Generalized Target-Type Inference in Java](https://www.baeldung.com/java-generalized-target-type-inference)
- [Monads in Java](https://www.baeldung.com/java-monads)
- [Monads in Java Optional](https://www.baeldung.com/java-monads)
- [[More -->]](/core-java-modules/core-java-8-2)
@@ -0,0 +1,48 @@
package com.baeldung.spliterator;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Consumer;
public class CustomSpliterator implements Spliterator<Integer> {
private final List<Integer> elements;
private int currentIndex;
public CustomSpliterator(List<Integer> elements) {
this.elements = elements;
this.currentIndex = 0;
}
@Override
public boolean tryAdvance(Consumer<? super Integer> action) {
if (currentIndex < elements.size()) {
action.accept(elements.get(currentIndex));
currentIndex++;
return true;
}
return false;
}
@Override
public Spliterator<Integer> trySplit() {
int currentSize = elements.size() - currentIndex;
if (currentSize < 2) {
return null;
}
int splitIndex = currentIndex + currentSize / 2;
CustomSpliterator newSpliterator = new CustomSpliterator(elements.subList(currentIndex, splitIndex));
currentIndex = splitIndex;
return newSpliterator;
}
@Override
public long estimateSize() {
return elements.size() - currentIndex;
}
@Override
public int characteristics() {
return ORDERED | SIZED | SUBSIZED | NONNULL;
}
}
@@ -1,12 +0,0 @@
package com.baeldung.spliterator;
import java.util.stream.Stream;
public class Executor {
public static int countAutors(Stream<Author> stream) {
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true),
RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
return wordCounter.getCounter();
}
}
@@ -1,27 +0,0 @@
package com.baeldung.spliterator;
public class RelatedAuthorCounter {
private final int counter;
private final boolean isRelated;
public RelatedAuthorCounter(int counter, boolean isRelated) {
this.counter = counter;
this.isRelated = isRelated;
}
public RelatedAuthorCounter accumulate(Author author) {
if (author.getRelatedArticleId() == 0) {
return isRelated ? this : new RelatedAuthorCounter(counter, true);
} else {
return isRelated ? new RelatedAuthorCounter(counter + 1, false) : this;
}
}
public RelatedAuthorCounter combine(RelatedAuthorCounter RelatedAuthorCounter) {
return new RelatedAuthorCounter(counter + RelatedAuthorCounter.counter, RelatedAuthorCounter.isRelated);
}
public int getCounter() {
return counter;
}
}
@@ -1,49 +0,0 @@
package com.baeldung.spliterator;
import java.util.List;
import java.util.Spliterator;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
public class RelatedAuthorSpliterator implements Spliterator<Author> {
private final List<Author> list;
AtomicInteger current = new AtomicInteger();
public RelatedAuthorSpliterator(List<Author> list) {
this.list = list;
}
@Override
public boolean tryAdvance(Consumer<? super Author> action) {
action.accept(list.get(current.getAndIncrement()));
return current.get() < list.size();
}
@Override
public Spliterator<Author> trySplit() {
int currentSize = list.size() - current.get();
if (currentSize < 10) {
return null;
}
for (int splitPos = currentSize / 2 + current.intValue(); splitPos < list.size(); splitPos++) {
if (list.get(splitPos).getRelatedArticleId() == 0) {
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current.get(), splitPos));
current.set(splitPos);
return spliterator;
}
}
return null;
}
@Override
public long estimateSize() {
return list.size() - current.get();
}
@Override
public int characteristics() {
return CONCURRENT;
}
}
@@ -6,10 +6,10 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Spliterator;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.junit.Before;
import org.junit.Test;
@@ -18,22 +18,50 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ExecutorUnitTest {
Article article;
Stream<Author> stream;
Spliterator<Author> spliterator;
@Before
public void init() {
article = new Article(Arrays.asList(new Author("Ahmad", 0), new Author("Eugen", 0), new Author("Alice", 1), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0),
new Author("Alice", 1), new Author("Mike", 0), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1),
new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0);
spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors());
}
@Test
public void givenAstreamOfAuthors_whenProcessedInParallelWithCustomSpliterator_coubtProducessRightOutput() {
Stream<Author> stream2 = StreamSupport.stream(spliterator, true);
assertThat(Executor.countAutors(stream2.parallel())).isEqualTo(9);
public void givenAStreamOfIntegers_whenProcessedSequentialCustomSpliterator_countProducesRightOutput() {
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
CustomSpliterator customSpliterator = new CustomSpliterator(numbers);
AtomicInteger sum = new AtomicInteger();
customSpliterator.forEachRemaining(sum::addAndGet);
assertThat(sum.get()).isEqualTo(15);
}
@Test
public void givenAStreamOfIntegers_whenProcessedInParallelWithCustomSpliterator_countProducesRightOutput() {
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
CustomSpliterator customSpliterator = new CustomSpliterator(numbers);
// Create a ForkJoinPool for parallel processing
ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
AtomicInteger sum = new AtomicInteger(0);
// Process elements in parallel using parallelStream
forkJoinPool.submit(() -> customSpliterator.forEachRemaining(sum::addAndGet)).join();
assertThat(sum.get()).isEqualTo(15);
}
@Test
@@ -43,9 +71,7 @@ public class ExecutorUnitTest {
.collect(Collectors.toList());
Spliterator<Article> spliterator = articles.spliterator();
while (spliterator.tryAdvance(article -> article.setName(article.getName()
.concat("- published by Baeldung"))))
;
while(spliterator.tryAdvance(article -> article.setName(article.getName().concat("- published by Baeldung"))));
articles.forEach(article -> assertThat(article.getName()).isEqualTo("Java- published by Baeldung"));
}
@@ -57,5 +57,9 @@
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.release>11</maven.compiler.release>
</properties>
</project>
@@ -0,0 +1,29 @@
package com.baeldung.arraycompare;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class ArraysCompareUnitTest {
@Test
void givenSameContents_whenCompare_thenCorrect() {
String[] array1 = new String[] { "A", "B", "C" };
String[] array2 = new String[] { "A", "B", "C" };
assertThat(Arrays.compare(array1, array2)).isEqualTo(0);
}
@Test
void givenDifferentContents_whenCompare_thenDifferent() {
String[] array1 = new String[] { "A", "B", "C" };
String[] array2 = new String[] { "A", "C", "B", "D" };
assertThat(Arrays.compare(array1, array2)).isLessThan(0);
assertThat(Arrays.compare(array2, array1)).isGreaterThan(0);
assertThat(Arrays.compare(array1, null)).isGreaterThan(0);
}
}
@@ -0,0 +1,6 @@
## Core Java Arrays - Basic Operations
This module contains articles about Java array fundamentals. They assume no previous background knowledge on working with arrays.
### Relevant Articles:
- [Arrays mismatch() Method in Java](https://www.baeldung.com/java-arrays-mismatch)
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-arrays-operations-basic-2</artifactId>
<name>core-java-arrays-operations-basic-2</name>
<packaging>jar</packaging>
<parent>
<artifactId>core-java-modules</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>
@@ -0,0 +1,269 @@
package com.baeldung.array.mismatch;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
import java.util.Comparator;
import org.junit.jupiter.api.Test;
class ArrayMismatchUnitTest {
@Test
void givenTwoArraysWithACommonPrefix_whenMismatch_thenIndexOfFirstMismatch() {
int[] firstArray = {1, 2, 3, 4, 5};
int[] secondArray = {1, 2, 3, 5, 8};
assertEquals(3, Arrays.mismatch(firstArray, secondArray));
}
@Test
void givenTwoIdenticalArrays_whenMismatch_thenMinusOne() {
int[] firstArray = {1, 2, 3, 4, 5};
int[] secondArray = {1, 2, 3, 4, 5};
assertEquals(-1, Arrays.mismatch(firstArray, secondArray));
}
@Test
void givenFirstArrayIsAPrefixOfTheSecond_whenMismatch_thenFirstArrayLength() {
int[] firstArray = {1, 2, 3, 4, 5};
int[] secondArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};
assertEquals(firstArray.length, Arrays.mismatch(firstArray, secondArray));
}
@Test
void givenNoCommonPrefix_whenMismatch_thenZero() {
int[] firstArray = {1, 2, 3, 4, 5};
int[] secondArray = {9, 8, 7};
assertEquals(0, Arrays.mismatch(firstArray, secondArray));
}
@Test
void givenAtLeastANullArray_whenMismatch_thenThrowsNullPointerException() {
int[] firstArray = null;
int[] secondArray = {1, 2, 3, 4, 5};
assertThrows(NullPointerException.class, () -> Arrays.mismatch(firstArray, secondArray));
assertThrows(NullPointerException.class, () -> Arrays.mismatch(secondArray, firstArray));
assertThrows(NullPointerException.class, () -> Arrays.mismatch(firstArray, firstArray));
}
@Test
void givenExactlyOneAnEmptyArray_whenMismatch_thenZero() {
int[] firstArray = {};
int[] secondArray = {1, 2, 3};
assertEquals(0, Arrays.mismatch(firstArray, secondArray));
assertEquals(0, Arrays.mismatch(secondArray, firstArray));
}
@Test
void givenTwoEmptyArrays_whenMismatch_thenMinusOne() {
assertEquals(-1, Arrays.mismatch(new int[] {}, new int[] {}));
}
@Test
void givenTwoSubArraysWithACommonPrefix_whenMismatch_thenIndexOfFirstMismatch() {
int[] firstArray = {1, 2, 3, 4, 5};
int[] secondArray = {0, 1, 2, 3, 5, 8};
assertEquals(3, Arrays.mismatch(firstArray, 0, 4, secondArray, 1, 6));
}
@Test
void givenTwoIdenticalSubArrays_whenMismatch_thenMinusOne() {
int[] firstArray = {0, 0, 1, 2};
int[] secondArray = {0, 1, 2, 3, 4};
assertEquals(-1, Arrays.mismatch(firstArray, 2, 4, secondArray, 1, 3));
}
@Test
void givenFirstSubArrayIsAPrefixOfTheSecond_whenMismatch_thenFirstArrayLength() {
int[] firstArray = {2, 3, 4, 5, 4, 3, 2};
int[] secondArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
assertEquals(4, Arrays.mismatch(firstArray, 0, 4, secondArray, 2, 9));
}
@Test
void givenNoCommonPrefixForSubArrays_whenMismatch_thenZero() {
int[] firstArray = {0, 0, 0, 0, 0};
int[] secondArray = {9, 8, 7, 6, 5};
assertEquals(0, Arrays.mismatch(firstArray, 1, 2, secondArray, 1, 2));
}
@Test
void givenAtLeastANullSubArray_whenMismatch_thenThrowsNullPointerException() {
int[] firstArray = null;
int[] secondArray = {1, 2, 3, 4, 5};
assertThrows(NullPointerException.class, () -> Arrays.mismatch(firstArray, 0, 1, secondArray, 0, 1));
assertThrows(NullPointerException.class, () -> Arrays.mismatch(secondArray, 0, 1, firstArray, 0, 1));
assertThrows(NullPointerException.class, () -> Arrays.mismatch(firstArray, 0, 1, firstArray, 0, 1));
}
@Test
void givenExactlyOneEmptySubArray_whenMismatch_thenZero() {
int[] firstArray = {1};
int[] secondArray = {1, 2, 3};
assertEquals(0, Arrays.mismatch(firstArray, 0, 0, secondArray, 0, 2));
assertEquals(0, Arrays.mismatch(firstArray, 0, 1, secondArray, 2, 2));
}
@Test
void givenTwoEmptySubArrays_whenMismatch_thenMinusOne() {
int[] firstArray = {1};
int[] secondArray = {1, 2, 3};
assertEquals(-1, Arrays.mismatch(firstArray, 0, 0, secondArray, 2, 2));
}
@Test
void givenToIndexGreaterThanFromIndex_whenMismatch_thenThrowsIllegalArgumentException() {
int[] firstArray = {2, 3, 4, 5, 4, 3, 2};
int[] secondArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
assertThrows(IllegalArgumentException.class, () -> Arrays.mismatch(firstArray, 4, 2, secondArray, 0, 6));
assertThrows(IllegalArgumentException.class, () -> Arrays.mismatch(firstArray, 2, 3, secondArray, 6, 0));
}
@Test
void givenIllegalIndexes_whenMismatch_thenThrowsArrayIndexOutOfBoundsException() {
int[] firstArray = {2, 3, 4, 5, 4, 3, 2};
int[] secondArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
assertThrows(ArrayIndexOutOfBoundsException.class, () -> Arrays.mismatch(firstArray, -1, 2, secondArray, 0, 6));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> Arrays.mismatch(firstArray, 0, 8, secondArray, 0, 6));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> Arrays.mismatch(firstArray, 2, 3, secondArray, -5, 0));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> Arrays.mismatch(firstArray, 2, 3, secondArray, 11, 12));
}
@Test
void givenTwoStringArraysAndAComparator_whenMismatch_thenIndexOfFirstMismatch() {
String[] firstArray = {"one", "two", "three"};
String[] secondArray = {"ONE", "TWO", "FOUR"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(2, Arrays.mismatch(firstArray, secondArray, comparator));
}
@Test
void givenTwoIdenticalStringArraysForTheComparator_whenMismatch_thenMinusOne() {
String[] firstArray = {"one", "two", "three"};
String[] secondArray = {"ONE", "TWO", "THREE"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(-1, Arrays.mismatch(firstArray, secondArray, comparator));
}
@Test
void givenFirstStringArrayIsAPrefixOfTheSecondForTheComparator_whenMismatch_thenFirstArrayLength() {
String[] firstArray = {"one", "two", "three"};
String[] secondArray = {"ONE", "TWO", "THREE", "FOUR", "FIVE"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(firstArray.length, Arrays.mismatch(firstArray, secondArray, comparator));
}
@Test
void givenNoCommonPrefixForTheComparator_whenMismatch_thenZero() {
String[] firstArray = {"one", "two", "three"};
String[] secondArray = {"six", "seven", "eight"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(0, Arrays.mismatch(firstArray, secondArray, comparator));
}
@Test
void givenAtLeastANullArrayOrNullComparator_whenMismatch_thenThrowsNullPointerException() {
String[] firstArray = null;
String[] secondArray = {"one"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertThrows(NullPointerException.class, () -> Arrays.mismatch(firstArray, secondArray, comparator));
assertThrows(NullPointerException.class, () -> Arrays.mismatch(secondArray, firstArray, comparator));
assertThrows(NullPointerException.class, () -> Arrays.mismatch(secondArray, secondArray, null));
}
@Test
void givenExactlyOneAnEmptyArrayAndAComparator_whenMismatch_thenZero() {
String[] firstArray = {};
String[] secondArray = {"one"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(0, Arrays.mismatch(firstArray, secondArray, comparator));
assertEquals(0, Arrays.mismatch(secondArray, firstArray, comparator));
}
@Test
void givenTwoEmptyStringArraysForTheComparator_whenMismatch_thenMinusOne() {
assertEquals(-1, Arrays.mismatch(new String[] {}, new String[] {}, String.CASE_INSENSITIVE_ORDER));
}
@Test
void givenTwoStringSubarraysAndAComparator_whenMismatch_thenIndexOfFirstMismatch() {
String[] firstArray = {"one", "two", "three", "four"};
String[] secondArray = {"ZERO", "ONE", "TWO", "FOUR", "EIGHT", "SIXTEEN"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(2, Arrays.mismatch(firstArray, 0, 4, secondArray, 1, 3, comparator));
}
@Test
void givenTwoIdenticalStringSubArraysForTheComparator_whenMismatch_thenMinusOne() {
String[] firstArray = {"zero", "zero", "one", "two"};
String[] secondArray = {"zero", "one", "two", "three", "four"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(-1, Arrays.mismatch(firstArray, 2, 4, secondArray, 1, 3, comparator));
}
@Test
void givenFirstSubArrayIsAPrefixOfTheSecondForTheComparator_whenMismatch_thenFirstArrayLength() {
String[] firstArray = {"two", "three", "four", "five", "four", "three", "two"};
String[] secondArray = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "EIGHT", "NINE", "TEN"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(4, Arrays.mismatch(firstArray, 0, 4, secondArray, 2, 9, comparator));
}
@Test
void givenNoCommonPrefixForSubArraysForTheComparator_whenMismatch_thenZero() {
String[] firstArray = {"zero", "one"};
String[] secondArray = {"TEN", "ELEVEN", "TWELVE"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(0, Arrays.mismatch(firstArray, 1, 2, secondArray, 1, 2, comparator));
}
@Test
void givenAtLeastANullSubArrayOrNullComparator_whenMismatch_thenThrowsNullPointerException() {
String[] firstArray = null;
String[] secondArray = {"ONE", "TWO", "THREE", "FOUR", "FIVE"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertThrows(NullPointerException.class, () -> Arrays.mismatch(firstArray, 0, 1, secondArray, 0, 1, comparator));
assertThrows(NullPointerException.class, () -> Arrays.mismatch(secondArray, 0, 1, firstArray, 0, 1, comparator));
assertThrows(NullPointerException.class, () -> Arrays.mismatch(firstArray, 0, 1, firstArray, 0, 1, comparator));
assertThrows(NullPointerException.class, () -> Arrays.mismatch(secondArray, 0, 1, secondArray, 0, 1, null));
}
@Test
void givenExactlyOneEmptySubArrayAndAComparator_whenMismatch_thenZero() {
String[] firstArray = {"one"};
String[] secondArray = {"ONE", "TWO", "THREE"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(0, Arrays.mismatch(firstArray, 0, 0, secondArray, 0, 2, comparator));
assertEquals(0, Arrays.mismatch(firstArray, 0, 1, secondArray, 2, 2, comparator));
}
@Test
void givenTwoEmptySubArraysAndAComparator_whenMismatch_thenMinusOne() {
String[] firstArray = {"one"};
String[] secondArray = {"ONE", "TWO", "THREE"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertEquals(-1, Arrays.mismatch(firstArray, 0, 0, secondArray, 2, 2, comparator));
}
@Test
void givenToIndexGreaterThanFromIndexAndAComparator_whenMismatch_thenThrowsIllegalArgumentException() {
String[] firstArray = {"two", "three", "four", "five", "four", "three", "two"};
String[] secondArray = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "EIGHT", "NINE", "TEN"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertThrows(IllegalArgumentException.class, () -> Arrays.mismatch(firstArray, 4, 2, secondArray, 0, 6, comparator));
assertThrows(IllegalArgumentException.class, () -> Arrays.mismatch(firstArray, 2, 3, secondArray, 6, 0, comparator));
}
@Test
void givenIllegalIndexesAndAComparator_whenMismatch_thenThrowsArrayIndexOutOfBoundsException() {
String[] firstArray = {"two", "three", "four", "five", "four", "three", "two"};
String[] secondArray = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "EIGHT", "NINE", "TEN"};
Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
assertThrows(ArrayIndexOutOfBoundsException.class, () -> Arrays.mismatch(firstArray, -1, 2, secondArray, 0, 6, comparator));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> Arrays.mismatch(firstArray, 0, 8, secondArray, 0, 6, comparator));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> Arrays.mismatch(firstArray, 2, 3, secondArray, -5, 0, comparator));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> Arrays.mismatch(firstArray, 2, 3, secondArray, 11, 12, comparator));
}
}
@@ -7,4 +7,5 @@
- [Creating Custom Iterator in Java](https://www.baeldung.com/java-creating-custom-iterator)
- [Difference Between Arrays.sort() and Collections.sort()](https://www.baeldung.com/java-arrays-collections-sort-methods)
- [Skipping the First Iteration in Java](https://www.baeldung.com/java-skip-first-iteration)
- [Remove Elements From a Queue Using Loop](https://www.baeldung.com/java-remove-elements-queue)
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-4)
@@ -12,7 +12,7 @@ public class RemoveQueueElementsUnitTest {
@Test
public void givenQueueWithEvenAndOddNumbers_whenRemovingEvenNumbers_thenOddNumbersRemain() {
Queue<Integer> queue = new LinkedList<>();
Queue<Integer> evenElementsQueue = new LinkedList<>();
Queue<Integer> oddElementsQueue = new LinkedList<>();
queue.add(1);
queue.add(2);
queue.add(3);
@@ -22,14 +22,14 @@ public class RemoveQueueElementsUnitTest {
while (queue.peek() != null) {
int element = queue.remove();
if (element % 2 != 0) {
evenElementsQueue.add(element);
oddElementsQueue.add(element);
}
}
assertEquals(3, evenElementsQueue.size());
assertTrue(evenElementsQueue.contains(1));
assertTrue(evenElementsQueue.contains(3));
assertTrue(evenElementsQueue.contains(5));
assertEquals(3, oddElementsQueue.size());
assertTrue(oddElementsQueue.contains(1));
assertTrue(oddElementsQueue.contains(3));
assertTrue(oddElementsQueue.contains(5));
}
@Test
@@ -1,2 +1,3 @@
## Relevant Articles
- [Difference Between putIfAbsent() and computeIfAbsent() in Javas Map](https://www.baeldung.com/java-map-putifabsent-computeifabsent)
- [How to Write Hashmap to CSV File](https://www.baeldung.com/java-write-hashmap-csv)
@@ -0,0 +1,3 @@
### Relevant Articles:
- [Why wait() Requires Synchronization?](https://www.baeldung.com/java-wait-necessary-synchronization)
@@ -10,4 +10,5 @@ This module contains articles about basic Java concurrency.
- [Returning a Value After Finishing Threads Job in Java](https://www.baeldung.com/java-return-value-after-thread-finish)
- [CompletableFuture and ThreadPool in Java](https://www.baeldung.com/java-completablefuture-threadpool)
- [CompletableFuture allOf().join() vs. CompletableFuture.join()](https://www.baeldung.com/java-completablefuture-allof-join)
- [Retry Logic with CompletableFuture](https://www.baeldung.com/java-completablefuture-retry-logic)
- [[<-- Prev]](../core-java-concurrency-basic-2)
@@ -0,0 +1,63 @@
package com.baeldung.concurrent.completablefuture.retry;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;
public class RetryCompletableFuture {
public static <T> CompletableFuture<T> retryTask(Supplier<T> supplier, int maxRetries) {
Supplier<T> retryableSupplier = retryFunction(supplier, maxRetries);
return CompletableFuture.supplyAsync(retryableSupplier);
}
static <T> Supplier<T> retryFunction(Supplier<T> supplier, int maxRetries) {
return () -> {
int retries = 0;
while (retries < maxRetries) {
try {
return supplier.get();
} catch (Exception e) {
retries++;
}
}
throw new IllegalStateException(String.format("Task failed after %s attempts", maxRetries));
};
}
public static <T> CompletableFuture<T> retryUnsafe(Supplier<T> supplier, int maxRetries) {
CompletableFuture<T> cf = CompletableFuture.supplyAsync(supplier);
sleep(100l);
for (int i = 0; i < maxRetries; i++) {
cf = cf.exceptionally(__ -> supplier.get());
}
return cf;
}
public static <T> CompletableFuture<T> retryNesting(Supplier<T> supplier, int maxRetries) {
CompletableFuture<T> cf = CompletableFuture.supplyAsync(supplier);
sleep(100);
for (int i = 0; i < maxRetries; i++) {
cf = cf.thenApply(CompletableFuture::completedFuture)
.exceptionally(__ -> CompletableFuture.supplyAsync(supplier))
.thenCompose(Function.identity());
}
return cf;
}
public static <T> CompletableFuture<T> retryExceptionallyAsync(Supplier<T> supplier, int maxRetries) {
CompletableFuture<T> cf = CompletableFuture.supplyAsync(supplier);
sleep(100);
for (int i = 0; i < maxRetries; i++) {
cf = cf.exceptionallyAsync(__ -> supplier.get());
}
return cf;
}
private static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,100 @@
package com.baeldung.concurrent.completablefuturelist;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Collectors;
public class Application {
ScheduledExecutorService asyncOperationEmulation;
Application initialize() {
asyncOperationEmulation = Executors.newScheduledThreadPool(10);
return this;
}
CompletableFuture<String> asyncOperation(String operationId) {
CompletableFuture<String> cf = new CompletableFuture<>();
asyncOperationEmulation.submit(() -> {
// The following lines simulate an exception happening on the 567th operation
// if (operationId.endsWith("567")) {
// cf.completeExceptionally(new Exception("Error on operation " + operationId));
// return;
// }
try {
Thread.sleep(100);
cf.complete(operationId);
} catch (InterruptedException e) {
System.err.println("Thread interrupted error");
cf.completeExceptionally(e);
}
});
return cf;
}
void startNaive() {
List<CompletableFuture<String>> futures = new ArrayList<>();
for (int i = 1; i <= 1000; i++) {
String operationId = "Naive-Operation-" + i;
futures.add(asyncOperation(operationId));
}
CompletableFuture<List<String>> aggregate = CompletableFuture.completedFuture(new ArrayList<>());
for (CompletableFuture<String> future : futures) {
aggregate = aggregate.thenCompose(list -> {
try {
list.add(future.get());
return CompletableFuture.completedFuture(list);
} catch (Exception e) {
final CompletableFuture<List<String>> excFuture = new CompletableFuture<>();
excFuture.completeExceptionally(e);
return excFuture;
}
});
}
try {
final List<String> results = aggregate.join();
System.out.println("Printing first 10 results");
for (int i = 0; i < 10; i++) {
System.out.println("Finished " + results.get(i));
}
} finally {
close();
}
}
void start() {
List<CompletableFuture<String>> futures = new ArrayList<>();
for (int i = 1; i <= 1000; i++) {
String operationId = "Operation-" + i;
futures.add(asyncOperation(operationId));
}
CompletableFuture<?>[] futuresArray = futures.toArray(new CompletableFuture<?>[0]);
CompletableFuture<List<String>> listFuture = CompletableFuture.allOf(futuresArray).thenApply(v -> futures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
try {
final List<String> results = listFuture.join();
System.out.println("Printing first 10 results");
for (int i = 0; i < 10; i++) {
System.out.println("Finished " + results.get(i));
}
} finally {
close();
}
}
void close() {
asyncOperationEmulation.shutdownNow();
}
public static void main(String[] args) {
new Application().initialize()
// Switch between .startNaive() and .start() to test both implementations
// .startNaive();
.start();
}
}
@@ -0,0 +1,120 @@
package com.baeldung.concurrent.completablefuture.retry;
import static com.baeldung.concurrent.completablefuture.retry.RetryCompletableFuture.retryExceptionallyAsync;
import static com.baeldung.concurrent.completablefuture.retry.RetryCompletableFuture.retryNesting;
import static com.baeldung.concurrent.completablefuture.retry.RetryCompletableFuture.retryTask;
import static com.baeldung.concurrent.completablefuture.retry.RetryCompletableFuture.retryUnsafe;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class RetryCompletableFutureUnitTest {
private AtomicInteger retriesCounter = new AtomicInteger(0);
@BeforeEach
void beforeEach() {
retriesCounter.set(0);
}
@Test
void whenRetryingTask_thenReturnsCorrectlyAfterFourInvocations() {
Supplier<Integer> codeToRun = () -> failFourTimesThenReturn(100);
CompletableFuture<Integer> result = retryTask(codeToRun, 10);
assertThat(result.join()).isEqualTo(100);
assertThat(retriesCounter).hasValue(4);
}
@Test
void whenRetryingTask_thenThrowsExceptionAfterThreeInvocations() {
Supplier<Integer> codeToRun = () -> failFourTimesThenReturn(100);
CompletableFuture<Integer> result = retryTask(codeToRun, 3);
assertThatThrownBy(result::join)
.isInstanceOf(CompletionException.class)
.hasMessageContaining("IllegalStateException: Task failed after 3 attempts");
}
@Test
void whenRetryingExceptionally_thenReturnsCorrectlyAfterFourInvocations() {
Supplier<Integer> codeToRun = () -> failFourTimesThenReturn(100);
CompletableFuture<Integer> result = retryUnsafe(codeToRun, 10);
assertThat(result.join()).isEqualTo(100);
assertThat(retriesCounter).hasValue(4);
}
@Test
void whenRetryingExceptionally_thenThrowsExceptionAfterThreeInvocations() {
Supplier<Integer> codeToRun = () -> failFourTimesThenReturn(100);
CompletableFuture<Integer> result = retryUnsafe(codeToRun, 3);
assertThatThrownBy(result::join)
.isInstanceOf(CompletionException.class)
.hasMessageContaining("RuntimeException: task failed for 3 time(s)");
}
@Test
void whenRetryingExceptionallyAsync_thenReturnsCorrectlyAfterFourInvocations() {
Supplier<Integer> codeToRun = () -> failFourTimesThenReturn(100);
CompletableFuture<Integer> result = retryExceptionallyAsync(codeToRun, 10);
assertThat(result.join()).isEqualTo(100);
assertThat(retriesCounter).hasValue(4);
}
@Test
void whenRetryingExceptionallyAsync_thenThrowsExceptionAfterThreeInvocations() {
Supplier<Integer> codeToRun = () -> failFourTimesThenReturn(100);
CompletableFuture<Integer> result = retryExceptionallyAsync(codeToRun, 3);
assertThatThrownBy(result::join)
.isInstanceOf(CompletionException.class)
.hasMessageContaining("RuntimeException: task failed for 3 time(s)");
}
@Test
void whenRetryingNesting_thenReturnsCorrectlyAfterFourInvocations() {
Supplier<Integer> codeToRun = () -> failFourTimesThenReturn(100);
CompletableFuture<Integer> result = retryNesting(codeToRun, 10);
assertThat(result.join()).isEqualTo(100);
assertThat(retriesCounter).hasValue(4);
}
@Test
void whenRetryingNesting_thenThrowsExceptionAfterThreeInvocations() {
Supplier<Integer> codeToRun = () -> failFourTimesThenReturn(100);
CompletableFuture<Integer> result = retryNesting(codeToRun, 3);
assertThatThrownBy(result::join)
.isInstanceOf(CompletionException.class)
.hasMessageContaining("RuntimeException: task failed for 3 time(s)");
}
int failFourTimesThenReturn(int returnValue) {
int retryNr = retriesCounter.get();
System.out.println(String.format("invocation: %s, thread: %s", retryNr, Thread.currentThread().getName()));
if (retryNr < 4) {
retriesCounter.set(retryNr + 1);
throw new RuntimeException(String.format("task failed for %s time(s)", retryNr));
}
return returnValue;
}
}
@@ -12,6 +12,7 @@ This module contains articles about Java Concurrency that are also part of an Eb
- [Guide to the Volatile Keyword in Java](https://www.baeldung.com/java-volatile)
- [A Guide to the Java ExecutorService](https://www.baeldung.com/java-executor-service-tutorial)
- [Guide To CompletableFuture](https://www.baeldung.com/java-completablefuture)
- [How To Manage Timeout for CompletableFuture](https://www.baeldung.com/java-completablefuture-timeout)
### NOTE:
@@ -12,6 +12,15 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-concurrency-simple</finalName>
<resources>
@@ -0,0 +1,182 @@
package com.baeldung.concurrent.completablefuture;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.*;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CompletableFutureTimeoutUnitTest {
private WireMockServer wireMockServer;
private ScheduledExecutorService executorService;
private static final int DEFAULT_TIMEOUT = 1000; //1 seconds
private static final String DEFAULT_PRODUCT = """
{
"products": [
{
"id": 2,
"title": "iPhone X",
"description": "SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...",
"price": 899,
"discountPercentage": 0.0,
"rating": 4.44,
"stock": 34,
"brand": "Apple",
"category": "smartphones"
},
{
"id": 3,
"title": "Samsung Universe 9",
"description": "Samsung's new variant which goes beyond Galaxy to the Universe",
"price": 1249,
"discountPercentage": 0.0,
"rating": 4.09,
"stock": 36,
"brand": "Samsung",
"category": "smartphones"
}
],
"total": 2
}""";
private static final String PRODUCT_OFFERS = """
{
"products": [
{
"id": 1,
"title": "iPhone 9",
"description": "An apple mobile which is nothing like apple",
"price": 549,
"discountPercentage": 12.96,
"rating": 4.69,
"stock": 94,
"brand": "Apple",
"category": "smartphones"
},
{
"id": 2,
"title": "iPhone X",
"description": "SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...",
"price": 899,
"discountPercentage": 17.94,
"rating": 4.44,
"stock": 34,
"brand": "Apple",
"category": "smartphones"
},
{
"id": 3,
"title": "Samsung Universe 9",
"description": "Samsung's new variant which goes beyond Galaxy to the Universe",
"price": 1249,
"discountPercentage": 15.46,
"rating": 4.09,
"stock": 36,
"brand": "Samsung",
"category": "smartphones"
},
{
"id": 4,
"title": "OPPOF19",
"description": "OPPO F19 is officially announced on April 2021.",
"price": 280,
"discountPercentage": 17.91,
"rating": 4.3,
"stock": 123,
"brand": "OPPO",
"category": "smartphones"
},
{
"id": 5,
"title": "Huawei P30",
"description": "Huaweis re-badged P30 Pro New Edition was officially unveiled yesterday in Germany and now the device has made its way to the UK.",
"price": 499,
"discountPercentage": 10.58,
"rating": 4.09,
"stock": 32,
"brand": "Huawei",
"category": "smartphones"
}
],
"total": 5
}""";
@BeforeAll
void setUp() {
wireMockServer = new WireMockServer(8080);
wireMockServer.start();
WireMock.configureFor("localhost", 8080);
stubFor(get(urlEqualTo("/api/dummy"))
.willReturn(aResponse()
.withFixedDelay(5000) // must be > DEFAULT_TIMEOUT for a timeout to occur.
.withBody(PRODUCT_OFFERS)));
executorService = Executors.newScheduledThreadPool(1);
}
@AfterAll
void tearDown() {
executorService.shutdown();
wireMockServer.stop();
}
private CompletableFuture<String> fetchProductData() {
return CompletableFuture.supplyAsync(() -> {
try {
URL url = new URL("http://localhost:8080/api/dummy");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
return response.toString();
} finally {
connection.disconnect();
}
} catch (IOException e) {
return "";
}
});
}
@Test
void whenorTimeout_thenGetThrow() {
CompletableFuture<String> productDataFuture = fetchProductData();
productDataFuture.orTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
assertThrows(ExecutionException.class, productDataFuture::get);
}
@Test
void whencompleteOnTimeout_thenReturnValue() throws ExecutionException, InterruptedException {
CompletableFuture<String> productDataFuture = fetchProductData();
productDataFuture.completeOnTimeout(DEFAULT_PRODUCT, DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
assertEquals(DEFAULT_PRODUCT, productDataFuture.get());
}
@Test
void whencompleteExceptionally_thenGetThrow() {
CompletableFuture<String> productDataFuture = fetchProductData();
executorService.schedule(() -> productDataFuture
.completeExceptionally(new TimeoutException("Timeout occurred")), DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
assertThrows(ExecutionException.class, productDataFuture::get);
}
}
@@ -0,0 +1,61 @@
package com.baeldung.firstdaymonth;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.junit.jupiter.api.Test;
public class FirstDayOfMonthUnitTest {
@Test
void givenMonth_whenUsingCalendar_thenReturnFirstDay() {
Date currentDate = new GregorianCalendar(2023, Calendar.NOVEMBER, 23).getTime();
Date expectedDate = new GregorianCalendar(2023, Calendar.NOVEMBER, 1).getTime();
Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
cal.set(Calendar.DAY_OF_MONTH, 1);
assertEquals(expectedDate, cal.getTime());
}
@Test
void givenMonth_whenUsingLocalDate_thenReturnFirstDay() {
LocalDate currentDate = LocalDate.of(2023, 9, 6);
LocalDate expectedDate = LocalDate.of(2023, 9, 1);
assertEquals(expectedDate, currentDate.withDayOfMonth(1));
}
@Test
void givenMonth_whenUsingTemporalAdjusters_thenReturnFirstDay() {
LocalDate currentDate = LocalDate.of(2023, 7, 19);
LocalDate expectedDate = LocalDate.of(2023, 7, 1);
assertEquals(expectedDate, currentDate.with(TemporalAdjusters.firstDayOfMonth()));
}
@Test
void givenMonth_whenUsingYearMonth_thenReturnFirstDay() {
YearMonth currentDate = YearMonth.of(2023, 4);
LocalDate expectedDate = LocalDate.of(2023, 4, 1);
assertEquals(expectedDate, currentDate.atDay(1));
}
@Test
void givenMonth_whenUsingJodaTime_thenReturnFirstDay() {
org.joda.time.LocalDate currentDate = org.joda.time.LocalDate.parse("2023-5-10");
org.joda.time.LocalDate expectedDate = org.joda.time.LocalDate.parse("2023-5-1");
assertEquals(expectedDate, currentDate.dayOfMonth()
.withMinimumValue());
}
}
+16
View File
@@ -35,6 +35,21 @@
<artifactId>simplemagic</artifactId>
<version>${simplemagic.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</dependency>
</dependencies>
<build>
@@ -62,5 +77,6 @@
<jmime-magic.version>0.1.5</jmime-magic.version>
<jodd-util.version>6.2.1</jodd-util.version>
<simplemagic.version>1.17</simplemagic.version>
<jmh.version>1.37</jmh.version>
</properties>
</project>
@@ -0,0 +1,112 @@
package com.baeldung.zip;
import java.io.*;
import java.util.concurrent.TimeUnit;
import java.util.*;
import java.util.zip.*;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 1, time = 2, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(value = 1)
public class ZipBenchmark {
public static final int NUM_OF_FILES = 10;
public static final int DATA_SIZE = 204800;
@State(Scope.Thread)
public static class SourceState {
public File compressedFile;
@Setup(Level.Trial)
public void setup() throws IOException {
ZipSampleFileStore sampleFileStore = new ZipSampleFileStore(NUM_OF_FILES, DATA_SIZE);
compressedFile = sampleFileStore.getFile();
}
@TearDown(Level.Trial)
public void cleanup() {
if (compressedFile.exists()) {
compressedFile.delete();
}
}
}
@Benchmark
public static void readAllEntriesByZipFile(SourceState sourceState, Blackhole blackhole) throws IOException {
try (ZipFile zipFile = new ZipFile(sourceState.compressedFile)) {
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
while (zipEntries.hasMoreElements()) {
ZipEntry zipEntry = zipEntries.nextElement();
try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry))) {
blackhole.consume(ZipSampleFileStore.getString(inputStream));
}
}
}
}
@Benchmark
public static void readAllEntriesByZipInputStream(SourceState sourceState, Blackhole blackhole) throws IOException {
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceState.compressedFile));
ZipInputStream zipInputStream = new ZipInputStream(bis)
) {
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
blackhole.consume(ZipSampleFileStore.getString(zipInputStream));
}
}
}
@Benchmark
public static void readLastEntryByZipFile(SourceState sourceState, Blackhole blackhole) throws IOException {
try (ZipFile zipFile = new ZipFile(sourceState.compressedFile)) {
ZipEntry zipEntry = zipFile.getEntry(getLastEntryName());
try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry))) {
blackhole.consume(ZipSampleFileStore.getString(inputStream));
}
}
}
@Benchmark
public static void readLastEntryByZipInputStream(SourceState sourceState, Blackhole blackhole) throws IOException {
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceState.compressedFile));
ZipInputStream zipInputStream = new ZipInputStream(bis)
) {
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
if (Objects.equals(entry.getName(), getLastEntryName())){
blackhole.consume(ZipSampleFileStore.getString(zipInputStream));
}
}
}
}
private static String getLastEntryName() {
return String.format(ZipSampleFileStore.ENTRY_NAME_PATTERN, NUM_OF_FILES);
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(ZipBenchmark.class.getSimpleName()).threads(1)
.shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}
@@ -0,0 +1,70 @@
package com.baeldung.zip;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
public class ZipSampleFileStore {
public static final String ENTRY_NAME_PATTERN = "str-data-%s.txt";
private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
private final File file;
private final List<String> dataList;
public ZipSampleFileStore(int numOfFiles, int fileSize) throws IOException {
dataList = new ArrayList<>(numOfFiles);
file = File.createTempFile("zip-sample", "");
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file))) {
for (int idx=0; idx<=numOfFiles; idx++) {
byte[] data = createRandomStringInByte(fileSize);
dataList.add(new String(data, DEFAULT_ENCODING));
ZipEntry entry = new ZipEntry(String.format(ENTRY_NAME_PATTERN, idx));
zos.putNextEntry(entry);
zos.write(data);
zos.closeEntry();
}
}
}
public static byte[] createRandomStringInByte(int size) {
Random random = new Random();
byte[] data = new byte[size];
for (int n = 0; n < data.length; n++) {
char randomChar;
int choice = random.nextInt(2); // 0 for uppercase, 1 for lowercase
if (choice == 0) {
randomChar = (char) ('A' + random.nextInt(26)); // 'A' to 'Z'
} else {
randomChar = (char) ('a' + random.nextInt(26)); // 'a' to 'z'
}
data[n] = (byte) randomChar;
}
return data;
}
public File getFile() {
return file;
}
public List<String> getDataList() {
return dataList;
}
public static String getString(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, byteArrayOutputStream);
return byteArrayOutputStream.toString(DEFAULT_ENCODING);
}
}
@@ -0,0 +1,83 @@
package com.baeldung.zip;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import org.junit.*;
public class ZipUnitTest {
private static File compressedFile;
private static List<String> dataList = new ArrayList<>();
@BeforeClass
public static void prepareData() throws IOException {
ZipSampleFileStore sampleFileStore = new ZipSampleFileStore(ZipBenchmark.NUM_OF_FILES, ZipBenchmark.DATA_SIZE);
compressedFile = sampleFileStore.getFile();
dataList = sampleFileStore.getDataList();
}
@Test
public void whenCreateZipFile_thenCompressedSizeShouldBeLessThanOriginal() throws IOException {
byte[] data = ZipSampleFileStore.createRandomStringInByte(10240);
File file = File.createTempFile("zip-temp", "");
try (
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
ZipOutputStream zos = new ZipOutputStream(bos)
) {
ZipEntry zipEntry = new ZipEntry("zip-entry.txt");
zos.putNextEntry(zipEntry);
zos.write(data);
zos.closeEntry();
assertThat(file.length()).isLessThan(data.length);
}
finally {
file.delete();
}
}
@Test
public void whenReadAllEntriesViaZipFile_thenDataIsEqualtoTheSource() throws IOException {
try (ZipFile zipFile = new ZipFile(compressedFile)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
List<? extends ZipEntry> entryList = Collections.list(entries);
for (int idx=0; idx<entryList.size(); idx++) {
ZipEntry zipEntry = entryList.get(idx);
try (InputStream inputStream = zipFile.getInputStream(zipEntry)) {
String actual = ZipSampleFileStore.getString(inputStream);
assertThat(actual).as("Data for ZIP entry: " + zipEntry.getName()).isEqualTo(dataList.get(idx));
}
}
}
}
@Test
public void whenReadAllEntriesViaZipInputStream_thenDataIsEqualtoTheSource() throws IOException {
int idx = 0;
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(compressedFile));
ZipInputStream zipInputStream = new ZipInputStream(bis)
) {
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String actual = ZipSampleFileStore.getString(zipInputStream);
assertThat(actual).as("Data for ZIP entry: " + zipEntry.getName()).isEqualTo(dataList.get(idx++));
}
}
}
@AfterClass
public static void cleanup() {
if (compressedFile.exists()) {
compressedFile.delete();
}
}
}
@@ -0,0 +1,2 @@
I'm going to Alabama
Alabama is a state in the US
@@ -0,0 +1 @@
Dreams from My Father by Barack Obama
@@ -0,0 +1 @@
Harry Potter and the Chamber of Secrets
@@ -0,0 +1,64 @@
package com.baeldung.printwritervsfilewriter;
import static org.junit.jupiter.api.Assertions.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.junit.jupiter.api.Test;
public class PrintWriterVsFilePrinterUnitTest {
@Test
public void whenWritingToTextFileUsingFileWriter_thenTextMatches() throws IOException {
String result = "Harry Potter and the Chamber of Secrets";
File file = new File("potter.txt");
try (FileWriter fw = new FileWriter(file);) {
fw.write("Harry Potter and the Chamber of Secrets");
}
try (BufferedReader reader = new BufferedReader(new FileReader(file));) {
String actualData = reader.readLine();
assertEquals(result, actualData);
}
}
@Test
public void whenWritingToTextFileUsingPrintWriterPrintf_thenTextMatches() throws IOException {
String result = "Dreams from My Father by Barack Obama";
File file = new File("dream.txt");
try (PrintWriter pw = new PrintWriter(file);) {
String author = "Barack Obama";
pw.printf("Dreams from My Father by %s", author);
assertTrue(!pw.checkError());
}
try (BufferedReader reader = new BufferedReader(new FileReader(file));) {
String actualData = reader.readLine();
assertEquals(result, actualData);
}
}
@Test
public void whenWritingToTextFileUsingPrintWriterPrintln_thenTextMatches() throws IOException {
String result = "I'm going to Alabama\nAlabama is a state in the US\n";
try (PrintWriter pw = new PrintWriter("alabama.txt");) {
pw.println("I'm going to Alabama");
pw.println("Alabama is a state in the US");
}
Path path = Paths.get("alabama.txt");
String actualData = new String(Files.readAllBytes(path));
assertEquals(result, actualData);
}
}
@@ -0,0 +1,73 @@
package com.baeldung.truncatedouble;
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.junit.Test;
public class TruncateDoubleUnitTest {
@Test
public void givenADouble_whenUsingDecimalFormat_truncateToTwoDecimalPlaces() {
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.DOWN);
double value = 1.55555555;
String truncated = df.format(value);
assertEquals("1.55", truncated);
double negativeValue = -1.55555555;
String negativeTruncated = df.format(negativeValue);
assertEquals("-1.55", negativeTruncated);
}
@Test
public void givenADouble_whenUsingNumberFormat_truncateToTwoDecimalPlaces() {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setRoundingMode(RoundingMode.DOWN);
double value = 1.55555555;
String truncated = nf.format(value);
assertEquals("1.55", truncated);
double negativeValue = -1.55555555;
String negativeTruncated = nf.format(negativeValue);
assertEquals("-1.55", negativeTruncated);
}
@Test
public void givenADouble_whenUsingBigDecimal_truncateToTwoDecimalPlaces() {
BigDecimal positive = new BigDecimal(2.555555).setScale(2, RoundingMode.DOWN);
BigDecimal negative = new BigDecimal(-2.555555).setScale(2, RoundingMode.DOWN);
assertEquals("2.55", positive.toString());
assertEquals("-2.55", negative.toString());
}
@Test
public void givenADouble_whenUsingMath_truncateToTwoDecimalPlaces() {
double positive = 1.55555555;
double truncated = Math.floor(positive * 100) / 100;
assertEquals("1.55", String.valueOf(truncated));
double negative = -1.55555555;
double negativeTruncated = Math.ceil(negative * 100) / 100;
assertEquals("-1.55", String.valueOf(negativeTruncated));
}
@Test
public void givenADouble_whenUsingStringFormat_truncateToTwoDecimalPlaces() {
double positive = 1.55555555;
String truncated = String.format("%.2f", positive);
assertEquals("1.56", truncated);
double negative = -1.55555555;
String negativeTruncated = String.format("%.2f", negative);
assertEquals("-1.56", negativeTruncated);
}
}
@@ -7,23 +7,27 @@ public class FloatDoubleConversionsTest {
@Test
public void whenDoubleType_thenFloatTypeSuccess(){
double interestRatesYearly = 13.333333333333334;
double interestRatesYearly = 13.333333333333333;
float interest = (float) interestRatesYearly;
Assert.assertEquals(13.333333f, interest, 0.000004f);
System.out.println(interest); //13.333333
Assert.assertEquals(13.333333f, interest, 0.000001f);
Double monthlyRates = 2.111111111111112;
Double monthlyRates = 2.111111111111111;
float rates = monthlyRates.floatValue();
Assert.assertEquals(2.1111112f, rates, 0.00000013);
System.out.println(rates); //2.1111112
Assert.assertEquals(2.1111111f, rates, 0.0000001f);
}
@Test
public void whenFloatType_thenDoubleTypeSuccess(){
float gradeAverage =2.05f;
double average = gradeAverage;
Assert.assertEquals(2.05, average, 0.06);
System.out.println(average); //2.049999952316284
Assert.assertEquals(2.05, average, 0.01);
Float monthlyRates = 2.1111112f;
Float monthlyRates = 2.1111111f;
Double rates = monthlyRates.doubleValue();
Assert.assertEquals(2.11111112, rates, 0.0000002);//true
System.out.println(rates); //2.1111111640930176
Assert.assertEquals(2.11111111, rates, 0.0000001);//true
}
}
@@ -85,7 +85,7 @@ public class AlphanumericPerformanceBenchmark {
public boolean isAlphanumeric(final int codePoint) {
return (codePoint >= 65 && codePoint <= 90) ||
(codePoint >= 97 && codePoint <= 172) ||
(codePoint >= 97 && codePoint <= 122) ||
(codePoint >= 48 && codePoint <= 57);
}
}
@@ -4,3 +4,4 @@
- [Aggregate Runtime Exceptions in Java Streams](https://www.baeldung.com/java-streams-aggregate-exceptions)
- [Streams vs. Loops in Java](https://www.baeldung.com/java-streams-vs-loops)
- [Partition a Stream in Java](https://www.baeldung.com/java-partition-stream)
- [Taking Every N-th Element from Finite and Infinite Streams in Java](https://www.baeldung.com/java-nth-element-finite-infinite-streams)
@@ -13,3 +13,4 @@ This module contains articles about string-related algorithms.
- [Find the Most Frequent Characters in a String](https://www.baeldung.com/java-string-find-most-frequent-characters)
- [Checking If a String Is a Repeated Substring](https://www.baeldung.com/java-repeated-substring)
- [Check if Letter Is Emoji With Java](https://www.baeldung.com/java-check-letter-emoji)
- [Wrapping a String After a Number of Characters Word-Wise](https://www.baeldung.com/java-wrap-string-number-characters-word-wise)
@@ -3,3 +3,5 @@
- [Convert String to Int Using Encapsulation](https://www.baeldung.com/java-encapsulation-convert-string-to-int)
- [HashMap with Multiple Values for the Same Key](https://www.baeldung.com/java-hashmap-multiple-values-per-key)
- [Split Java String Into Key-Value Pairs](https://www.baeldung.com/java-split-string-map)
- [How to Center Text Output in Java](https://www.baeldung.com/java-center-text-output)
- [How to Convert an Object to String](https://www.baeldung.com/java-object-string-representation)
@@ -54,6 +54,11 @@
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-core.version}</version>
</dependency>
</dependencies>
<build>
@@ -95,6 +100,7 @@
<javax.el-api.version>3.0.0</javax.el-api.version>
<javax.el.version>2.2.6</javax.el.version>
<commons-codec.version>1.14</commons-codec.version>
<spring-core.version>5.3.0</spring-core.version>
</properties>
</project>
@@ -3,6 +3,7 @@ package com.baeldung.emptystrings;
import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.util.ObjectUtils;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
@@ -109,6 +110,24 @@ public class EmptyStringsUnitTest {
assertFalse(Strings.isNullOrEmpty(blankString));
}
/*
* Spring Core ObjectUtils
*/
@Test
public void givenSomeEmptyString_thenObjectUtilsIsEmptyReturnsTrue() {
assertTrue(ObjectUtils.isEmpty(emptyString));
}
@Test
public void givenSomeNonEmptyString_thenObjectUtilsIsEmptyReturnsFalse() {
assertFalse(ObjectUtils.isEmpty(nonEmptyString));
}
@Test
public void givenSomeBlankString_thenObjectUtilsIsEmptyReturnsFalse() {
assertFalse(ObjectUtils.isEmpty(blankString));
}
/*
* Bean Validation
*/
@@ -11,4 +11,4 @@
- [Check if a String Has All Unique Characters in Java](https://www.baeldung.com/java-check-string-all-unique-chars)
- [Performance Comparison Between Different Java String Concatenation Methods](https://www.baeldung.com/java-string-concatenation-methods)
- [Replacing Single Quote with \ in Java String](https://www.baeldung.com/java-replacing-single-quote-string)
- [Check if a String Contains a Number Value in Java](https://www.baeldung.com/java-string-number-presence)
@@ -1,2 +1,4 @@
### Relevant Articles:
- [How to Center Text Output in Java](https://www.baeldung.com/java-center-text-output)
- [Capitalize the First Letter of Each Word in a String](https://www.baeldung.com/java-string-initial-capital-letter-every-word)
@@ -1,72 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-operations-7</artifactId>
<name>core-java-string-operations-7</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<apache.commons.lang3.version>3.12.0</apache.commons.lang3.version>
<commons-text.version>1.10.0</commons-text.version>
</properties>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-operations-7</artifactId>
<name>core-java-string-operations-7</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<apache.commons.lang3.version>3.13.0</apache.commons.lang3.version>
<commons-text.version>1.10.0</commons-text.version>
</properties>
</project>
@@ -0,0 +1,40 @@
package com.baeldung.capitalizefirstcharactereachword;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
class CapitalizeFirstCharacterEachWordUtils {
static String usingCharacterToUpperCaseMethod(String input) {
if (input == null || input.isEmpty()) {
return null;
}
return Arrays.stream(input.split("\\s+"))
.map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1))
.collect(Collectors.joining(" "));
}
static String usingStringToUpperCaseMethod(String input) {
if (input == null || input.isEmpty()) {
return null;
}
return Arrays.stream(input.split("\\s+"))
.map(word -> word.substring(0, 1).toUpperCase() + word.substring(1))
.collect(Collectors.joining(" "));
}
static String usingStringUtilsClass(String input) {
if (input == null || input.isEmpty()) {
return null;
}
return Arrays.stream(input.split("\\s+"))
.map(StringUtils::capitalize)
.collect(Collectors.joining(" "));
}
}
@@ -0,0 +1,77 @@
package com.baeldung.strcontainsnumber;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.CharMatcher;
public class StrContainsNumberUtils {
static boolean checkUsingMatchesMethod(String input) {
if (input == null || input.isEmpty()) {
return false;
}
return input.matches(".*\\d.*");
}
static boolean checkUsingPatternClass(String input) {
if (input == null || input.isEmpty()) {
return false;
}
return Pattern.compile(".*\\d.*")
.matcher(input)
.matches();
}
static boolean checkUsingReplaceAllMethod(String input) {
if (input == null || input.isEmpty()) {
return false;
}
String result = input.replaceAll("\\d", "");
return result.length() != input.length();
}
static boolean checkUsingIsDigitMethod(String input) {
if (input == null || input.isEmpty()) {
return false;
}
for (char c : input.toCharArray()) {
if (Character.isDigit(c)) {
return true;
}
}
return false;
}
static boolean checkUsingStreamApi(String input) {
if (input == null || input.isEmpty()) {
return false;
}
return input.chars()
.anyMatch(Character::isDigit);
}
static boolean checkUsingApacheCommonsLang(String input) {
String result = StringUtils.getDigits(input);
return result != null && !result.isEmpty();
}
static boolean checkUsingGuava(String input) {
if (input == null || input.isEmpty()) {
return false;
}
String result = CharMatcher.forPredicate(Character::isDigit)
.retainFrom(input);
return !result.isEmpty();
}
}
@@ -0,0 +1,30 @@
package com.baeldung.unicode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
public class UnicodeLetterChecker {
public boolean characterClassCheck(String input) {
for (char c : input.toCharArray()) {
if (!Character.isLetter(c)) {
return false;
}
}
return true;
}
public boolean regexCheck(String input) {
Pattern pattern = Pattern.compile("^\\p{L}+$");
Matcher matcher = pattern.matcher(input);
return matcher.matches();
}
public boolean isAlphaCheck(String input) {
return StringUtils.isAlpha(input);
}
public boolean StreamsCheck(String input) {
return input.codePoints().allMatch(Character::isLetter);
}
}
@@ -0,0 +1,38 @@
package com.baeldung.capitalizefirstcharactereachword;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.commons.text.WordUtils;
import org.junit.jupiter.api.Test;
class CapitalizeFirstCharacterEachWordUtilsUnitTest {
@Test
void givenString_whenUsingCharacterToUpperCaseMethod_thenCapitalizeFirstCharacter() {
String input = "hello baeldung visitors";
assertEquals("Hello Baeldung Visitors", CapitalizeFirstCharacterEachWordUtils.usingCharacterToUpperCaseMethod(input));
}
@Test
void givenString_whenUsingSubstringMethod_thenCapitalizeFirstCharacter() {
String input = "Hi, my name is azhrioun";
assertEquals("Hi, My Name Is Azhrioun", CapitalizeFirstCharacterEachWordUtils.usingStringToUpperCaseMethod(input));
}
@Test
void givenString_whenUsingStringUtilsClass_thenCapitalizeFirstCharacter() {
String input = "life is short the world is wide";
assertEquals("Life Is Short The World Is Wide", CapitalizeFirstCharacterEachWordUtils.usingStringUtilsClass(input));
}
@Test
void givenString_whenUsingWordUtilsClass_thenCapitalizeFirstCharacter() {
String input = "smile sunshine is good for your teeth";
assertEquals("Smile Sunshine Is Good For Your Teeth", WordUtils.capitalizeFully(input));
}
}
@@ -0,0 +1,69 @@
package com.baeldung.strcontainsnumber;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.Test;
public class StrContainsNumberUtilsUnitTest {
private static final String INPUT_WITH_NUMBERS = "We hope 2024 will be great";
private static final String INPUT_WITHOUT_NUMBERS = "Hello world";
@Test
public void givenInputString_whenUsingMatchesMethod_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingMatchesMethod(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingMatchesMethod(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingMatchesMethod(""));
assertFalse(StrContainsNumberUtils.checkUsingMatchesMethod(null));
}
@Test
public void givenInputString_whenUsingPatternClass_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingPatternClass(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingPatternClass(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingPatternClass(""));
assertFalse(StrContainsNumberUtils.checkUsingPatternClass(null));
}
@Test
public void givenInputString_whenUsingReplaceAllMethod_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingReplaceAllMethod(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingReplaceAllMethod(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingReplaceAllMethod(""));
assertFalse(StrContainsNumberUtils.checkUsingReplaceAllMethod(null));
}
@Test
public void givenInputString_whenUsingIsDigitMethod_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingIsDigitMethod(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingIsDigitMethod(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingIsDigitMethod(""));
assertFalse(StrContainsNumberUtils.checkUsingIsDigitMethod(null));
}
@Test
public void givenInputString_whenUsingStreamApi_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingStreamApi(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingStreamApi(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingStreamApi(""));
assertFalse(StrContainsNumberUtils.checkUsingStreamApi(null));
}
@Test
public void givenInputString_whenUsingApacheCommonsLang_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingApacheCommonsLang(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingApacheCommonsLang(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingApacheCommonsLang(""));
assertFalse(StrContainsNumberUtils.checkUsingApacheCommonsLang(null));
}
@Test
public void givenInputString_whenUsingGuava_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingGuava(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingGuava(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingGuava(""));
assertFalse(StrContainsNumberUtils.checkUsingGuava(null));
}
}
@@ -0,0 +1,38 @@
package com.baeldung.unicode;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class UnicodeLetterCheckerUnitTest {
@Test
public void givenString_whenUsingIsLetter_thenReturnTrue() {
UnicodeLetterChecker checker = new UnicodeLetterChecker();
boolean isUnicodeLetter = checker.characterClassCheck("HelloWorld");
assertTrue(isUnicodeLetter);
}
@Test
public void givenString_whenUsingRegex_thenReturnTrue() {
UnicodeLetterChecker checker = new UnicodeLetterChecker();
boolean isUnicodeLetter = checker.regexCheck("HelloWorld");
assertTrue(isUnicodeLetter);
}
@Test
public void givenString_whenUsingIsAlpha_thenReturnTrue() {
UnicodeLetterChecker checker = new UnicodeLetterChecker();
boolean isUnicodeLetter = checker.isAlphaCheck("HelloWorld");
assertTrue(isUnicodeLetter);
}
@Test
public void givenString_whenUsingStreams_thenReturnTrue() {
UnicodeLetterChecker checker = new UnicodeLetterChecker();
boolean isUnicodeLetter = checker.StreamsCheck("HelloWorld");
assertTrue(isUnicodeLetter);
}
}
@@ -0,0 +1,14 @@
package com.baeldung.passstringbyreference;
public class Dummy {
String dummyString;
public String getDummyString() {
return dummyString;
}
public void setDummyString(String dummyString) {
this.dummyString = dummyString;
}
}
@@ -0,0 +1,85 @@
package com.baeldung.passstringbyreference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
class PassStringUnitTest {
@Test
void givenAString_whenPassedToVoidMethod_thenStringIsNotModified() {
String s = "hello";
concatStringWithNoReturn(s);
assertEquals("hello", s);
}
void concatStringWithNoReturn(String input) {
input += " world";
assertEquals("hello world", input);
}
@Test
void givenAString_whenPassedToMethodAndReturnNewString_thenStringIsModified() {
String s = "hello";
assertEquals("hello world", concatStringWithReturn(s));
}
String concatStringWithReturn(String input) {
return input + " world";
}
@Test
void givenAString_whenPassStringBuilderToVoidMethod_thenConcatNewStringOk() {
StringBuilder builder = new StringBuilder("hello");
concatWithStringBuilder(builder);
assertEquals("hello world", builder.toString());
}
void concatWithStringBuilder(StringBuilder input) {
input.append(" world");
}
@Test
void givenAString_whenPassStringBufferToVoidMethod_thenConcatNewStringOk() {
StringBuffer builder = new StringBuffer("hello");
concatWithStringBuffer(builder);
assertEquals("hello world", builder.toString());
}
void concatWithStringBuffer(StringBuffer input) {
input.append(" world");
}
@Test
void givenObjectWithStringField_whenSetDifferentValue_thenObjectIsModified() {
Dummy dummy = new Dummy();
assertNull(dummy.getDummyString());
modifyStringValueInInputObject(dummy, "hello world");
assertEquals("hello world", dummy.getDummyString());
}
void modifyStringValueInInputObject(Dummy dummy, String dummyString) {
dummy.setDummyString(dummyString);
}
@Test
void givenObjectWithStringField_whenSetDifferentValueWithStringBuilder_thenSetStringInNewObject() {
assertEquals("hello world", getDummy("hello", "world").getDummyString());
}
Dummy getDummy(String hello, String world) {
StringBuilder builder = new StringBuilder();
builder.append(hello)
.append(" ")
.append(world);
Dummy dummy = new Dummy();
dummy.setDummyString(builder.toString());
return dummy;
}
}
+1
View File
@@ -73,6 +73,7 @@
<module>core-java-arrays-multidimensional</module>
<module>core-java-arrays-convert</module>
<module>core-java-arrays-operations-basic</module>
<module>core-java-arrays-operations-basic-2</module>
<module>core-java-arrays-operations-advanced</module>
<module>core-java-arrays-operations-advanced-2</module>
<module>core-java-booleans</module>