Merge branch 'eugenp:master' into master

This commit is contained in:
press0
2022-11-08 07:17:12 -06:00
committed by GitHub
552 changed files with 36042 additions and 1920 deletions

View File

@@ -0,0 +1,43 @@
package com.baeldung.arrayslicing;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class SlicingArrayUnitTest {
private static final String[] LANGUAGES = new String[] { "Python", "Java", "Kotlin", "Scala", "Ruby", "Go", "Rust" };
private static final String[] JVM_LANGUAGES = new String[] { "Java", "Kotlin", "Scala" };
@Test
void givenAnArray_whenSlicingUsingStream_shouldGetExpectedResult() {
String[] result = Arrays.stream(LANGUAGES, 1, 4).toArray(String[]::new);
assertArrayEquals(JVM_LANGUAGES, result);
}
@Test
void givenAnArray_whenSlicingUsingArraysCopyOfRange_shouldGetExpectedResult() {
String[] result = Arrays.copyOfRange(LANGUAGES, 1, 4);
assertArrayEquals(JVM_LANGUAGES, result);
}
@Test
void givenAnArray_whenSlicingUsingSystemArraycopy_shouldGetExpectedResult() {
String[] result = new String[3];
System.arraycopy(LANGUAGES, 1, result, 0, 3);
assertArrayEquals(JVM_LANGUAGES, result);
String[] result2 = new String[] { "value one", "value two", "value three", "value four", "value five", "value six", "value seven" };
System.arraycopy(LANGUAGES, 1, result2, 2, 3);
assertArrayEquals(new String[] { "value one", "value two", "Java", "Kotlin", "Scala", "value six", "value seven" }, result2);
}
@Test
void givenAnArray_whenSlicingUsingArrayUtils_shouldGetExpectedResult() {
String[] result = ArrayUtils.subarray(LANGUAGES, 1, 4);
assertArrayEquals(JVM_LANGUAGES, result);
}
}

View File

@@ -10,3 +10,4 @@ This module contains articles about Java array fundamentals. They assume no prev
- [Removing an Element from an Array in Java](https://www.baeldung.com/java-array-remove-element)
- [Removing the First Element of an Array](https://www.baeldung.com/java-array-remove-first-element)
- [Extending an Arrays Length](https://www.baeldung.com/java-array-add-element-at-the-end)
- [Initializing a Boolean Array in Java](https://www.baeldung.com/java-initializing-boolean-array)

View File

@@ -10,3 +10,4 @@
- [Create an Empty Map in Java](https://www.baeldung.com/java-create-empty-map)
- [Sorting Objects in a List by Date](https://www.baeldung.com/java-sort-list-by-date)
- [Fixed Size Queue Implementations in Java](https://www.baeldung.com/java-fixed-size-queue)
- [Difference Between Java Enumeration and Iterator](https://www.baeldung.com/java-enumeration-vs-iterator)

View File

@@ -0,0 +1,18 @@
package com.baeldung.enumerationiteratordifferences;
import java.util.Arrays;
import java.util.List;
public final class DataUtil {
private DataUtil() {
}
static List<Person> getPersons() {
Person person1 = new Person("amit", "kumar");
Person person2 = new Person("yogi", "kuki");
Person person3 = new Person("raj", "dosi");
Person person4 = new Person("prakash", "kumar");
return Arrays.asList(person1, person2, person3, person4);
}
}

View File

@@ -0,0 +1,19 @@
package com.baeldung.enumerationiteratordifferences;
import java.util.Enumeration;
import java.util.Vector;
import static com.baeldung.enumerationiteratordifferences.DataUtil.getPersons;
public class EnumerationExample {
public static void main(String[] args) {
Vector<Person> people = new Vector<>(getPersons());
Enumeration<Person> enumeration = people.elements();
while (enumeration.hasMoreElements()) {
System.out.println("First Name = " + enumeration.nextElement()
.getFirstName());
}
}
}

View File

@@ -0,0 +1,17 @@
package com.baeldung.enumerationiteratordifferences;
import java.util.Iterator;
import java.util.List;
import static com.baeldung.enumerationiteratordifferences.DataUtil.getPersons;
public class IteratorExample {
public static void main(String[] args) {
List<Person> persons = getPersons();
Iterator<Person> iterator = persons.iterator();
while (iterator.hasNext()) {
System.out.println("First Name = " + iterator.next()
.getFirstName());
}
}
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.enumerationiteratordifferences;
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
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;
}
}

View File

@@ -27,15 +27,13 @@ class ListToStringUnitTest {
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
StringJoiner stringJoiner = new StringJoiner(",");
arraysAsList.stream()
.forEach(v -> stringJoiner.add(v));
arraysAsList.forEach(stringJoiner::add);
String commaSeparatedString = stringJoiner.toString();
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
StringJoiner stringJoinerWithDelimiterPrefixSuffix = new StringJoiner(",", "[", "]");
arraysAsList.stream()
.forEach(v -> stringJoinerWithDelimiterPrefixSuffix.add(v));
arraysAsList.forEach(stringJoinerWithDelimiterPrefixSuffix::add);
String commaSeparatedStringWithDelimiterPrefixSuffix = stringJoinerWithDelimiterPrefixSuffix.toString();
assertThat(commaSeparatedStringWithDelimiterPrefixSuffix).isEqualTo("[ONE,TWO,THREE]");

View File

@@ -54,11 +54,11 @@
</dependencies>
<properties>
<streamex.version>0.6.5</streamex.version>
<streamex.version>0.8.1</streamex.version>
<avaitility.version>1.7.0</avaitility.version>
<eclipse-collections.version>8.2.0</eclipse-collections.version>
<hppc.version>0.7.2</hppc.version>
<fastutil.version>8.1.0</fastutil.version>
</properties>
</project>
</project>

View File

@@ -15,6 +15,11 @@
</parent>
<dependencies>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
@@ -62,8 +67,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

View File

@@ -0,0 +1,43 @@
package com.baeldung.multivaluedmap;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
// Unit test for MultivaluedMap.
public class MultivaluedMapUnitTest {
// Testing MultivaluedHashMap.
@Test
public void givenMultivaluedHashMap_whenEquals_thenTrue() {
jakarta.ws.rs.core.MultivaluedMap<String, Integer> mulmap = new jakarta.ws.rs.core.MultivaluedHashMap<>();
// Mapping keys to values.
mulmap.addAll("first", 1, 2, 3);
mulmap.add(null, null);
assertNotNull(mulmap, "The MultivaluedHashMap is null!");
assertEquals(1, mulmap.getFirst("first"), "The key isn't mapped to the right values!");
assertEquals(null, mulmap.getFirst(null), "MultivaluedHashMap didn't accept null!");
}
// Testing HashMap.
@Test
public void givenHashMap_whenEquals_thenTrue() {
Map<String, Integer> map = new HashMap<>();
// Putting key-value pairs into our map.
map.put("first", 1);
map.put(null, 2);
map.put("third", null);
assertNotNull(map, "The HashMap is null!");
assertEquals(1, map.get("first"), "The key isn't mapped to the right value!");
assertEquals(2, map.get(null), "HashMap didn't accept null as key!");
assertEquals(null, map.get("third"), "HashMap didn't accept null value!");
}
}

View File

@@ -0,0 +1,4 @@
## Relevant articles
- [Using Streams to Collect Into a TreeSet](https://www.baeldung.com/java-stream-collect-into-treeset)
- [A Guide to LinkedHashSet in Java](https://www.baeldung.com/java-linkedhashset)

View File

@@ -0,0 +1,39 @@
<?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-collections-set-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>core-java-collections-set-2</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>

View File

@@ -0,0 +1,36 @@
package com.baeldung.treeset.collectintotreeset;
public class Player implements Comparable<Player> {
private String name;
private int age;
private int numberOfPlayed;
private int numberOfWins;
public Player(String name, int age, int numberOfPlayed, int numberOfWins) {
this.name = name;
this.age = age;
this.numberOfPlayed = numberOfPlayed;
this.numberOfWins = numberOfWins;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getNumberOfPlayed() {
return numberOfPlayed;
}
public int getNumberOfWins() {
return numberOfWins;
}
@Override
public int compareTo(Player o) {
return Integer.compare(age, o.age);
}
}

View File

@@ -0,0 +1,141 @@
package com.baeldung.linkedhashset;
import org.junit.jupiter.api.Test;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.*;
public class LinkedHashSetUnitTest{
@Test
void whenCreatingLinkedHashSet_shouldBeEmpty(){
Set<String> linkedHashSet = new LinkedHashSet<>();
assertTrue(linkedHashSet.isEmpty());
}
@Test
void whenCreatingLinkedHashSetWithInitialCapacity_shouldBeEmpty(){
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>(20);
assertTrue(linkedHashSet.isEmpty());
}
@Test
void whenCreatingLinkedHashSetWithExistingCollection_shouldContainAllElementOfCollection(){
Collection<String> data = Arrays.asList("first", "second", "third", "fourth", "fifth");
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>(data);
assertFalse(linkedHashSet.isEmpty());
assertEquals(data.size(), linkedHashSet.size());
assertTrue(linkedHashSet.containsAll(data) && data.containsAll(linkedHashSet));
}
@Test
void whenCreatingLinkedHashSetWithInitialCapacityAndLoadFactor_shouldBeEmpty(){
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>(20, 3);
assertTrue(linkedHashSet.isEmpty());
}
@Test
void whenAddingElement_shouldAddElement(){
Set<Integer> linkedHashSet = new LinkedHashSet<>();
assertTrue(linkedHashSet.add(0));
assertFalse(linkedHashSet.add(0));
assertTrue(linkedHashSet.contains(0));
}
@Test
void whenAddingCollection_shouldAddAllContentOfCollection(){
Collection<Integer> data = Arrays.asList(1,2,3);
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();
assertTrue(linkedHashSet.addAll(data));
assertTrue(data.containsAll(linkedHashSet) && linkedHashSet.containsAll(data));
}
@Test
void whenAddingCollectionWithDuplicateElements_shouldMaintainUniqueValuesInSet(){
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(2);
Collection<Integer> data = Arrays.asList(1, 1, 2, 3);
assertTrue(linkedHashSet.addAll(data));
assertEquals(3, linkedHashSet.size());
assertTrue(data.containsAll(linkedHashSet) && linkedHashSet.containsAll(data));
}
@Test
void whenIteratingWithIterator_assertThatElementIsPresent(){
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(0);
linkedHashSet.add(1);
linkedHashSet.add(2);
Iterator<Integer> iterator = linkedHashSet.iterator();
for (int i = 0; i < linkedHashSet.size(); i++) {
int nextData = iterator.next();
assertEquals(i, nextData);
}
}
@Test
void whenIteratingWithSpliterator_assertThatElementIsPresent(){
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(0);
linkedHashSet.add(1);
linkedHashSet.add(2);
Spliterator<Integer> spliterator = linkedHashSet.spliterator();
AtomicInteger counter = new AtomicInteger();
spliterator.forEachRemaining(data -> {
assertEquals(counter.get(), (int)data);
counter.getAndIncrement();
});
}
@Test
void whenRemovingAnElement_shouldRemoveElement(){
Collection<String> data = Arrays.asList("first", "second", "third", "fourth", "fifth");
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>(data);
assertTrue(linkedHashSet.remove("second"));
assertFalse(linkedHashSet.contains("second"));
}
@Test
void whenRemovingAnElementGreaterThanTwo_shouldRemoveElement(){
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(0);
linkedHashSet.add(1);
linkedHashSet.add(2);
linkedHashSet.add(3);
linkedHashSet.add(4);
linkedHashSet.removeIf(data -> data > 2);
assertFalse(linkedHashSet.contains(3));
assertFalse(linkedHashSet.contains(4));
}
@Test
void whenRemovingAnElementWithIterator_shouldRemoveElement(){
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(0);
linkedHashSet.add(1);
linkedHashSet.add(2);
Iterator<Integer> iterator = linkedHashSet.iterator();
int elementToRemove = 1;
assertTrue(linkedHashSet.contains(elementToRemove));
while(iterator.hasNext()){
if(elementToRemove == iterator.next()){
iterator.remove();
}
}
assertFalse(linkedHashSet.contains(elementToRemove));
}
}

View File

@@ -0,0 +1,52 @@
package com.baeldung.treeset.collectintotreeset;
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Comparator;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
public class CollectIntoTreeSetUnitTest {
Player kai = new Player("Kai", 26, 28, 7);
Player eric = new Player("Eric", 28, 30, 11);
Player saajan = new Player("Saajan", 30, 100, 66);
Player kevin = new Player("Kevin", 24, 50, 49);
@Test
void givenAStream_whenCollectIntoTreeSetWithNaturalOrder_thenGetExpectedResult() {
String kotlin = "Kotlin";
String java = "Java";
String python = "Python";
String ruby = "Ruby";
TreeSet<String> myTreeSet = Stream.of(ruby, java, kotlin, python)
.collect(Collectors.toCollection(TreeSet::new));
assertThat(myTreeSet).containsExactly(java, kotlin, python, ruby);
}
@Test
void givenAPlayerStream_whenCollectIntoTreeSet_thenGetExpectedResult() {
TreeSet<Player> myTreeSet = Stream.of(saajan, eric, kai, kevin)
.collect(Collectors.toCollection(TreeSet::new));
assertThat(myTreeSet).containsExactly(kevin, kai, eric, saajan);
}
@Test
void givenAPlayerStream_whenCollectIntoTreeSetWithNoOfWinsComparator_thenGetExpectedResult() {
TreeSet<Player> myTreeSet = Stream.of(saajan, eric, kai, kevin)
.collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparingInt(Player::getNumberOfWins))));
assertThat(myTreeSet).containsExactly(kai, eric, kevin, saajan);
}
@Test
void givenAPlayerStream_whenCollectIntoTreeSetWithWinRateComparator_thenGetExpectedResult() {
TreeSet<Player> myTreeSet = Stream.of(saajan, eric, kai, kevin)
.collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(player -> BigDecimal.valueOf(player.getNumberOfWins())
.divide(BigDecimal.valueOf(player.getNumberOfPlayed()), 2, RoundingMode.HALF_UP)))));
assertThat(myTreeSet).containsExactly(kai, eric, saajan, kevin);
}
}

View File

@@ -0,0 +1,46 @@
package com.baeldung.donerunnables;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RunnableCompletionCheckerWithCompletableFuture {
private static final Logger LOGGER = LoggerFactory.getLogger(RunnableCompletionCheckerWithCompletableFuture.class);
private static final int NUMBER_OF_RUNNABLES = 5;
private static final int PAUSE_MILLIS = 1000;
private static Runnable RUNNABLE = () -> {
try {
LOGGER.info("launching runnable");
Thread.sleep(PAUSE_MILLIS);
} catch (InterruptedException e) {
}
};
public static void main(String args[]) throws InterruptedException {
List<Runnable> runnables = IntStream.range(0, NUMBER_OF_RUNNABLES)
.mapToObj(x -> RUNNABLE)
.collect(Collectors.toList());
CompletableFuture<?>[] completableFutures = runAsynchronousTasks(runnables);
LOGGER.info("Right after the creation of the completable future array, every completable future is done: {}", isEveryCompletableFutureDone(completableFutures));
Thread.sleep((NUMBER_OF_RUNNABLES + 1) * PAUSE_MILLIS);
LOGGER.info("After {} seconds, every completable future is done: {}", NUMBER_OF_RUNNABLES + 1, isEveryCompletableFutureDone(completableFutures));
}
public static CompletableFuture<?>[] runAsynchronousTasks(List<Runnable> runnables) {
return runnables.stream()
.map(CompletableFuture::runAsync)
.toArray(CompletableFuture<?>[]::new);
}
public static boolean isEveryCompletableFutureDone(CompletableFuture<?>[] completableFutures) {
return CompletableFuture.allOf(completableFutures)
.isDone();
}
}

View File

@@ -0,0 +1,51 @@
package com.baeldung.donerunnables;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RunnableCompletionCheckerWithThreadPoolExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger(RunnableCompletionCheckerWithCompletableFuture.class);
private static final int NUMBER_OF_RUNNABLES = 5;
private static final int PAUSE_MILLIS = 1000;
private static final int NUMBER_OF_THREADS = 5;
private static Runnable RUNNABLE = () -> {
try {
LOGGER.info("launching runnable");
Thread.sleep(PAUSE_MILLIS);
} catch (InterruptedException e) {
}
};
public static void main(String args[]) throws InterruptedException {
List<Runnable> runnables = IntStream.range(0, NUMBER_OF_RUNNABLES)
.mapToObj(x -> RUNNABLE)
.collect(Collectors.toList());
ThreadPoolExecutor executor = createThreadPoolExecutor(runnables);
executor.shutdown();
LOGGER.info("After a timeout of 0 seconds, every Runnable is done: {}", isEveryRunnableDone(executor, 0));
Thread.sleep(100);
LOGGER.info("After a timeout of 100 milliseconds, every Runnable is done: {}", isEveryRunnableDone(executor, 100));
Thread.sleep(2000);
LOGGER.info("After a timeout of 2 seconds, every Runnable is done: {}", isEveryRunnableDone(executor, 1500));
}
public static ThreadPoolExecutor createThreadPoolExecutor(List<Runnable> runnables) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(NUMBER_OF_THREADS);
runnables.forEach(executor::execute);
return executor;
}
public static boolean isEveryRunnableDone(ThreadPoolExecutor executor, int timeout) throws InterruptedException {
return executor.awaitTermination(timeout, TimeUnit.MILLISECONDS);
}
}

View File

@@ -9,3 +9,4 @@
- [Differences Between set() and lazySet() in Java Atomic Variables](https://www.baeldung.com/java-atomic-set-vs-lazyset)
- [Volatile vs. Atomic Variables in Java](https://www.baeldung.com/java-volatile-vs-atomic)
- [What Is “Locked Ownable Synchronizers” in Thread Dump?](https://www.baeldung.com/locked-ownable-synchronizers)
- [Understanding java.lang.Thread.State: WAITING (parking)](https://www.baeldung.com/java-lang-thread-state-waiting-parking)

View File

@@ -0,0 +1,25 @@
package com.baeldung.threadparking;
import java.util.concurrent.locks.LockSupport;
public class Application {
public static void main(String[] args) throws InterruptedException {
final Object syncObj = new Object();
Thread t = new Thread(() -> {
int acc = 0;
for (int i = 1; i <= 100; i++) {
acc += i;
}
System.out.println("Work finished");
LockSupport.park(syncObj);
System.out.println(acc);
});
t.setName("PARK-THREAD");
t.start();
// Thread.sleep(1000);
LockSupport.unpark(t);
}
}

View File

@@ -1,4 +1,4 @@
package com.baeldung.concurrent.daemon;
package com.baeldung.concurrent.startathread;
public class MultipleThreadsExample {
public static void main(String[] args) {

View File

@@ -1,4 +1,4 @@
package com.baeldung.concurrent.daemon;
package com.baeldung.concurrent.startathread;
public class NewThread extends Thread {
public void run() {

View File

@@ -1,4 +1,4 @@
package com.baeldung.concurrent.daemon;
package com.baeldung.concurrent.startathread;
public class SingleThreadExample {
public static void main(String[] args) {

View File

@@ -0,0 +1,39 @@
package com.baeldung.concurrent.startathread;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimerDemoExample {
private void scheduleOnce() {
TimerTask task = new TimerTask() {
public void run() {
System.out.println("Task performed on: " + new Date() + "\n" + "Thread's name: " + Thread.currentThread()
.getName());
}
};
Timer timer = new Timer("Timer");
long delay = 1000L;
timer.schedule(task, delay);
}
private void scheduleRecurrently() {
TimerTask task = new TimerTask() {
public void run() {
System.out.println("Recurrent Task performed on: " + new Date() + "\n" + "Thread's name: " + Thread.currentThread()
.getName());
}
};
Timer timer = new Timer("Timer");
long delay = 1000L;
final long period = 1000L;
timer.scheduleAtFixedRate(task, delay, period);
}
public static void main(String[] args) {
TimerDemoExample timerDemoExample = new TimerDemoExample();
timerDemoExample.scheduleOnce();
timerDemoExample.scheduleRecurrently();
}
}

View File

@@ -2,38 +2,40 @@ package com.baeldung.concurrent.waitandnotify;
public class Data {
private String packet;
// True if receiver should wait
// False if sender should wait
private boolean transfer = true;
public synchronized String receive() {
while (transfer) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
Thread.currentThread()
.interrupt();
System.err.println("Thread Interrupted");
}
}
transfer = true;
String returnPacket = packet;
notifyAll();
return returnPacket;
}
public synchronized void send(String packet) {
while (!transfer) {
try {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
Thread.currentThread()
.interrupt();
System.err.println("Thread Interrupted");
}
}
transfer = false;
this.packet = packet;
notifyAll();
}

View File

@@ -5,7 +5,7 @@ public class NetworkDriver {
Data data = new Data();
Thread sender = new Thread(new Sender(data));
Thread receiver = new Thread(new Receiver(data));
sender.start();
receiver.start();
}

View File

@@ -4,24 +4,24 @@ import java.util.concurrent.ThreadLocalRandom;
public class Receiver implements Runnable {
private Data load;
public Receiver(Data load) {
this.load = load;
}
public void run() {
for(String receivedMessage = load.receive();
!"End".equals(receivedMessage) ;
receivedMessage = load.receive()) {
for (String receivedMessage = load.receive(); !"End".equals(receivedMessage); receivedMessage = load.receive()) {
System.out.println(receivedMessage);
//Thread.sleep() to mimic heavy server-side processing
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
Thread.sleep(ThreadLocalRandom.current()
.nextInt(1000, 5000));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
Thread.currentThread()
.interrupt();
System.err.println("Thread Interrupted");
}
}
}

View File

@@ -4,29 +4,25 @@ import java.util.concurrent.ThreadLocalRandom;
public class Sender implements Runnable {
private Data data;
public Sender(Data data) {
this.data = data;
}
public void run() {
String packets[] = {
"First packet",
"Second packet",
"Third packet",
"Fourth packet",
"End"
};
String packets[] = { "First packet", "Second packet", "Third packet", "Fourth packet", "End" };
for (String packet : packets) {
data.send(packet);
//Thread.sleep() to mimic heavy server-side processing
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
Thread.sleep(ThreadLocalRandom.current()
.nextInt(1000, 5000));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
Thread.currentThread()
.interrupt();
System.err.println("Thread Interrupted");
}
}
}

View File

@@ -1,4 +1,4 @@
package com.baeldung.thread.join;
package com.baeldung.concurrent.threadjoin;
import org.junit.Ignore;
import org.junit.Test;
@@ -21,49 +21,47 @@ public class ThreadJoinUnitTest {
SampleThread(int processingCount) {
this.processingCount = processingCount;
LOGGER.debug("Thread " + this.getName() + " created");
LOGGER.info("Thread " + this.getName() + " created");
}
@Override
public void run() {
LOGGER.debug("Thread " + this.getName() + " started");
LOGGER.info("Thread " + this.getName() + " started");
while (processingCount > 0) {
try {
Thread.sleep(1000); // Simulate some work being done by thread
} catch (InterruptedException e) {
LOGGER.debug("Thread " + this.getName() + " interrupted.");
LOGGER.info("Thread " + this.getName() + " interrupted.");
}
processingCount--;
LOGGER.debug("Inside Thread " + this.getName() + ", processingCount = " + processingCount);
LOGGER.info("Inside Thread " + this.getName() + ", processingCount = " + processingCount);
}
LOGGER.debug("Thread " + this.getName() + " exiting");
LOGGER.info("Thread " + this.getName() + " exiting");
}
}
@Test
public void givenNewThread_whenJoinCalled_returnsImmediately() throws InterruptedException {
Thread t1 = new SampleThread(0);
LOGGER.debug("Invoking join.");
LOGGER.info("Invoking join");
t1.join();
LOGGER.debug("Returned from join");
LOGGER.debug("Thread state is" + t1.getState());
LOGGER.info("Returned from join");
LOGGER.info("Thread state is" + t1.getState());
assertFalse(t1.isAlive());
}
@Test
public void givenStartedThread_whenJoinCalled_waitsTillCompletion()
throws InterruptedException {
public void givenStartedThread_whenJoinCalled_waitsTillCompletion() throws InterruptedException {
Thread t2 = new SampleThread(1);
t2.start();
LOGGER.debug("Invoking join.");
LOGGER.info("Invoking join");
t2.join();
LOGGER.debug("Returned from join");
LOGGER.info("Returned from join");
assertFalse(t2.isAlive());
}
@Test
public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout()
throws InterruptedException {
public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout() throws InterruptedException {
Thread t3 = new SampleThread(10);
t3.start();
t3.join(1000);
@@ -72,19 +70,17 @@ public class ThreadJoinUnitTest {
@Test
@Ignore
public void givenThreadTerminated_checkForEffect_notGuaranteed()
throws InterruptedException {
public void givenThreadTerminated_checkForEffect_notGuaranteed() throws InterruptedException {
SampleThread t4 = new SampleThread(10);
t4.start();
//not guaranteed to stop even if t4 finishes.
//not guaranteed to stop even if t4 finishes.
do {
} while (t4.processingCount > 0);
}
@Test
public void givenJoinWithTerminatedThread_checkForEffect_guaranteed()
throws InterruptedException {
public void givenJoinWithTerminatedThread_checkForEffect_guaranteed() throws InterruptedException {
SampleThread t4 = new SampleThread(10);
t4.start();
do {

View File

@@ -16,24 +16,24 @@ public class NetworkIntegrationTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private String expected;
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@Before
public void setUpExpectedOutput() {
StringWriter expectedStringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(expectedStringWriter);
printWriter.println("First packet");
printWriter.println("Second packet");
printWriter.println("Third packet");
printWriter.println("Fourth packet");
printWriter.close();
expected = expectedStringWriter.toString();
}
@@ -42,25 +42,26 @@ public class NetworkIntegrationTest {
System.setOut(null);
System.setErr(null);
}
@Test
public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() {
Data data = new Data();
Thread sender = new Thread(new Sender(data));
Thread receiver = new Thread(new Receiver(data));
sender.start();
receiver.start();
//wait for sender and receiver to finish before we test against expected
try {
sender.join();
receiver.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
Thread.currentThread()
.interrupt();
System.err.println("Thread Interrupted");
}
assertEquals(expected, outContent.toString());
}
}

View File

@@ -6,3 +6,4 @@
- [Formatting Output with printf() in Java](https://www.baeldung.com/java-printstream-printf)
- [ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java)
- [System.console() vs. System.out](https://www.baeldung.com/java-system-console-vs-system-out)
- [How to Log to the Console in Color](https://www.baeldung.com/java-log-console-in-color)

View File

@@ -14,6 +14,14 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-console</finalName>
<resources>

View File

@@ -0,0 +1,21 @@
package com.baeldung.color;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ColorLogger {
private static final Logger LOGGER = LoggerFactory.getLogger(ColorLogger.class);
public void logDebug(String logging) {
LOGGER.debug("\u001B[34m" + logging + "\u001B[0m");
}
public void logInfo(String logging) {
LOGGER.info("\u001B[32m" + logging + "\u001B[0m");
}
public void logError(String logging) {
LOGGER.error("\u001B[31m" + logging + "\u001B[0m");
}
}

View File

@@ -0,0 +1,60 @@
package com.baeldung.color;
import static org.fusesource.jansi.Ansi.ansi;
import org.fusesource.jansi.AnsiConsole;
public class PrintColor {
public static void main(String[] args) {
logColorUsingANSICodes();
logColorUsingLogger();
logColorUsingJANSI();
}
private static void logColorUsingANSICodes() {
System.out.println("Here's some text");
System.out.println("\u001B[31m" + "and now the text is red" + "\u001B[0m");
System.out.println("and now back to the default");
}
private static void logColorUsingLogger() {
ColorLogger colorLogger = new ColorLogger();
colorLogger.logDebug("Some debug logging");
colorLogger.logInfo("Some info logging");
colorLogger.logError("Some error logging");
}
private static void logColorUsingJANSI() {
AnsiConsole.systemInstall();
System.out.println(ansi().fgRed().a("Some red text").fgYellow().a(" and some yellow text").reset());
AnsiConsole.systemUninstall();
}
}
/*
* More ANSI codes:
*
* Always conclude your logging with the ANSI reset code: "\u001B[0m"
*
* In each case, replace # with the corresponding number:
*
* 0 = black
* 1 = red
* 2 = green
* 3 = yellow
* 4 = blue
* 5 = purple
* 6 = cyan (light blue)
* 7 = white
*
* \u001B[3#m = color font
* \u001B[4#m = color background
* \u001B[1;3#m = bold font
* \u001B[4;3#m = underlined font
* \u001B[3;3#m = italics font (not widely supported, works in VS Code)
*/

View File

@@ -0,0 +1,24 @@
package com.baeldung.utiltosqldate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.TimeZone;
public class UtilToSqlDateUtils {
public static java.util.Date createAmericanDate(String date) throws ParseException {
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
return isoFormat.parse(date);
}
public static void switchTimezone(String timeZone) {
TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
}
public static LocalDate getLocalDate(java.util.Date date, String timeZone) {
return date.toInstant().atZone(ZoneId.of(timeZone)).toLocalDate();
}
}

View File

@@ -0,0 +1,53 @@
package com.baeldung.utiltosqldate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.text.ParseException;
public class UtilToSqlDateUtilsUnitTest {
@Test
public void givenUtilDate_whenCastingToSqlDate_thenThrowException() {
Assertions.assertThrows(ClassCastException.class, () -> {
java.sql.Date date = (java.sql.Date) new java.util.Date();
});
}
@Test
public void givenUtilDate_whenStandardConversion_thenTimezoneLost() throws ParseException {
java.util.Date date = UtilToSqlDateUtils.createAmericanDate("2010-05-23T22:01:02");
UtilToSqlDateUtils.switchTimezone("America/Los_Angeles");
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
Assertions.assertEquals("2010-05-23", sqlDate.toString());
UtilToSqlDateUtils.switchTimezone("Rome");
sqlDate = new java.sql.Date(date.getTime());
Assertions.assertEquals("2010-05-24",sqlDate.toString());
}
@Test
public void givenUtilDate_whenConversionToTimestamp_thenKeepTimeInfo() throws ParseException {
java.util.Date date = UtilToSqlDateUtils.createAmericanDate("2010-05-23T22:01:02");
UtilToSqlDateUtils.switchTimezone("America/Los_Angeles");
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
Assertions.assertEquals("2010-05-23 22:01:02.0",timestamp.toString());
}
@Test
public void givenUtilDate_whenUsingJavaTimeConversion_thenTimezoneKept() throws ParseException {
java.util.Date date = UtilToSqlDateUtils.createAmericanDate("2010-05-23T22:01:02");
UtilToSqlDateUtils.switchTimezone("America/Los_Angeles");
java.time.LocalDate localDate = UtilToSqlDateUtils.getLocalDate(date,"America/Los_Angeles");
Assertions.assertEquals(localDate.toString(), "2010-05-23");
UtilToSqlDateUtils.switchTimezone("Rome");
localDate = UtilToSqlDateUtils.getLocalDate(date,"America/Los_Angeles");
Assertions.assertEquals(localDate.toString(), "2010-05-23");
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.instant;
import org.junit.Test;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import static org.assertj.core.api.Assertions.assertThat;
public class StringToInstantConverterUnitTest {
String stringDate = "09:15:30 PM, Sun 10/09/2022";
String pattern = "hh:mm:ss a, EEE M/d/uuuu";
@Test public void givenStringTimeStamp_whenConvertingWithInstantUsingTimeZone_thenConvertToInstant() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern, Locale.US);
LocalDateTime localDateTime = LocalDateTime.parse(stringDate, dateTimeFormatter);
ZoneId zoneId = ZoneId.of("America/Chicago");
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
Instant instant = zonedDateTime.toInstant();
assertThat(instant.toString()).isEqualTo("2022-10-10T02:15:30Z");
}
@Test public void givenStringTimeStamp_whenConvertingWithLocalDateTime_thenConvertToInstant() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern, Locale.US);
LocalDateTime localDateTime = LocalDateTime.parse(stringDate, dateTimeFormatter);
assertThat(localDateTime.toString()).isEqualTo("2022-10-09T21:15:30");
}
}

View File

@@ -46,8 +46,4 @@
</plugins>
</build>
<properties>
<h2.version>1.4.191</h2.version>
</properties>
</project>

View File

@@ -12,4 +12,6 @@ This module contains articles about core Java input and output (IO)
- [Java Scanner.skip method with examples](https://www.baeldung.com/java-scanner-skip)
- [Generate the MD5 Checksum for a File in Java](https://www.baeldung.com/java-md5-checksum-file)
- [Getting the Filename From a String Containing an Absolute File Path](https://www.baeldung.com/java-filename-full-path)
- [Mocking Java InputStream Object](https://www.baeldung.com/java-mocking-inputstream)
- [[<-- Prev]](/core-java-modules/core-java-io-3)

View File

@@ -0,0 +1,23 @@
package com.baeldung.mockinginputstream;
import java.io.IOException;
import java.io.InputStream;
public class GeneratingInputStream extends InputStream {
private final int desiredSize;
private int actualSize = 0;
private final byte[] seed;
public GeneratingInputStream(int desiredSize, String seed) {
this.desiredSize = desiredSize;
this.seed = seed.getBytes();
}
@Override
public int read() {
if (actualSize >= desiredSize) {
return -1;
}
return seed[actualSize++ % seed.length];
}
}

View File

@@ -0,0 +1,59 @@
package com.baeldung.mockinginputstream;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
public class MockingInputStreamUnitTest {
@Test
public void givenSimpleImplementation_shouldProcessInputStream() throws IOException {
int byteCount = processInputStream(new InputStream() {
private final byte[] msg = "Hello World".getBytes();
private int index = 0;
@Override
public int read() {
if (index >= msg.length) {
return -1;
}
return msg[index++];
}
});
assertThat(byteCount).isEqualTo(11);
}
@Test
public void givenByteArrayInputStream_shouldProcessInputStream() throws IOException {
String msg = "Hello World";
int bytesCount = processInputStream(new ByteArrayInputStream(msg.getBytes()));
assertThat(bytesCount).isEqualTo(11);
}
@Test
public void givenFileInputStream_shouldProcessInputStream() throws IOException {
InputStream inputStream = MockingInputStreamUnitTest.class.getResourceAsStream("/mockinginputstreams/msg.txt");
int bytesCount = processInputStream(inputStream);
assertThat(bytesCount).isEqualTo(11);
inputStream.close();
}
@Test
public void givenGeneratingInputStream_shouldProcessInputStream() throws IOException {
InputStream inputStream = new GeneratingInputStream(10_000, "Hello World");
int bytesCount = processInputStream(inputStream);
assertThat(bytesCount).isEqualTo(10_000);
inputStream.close();
}
int processInputStream(InputStream inputStream) throws IOException {
int count = 0;
while(inputStream.read() != -1) {
count++;
}
return count;
}
}

View File

@@ -0,0 +1 @@
Hello World

View File

@@ -64,7 +64,6 @@
<properties>
<spring.version>5.0.9.RELEASE</spring.version>
<h2.version>1.4.199</h2.version>
<apacheds.version>2.0.0.AM26</apacheds.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>

View File

@@ -8,3 +8,5 @@ This module contains articles about core features in the Java language
- [Advantages and Disadvantages of Using Java Wildcard Imports](https://www.baeldung.com/java-wildcard-imports)
- [Toggle a Boolean Variable in Java](https://www.baeldung.com/java-toggle-boolean)
- [Handle Classes With the Same Name in Java](https://www.baeldung.com/java-classes-same-name)
- [Variable Instantiation on Declaration vs. on Constructor in Java](https://www.baeldung.com/java-variable-instantiation-declaration-vs-constructor)
- [Infinity in Java](https://www.baeldung.com/java-infinity)

View File

@@ -0,0 +1,81 @@
package com.baeldung.infinity;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DoubleInfinityUnitTest {
@Test
void givenInfinities_whenOperatingWithThem_thenNaNOrInfinity() {
Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
assertEquals(Double.NaN, (positiveInfinity + negativeInfinity));
assertEquals(Double.NaN, (positiveInfinity / negativeInfinity));
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeInfinity));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveInfinity));
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeInfinity));
}
@Test
void givenInfinityAndPositiveNumber_whenOperatingWithThem_thenInfinity() {
Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
double positiveNumber = 10.0;
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + positiveNumber));
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveNumber));
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity * positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity * positiveNumber));
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity / positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity / positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (positiveNumber - positiveInfinity));
assertEquals(Double.POSITIVE_INFINITY, (positiveNumber - negativeInfinity));
assertEquals(0.0, (positiveNumber / positiveInfinity));
assertEquals(-0.0, (positiveNumber / negativeInfinity));
}
@Test
void givenInfinityAndBegativeNumber_whenOperatingWithThem_thenInfinity() {
Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
double negativeNumber = -10.0;
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + negativeNumber));
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeNumber));
assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity * negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity / negativeNumber));
assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity / negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeNumber - positiveInfinity));
assertEquals(Double.POSITIVE_INFINITY, (negativeNumber - negativeInfinity));
assertEquals(-0.0, (negativeNumber / positiveInfinity));
assertEquals(0.0, (negativeNumber / negativeInfinity));
}
@Test
void givenRealNumbers_whenDivisionByZero_thenInfinity() {
double d = 1.0;
assertEquals(Double.POSITIVE_INFINITY, (d / 0.0));
assertEquals(Double.NEGATIVE_INFINITY, (d / -0.0));
assertEquals(Double.NEGATIVE_INFINITY, (-d / 0.0));
assertEquals(Double.POSITIVE_INFINITY, (-d / -0.0));
}
}

View File

@@ -6,6 +6,6 @@
- [Evaluating a Math Expression in Java](https://www.baeldung.com/java-evaluate-math-expression-string)
- [Swap Two Variables in Java](https://www.baeldung.com/java-swap-two-variables)
- [Java Program to Find the Roots of a Quadratic Equation](https://www.baeldung.com/roots-quadratic-equation/)
- [Java Program to Find the Roots of a Quadratic Equation](https://www.baeldung.com/roots-quadratic-equation)
- [Create a BMI Calculator in Java](https://www.baeldung.com/java-body-mass-index-calculator)
- More articles: [[<-- Prev]](/core-java-modules/core-java-lang-math-2)

View File

@@ -0,0 +1,35 @@
package com.baeldung.math.standarddeviation;
import java.util.Arrays;
public class StandardDeviation {
public static double calculateStandardDeviation(double[] array) {
// get the sum of array
double sum = 0.0;
for (double i : array) {
sum += i;
}
// get the mean of array
int length = array.length;
double mean = sum / length;
// calculate the standard deviation
double standardDeviation = 0.0;
for (double num : array) {
standardDeviation += Math.pow(num - mean, 2);
}
return Math.sqrt(standardDeviation / length);
}
public static void main(String[] args) {
double[] array = {25, 5, 45, 68, 61, 46, 24, 95};
System.out.println("List of elements: " + Arrays.toString(array));
double standardDeviation = calculateStandardDeviation(array);
System.out.format("Standard Deviation = %.6f", standardDeviation);
}
}

View File

@@ -13,4 +13,6 @@ This module contains articles about networking in Java
- [Get the IP Address of the Current Machine Using Java](https://www.baeldung.com/java-get-ip-address)
- [Get Domain Name From Given URL in Java](https://www.baeldung.com/java-domain-name-from-url)
- [Java HttpClient Timeout](https://www.baeldung.com/java-httpclient-timeout)
- [Port Scanning With Java](https://www.baeldung.com/java-port-scanning)
- [Validating URL in Java](https://www.baeldung.com/java-validate-url)
- [[<-- Prev]](/core-java-modules/core-java-networking-2)

View File

@@ -58,6 +58,12 @@
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>${apache.commons-validator.version}</version>
</dependency>
</dependencies>
<build>
@@ -90,6 +96,7 @@
<jgonian.commons-ip-math.version>1.32</jgonian.commons-ip-math.version>
<googlecode.ipv6.version>0.17</googlecode.ipv6.version>
<javax.mail.version>1.6.2</javax.mail.version>
<apache.commons-validator.version>1.7</apache.commons-validator.version>
</properties>
</project>

View File

@@ -0,0 +1,36 @@
package com.baeldung.urlvalidation;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.commons.validator.routines.UrlValidator;
public class UrlValidation {
public boolean isValidURLJavaNet(String url) throws MalformedURLException, URISyntaxException {
try {
new URL(url).toURI();
return true;
} catch (MalformedURLException e) {
return false;
} catch (URISyntaxException e) {
return false;
}
}
public boolean invalidUrlAsValidJavaNet(String url) throws MalformedURLException {
try {
new URL(url);
return true;
} catch (MalformedURLException e) {
return false;
}
}
public boolean isValidURLApache(String url) throws MalformedURLException {
UrlValidator validator = new UrlValidator();
return validator.isValid(url);
}
}

View File

@@ -0,0 +1,44 @@
package com.baeldung.urlvalidation;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import org.junit.Test;
import com.baeldung.urlvalidation.UrlValidation;
public class UrlValidateUnitTest {
@Test
public void givenValidStringAsURL_whenUsingJDK_shouldReturnTrue() throws MalformedURLException, URISyntaxException {
UrlValidation urlValidator = new UrlValidation();
assertTrue(urlValidator.isValidURLJavaNet("http://baeldung.com/"));
}
@Test
public void givenInvalidStringAsURL_whenUsingJDK_shouldReturnFalse() throws MalformedURLException, URISyntaxException {
UrlValidation urlValidator = new UrlValidation();
assertFalse(urlValidator.isValidURLJavaNet("https://www.baeldung.com/ java-%%$^&& iuyi"));
}
@Test
public void givenInvalidStringAsURL_whenUsingJDK_shouldReturnTrue() throws MalformedURLException {
UrlValidation urlValidator = new UrlValidation();
assertTrue(urlValidator.invalidUrlAsValidJavaNet("https://www.baeldung.com/ java-%%$^&& iuyi"));
}
@Test
public void givenValidStringAsURL_whenUsingApache_shouldReturnTrue() throws MalformedURLException {
UrlValidation urlValidator = new UrlValidation();
assertTrue(urlValidator.isValidURLApache("http://baeldung.com/"));
}
@Test
public void givenInvalidStringAsURL_whenUsingApache_shouldReturnFalse() throws MalformedURLException {
UrlValidation urlValidator = new UrlValidation();
assertFalse(urlValidator.isValidURLApache("https://www.baeldung.com/ java-%%$^&& iuyi"));
}
}

View File

@@ -0,0 +1,2 @@
## Relevant Articles:
- [Difference Between URI.create() and new URI()](https://www.baeldung.com/java-uri-create-and-new-uri)

View File

@@ -0,0 +1,19 @@
<?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-networking-4</artifactId>
<name>core-java-networking-4</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<finalName>core-java-networking-4</finalName>
</build>
</project>

View File

@@ -0,0 +1,36 @@
package com.baeldung.uricreation;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.net.URISyntaxException;
import static org.junit.jupiter.api.Assertions.*;
public class UriCreationUnitTest {
@Test
void givenValidUriString_whenUsingConstructor_shouldGetExpectedResult() {
try {
URI myUri = new URI("https://www.baeldung.com/articles");
assertNotNull(myUri);
} catch (URISyntaxException e) {
fail();
}
}
@Test
void givenInvalidUriString_whenUsingConstructor_shouldGetExpectedResult() {
assertThrows(URISyntaxException.class, () -> new URI("I am an invalid URI string."));
}
@Test
void givenValidUriString_whenUsingCreateMethod_shouldGetExpectedResult() {
URI myUri = URI.create("https://www.baeldung.com/articles");
assertNotNull(myUri);
}
@Test
void givenInvalidUriString_whenUsingCreateMethod_shouldGetExpectedResult() {
assertThrows(IllegalArgumentException.class, () -> URI.create("I am an invalid URI string."));
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.uniquerng;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
public class BigUniqueRng implements Iterator<Integer> {
private Random random = new Random();
private Set<Integer> generated = new LinkedHashSet<>();
public BigUniqueRng(int size, int max) {
while (generated.size() < size) {
Integer next = random.nextInt(max);
generated.add(next);
}
}
@Override
public Integer next() {
if (!hasNext())
throw new NoSuchElementException();
Iterator<Integer> iterator = generated.iterator();
Integer next = iterator.next();
iterator.remove();
return next;
}
@Override
public boolean hasNext() {
return !generated.isEmpty();
}
}

View File

@@ -0,0 +1,42 @@
package com.baeldung.uniquerng;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class UniqueRng implements Iterator<Integer> {
private int size;
private List<Integer> numbers = new ArrayList<>();
public UniqueRng(int size, boolean zeroBased) {
this.size = size;
int start = (zeroBased ? 0 : 1);
int limit = (zeroBased ? size - 1 : size);
for (int i = start; i <= limit; i++) {
numbers.add(i);
}
Collections.shuffle(numbers);
}
@Override
public Integer next() {
if (!hasNext())
throw new NoSuchElementException();
return numbers.remove(0);
}
@Override
public boolean hasNext() {
return !numbers.isEmpty();
}
public int getSize() {
return size;
}
}

View File

@@ -0,0 +1,56 @@
package com.baeldung.uniquerng;
import static org.junit.Assert.assertEquals;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.junit.Test;
public class RngUtilsUnitTest {
@Test
public void whenNUniqueNumRequested_thenNUniqueNumConsumed() {
TreeSet<Integer> set = new TreeSet<>();
int n = 5;
UniqueRng rng = new UniqueRng(n, true);
while (rng.hasNext()) {
set.add(rng.next());
}
assertEquals(n, set.size());
}
@Test
public void givenYRange_whenNUniqueNumRequested_thenNUniqueNumConsumed() {
TreeSet<Integer> set = new TreeSet<>();
int n = 5;
int y = n * 10;
BigUniqueRng rng = new BigUniqueRng(n, y);
while (rng.hasNext()) {
set.add(rng.next());
}
assertEquals(n, set.size());
}
@Test
public void givenIntStreamSizeN_whenCollected_thenSetSizeN() {
int n = 5;
int from = -5;
int to = n * 2;
Random r = new Random();
Set<Integer> set = r.ints(from, to)
.distinct()
.limit(n)
.boxed()
.collect(Collectors.toSet());
assertEquals(n, set.size());
}
}

View File

@@ -3,3 +3,6 @@
- [Compare Characters in Java](https://www.baeldung.com/java-compare-characters)
- [String Concatenation in Java](https://www.baeldung.com/java-string-concatenation)
- [Capitalize the First Letter of a String in Java](https://www.baeldung.com/java-string-uppercase-first-letter)
- [Convert String to char in Java](https://www.baeldung.com/java-convert-string-to-char)
- [Convert String to String Array](https://www.baeldung.com/java-convert-string-to-string-array)

View File

@@ -1,7 +1,7 @@
<?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">
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-5</artifactId>
<version>0.1.0-SNAPSHOT</version>
@@ -14,6 +14,13 @@
<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>
</dependencies>
<build>
<plugins>
<plugin>
@@ -30,6 +37,7 @@
<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>
</properties>
</project>

View File

@@ -0,0 +1,45 @@
package com.baeldung.capitalizethefirstletter;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.*;
public class CapitalizeTheFirstLetterUnitTest {
private static final String EMPTY_INPUT = "";
private static final String EMPTY_EXPECTED = "";
private static final String INPUT = "hi there, Nice to Meet You!";
private static final String EXPECTED = "Hi there, Nice to Meet You!";
@Test
void givenString_whenCapitalizeUsingSubString_shouldGetExpectedResult() {
String output = INPUT.substring(0, 1).toUpperCase() + INPUT.substring(1);
assertEquals(EXPECTED, output);
assertThrows(IndexOutOfBoundsException.class, () -> EMPTY_INPUT.substring(1));
}
@Test
void givenString_whenCapitalizeUsingRegexReplace_shouldGetExpectedResult() {
String output = Pattern.compile("^.").matcher(INPUT).replaceFirst(m -> m.group().toUpperCase());
assertEquals(EXPECTED, output);
String emptyOutput = Pattern.compile("^.").matcher(EMPTY_INPUT).replaceFirst(m -> m.group().toUpperCase());
assertEquals(EMPTY_EXPECTED, emptyOutput);
}
@Test
void givenString_whenCapitalizeUsingApacheCommons_shouldGetExpectedResult() {
String output = StringUtils.capitalize(INPUT);
assertEquals(EXPECTED, output);
String emptyOutput = StringUtils.capitalize(EMPTY_INPUT);
assertEquals(EMPTY_EXPECTED, emptyOutput);
String nullOutput = StringUtils.capitalize(null);
assertNull(nullOutput);
}
}

View File

@@ -0,0 +1,34 @@
package com.baeldung.strtochar;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class ConvertStringToCharUnitTest {
private static final String STRING_b = "b";
private static final String STRING_Baeldung = "Baeldung";
@Test
void givenStringWithSingleChar_whenCallingCharAt_thenGetExpectedResult() {
assertEquals('b', STRING_b.charAt(0));
assertEquals('l', STRING_Baeldung.charAt(3));
assertThrows(StringIndexOutOfBoundsException.class, () -> "".charAt(0));
}
@Test
void givenStringWithMultipleChars_whenCallingCharAt_thenGetExpectedResult() {
assertArrayEquals(new char[] { 'B', 'a', 'e', 'l', 'd', 'u', 'n', 'g' }, STRING_Baeldung.toCharArray());
assertArrayEquals(new char[] {}, "".toCharArray());
}
@Test
void givenStringWithMultipleChars_whenCallingGetChars_thenGetExpectedResult() {
char[] aeld = new char[4];
STRING_Baeldung.getChars(1, 5, aeld, 0);
assertArrayEquals(new char[] { 'a', 'e', 'l', 'd' }, aeld);
char[] anotherArray = new char[] { '#', '#', '#', '#', '#', '#' };
STRING_Baeldung.getChars(1, 5, anotherArray, 1);
assertArrayEquals(new char[] { '#', 'a', 'e', 'l', 'd', '#' }, anotherArray);
}
}

View File

@@ -0,0 +1,49 @@
package com.baeldung.strtostrarray;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class StringToStringArrayUnitTest {
private static final String INPUT = "Hi there, nice to meet you!";
private static final String FLIGHT_INPUT = "20221018LH720FRAPEK";
@Test
void givenAString_whenConvertToSingletonArray_shouldGetExpectedResult() {
String[] myArray = new String[] { INPUT };
assertArrayEquals(new String[] { "Hi there, nice to meet you!" }, myArray);
}
@Test
void givenAString_whenSplitToWordArray_shouldGetExpectedResult() {
String[] myArray = INPUT.split("\\W+");
assertArrayEquals(new String[] { "Hi", "there", "nice", "to", "meet", "you" }, myArray);
}
@Test
void givenAString_whenSplitToSentenceArray_shouldGetExpectedResult() {
String[] myArray = INPUT.split("[-,.!;?]\\s*");
assertArrayEquals(new String[] { "Hi there", "nice to meet you" }, myArray);
}
@Test
void givenAString_whenSplitToCharArray_shouldGetExpectedResult() {
String[] myArray = INPUT.split("");
assertArrayEquals(new String[] {
"H", "i", " ", "t", "h", "e", "r", "e", ",", " ",
"n", "i", "c", "e", " ", "t", "o", " ", "m", "e", "e", "t", " ", "y", "o", "u", "!"
}, myArray);
}
@Test
void givenAString_whenSpecialRuleRequired_shouldGetExpectedResult() {
String dateStr = FLIGHT_INPUT.substring(0, 8);
String flightNo = FLIGHT_INPUT.substring(8, FLIGHT_INPUT.length() - 6);
int airportStart = dateStr.length() + flightNo.length();
String from = FLIGHT_INPUT.substring(airportStart, airportStart + 3);
String to = FLIGHT_INPUT.substring(airportStart + 3);
String[] myArray = new String[] { dateStr, flightNo, from, to };
assertArrayEquals(new String[] { "20221018", "LH720", "FRA", "PEK" }, myArray);
}
}

View File

@@ -34,6 +34,7 @@
<module>core-java-collections-array-list</module>
<module>core-java-collections-conversions</module>
<module>core-java-collections-conversions-2</module>
<module>core-java-collections-set-2</module>
<module>core-java-collections-list</module>
<module>core-java-collections-list-2</module>
<module>core-java-collections-list-3</module>
@@ -97,6 +98,7 @@
<module>core-java-lang-syntax-2</module>
<module>core-java-networking</module>
<module>core-java-networking-2</module>
<module>core-java-networking-4</module>
<module>core-java-nio</module>
<module>core-java-nio-2</module>
<module>core-java-numbers</module>
@@ -147,4 +149,4 @@
</dependencies>
</dependencyManagement>
</project>
</project>