Merge branch 'eugenp:master' into PR-6910

This commit is contained in:
parthiv39731 2023-10-11 10:34:08 +05:30 committed by GitHub
commit e6d8cc8998
177 changed files with 3875 additions and 362 deletions

View File

@ -0,0 +1,102 @@
package com.baeldung.algorithms.rotatearray;
/**
* To speed up the rotation, we narrow k rotations to the remainder of k divided by the array length, or k module the array length.
* Therefore, a large rotation number will be translated into the relative smallest rotation.
* All solutions replace the original array, although they might use an extra array to compute the rotation.
*/
public class RotateArray {
private RotateArray() {
throw new IllegalStateException("Rotate array algorithm utility methods class");
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void bruteForce(int[] arr, int k) {
checkInvalidInput(arr, k);
k %= arr.length;
int temp;
int previous;
for (int i = 0; i < k; i++) {
previous = arr[arr.length - 1];
for (int j = 0; j < arr.length; j++) {
temp = arr[j];
arr[j] = previous;
previous = temp;
}
}
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void withExtraArray(int[] arr, int k) {
checkInvalidInput(arr, k);
int[] extraArray = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
extraArray[(i + k) % arr.length] = arr[i];
}
System.arraycopy(extraArray, 0, arr, 0, arr.length);
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void cyclicReplacement(int[] arr, int k) {
checkInvalidInput(arr, k);
k = k % arr.length;
int count = 0;
for (int start = 0; count < arr.length; start++) {
int current = start;
int prev = arr[start];
do {
int next = (current + k) % arr.length;
int temp = arr[next];
arr[next] = prev;
prev = temp;
current = next;
count++;
} while (start != current);
}
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void reverse(int[] arr, int k) {
checkInvalidInput(arr, k);
k %= arr.length;
reverse(arr, 0, arr.length - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
}
private static void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
private static void checkInvalidInput(int[] arr, int rotation) {
if (rotation < 1 || arr == null)
throw new IllegalArgumentException("Rotation must be greater than zero or array must be not null");
}
}

View File

@ -0,0 +1,124 @@
package com.baeldung.algorithms.rotatearray;
import static com.baeldung.algorithms.rotatearray.RotateArray.bruteForce;
import static com.baeldung.algorithms.rotatearray.RotateArray.cyclicReplacement;
import static com.baeldung.algorithms.rotatearray.RotateArray.reverse;
import static com.baeldung.algorithms.rotatearray.RotateArray.withExtraArray;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class RotateArrayUnitTest {
private final int[] arr = { 1, 2, 3, 4, 5, 6 };
private final int rotationLtArrayLength = 1;
private final int rotationGtArrayLength = arr.length + 2;
private final int[] ltArrayLengthRotation = { 6, 1, 2, 3, 4, 5 };
private final int[] gtArrayLengthRotation = { 5, 6, 1, 2, 3, 4 };
@Test
void givenInputArray_whenNoRotationOrEmptyArray_thenThrowIllegalArgumentException() {
final int noRotation = 0;
final int someRotation = arr.length - 1;
assertThrows(IllegalArgumentException.class, () -> bruteForce(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> withExtraArray(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> cyclicReplacement(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> reverse(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> bruteForce(null, someRotation));
assertThrows(IllegalArgumentException.class, () -> withExtraArray(null, someRotation));
assertThrows(IllegalArgumentException.class, () -> cyclicReplacement(null, someRotation));
assertThrows(IllegalArgumentException.class, () -> reverse(null, someRotation));
}
@Test
void givenInputArray_whenUseBruteForceRotationLtArrayLength_thenRotateArrayOk() {
bruteForce(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseBruteForceRotationGtArrayLength_thenRotateArrayOk() {
bruteForce(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseBruteForceRotationEqArrayLength_thenDoNothing() {
int[] expected = arr.clone();
bruteForce(arr, arr.length);
assertArrayEquals(expected, arr);
}
@Test
void givenInputArray_whenUseExtraArrayRotationLtArrayLength_thenRotateArrayOk() {
withExtraArray(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseExtraArrayRotationGtArrayLength_thenRotateArrayOk() {
withExtraArray(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseExtraArrayWithRotationEqArrayLength_thenDoNothing() {
int[] clone = arr.clone();
withExtraArray(arr, arr.length);
assertArrayEquals(clone, arr);
}
@Test
void givenInputArray_whenUseCyclicReplacementRotationLtArrayLength_thenRotateArrayOk() {
cyclicReplacement(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseCyclicReplacementRotationGtArrayLength_thenRotateArrayOk() {
cyclicReplacement(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseCyclicReplacementRotationEqArrayLength_thenDoNothing() {
int[] clone = arr.clone();
cyclicReplacement(arr, arr.length);
assertArrayEquals(clone, arr);
}
@Test
void givenInputArray_whenUseReverseRotationLtArrayLength_thenRotateArrayOk() {
reverse(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseReverseRotationGtArrayLength_thenRotateArrayOk() {
reverse(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseReverseRotationEqArrayLength_thenDoNothing() {
int[] clone = arr.clone();
reverse(arr, arr.length);
assertArrayEquals(clone, arr);
}
}

View File

@ -13,3 +13,4 @@ You can build the project from the command line using: *mvn clean install*, or i
- [Read Data From the Beginning Using Kafka Consumer API](https://www.baeldung.com/java-kafka-consumer-api-read)
- [Get Partition Count for a Topic in Kafka](https://www.baeldung.com/java-kafka-partition-count-topic)
- [bootstrap-server in Kafka Configuration](https://www.baeldung.com/java-kafka-bootstrap-server)
- [Introduction to Apache Kafka](https://www.baeldung.com/apache-kafka)

View File

@ -0,0 +1,103 @@
package com.baeldung.kafka.multipletopics;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
// This live test needs a Docker Daemon running so that a kafka container can be created
@Testcontainers
public class MultipleTopicsLiveTest {
private final Logger log = LoggerFactory.getLogger(MultipleTopicsLiveTest.class);
private static final String CARD_PAYMENTS_TOPIC = "card-payments";
private static final String BANK_TRANSFERS_TOPIC = "bank-transfers";
private static KafkaProducer<String, String> producer;
private static KafkaConsumer<String, String> consumer;
@Container
private static final KafkaContainer KAFKA_CONTAINER = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"));
@BeforeAll
static void setup() {
KAFKA_CONTAINER.addExposedPort(9092);
producer = new KafkaProducer<>(getProducerProperties());
consumer = new KafkaConsumer<>(getConsumerProperties());
}
@AfterAll
static void destroy() {
KAFKA_CONTAINER.stop();
}
private static Properties getProducerProperties() {
Properties producerProperties = new Properties();
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_CONTAINER.getBootstrapServers());
producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
return producerProperties;
}
private static Properties getConsumerProperties() {
Properties consumerProperties = new Properties();
consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_CONTAINER.getBootstrapServers());
consumerProperties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerProperties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, "payments");
return consumerProperties;
}
@Test
void whenSendingMessagesOnTwoTopics_thenConsumerReceivesMessages() throws Exception {
publishMessages();
consumer.subscribe(Arrays.asList(CARD_PAYMENTS_TOPIC, BANK_TRANSFERS_TOPIC));
int eventsProcessed = 0;
for (ConsumerRecord<String, String> record : consumer.poll(Duration.ofSeconds(10))) {
log.info("Event on topic={}, payload={}", record.topic(), record.value());
eventsProcessed++;
}
assertThat(eventsProcessed).isEqualTo(2);
}
private void publishMessages() throws ExecutionException, InterruptedException {
ProducerRecord<String, String> cardPayment = new ProducerRecord<>(CARD_PAYMENTS_TOPIC, createCardPayment());
producer.send(cardPayment).get();
ProducerRecord<String, String> bankTransfer = new ProducerRecord<>(BANK_TRANSFERS_TOPIC, createBankTransfer());
producer.send(bankTransfer).get();
}
private String createCardPayment() {
return "{\"paymentReference\":\"A184028KM0013790\", \"type\":\"card\", \"amount\":\"275\", \"currency\":\"GBP\"}";
}
private String createBankTransfer() {
return "{\"paymentReference\":\"19ae2-18mk73-009\", \"type\":\"bank\", \"amount\":\"150\", \"currency\":\"EUR\"}";
}
}

View File

@ -30,6 +30,11 @@ public final class Point {
return new Point(x, y, z);
}
@Override
public String toString() {
return "Point{" + "x=" + x + ", y=" + y + ", z=" + z + '}';
}
@Override
public boolean equals(Object other) {
if (other == null || getClass() != other.getClass())

View File

@ -1,14 +1,17 @@
package com.baeldung.value_based_class;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
public class ValueBasedClassUnitTest {
@Test
public void givenAutoboxedAndPrimitive_whenCompared_thenReturnEquals() {
int primitive_a = 125;
Integer obj_a = 125; // this is autoboxed
Assert.assertSame(primitive_a, obj_a);
List<Integer> list = new ArrayList<>();
list.add(1); // this is autoboxed
Assert.assertEquals(list.get(0), Integer.valueOf(1));
}
@Test

View File

View File

@ -0,0 +1,55 @@
<?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-18</artifactId>
<name>core-java-18</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>${maven.compiler.release}</release>
<compilerArgs>--enable-preview</compilerArgs>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<forkCount>1</forkCount>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-api</artifactId>
<version>${surefire.plugin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source.version>18</maven.compiler.source.version>
<maven.compiler.target.version>18</maven.compiler.target.version>
<maven.compiler.release>18</maven.compiler.release>
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
</properties>
</project>

View File

@ -0,0 +1,27 @@
package com.baeldung.finalization_closeable_cleaner;
import java.io.FileInputStream;
import java.io.IOException;
public class FinalizationExamples {
FileInputStream fis = null;
public void readFileOperationWithFinalization() throws IOException {
try {
fis = new FileInputStream("input.txt");
// perform operation on the file
System.out.println(fis.readAllBytes().length);
} finally {
if (fis != null)
fis.close();
}
}
public void readFileOperationWithTryWith() throws IOException {
try (FileInputStream fis = new FileInputStream("input.txt")) {
// perform operations
System.out.println(fis.readAllBytes().length);
}
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.finalization_closeable_cleaner;
import java.lang.ref.Cleaner;
public class MyCleanerResourceClass implements AutoCloseable {
private static Resource resource;
private static final Cleaner cleaner = Cleaner.create();
private final Cleaner.Cleanable cleanable;
public MyCleanerResourceClass() {
resource = new Resource();
this.cleanable = cleaner.register(this, new CleaningState());
}
public void useResource() {
// using the resource here
resource.use();
}
@Override
public void close() {
// perform actions to close all underlying resources
this.cleanable.clean();
}
static class CleaningState implements Runnable {
CleaningState() {
// constructor
}
@Override
public void run() {
// some cleanup action
System.out.println("Cleanup done");
}
}
static class Resource {
void use() {
System.out.println("Using the resource");
}
void close() {
System.out.println("Cleanup done");
}
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.finalization_closeable_cleaner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MyCloseableResourceClass implements AutoCloseable {
private final FileInputStream fis;
public MyCloseableResourceClass() throws FileNotFoundException {
this.fis = new FileInputStream("src/main/resources/file.txt");
}
public int getByteLength() throws IOException {
System.out.println("Some operation");
return this.fis.readAllBytes().length;
}
@Override
public void close() throws IOException {
System.out.println("Finalized object");
this.fis.close();
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.finalization_closeable_cleaner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MyFinalizableResourceClass {
private FileInputStream fis;
public MyFinalizableResourceClass() throws FileNotFoundException {
this.fis = new FileInputStream("src/main/resources/file.txt");
}
public int getByteLength() throws IOException {
System.out.println("Some operation");
return this.fis.readAllBytes().length;
}
@Override
protected void finalize() throws Throwable {
System.out.println("Finalized object");
this.fis.close();
}
}

View File

@ -0,0 +1 @@
This is a test file.

View File

@ -0,0 +1,36 @@
package com.baeldung.finalization_closeable_cleaner;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
public class FinalizationCloseableCleanerUnitTest {
@Test
public void givenMyFinalizationResource_whenUsingFinalize_thenShouldClean() {
assertDoesNotThrow(() -> {
MyFinalizableResourceClass mfr = new MyFinalizableResourceClass();
mfr.getByteLength();
});
}
@Test
public void givenMyCleanerResource_whenUsingCleanerAPI_thenShouldClean() {
assertDoesNotThrow(() -> {
try (MyCleanerResourceClass myCleanerResourceClass = new MyCleanerResourceClass()) {
myCleanerResourceClass.useResource();
}
});
}
@Test
public void givenCloseableResource_whenUsingTryWith_thenShouldClose() throws IOException {
int length = 0;
try (MyCloseableResourceClass mcr = new MyCloseableResourceClass()) {
length = mcr.getByteLength();
}
Assert.assertEquals(20, length);
}
}

View File

@ -1,3 +1,5 @@
## Relevant Articles
- [Sequenced Collections in Java 21](https://www.baeldung.com/java-21-sequenced-collections)
- [String Templates in Java 21](https://www.baeldung.com/java-21-string-templates)
- [Unnamed Classes and Instance Main Methods in Java 21](https://www.baeldung.com/java-21-unnamed-class-instance-main)
- [Unnamed Patterns and Variables in Java 21](https://www.baeldung.com/java-unnamed-patterns-variables)

View File

@ -0,0 +1,67 @@
package com.baeldung.array.conversions;
import java.util.Arrays;
public class CharArrayToIntArrayUtils {
static int[] usingGetNumericValueMethod(char[] chars) {
if (chars == null) {
return null;
}
int[] ints = new int[chars.length];
for (int i = 0; i < chars.length; i++) {
ints[i] = Character.getNumericValue(chars[i]);
}
return ints;
}
static int[] usingDigitMethod(char[] chars) {
if (chars == null) {
return null;
}
int[] ints = new int[chars.length];
for (int i = 0; i < chars.length; i++) {
ints[i] = Character.digit(chars[i], 10);
}
return ints;
}
static int[] usingStreamApiMethod(char[] chars) {
if (chars == null) {
return null;
}
return new String(chars).chars()
.map(c -> c - 48)
.toArray();
}
static int[] usingParseIntMethod(char[] chars) {
if (chars == null) {
return null;
}
int[] ints = new int[chars.length];
for (int i = 0; i < chars.length; i++) {
ints[i] = Integer.parseInt(String.valueOf(chars[i]));
}
return ints;
}
static int[] usingArraysSetAllMethod(char[] chars) {
if (chars == null) {
return null;
}
int[] ints = new int[chars.length];
Arrays.setAll(ints, i -> Character.getNumericValue(chars[i]));
return ints;
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.array.conversions;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
class CharArrayToIntArrayUtilsUnitTest {
@Test
void givenCharArray_whenUsingGetNumericValueMethod_shouldGetIntArray() {
int[] expected = { 2, 3, 4, 5 };
char[] chars = { '2', '3', '4', '5' };
int[] result = CharArrayToIntArrayUtils.usingGetNumericValueMethod(chars);
assertArrayEquals(expected, result);
}
@Test
void givenCharArray_whenUsingDigitMethod_shouldGetIntArray() {
int[] expected = { 1, 2, 3, 6 };
char[] chars = { '1', '2', '3', '6' };
int[] result = CharArrayToIntArrayUtils.usingDigitMethod(chars);
assertArrayEquals(expected, result);
}
@Test
void givenCharArray_whenUsingStreamApi_shouldGetIntArray() {
int[] expected = { 9, 8, 7, 6 };
char[] chars = { '9', '8', '7', '6' };
int[] result = CharArrayToIntArrayUtils.usingStreamApiMethod(chars);
assertArrayEquals(expected, result);
}
@Test
void givenCharArray_whenUsingParseIntMethod_shouldGetIntArray() {
int[] expected = { 9, 8, 7, 6 };
char[] chars = { '9', '8', '7', '6' };
int[] result = CharArrayToIntArrayUtils.usingParseIntMethod(chars);
assertArrayEquals(expected, result);
}
@Test
void givenCharArray_whenUsingArraysSetAllMethod_shouldGetIntArray() {
int[] expected = { 4, 9, 2, 3 };
char[] chars = { '4', '9', '2', '3' };
int[] result = CharArrayToIntArrayUtils.usingArraysSetAllMethod(chars);
assertArrayEquals(expected, result);
}
}

View File

@ -5,3 +5,4 @@ This module contains articles about Java Character Class
### Relevant Articles:
- [Character#isAlphabetic vs. Character#isLetter](https://www.baeldung.com/java-character-isletter-isalphabetic)
- [Difference Between Javas “char” and “String”](https://www.baeldung.com/java-char-vs-string)
- [Increment Character in Java](https://www.baeldung.com/java-char-sequence)

View File

@ -5,4 +5,6 @@
### Relevant Articles:
- [Introduction to Roaring Bitmap](https://www.baeldung.com/java-roaring-bitmap-intro)
- [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)
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-4)

View File

@ -3,4 +3,4 @@
This module contains articles about the Java ArrayList collection
### Relevant Articles:
- [Create an ArrayList with Multiple Object Types](https://www.baeldung.com/arraylist-with-multiple-object-types)
- [Create an ArrayList with Multiple Object Types](https://www.baeldung.com/java-arraylist-multiple-object-types)

View File

@ -3,3 +3,4 @@
This module contains articles about conversions among Collection types in Java.
### Relevant Articles:
- [Converting HashMap Values to an ArrayList in Java](https://www.baeldung.com/java-hashmap-arraylist)

View File

@ -0,0 +1 @@
## Relevant Articles

View File

@ -0,0 +1,78 @@
<?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-maps-7</artifactId>
<name>core-java-collections-maps-7</name>
<packaging>jar</packaging>
<parent>
<artifactId>core-java-modules</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<spring.version>5.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.36</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.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.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,60 @@
package com.baeldung.map;
import java.util.HashMap;
import java.util.Map;
public class ConvertHashMapStringToHashMapObjectUsingtoString {
public String name;
public int age;
public ConvertHashMapStringToHashMapObjectUsingtoString(String name, int age) {
this.name = name;
this.age = age;
}
public static ConvertHashMapStringToHashMapObjectUsingtoString deserializeCustomObject(String valueString) {
if (valueString.startsWith("{") && valueString.endsWith("}")) {
valueString = valueString.substring(1, valueString.length() - 1);
String[] parts = valueString.split(",");
String name = null;
int age = -1;
for (String part : parts) {
String[] keyValue = part.split("=");
if (keyValue.length == 2) {
String key = keyValue[0].trim();
String val = keyValue[1].trim();
if (key.equals("name")) {
name = val;
} else if (key.equals("age")) {
age = Integer.parseInt(val);
}
}
}
if (name != null && age >= 0) {
return new ConvertHashMapStringToHashMapObjectUsingtoString(name, age);
}
}
return new ConvertHashMapStringToHashMapObjectUsingtoString("", -1);
}
public static void main(String[] args) {
String hashMapString = "{key1={name=John, age=30}, key2={name=Alice, age=25}}";
String keyValuePairs = hashMapString.replaceAll("[{}\\s]", "");
String[] pairs = keyValuePairs.split(",");
Map<String, ConvertHashMapStringToHashMapObjectUsingtoString> actualHashMap = new HashMap<>();
for (String pair : pairs) {
String[] keyValue = pair.split("=");
if (keyValue.length == 2) {
String key = keyValue[0];
ConvertHashMapStringToHashMapObjectUsingtoString value = deserializeCustomObject(keyValue[1]);
actualHashMap.put(key, value);
}
}
System.out.println(actualHashMap);
}
@Override
public String toString() {
return "{name=" + name + ", age=" + age + "}";
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.map;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ConvertHashMapStringToHashMapObjectUsingtoStringUnitTest {
@Test
void givenValidCustomObject_whenSerializing_thenSerializedStringIsCorrect() {
ConvertHashMapStringToHashMapObjectUsingtoString customObject = new ConvertHashMapStringToHashMapObjectUsingtoString("John", 30);
String expectedSerializedString = "{name=John, age=30}";
assertEquals(expectedSerializedString, customObject.toString());
}
@Test
void givenValidSerializedString_whenDeserializing_thenCustomObjectIsCorrect() {
String serializedString = "{name=Alice, age=25}";
ConvertHashMapStringToHashMapObjectUsingtoString customObject = ConvertHashMapStringToHashMapObjectUsingtoString.deserializeCustomObject(serializedString);
assertEquals("Alice", customObject.name);
assertEquals(25, customObject.age);
}
@Test
void givenInvalidSerializedString_whenDeserializing_thenDefaultCustomObjectIsCreated() {
String invalidSerializedString = "{invalidString}";
ConvertHashMapStringToHashMapObjectUsingtoString customObject = ConvertHashMapStringToHashMapObjectUsingtoString.deserializeCustomObject(invalidSerializedString);
assertEquals("", customObject.name);
assertEquals(-1, customObject.age);
}
}

View File

@ -12,10 +12,10 @@ public class EpochTimeToLocalDateTimeConverterUnitTest {
@Test
public void testConvertEpochTimeToLocalDateTime() {
long epochTimeMillis = 1624962431000L; // Example epoch time in milliseconds
LocalDateTime expectedDateTime = LocalDateTime.of(2021, 6, 29, 12, 13, 51);
LocalDateTime expectedDateTime = LocalDateTime.of(2021, 6, 29, 10, 27, 11);
Instant instant = Instant.ofEpochMilli(epochTimeMillis);
ZoneId zoneId = ZoneId.systemDefault();
ZoneId zoneId = ZoneId.of("UTC");
LocalDateTime actualDateTime = instant.atZone(zoneId).toLocalDateTime();
assertEquals(expectedDateTime, actualDateTime);

View File

@ -3,6 +3,6 @@
This module contains articles about core Java input and output (IO)
### Relevant Articles:
- [Get File Extension From MIME Type in Java](https://www.baeldung.com/java-mime-type-file-extension)
- [[<-- Prev]](/core-java-modules/core-java-io-4)

View File

@ -0,0 +1,72 @@
package com.baeldung.rmlinebreaks;
import org.junit.jupiter.api.Test;
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 static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
public class RemoveLinebreaksUnitTest {
private Path file1Path() throws Exception {
return Paths.get(this.getClass().getClassLoader().getResource("multiple-line-1.txt").toURI());
}
private Path file2Path() throws Exception {
return Paths.get(this.getClass().getClassLoader().getResource("multiple-line-2.txt").toURI());
}
@Test
void whenRemovingLineSeparatorFromFile1_thenGetTheExpectedResult() throws Exception {
String content = Files.readString(file1Path(), StandardCharsets.UTF_8);
String result = content.replace(System.getProperty("line.separator"), "");
assertEquals("A, B, C, D, E, F", result);
}
@Test
void whenRemovingLineSeparatorFromFile2_thenNotGetTheExpectedResult() throws Exception {
String content = Files.readString(file2Path(), StandardCharsets.UTF_8);
String result = content.replace(System.getProperty("line.separator"), "");
assertNotEquals("A, B, C, D, E, F", result); // <-- NOT equals assertion!
}
@Test
void whenRemovingAllLinebreaks_thenGetTheExpectedResult() throws Exception {
String content1 = Files.readString(file1Path(), StandardCharsets.UTF_8);
// file contains CRLF
String content2 = Files.readString(file2Path(), StandardCharsets.UTF_8);
String result1 = content1.replace("\r", "").replace("\n", "");
String result2 = content2.replace("\r", "").replace("\n", "");
assertEquals("A, B, C, D, E, F", result1);
assertEquals("A, B, C, D, E, F", result2);
String resultReplaceAll = content2.replaceAll("[\\n\\r]", "");
assertEquals("A, B, C, D, E, F", resultReplaceAll);
}
@Test
void whenUsingReadAllLinesAndJoin_thenGetExpectedResult() throws Exception {
List<String> lines1 = Files.readAllLines(file1Path(), StandardCharsets.UTF_8);
// file contains CRLF
List<String> lines2 = Files.readAllLines(file2Path(), StandardCharsets.UTF_8);
String result1 = String.join("", lines1);
String result2 = String.join("", lines2);
assertEquals("A, B, C, D, E, F", result1);
assertEquals("A, B, C, D, E, F", result2);
}
}

View File

@ -0,0 +1,6 @@
A,
B,
C,
D,
E,
F

View File

@ -0,0 +1,4 @@
A, B,
C,
D, E,
F

View File

@ -2,10 +2,15 @@ package com.baeldung.networking.url;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import org.apache.http.client.utils.URIBuilder;
@ -149,4 +154,24 @@ public class UrlUnitTest {
assertEquals("http://baeldung.com:9090/articles?topic=java&version=8", url.toString());
}
@Test
public void givenURI_whenConvertingToURL_thenCorrect() throws IOException, URISyntaxException {
String aURIString = "http://courses.baeldung.com";
URI uri = new URI(aURIString);
URL url = uri.toURL();
assertNotNull(url);
assertEquals(aURIString, url.toString());
}
@Test
public void givenPath_whenConvertingToURIAndThenURL_thenCorrect() throws IOException, URISyntaxException {
String finalPath = "file:/D:/baeldung/java-url";
Path path = Paths.get("/baeldung/java-url");
URI uri = path.toUri();
URL url = uri.toURL();
assertNotNull(url);
// Adapt the finalPath value to match your own path
// assertEquals(finalPath, url.toString());
}
}

View File

@ -1,6 +1,8 @@
package com.baeldung.integerToBinary;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class IntegerToBinaryUnitTest {
@ -24,4 +26,18 @@ public class IntegerToBinaryUnitTest {
String binaryString = Integer.toString(n, 2);
assertEquals("111", binaryString);
}
}
@Test
public void givenAnInteger_whenFormatAndReplaceCalled_thenZeroPaddedBinaryString() {
int n = 7;
String binaryString = String.format("%8s", Integer.toBinaryString(n)).replace(" ", "0");
assertEquals("00000111", binaryString);
}
@Test
public void givenAnInteger_whenUsingApacheStringUtils_thenZeroPaddedBinaryString() {
int n = 7;
String binaryString = StringUtils.leftPad(Integer.toBinaryString(n), 8, "0");
assertEquals("00000111", binaryString);
}
}

View File

@ -25,8 +25,12 @@
<version>${commons-codec}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-numbers-6</finalName>
<resources>
@ -39,5 +43,6 @@
<properties>
<commons-codec>1.15</commons-codec>
<guava.version>32.1.2-jre</guava.version>
</properties>
</project>

View File

@ -0,0 +1,8 @@
package com.baeldung.bigintegerroot;
public class BenchmarkRunner {
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.bigintegerroot;
public class BigIntegerHolder {
private BigIntegerHolder() {
}
public static final String BIG_NUMBER = "179769313000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
public static final String VERY_BIG_NUMBER = "32473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834";
public static final String INSANELY_BIG_NUMBER = "3247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834";
}

View File

@ -0,0 +1,60 @@
package com.baeldung.bigintegerroot;
import static com.baeldung.bigintegerroot.BigIntegerHolder.*;
import com.baeldung.bigintegerroot.algorithms.Newton;
import com.baeldung.bigintegerroot.algorithms.NewtonPlus;
import com.google.common.math.BigIntegerMath;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
@Warmup(iterations = 1)
@Measurement(iterations = 1, time = 1, timeUnit = TimeUnit.MINUTES)
@Fork(1)
@State(Scope.Benchmark)
public class BigIntegerSquareRootBenchmark {
@Param({BIG_NUMBER, VERY_BIG_NUMBER, INSANELY_BIG_NUMBER})
public String number;
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void calculateRootWithJava(Blackhole blackhole) {
final BigInteger integer = new BigInteger(number);
final BigInteger root = integer.sqrt();
blackhole.consume(root);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void calculateRootWithGuava(Blackhole blackhole) {
final BigInteger integer = new BigInteger(number);
final BigInteger root = BigIntegerMath.sqrt(integer, RoundingMode.DOWN);
blackhole.consume(root);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void calculateRootWithNewtonPlus(Blackhole blackhole) {
final BigInteger integer = new BigInteger(number);
final BigInteger root = NewtonPlus.sqrt(integer);
blackhole.consume(root);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void calculateRootWithNewton(Blackhole blackhole) {
final BigInteger integer = new BigInteger(number);
final BigInteger root = Newton.sqrt(integer);
blackhole.consume(root);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.bigintegerroot.algorithms;
import java.math.BigInteger;
public class Newton {
private Newton() {
}
public static BigInteger sqrt(BigInteger n) {
// Initial approximation
BigInteger x = n.divide(BigInteger.TWO);
// Tolerance level (small positive integer)
BigInteger tolerance = BigInteger.ONE;
while (true) {
// x_new = 0.5 * (x + n / x)
BigInteger xNew = x.add(n.divide(x)).divide(BigInteger.TWO);
// Check for convergence within tolerance
if (x.subtract(xNew).abs().compareTo(tolerance) <= 0) {
return xNew;
}
x = xNew;
}
}
}

View File

@ -0,0 +1,108 @@
package com.baeldung.bigintegerroot.algorithms;
import java.math.BigInteger;
public class NewtonPlus {
private NewtonPlus() {
}
// A fast square root by Ryan Scott White.
public static BigInteger sqrt(BigInteger x) {
if (x.compareTo(BigInteger.valueOf(144838757784765629L)) < 0) {
long xAsLong = x.longValue();
long vInt = (long)Math.sqrt(xAsLong);
if (vInt * vInt > xAsLong)
vInt--;
return BigInteger.valueOf(vInt); }
double xAsDub = x.doubleValue();
BigInteger val;
if (xAsDub < 2.1267e37) // 2.12e37 largest here
// since sqrt(long.max*long.max) > long.max
{
long vInt = (long)Math.sqrt(xAsDub);
val = BigInteger.valueOf
((vInt + x.divide(BigInteger.valueOf(vInt)).longValue()) >> 1);
}
else if (xAsDub < 4.3322e127) {
// Convert a double to a BigInteger
long bits = Double.doubleToLongBits(Math.sqrt(xAsDub));
int exp = ((int) (bits >> 52) & 0x7ff) - 1075;
val = BigInteger.valueOf((bits & ((1L << 52)) - 1) | (1L << 52)).shiftLeft(exp);
val = x.divide(val).add(val).shiftRight(1);
if (xAsDub > 2e63) {
val = x.divide(val).add(val).shiftRight(1); }
}
else // handle large numbers over 4.3322e127
{
int xLen = x.bitLength();
int wantedPrecision = ((xLen + 1) / 2);
int xLenMod = xLen + (xLen & 1) + 1;
//////// Do the first Sqrt on Hardware ////////
long tempX = x.shiftRight(xLenMod - 63).longValue();
double tempSqrt1 = Math.sqrt(tempX);
long valLong = Double.doubleToLongBits(tempSqrt1) & 0x1fffffffffffffL;
if (valLong == 0)
valLong = 1L << 53;
//////// Classic Newton Iterations ////////
val = BigInteger.valueOf(valLong).shiftLeft(53 - 1)
.add((x.shiftRight(xLenMod -
(3 * 53))).divide(BigInteger.valueOf(valLong)));
int size = 106;
for (; size < 256; size <<= 1) {
val = val.shiftLeft(size - 1).add(x.shiftRight
(xLenMod - (3*size)).divide(val));}
if (xAsDub > 4e254) { // 4e254 = 1<<845.77
int numOfNewtonSteps = 31 -
Integer.numberOfLeadingZeros(wantedPrecision / size)+1;
////// Apply Starting Size ////////
int wantedSize = (wantedPrecision >> numOfNewtonSteps) + 2;
int needToShiftBy = size - wantedSize;
val = val.shiftRight(needToShiftBy);
size = wantedSize;
do {
//////// Newton Plus Iteration ////////
int shiftX = xLenMod - (3 * size);
BigInteger valSqrd = val.multiply(val).shiftLeft(size - 1);
BigInteger valSU = x.shiftRight(shiftX).subtract(valSqrd);
val = val.shiftLeft(size).add(valSU.divide(val));
size *= 2;
} while (size < wantedPrecision);
}
val = val.shiftRight(size - wantedPrecision);
}
// Detect a round ups. This function can be further optimized - see article.
// For a ~7% speed bump the following line can be removed but round-ups will occur.
if (val.multiply(val).compareTo(x) > 0)
val = val.subtract(BigInteger.ONE);
// Enabling the below will guarantee an error is stopped for larger numbers.
// Note: As of this writing, there are no known errors.
BigInteger tmp = val.multiply(val);
if (tmp.compareTo(x) > 0) {
System.out.println("val^2(" + val.multiply(val).toString()
+ ") ≥ x(" + x.toString()+")");
System.console().readLine();
//throw new Exception("Sqrt function had internal error - value too high");
}
if (tmp.add(val.shiftLeft(1)).add(BigInteger.ONE).compareTo(x) <= 0) {
System.out.println("(val+1)^2("
+ val.add(BigInteger.ONE).multiply(val.add(BigInteger.ONE)).toString()
+ ") ≥ x(" + x.toString() + ")");
System.console().readLine();
//throw new Exception("Sqrt function had internal error - value too low");
}
return val;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.bigintegerroot;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.baeldung.bigintegerroot.algorithms.Newton;
import com.baeldung.bigintegerroot.algorithms.NewtonPlus;
import com.google.common.math.BigIntegerMath;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.stream.Stream;
import org.apache.commons.math3.util.Pair;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class BigIntegerSquareRootUnitTest {
@ParameterizedTest
@ValueSource(strings = {
BigIntegerHolder.BIG_NUMBER,
BigIntegerHolder.VERY_BIG_NUMBER,
BigIntegerHolder.VERY_BIG_NUMBER
})
void squareRootTest(String number) {
final BigInteger bigInteger = new BigInteger(number);
final BigInteger javaRoot = bigInteger.sqrt();
final BigInteger guavaRoot = BigIntegerMath.sqrt(bigInteger, RoundingMode.DOWN);
final BigInteger newtonRoot = Newton.sqrt(bigInteger);
final BigInteger newtonPlusRoot = NewtonPlus.sqrt(bigInteger);
assertTrue(Stream.of(
new Pair<>(javaRoot, guavaRoot),
new Pair<>(guavaRoot, newtonRoot),
new Pair<>(newtonRoot, newtonPlusRoot)
).allMatch(pair -> pair.getFirst().equals(pair.getSecond())));
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.javadoublevsbigdecimal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.junit.jupiter.api.Test;
public class BigDecimalConversionUnitTest {
@Test
void whenConvertingDoubleToBigDecimal_thenConversionIsCorrect() {
double doubleValue = 123.456;
BigDecimal bigDecimalValue = BigDecimal.valueOf(doubleValue);
BigDecimal expected = new BigDecimal("123.456").setScale(3, RoundingMode.HALF_UP);
assertEquals(expected, bigDecimalValue.setScale(3, RoundingMode.HALF_UP));
}
@Test
void givenDecimalPlacesGreaterThan15_whenConvertingBigDecimalToDouble_thenPrecisionIsLost() {
BigDecimal bigDecimalValue = new BigDecimal("789.1234567890123456");
double doubleValue = bigDecimalValue.doubleValue();
BigDecimal convertedBackToBigDecimal = BigDecimal.valueOf(doubleValue);
assertNotEquals(bigDecimalValue, convertedBackToBigDecimal);
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.javadoublevsbigdecimal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.junit.jupiter.api.Test;
public class BigDecimalUnitTest {
private BigDecimal bigDecimal1 = new BigDecimal("124567890.0987654321");
private BigDecimal bigDecimal2 = new BigDecimal("987654321.123456789");
@Test
public void givenTwoBigDecimals_whenAdd_thenCorrect() {
BigDecimal expected = new BigDecimal("1112222211.2222222211");
BigDecimal actual = bigDecimal1.add(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenMultiply_thenCorrect() {
BigDecimal expected = new BigDecimal("123030014929277547.5030955772112635269");
BigDecimal actual = bigDecimal1.multiply(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenSubtract_thenCorrect() {
BigDecimal expected = new BigDecimal("-863086431.0246913569");
BigDecimal actual = bigDecimal1.subtract(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenDivide_thenCorrect() {
BigDecimal expected = new BigDecimal("0.13");
BigDecimal actual = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
assertEquals(expected, actual);
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.javadoublevsbigdecimal;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class JavaDoubleUnitTest {
@Test
public void givenDoubleLiteral_whenAssigningToDoubleVariable_thenValueIsNotExactlyEqual() {
double doubleValue = 0.1;
double epsilon = 0.0000000000000001;
assertEquals(0.1, doubleValue, epsilon);
}
}

View File

@ -9,25 +9,21 @@ public class FloatDoubleConversionsTest {
public void whenDoubleType_thenFloatTypeSuccess(){
double interestRatesYearly = 13.333333333333334;
float interest = (float) interestRatesYearly;
System.out.println(interest); //13.333333
Assert.assertTrue(Float.class.isInstance(interest));//true
Assert.assertEquals(13.333333f, interest, 0.000004f);
Double monthlyRates = 2.111111111111112;
float rates = monthlyRates.floatValue();
System.out.println(rates); //2.1111112
Assert.assertTrue(Float.class.isInstance(rates));//true
Assert.assertEquals(2.1111112f, rates, 0.00000013);
}
@Test
public void whenFloatType_thenDoubleTypeSuccess(){
float gradeAverage =2.05f;
double average = gradeAverage;
System.out.println(average); //2.049999952316284
Assert.assertTrue(Double.class.isInstance(average));//true
Assert.assertEquals(2.05, average, 0.06);
Float monthlyRates = 2.1111112f;
Double rates = monthlyRates.doubleValue();
System.out.println(rates); //2.1111112
Assert.assertTrue(Double.class.isInstance(rates));//true
Assert.assertEquals(2.11111112, rates, 0.0000002);//true
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.floattobigdecimal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;
class FloatToBigDecimalUnitTest {
@Test
public void whenFloatComparedWithDifferentValues_thenCouldMatch() {
assertNotEquals(1.1f, 1.09f);
assertEquals(1.1f, 1.09999999f);
}
@Test
public void whenCreatedFromFloat_thenMatchesInternallyStoredValue() {
float floatToConvert = 1.10000002384185791015625f;
BigDecimal bdFromFloat = new BigDecimal(floatToConvert);
assertEquals("1.10000002384185791015625", bdFromFloat.toString());
}
@Test
public void whenCreatedFromString_thenPreservesTheOriginal() {
BigDecimal bdFromString = new BigDecimal("1.1");
assertEquals("1.1", bdFromString.toString());
}
@Test
public void whenCreatedFromFloatConvertedToString_thenFloatInternalValueGetsTruncated() {
String floatValue = Float.toString(1.10000002384185791015625f);
BigDecimal bdFromString = new BigDecimal(floatValue);
assertEquals("1.1", floatValue);
assertEquals("1.1", bdFromString.toString());
}
@Test
public void whenCreatedByValueOf_thenFloatValueGetsTruncated() {
assertEquals("1.100000023841858", BigDecimal.valueOf(1.10000002384185791015625f).toString());
}
@Test
public void whenDoubleConvertsFloatToString_thenFloatValueGetsTruncated() {
assertEquals("1.100000023841858", Double.toString(1.10000002384185791015625f));
}
}

View File

@ -14,5 +14,6 @@ This module contains articles about working with the operating system (OS) in Ja
- [How to Run a Shell Command in Java](http://www.baeldung.com/run-shell-command-in-java)
- [Taking Screenshots Using Java](https://www.baeldung.com/java-taking-screenshots)
- [Java Sound API Capturing Microphone](https://www.baeldung.com/java-sound-api-capture-mic)
- [How to Detect the Username Using Java](https://www.baeldung.com/java-get-username)
This module uses Java 9, so make sure to have the JDK 9 installed to run it.

View File

@ -9,4 +9,5 @@
- [Regular Expression: \z vs \Z Anchors in Java](https://www.baeldung.com/java-regular-expression-z-vs-z-anchors)
- [Extract Text Between Square Brackets](https://www.baeldung.com/java-get-content-between-square-brackets)
- [Get the Indexes of Regex Pattern Matches in Java](https://www.baeldung.com/java-indexes-regex-pattern-matches)
- [Check if a String is Strictly Alphanumeric With Java](https://www.baeldung.com/java-check-string-contains-only-letters-numbers)
- More articles: [[<-- prev]](/core-java-modules/core-java-regex)

View File

@ -7,7 +7,7 @@ import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class SecureConnection {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Use: SecureConnection host port");
@ -20,20 +20,20 @@ public class SecureConnection {
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(host, port);
InputStream in = sslsocket.getInputStream();
OutputStream out = sslsocket.getOutputStream();
out.write(1);
while (in.available() > 0) {
System.out.print(in.read());
}
System.out.println("Secured connection performed successfully");
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Get the host from arguments
* @param args the arguments
@ -42,7 +42,7 @@ public class SecureConnection {
private static String getHost(String[] args) {
return args[0];
}
/**
* Get the port from arguments
* @param args the arguments

View File

@ -15,10 +15,8 @@ public class SimpleClient {
SocketFactory factory = SSLSocketFactory.getDefault();
try (Socket connection = factory.createSocket(host, port)) {
((SSLSocket) connection).setEnabledCipherSuites(
new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"});
((SSLSocket) connection).setEnabledProtocols(
new String[] { "TLSv1.2"});
((SSLSocket) connection).setEnabledCipherSuites(new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" });
((SSLSocket) connection).setEnabledProtocols(new String[] { "TLSv1.2" });
SSLParameters sslParams = new SSLParameters();
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
((SSLSocket) connection).setSSLParameters(sslParams);
@ -28,6 +26,7 @@ public class SimpleClient {
}
public static void main(String[] args) throws IOException {
System.setProperty("javax.net.debug", "ssl:handshake");
System.out.println(startClient("localhost", 8443));
}
}

View File

@ -14,11 +14,10 @@ public class SimpleServer {
ServerSocketFactory factory = SSLServerSocketFactory.getDefault();
try (ServerSocket listener = factory.createServerSocket(port)) {
System.setProperty("javax.net.debug", "ssl:handshake");
((SSLServerSocket) listener).setNeedClientAuth(true);
((SSLServerSocket) listener).setEnabledCipherSuites(
new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"});
((SSLServerSocket) listener).setEnabledProtocols(
new String[] { "TLSv1.2"});
((SSLServerSocket) listener).setEnabledCipherSuites(new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" });
((SSLServerSocket) listener).setEnabledProtocols(new String[] { "TLSv1.2" });
while (true) {
try (Socket socket = listener.accept()) {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
@ -29,6 +28,7 @@ public class SimpleServer {
}
public static void main(String[] args) throws IOException {
System.setProperty("javax.net.debug", "ssl:handshake");
startServer(8443);
}
}

View File

@ -43,6 +43,11 @@
<artifactId>vavr</artifactId>
<version>${vavr.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<build>
@ -72,6 +77,7 @@
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
<vavr.version>0.10.2</vavr.version>
<guava.version>32.1.2-jre</guava.version>
</properties>
</project>

View File

@ -0,0 +1,90 @@
package com.baeldung.streams.partitioning;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import com.google.common.collect.Iterables;
public class PartitionStream {
public static <T> Stream<List<T>> partitionList(List<T> source, int batchSize) {
if (batchSize <= 0) {
throw new IllegalArgumentException(String.format("Expected the batchSize to be greater than ZERO, actual value was: %s", batchSize));
}
if (source.isEmpty()) {
return Stream.empty();
}
int nrOfFullBatches = (source.size() - 1) / batchSize;
return IntStream.rangeClosed(0, nrOfFullBatches)
.mapToObj(batch -> {
int startIndex = batch * batchSize;
int endIndex = (batch == nrOfFullBatches) ? source.size() : (batch + 1) * batchSize;
return source.subList(startIndex, endIndex);
});
}
public static <T> Iterable<List<T>> partitionUsingGuava(Stream<T> source, int batchSize) {
return Iterables.partition(source::iterator, batchSize);
}
public static <T> List<List<T>> partitionStream(Stream<T> source, int batchSize) {
return source.collect(partitionBySize(batchSize, Collectors.toList()));
}
public static <T, A, R> Collector<T, ?, R> partitionBySize(int batchSize, Collector<List<T>, A, R> downstream) {
Supplier<Accumulator<T, A>> supplier = () -> new Accumulator<>(
batchSize,
downstream.supplier().get(),
downstream.accumulator()::accept
);
BiConsumer<Accumulator<T, A>, T> accumulator = (acc, value) -> acc.add(value);
BinaryOperator<Accumulator<T, A>> combiner = (acc1, acc2) -> acc1.combine(acc2, downstream.combiner());
Function<Accumulator<T, A>, R> finisher = acc -> {
if (!acc.values.isEmpty()) {
downstream.accumulator().accept(acc.downstreamAccumulator, acc.values);
}
return downstream.finisher().apply(acc.downstreamAccumulator);
};
return Collector.of(supplier, accumulator, combiner, finisher, Collector.Characteristics.UNORDERED);
}
static class Accumulator<T, A> {
private final List<T> values = new ArrayList<>();
private final int batchSize;
private A downstreamAccumulator;
private final BiConsumer<A, List<T>> batchFullListener;
Accumulator(int batchSize, A accumulator, BiConsumer<A, List<T>> onBatchFull) {
this.batchSize = batchSize;
this.downstreamAccumulator = accumulator;
this.batchFullListener = onBatchFull;
}
void add(T value) {
values.add(value);
if (values.size() == batchSize) {
batchFullListener.accept(downstreamAccumulator, new ArrayList<>(values));
values.clear();
}
}
Accumulator<T, A> combine(Accumulator<T, A> other, BinaryOperator<A> accumulatorCombiner) {
this.downstreamAccumulator = accumulatorCombiner.apply(downstreamAccumulator, other.downstreamAccumulator);
other.values.forEach(this::add);
return this;
}
}
}

View File

@ -0,0 +1,82 @@
package com.baeldung.partitioning;
import static com.baeldung.streams.partitioning.PartitionStream.partitionList;
import static com.baeldung.streams.partitioning.PartitionStream.partitionStream;
import static com.baeldung.streams.partitioning.PartitionStream.partitionUsingGuava;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.atIndex;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
public class PartitionStreamsUnitTest {
@Test
void whenPartitionList_thenReturnThreeSubLists() {
List<Integer> source = List.of(1, 2, 3, 4, 5, 6, 7, 8);
Stream<List<Integer>> result = partitionList(source, 3);
assertThat(result)
.containsExactlyInAnyOrder(
List.of(1, 2, 3),
List.of(4, 5, 6),
List.of(7, 8)
);
}
@Test
void whenPartitionEmptyList_thenReturnEmptyStream() {
Stream<List<Integer>> result = partitionList(Collections.emptyList(), 3);
assertThat(result).isEmpty();
}
@Test
void whenPartitionListWithNegativeBatchSize_thenThrowException() {
assertThatThrownBy(() -> partitionList(List.of(1,2,3), -1))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Expected the batchSize to be greater than ZERO, actual value was: -1");
}
@Test
void whenPartitionParallelStream_thenReturnThreeSubLists() {
Stream<Integer> source = Stream.of(1, 2, 3, 4, 5, 6, 7, 8).parallel();
List<List<Integer>> result = partitionStream(source, 3);
assertThat(result)
.hasSize(3)
.satisfies(batch -> assertThat(batch).hasSize(3), atIndex(0))
.satisfies(batch -> assertThat(batch).hasSize(3), atIndex(1))
.satisfies(batch -> assertThat(batch).hasSize(2), atIndex(2));
}
@Test
void whenPartitionEmptyParallelStream_thenReturnEmptyList() {
Stream<Integer> source = Stream.<Integer>empty().parallel();
List<List<Integer>> result = partitionStream(source, 3);
assertThat(result).isEmpty();
}
@Test
void whenPartitionParallelStreamWithGuava_thenReturnThreeSubLists() {
Stream<Integer> source = Stream.of(1, 2, 3, 4, 5, 6, 7, 8).parallel();
Iterable<List<Integer>> result = partitionUsingGuava(source, 3);
assertThat(result)
.map(ArrayList::new)
.hasSize(3)
.satisfies(batch -> assertThat(batch).asList().hasSize(3), atIndex(0))
.satisfies(batch -> assertThat(batch).asList().hasSize(3), atIndex(1))
.satisfies(batch -> assertThat(batch).asList().hasSize(2), atIndex(2));
}
}

View File

@ -1,11 +1,11 @@
package com.baeldung.charsequence;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class CharSequenceVsStringUnitTest {
@Test
@ -44,4 +44,43 @@ public class CharSequenceVsStringUnitTest {
assertEquals(firstAddressOfTest, secondAddressOfTest);
}
@Test
public void givenCharSequenceAsString_whenConvertingUsingCasting_thenCorrect() {
String expected = "baeldung";
CharSequence charSequence = "baeldung";
String explicitCastedString = (String) charSequence;
assertEquals(expected, charSequence);
assertEquals(expected, explicitCastedString);
}
@Test(expected = ClassCastException.class)
public void givenCharSequenceAsStringBuiler_whenConvertingUsingCasting_thenThrowException() {
CharSequence charSequence = new StringBuilder("baeldung");
String castedString = (String) charSequence;
}
@Test
public void givenCharSequence_whenConvertingUsingToString_thenCorrect() {
String expected = "baeldung";
CharSequence charSequence1 = "baeldung";
CharSequence charSequence2 = new StringBuilder("baeldung");
assertEquals(expected, charSequence1.toString());
assertEquals(expected, charSequence2.toString());
}
@Test
public void givenCharSequence_whenConvertingUsingValueOf_thenCorrect() {
String expected = "baeldung";
CharSequence charSequence1 = "baeldung";
CharSequence charSequence2 = new StringBuilder("baeldung");
CharSequence charSequence3 = null;
assertEquals(expected, String.valueOf(charSequence1));
assertEquals(expected, String.valueOf(charSequence2));
assertEquals("null", String.valueOf(charSequence3));
}
}

View File

@ -83,6 +83,7 @@
<module>core-java-collections-maps</module>
<module>core-java-collections-maps-2</module>
<module>core-java-collections-maps-3</module>
<module>core-java-collections-maps-7</module>
<module>core-java-compiler</module>
<module>core-java-concurrency-2</module>
<module>core-java-concurrency-advanced</module>

View File

@ -28,12 +28,18 @@
<artifactId>commons-vfs2</artifactId>
<version>${commons-vfs2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${apache-commons-text.version}</version>
</dependency>
</dependencies>
<properties>
<commons-compress.version>1.23.0</commons-compress.version>
<ant.version>1.10.13</ant.version>
<commons-vfs2.version>2.9.0</commons-vfs2.version>
<apache-commons-text.version>1.10.0</apache-commons-text.version>
</properties>
</project>

View File

@ -0,0 +1,29 @@
package com.baeldung.commons.convertunicode;
import org.apache.commons.text.StringEscapeUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UnicodeConverterUtil {
public static String decodeWithApacheCommons(String input) {
return StringEscapeUtils.unescapeJava(input);
}
public static String decodeWithPlainJava(String input) {
Pattern pattern = Pattern.compile("\\\\u[0-9a-fA-F]{4}");
Matcher matcher = pattern.matcher(input);
StringBuilder decodedString = new StringBuilder();
while (matcher.find()) {
String unicodeSequence = matcher.group();
char unicodeChar = (char) Integer.parseInt(unicodeSequence.substring(2), 16);
matcher.appendReplacement(decodedString, Character.toString(unicodeChar));
}
matcher.appendTail(decodedString);
return decodedString.toString();
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.commons.convertunicode;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class UnicodeConverterUnitTest {
@Test
public void whenInputHaveUnicodeSequences_ThenDecode() {
String encodedString = "\\u0048\\u0065\\u006C\\u006C\\u006F World";
String expectedDecodedString = "Hello World";
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithApacheCommons(encodedString));
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithPlainJava(encodedString));
}
@Test
public void whenInputHaveNoUnicodeSequences_ThenDoNothing() {
String inputString = "Hello World";
assertEquals(inputString, UnicodeConverterUtil.decodeWithApacheCommons(inputString));
assertEquals(inputString, UnicodeConverterUtil.decodeWithPlainJava(inputString));
}
@Test
public void whenInputHaveUnicodeSequencesInMiddle_ThenDecode() {
String encodedString = "This is a test \\u0069\\u006E the middle.";
String expectedDecodedString = "This is a test in the middle.";
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithApacheCommons(encodedString));
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithPlainJava(encodedString));
}
@Test
public void whenInputHaveMultipleUnicodeSequences_ThenDecode() {
String encodedString = "Unicode: \\u0048\\u0065\\u006C\\u006C\\u006F \\u0057\\u006F\\u0072\\u006C\\u0064";
String expectedDecodedString = "Unicode: Hello World";
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithApacheCommons(encodedString));
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithPlainJava(encodedString));
}
}

View File

@ -5,6 +5,18 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-io</artifactId>
<name>libraries-io</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>com.baeldung</groupId>
@ -34,6 +46,11 @@
<artifactId>zip4j</artifactId>
<version>${zip4j.version}</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>${opencsv.version}</version>
</dependency>
</dependencies>
<properties>
@ -42,6 +59,10 @@
<sshj.version>0.27.0</sshj.version>
<vfs.version>2.4</vfs.version>
<zip4j.version>2.9.0</zip4j.version>
<opencsv.version>5.7.1</opencsv.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,4 @@
package com.baeldung.java.io.pojotocsv;
public record Application(String id, String name, Integer age, String created_at) {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.java.io.pojotocsv;
import com.opencsv.bean.CsvBindByName;
import com.opencsv.bean.CsvBindByPosition;
public record ApplicationWithAnnotation(@CsvBindByName(column = "id", required = true) @CsvBindByPosition(position = 1) String id, @CsvBindByName(column = "name", required = true) @CsvBindByPosition(position = 0) String name,
@CsvBindByName(column = "age", required = true) @CsvBindByPosition(position = 2) Integer age, @CsvBindByName(column = "position", required = true) @CsvBindByPosition(position = 3) String created_at) {
}

View File

@ -0,0 +1,55 @@
package com.baeldung.java.io.pojotocsv;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import com.opencsv.CSVWriter;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
public class BeanToCsv {
public void beanToCSVWithDefault(List<Application> applications) throws Exception {
try (FileWriter writer = new FileWriter("src/main/resources/application.csv")) {
var builder = new StatefulBeanToCsvBuilder<Application>(writer).withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
.withSeparator(',')
.build();
builder.write(applications);
}
}
public void beanToCSVWithCustomHeaderStrategy(List<Application> applications) throws IOException, CsvRequiredFieldEmptyException, CsvDataTypeMismatchException {
try (FileWriter writer = new FileWriter("src/main/resources/application2.csv")) {
var mappingStrategy = new CustomHeaderStrategy<Application>();
mappingStrategy.setType(Application.class);
var builder = new StatefulBeanToCsvBuilder<Application>(writer).withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
.withMappingStrategy(mappingStrategy)
.build();
builder.write(applications);
}
}
public void beanToCSVWithCustomPositionStrategy(List<ApplicationWithAnnotation> applications) throws Exception {
try (FileWriter writer = new FileWriter("src/main/resources/application3.csv")) {
var builder = new StatefulBeanToCsvBuilder<ApplicationWithAnnotation>(writer).withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
.build();
builder.write(applications);
}
}
public void beanToCSVWithCustomHeaderAndPositionStrategy(List<ApplicationWithAnnotation> applications) throws IOException, CsvRequiredFieldEmptyException, CsvDataTypeMismatchException {
try (FileWriter writer = new FileWriter("src/main/resources/application4.csv")) {
var mappingStrategy = new CustomColumnPositionStrategy<ApplicationWithAnnotation>();
mappingStrategy.setType(ApplicationWithAnnotation.class);
var builder = new StatefulBeanToCsvBuilder<ApplicationWithAnnotation>(writer).withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
.withMappingStrategy(mappingStrategy)
.build();
builder.write(applications);
}
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.java.io.pojotocsv;
import com.opencsv.bean.ColumnPositionMappingStrategy;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
public class CustomColumnPositionStrategy<T> extends ColumnPositionMappingStrategy<T> {
@Override
public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException {
super.generateHeader(bean);
return super.getColumnMapping();
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.java.io.pojotocsv;
import java.util.Arrays;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
public class CustomHeaderStrategy<T> extends HeaderColumnNameMappingStrategy<T> {
@Override
public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException {
String[] header = super.generateHeader(bean);
return Arrays.stream(header)
.map(String::toLowerCase)
.toArray(String[]::new);
}
}

View File

@ -0,0 +1,4 @@
AGE,CREATED_AT,ID,NAME
34,2023-08-11,123,Sam
44,2023-02-11,456,Tam
54,2023-03-11,890,Jam
1 AGE CREATED_AT ID NAME
2 34 2023-08-11 123 Sam
3 44 2023-02-11 456 Tam
4 54 2023-03-11 890 Jam

View File

@ -0,0 +1,4 @@
age,created_at,id,name
34,2023-08-11,123,Sam
44,2023-02-11,456,Tam
54,2023-03-11,890,Jam
1 age created_at id name
2 34 2023-08-11 123 Sam
3 44 2023-02-11 456 Tam
4 54 2023-03-11 890 Jam

View File

@ -0,0 +1,3 @@
Sam,123,34,2023-08-11
Tam,456,44,2023-02-11
Jam,789,54,2023-03-11
1 Sam 123 34 2023-08-11
2 Tam 456 44 2023-02-11
3 Jam 789 54 2023-03-11

View File

@ -0,0 +1,4 @@
name,id,age,created_at
Sam,123,34,2023-08-11
Tam,456,44,2023-02-11
Jam,789,54,2023-03-11
1 name id age created_at
2 Sam 123 34 2023-08-11
3 Tam 456 44 2023-02-11
4 Jam 789 54 2023-03-11

View File

@ -0,0 +1,80 @@
package com.baeldung.java.io.pojotocsv;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class BeanToCsvUnitTest {
List<Application> applications = new ArrayList<>();
List<ApplicationWithAnnotation> applicationsWithAnnotation = new ArrayList<>();
@BeforeEach
public void beforeEach() {
applications = List.of(new Application("123", "Sam", 34, "2023-08-11"), new Application("456", "Tam", 44, "2023-02-11"), new Application("890", "Jam", 54, "2023-03-11"));
applicationsWithAnnotation = List.of(new ApplicationWithAnnotation("123", "Sam", 34, "2023-08-11"), new ApplicationWithAnnotation("456", "Tam", 44, "2023-02-11"), new ApplicationWithAnnotation("789", "Jam", 54, "2023-03-11"));
}
@Test
public void givenApplicationPOJO_whenUsingDefaultStrategy_thenReceiveCSVFormatWithAscendingOrderOfField() throws Exception {
BeanToCsv beanToCsv = new BeanToCsv();
beanToCsv.beanToCSVWithDefault(applications);
try (BufferedReader bufferedReader = Files.newBufferedReader(Path.of("src/main/resources/application.csv"))) {
List<String> content = bufferedReader.lines()
.toList();
assertThat(content.get(0)).isEqualTo("AGE,CREATED_AT,ID,NAME");
assertThat(content.get(1)).isEqualTo("34,2023-08-11,123,Sam");
assertThat(content.get(2)).isEqualTo("44,2023-02-11,456,Tam");
assertThat(content.get(3)).isEqualTo("54,2023-03-11,890,Jam");
}
}
@Test
public void givenApplicationPOJO_whenUsingCustomHeaderStrategy_thenReceiveCSVFormatWithCustomHeaders() throws Exception {
BeanToCsv beanToCsv = new BeanToCsv();
beanToCsv.beanToCSVWithCustomHeaderStrategy(applications);
try (BufferedReader bufferedReader = Files.newBufferedReader(Path.of("src/main/resources/application2.csv"))) {
List<String> content = bufferedReader.lines()
.toList();
assertThat(content.get(0)).isEqualTo("age,created_at,id,name");
assertThat(content.get(1)).isEqualTo("34,2023-08-11,123,Sam");
assertThat(content.get(2)).isEqualTo("44,2023-02-11,456,Tam");
assertThat(content.get(3)).isEqualTo("54,2023-03-11,890,Jam");
}
}
@Test
public void givenApplicationPOJOWithAnnotation_whenUsingCustomPositionStrategy_thenReceiveCSVFormatWithCustomPosition() throws Exception {
BeanToCsv beanToCsv = new BeanToCsv();
beanToCsv.beanToCSVWithCustomPositionStrategy(applicationsWithAnnotation);
try (BufferedReader bufferedReader = Files.newBufferedReader(Path.of("src/main/resources/application3.csv"))) {
List<String> content = bufferedReader.lines()
.toList();
assertThat(content.get(0)).isEqualTo("Sam,123,34,2023-08-11");
assertThat(content.get(1)).isEqualTo("Tam,456,44,2023-02-11");
assertThat(content.get(2)).isEqualTo("Jam,789,54,2023-03-11");
}
}
@Test
public void givenApplicationPOJOWithAnnotation_whenUsingCustomHeaderPositionStrategy_thenReceiveCSVFormatWithCustomHeaderPosition() throws Exception {
BeanToCsv beanToCsv = new BeanToCsv();
beanToCsv.beanToCSVWithCustomHeaderAndPositionStrategy(applicationsWithAnnotation);
try (BufferedReader bufferedReader = Files.newBufferedReader(Path.of("src/main/resources/application4.csv"))) {
List<String> content = bufferedReader.lines()
.toList();
assertThat(content.get(0)).isEqualTo("name,id,age,created_at");
assertThat(content.get(1)).isEqualTo("Sam,123,34,2023-08-11");
assertThat(content.get(2)).isEqualTo("Tam,456,44,2023-02-11");
assertThat(content.get(3)).isEqualTo("Jam,789,54,2023-03-11");
}
}
}

View File

@ -11,6 +11,8 @@ public class LicenseDto {
private LocalDateTime endDate;
private String licenseType;
public UUID getId() {
return id;
}
@ -35,4 +37,12 @@ public class LicenseDto {
this.endDate = endDate;
}
public String getLicenseType() {
return licenseType;
}
public void setLicenseType(String licenseType) {
this.licenseType = licenseType;
}
}

View File

@ -6,6 +6,7 @@ import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import org.mapstruct.AfterMapping;
import org.mapstruct.Condition;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
@ -40,4 +41,13 @@ public interface LicenseMapper {
.toDays() <= 14;
}
}
@Condition
default boolean mapsToExpectedLicenseType(String licenseType) {
try {
return licenseType != null && License.LicenseType.valueOf(licenseType) != null;
} catch (IllegalArgumentException e) {
return false;
}
}
}

View File

@ -3,7 +3,6 @@ package com.baeldung.expression.model;
import java.time.OffsetDateTime;
import java.util.UUID;
public class License {
private UUID id;
@ -16,6 +15,8 @@ public class License {
private boolean renewalRequired;
private LicenseType licenseType;
public UUID getId() {
return id;
}
@ -55,4 +56,16 @@ public class License {
public void setRenewalRequired(boolean renewalRequired) {
this.renewalRequired = renewalRequired;
}
}
public enum LicenseType {
INDIVIDUAL, FAMILY
}
public LicenseType getLicenseType() {
return licenseType;
}
public void setLicenseType(LicenseType licenseType) {
this.licenseType = licenseType;
}
}

View File

@ -6,6 +6,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
@ -74,4 +75,31 @@ class LicenseMapperUnitTest {
assertThat(license.getId()).isSameAs(id);
}
@Test
void givenLicenseDtoWithoutLicenseTypeString_whenMapperMethodIsInvoked_thenLicenseShouldBePopulatedWithoutLicenseType() {
LicenseDto licenseDto = new LicenseDto();
License license = licenseMapper.toLicense(licenseDto);
assertThat(license).isNotNull();
Assertions.assertNull(license.getLicenseType());
}
@Test
void givenLicenseDtoWithInvalidLicenseTypeString_whenMapperMethodIsInvoked_thenLicenseShouldBePopulatedWithoutLicenseType() {
LicenseDto licenseDto = new LicenseDto();
licenseDto.setLicenseType("invalid_license_type");
License license = licenseMapper.toLicense(licenseDto);
assertThat(license).isNotNull();
Assertions.assertNull(license.getLicenseType());
}
@Test
void givenLicenseDtoWithValidLicenseTypeString_whenMapperMethodIsInvoked_thenLicenseShouldBePopulatedWithMatchingLicenseType() {
LicenseDto licenseDto = new LicenseDto();
licenseDto.setLicenseType("INDIVIDUAL");
License license = licenseMapper.toLicense(licenseDto);
assertThat(license).isNotNull();
Assertions.assertNotNull(license.getLicenseType());
assertThat(license.getLicenseType()).isEqualTo(License.LicenseType.INDIVIDUAL);
}
}

View File

@ -45,8 +45,8 @@ public class FruitArrayJacksonUnmarshalUnitTest {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:jsonInput").unmarshal(new ListJacksonDataFormat(Fruit.class))
.to("mock:marshalledObject");
from("direct:jsonInput").unmarshal(new ListJacksonDataFormat(Fruit.class))
.to("mock:marshalledObject");
}
};
}

View File

@ -7,6 +7,7 @@
<artifactId>micronaut</artifactId>
<version>0.1</version>
<name>micronaut</name>
<packaging>${packaging}</packaging>
<parent>
<groupId>com.baeldung</groupId>
@ -18,7 +19,7 @@
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>bom</artifactId>
<artifactId>micronaut-bom</artifactId>
<version>${micronaut.version}</version>
<type>pom</type>
<scope>import</scope>
@ -29,120 +30,136 @@
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>http-client</artifactId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>http-server-netty</artifactId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>inject</artifactId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>runtime</artifactId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${annotation.api.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>inject-java</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>${reactor.version}</version>
<groupId>io.micronaut.rxjava3</groupId>
<artifactId>micronaut-rxjava3</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.rxjava3</groupId>
<artifactId>micronaut-rxjava3-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.serde</groupId>
<artifactId>micronaut-serde-jackson</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
<annotationProcessorPaths combine.self="append">
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>${micronaut.version}</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-validation</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${shade.plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${exec.mainClass}</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
<id>default-shade</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>${exec.mainClass}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.plugin.version}</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>io.micronaut</groupId>
<artifactId>inject-java</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<!-- <exec.mainClass>com.baeldung.micronaut.helloworld.server.ServerApplication</exec.mainClass> -->
<exec.mainClass>com.baeldung.micronaut.vs.springboot.CompareApplication</exec.mainClass>
<micronaut.version>1.0.0.RC2</micronaut.version>
<micronaut.version>3.10.1</micronaut.version>
<jdk.version>17</jdk.version>
<annotation.api.version>1.3.2</annotation.api.version>
<reactor.version>3.1.6.RELEASE</reactor.version>
<release.version>17</release.version>
<packaging>jar</packaging>
<compiler.plugin.version>3.7.0</compiler.plugin.version>
<shade.plugin.version>3.1.0</shade.plugin.version>
<micronaut.runtime>netty</micronaut.runtime>
<shade.plugin.version>3.2.0</shade.plugin.version>
</properties>
</project>

View File

@ -0,0 +1,42 @@
package com.baeldung.micronaut.apiversioning.custom.client;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.rxjava3.core.Flowable;
@Client("/")
@Header(name = HttpHeaders.ACCEPT, value = "application/json")
public interface BirdCountClient {
@Get(
uri = "/bird/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@Header(name = "api-key", value = "11")
@SingleResult
Flowable<String> countV1(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/bird/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@Header(name = "api-key", value = "10")
@SingleResult
Flowable<String> countV2(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/bird/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@Header(name = "api-key", value = "")
@SingleResult
Flowable<String> countDefault(@QueryValue("max") @Nullable Integer max);
}

View File

@ -0,0 +1,36 @@
package com.baeldung.micronaut.apiversioning.custom.server;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.version.annotation.Version;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import io.reactivex.rxjava3.core.Flowable;
import java.util.stream.Stream;
@Controller("/bird")
public class BirdCountController {
@Get(value = "/count", produces = {"application/json"})
@Version("1")
public Flowable<String> countV1(@QueryValue("max") @Nullable Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Bird " + index)
.limit(max == null ? 10 : max)
);
}
@Get(value = "/count", produces = {"application/json"})
@Version("2")
public Flowable<String> countV2(@QueryValue("max") @NonNull Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Bird " + index)
.limit(max)
);
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.micronaut.apiversioning.custom.server;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.annotation.Value;
import io.micronaut.http.HttpRequest;
import io.micronaut.web.router.version.resolution.RequestVersionResolver;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.util.Optional;
@Singleton
@Requires(property = "my.router.versioning.enabled", value = "true")
public class CustomVersionResolver implements RequestVersionResolver {
@Inject
@Value("${micronaut.router.versioning.default-version}")
private String defaultVersion;
@Override
public Optional<String> resolve(HttpRequest<?> request) {
var apiKey = Optional.ofNullable(request.getHeaders().get("api-key"));
if (apiKey.isPresent() && !apiKey.get().isEmpty()) {
return Optional.of(Integer.parseInt(apiKey.get()) % 2 == 0 ? "2" : "1");
}
return Optional.of(defaultVersion);
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.micronaut.apiversioning.header.client;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.core.version.annotation.Version;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.rxjava3.core.Flowable;
@Client("/")
@Header(name = HttpHeaders.ACCEPT, value = "application/json")
public interface DogCountClient {
@Get(
uri = "/dog/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@Version("1")
@SingleResult
Flowable<String> countV1(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/dog/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@Version("2")
@SingleResult
Flowable<String> countV2(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/dog/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@SingleResult
Flowable<String> countDefault(@QueryValue("max") @Nullable Integer max);
}

View File

@ -0,0 +1,36 @@
package com.baeldung.micronaut.apiversioning.header.server;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.version.annotation.Version;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import io.reactivex.rxjava3.core.Flowable;
import java.util.stream.Stream;
@Controller("/dog")
public class DogCountController {
@Get(value = "/count", produces = {"application/json"})
@Version("1")
public Flowable<String> countV1(@QueryValue("max") @Nullable Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Dog " + index)
.limit(max == null ? 10 : max)
);
}
@Get(value = "/count", produces = {"application/json"})
@Version("2")
public Flowable<String> countV2(@QueryValue("max") @NonNull Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Dog " + index)
.limit(max)
);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.micronaut.apiversioning.param.client;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.rxjava3.core.Flowable;
@Client("/")
@Header(name = HttpHeaders.ACCEPT, value = "application/json")
public interface CatCountClient {
@Get(
uri = "/cat/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@SingleResult
Flowable<String> count(@QueryValue("max") @Nullable Integer max, @QueryValue(value = "v", defaultValue = "1") @Nullable Integer version);
}

View File

@ -0,0 +1,37 @@
package com.baeldung.micronaut.apiversioning.param.server;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.version.annotation.Version;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import io.reactivex.rxjava3.core.Flowable;
import java.util.stream.Stream;
@Controller("/cat")
public class CatCountController {
@Get(value = "/count", produces = {"application/json"})
@Version("1")
public Flowable<String> countV1(@QueryValue("max") @Nullable Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Cat " + index)
.limit(max == null ? 10 : max)
);
}
@Get(value = "/count", produces = {"application/json"})
@Version("2")
public Flowable<String> countV2(@QueryValue("max") @NonNull Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Cat " + index)
.limit(max)
);
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.micronaut.apiversioning.url.client;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.rxjava3.core.Flowable;
@Client("/")
@Header(name = HttpHeaders.ACCEPT, value = "application/json")
public interface SheepCountClient {
@Get(
uri = "/v1/sheep/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@SingleResult
Flowable<String> countV1(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/v2/sheep/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@SingleResult
Flowable<String> countV2(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/sheep/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@SingleResult
Flowable<String> countDefault(@QueryValue("max") @Nullable Integer max);
}

View File

@ -0,0 +1,26 @@
package com.baeldung.micronaut.apiversioning.url.server;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.reactivex.rxjava3.core.Flowable;
import java.util.stream.Stream;
@Controller("/v1/sheep/count")
public class SheepCountControllerV1 {
@Get(
uri = "{?max}",
consumes = {"application/json"},
produces = {"application/json"}
)
Flowable<String> countV1(@Nullable Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Sheep " + index)
.limit(max == null ? 10 : max)
);
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.micronaut.apiversioning.url.server;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import io.reactivex.rxjava3.core.Flowable;
import java.util.stream.Stream;
@Controller
public class SheepCountControllerV2 {
@Get(
uris = {"/v2/sheep/count", "/sheep/count"},
consumes = {"application/json"},
produces = {"application/json"}
)
Flowable<String> countV2(@QueryValue("max") @NonNull Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Sheep " + index)
.limit(max)
);
}
}

View File

@ -1,28 +1,27 @@
package com.baeldung.micronaut.helloworld.client;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.client.RxHttpClient;
import io.reactivex.Single;
import javax.inject.Singleton;
import io.reactivex.rxjava3.core.Single;
import jakarta.inject.Singleton;
@Singleton
public class ConcreteGreetingClient
{
private RxHttpClient httpClient;
private HttpClient httpClient;
public ConcreteGreetingClient(@Client("/") RxHttpClient httpClient) {
public ConcreteGreetingClient(@Client("/") HttpClient httpClient) {
this.httpClient = httpClient;
}
public String greet(String name) {
HttpRequest<String> req = HttpRequest.GET("/greet/" + name);
return httpClient.retrieve(req).blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req)).blockingGet();
}
public Single<String> greetAsync(String name) {
HttpRequest<String> req = HttpRequest.GET("/async/greet/" + name);
return httpClient.retrieve(req).first("An error as occurred");
return Single.fromPublisher(httpClient.retrieve(req));
}
}

View File

@ -3,9 +3,9 @@ package com.baeldung.micronaut.helloworld.server.controller;
import com.baeldung.micronaut.helloworld.server.service.GreetingService;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.reactivex.Single;
import javax.inject.Inject;
import io.reactivex.rxjava3.core.Single;
import jakarta.inject.Inject;
@Controller("/async/greet")
public class AsyncGreetController {

View File

@ -7,7 +7,7 @@ import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import javax.inject.Inject;
import jakarta.inject.Inject;
@Controller("/greet")
public class GreetController {

View File

@ -2,7 +2,7 @@ package com.baeldung.micronaut.helloworld.server.service;
import io.micronaut.context.annotation.Primary;
import javax.inject.Singleton;
import jakarta.inject.Singleton;
@Primary
@Singleton

View File

@ -1,6 +1,6 @@
package com.baeldung.micronaut.helloworld.server.service;
import javax.inject.Singleton;
import jakarta.inject.Singleton;
@Singleton
public class SpanishGreetingService implements GreetingService {

View File

@ -1,9 +1,11 @@
package com.baeldung.micronaut.vs.springboot;
import io.micronaut.runtime.Micronaut;
public class CompareApplication {
public static void main(String[] args) {
Micronaut.run(CompareApplication.class);
Micronaut.run(CompareApplication.class, args);
}
}
}

View File

@ -1,46 +1,46 @@
package com.baeldung.micronaut.vs.springboot.client;
import javax.inject.Singleton;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.rxjava3.core.Single;
import jakarta.inject.Singleton;
@Singleton
public class ArithmeticClientImpl {
private RxHttpClient httpClient;
private HttpClient httpClient;
public ArithmeticClientImpl(@Client("/") RxHttpClient httpClient) {
public ArithmeticClientImpl(@Client("/") HttpClient httpClient) {
this.httpClient = httpClient;
}
public String sum(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/sum/" + number1 + "/" + number2);
return httpClient.retrieve(req)
.blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req))
.blockingGet();
}
public String subtract(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/subtract/" + number1 + "/" + number2);
return httpClient.retrieve(req)
.blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req))
.blockingGet();
}
public String multiply(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/multiply/" + number1 + "/" + number2);
return httpClient.retrieve(req)
.blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req))
.blockingGet();
}
public String divide(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/divide/" + number1 + "/" + number2);
return httpClient.retrieve(req)
.blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req))
.blockingGet();
}
public String memory() {
HttpRequest<String> req = HttpRequest.GET("/math/memory");
return httpClient.retrieve(req)
.blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req))
.blockingGet();
}
}

View File

@ -3,7 +3,7 @@ package com.baeldung.micronaut.vs.springboot.controller;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import javax.inject.Inject;
import jakarta.inject.Inject;
import com.baeldung.micronaut.vs.springboot.service.ArithmeticService;

View File

@ -1,6 +1,6 @@
package com.baeldung.micronaut.vs.springboot.service;
import javax.inject.Singleton;
import jakarta.inject.Singleton;
@Singleton
public class ArithmeticService {

View File

@ -1,5 +1,19 @@
context-path: /
micronaut:
application:
name: hello-world-server
server:
port: ${random.port}
router:
versioning:
enabled: true
default-version: 2
parameter:
enabled: true
names: 'v,api-version'
header:
enabled: true
names:
- 'X-API-VERSION'
application:
name: hello-world-server
server:
port: ${random.port}

View File

@ -8,6 +8,12 @@
</encoder>
</appender>
<!-- Uncomment these lines to see the request logs -->
<!--
<logger name="io.micronaut.http.client" level="TRACE" />
<logger name="io.micronaut.http.server" level="DEBUG" />
-->
<root level="info">
<appender-ref ref="STDOUT" />
</root>

View File

@ -0,0 +1,42 @@
package com.baeldung.micronaut.apiversioning.custom.client;
import io.micronaut.context.annotation.Property;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@MicronautTest
@Property(name = "my.router.versioning.enabled", value = "true")
class BirdCountClientUnitTest {
@Inject
private EmbeddedApplication<?> application;
@Inject
private BirdCountClient client;
@Test
void givenTheCountApi_whenUsingV1ViaCustomStrategy_shouldRouteToProperHandler() {
Assertions.assertEquals(10, client.countV1(null).blockingSingle().split(",").length);
Assertions.assertEquals(4, client.countV1(4).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingV2ViaCustomStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> client.countV2(null).count().blockingGet());
Assertions.assertEquals(6, client.countV2(6).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingDefaultVersionViaCustomStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> client.countDefault(null).count().blockingGet());
Assertions.assertEquals(6, client.countDefault(6).blockingSingle().split(",").length);
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.micronaut.apiversioning.header.client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@MicronautTest
class DogCountClientUnitTest {
@Inject
private EmbeddedApplication<?> application;
@Inject
private DogCountClient dogCountClient;
@Test
void givenTheCountApi_whenUsingV1ViaHeaderStrategy_shouldRouteToProperHandler() {
Assertions.assertEquals(10, dogCountClient.countV1(null).blockingSingle().split(",").length);
Assertions.assertEquals(4, dogCountClient.countV1(4).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingV2ViaHeaderStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> dogCountClient.countV2(null).count().blockingGet());
Assertions.assertEquals(6, dogCountClient.countV2(6).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingDefaultVersionViaHeaderStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> dogCountClient.countDefault(null).count().blockingGet());
Assertions.assertEquals(6, dogCountClient.countDefault(6).blockingSingle().split(",").length);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.micronaut.apiversioning.param.client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@MicronautTest
class CatCountClientUnitTest {
@Inject
private EmbeddedApplication<?> application;
@Inject
private CatCountClient client;
@Test
void givenTheCountApi_whenUsingV1ViaParameterStrategy_shouldRouteToProperHandler() {
Assertions.assertEquals(10, client.count(null, 1).blockingSingle().split(",").length);
Assertions.assertEquals(5, client.count(5, 1).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingV2ViaParameterStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> client.count(null, 2).count().blockingGet());
Assertions.assertEquals(6, client.count(6, 2).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingDefaultVersionViaParameterStrategy_shouldRouteToProperHandler() {
Assertions.assertEquals(10, client.count(null, null).blockingSingle().split(",").length);
Assertions.assertEquals(6, client.count(6, null).blockingSingle().split(",").length);
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.micronaut.apiversioning.url.client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.stream.Collectors;
@MicronautTest
class SheepCountClientUnitTest {
@Inject
private EmbeddedApplication<?> application;
@Inject
private SheepCountClient client;
@Test
void givenTheCountApi_whenUsingV1ViaUrlStrategy_shouldRouteToProperHandler() {
Assertions.assertEquals(10, client.countV1(null).blockingSingle().split(",").length);
Assertions.assertEquals(4, client.countV1(4).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingV2ViaUrlStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> client.countV2(null).count().blockingGet());
final var actual = client.countV2(4).blockingSingle().split(",").length;
Assertions.assertEquals(4, actual);
}
@Test
void givenTheCountApi_whenUsingDefaultVersionViaUrlStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> client.countDefault(null).count().blockingGet());
final var actual = client.countDefault(4).blockingSingle().split(",").length;
Assertions.assertEquals(4, actual);
}
}

View File

@ -1,31 +1,19 @@
package com.baeldung.micronaut.helloworld.client;
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import static junit.framework.TestCase.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ConcreteGreetingClientUnitTest
{
private EmbeddedServer server;
@MicronautTest
public class ConcreteGreetingClientUnitTest {
@Inject
private EmbeddedApplication<?> application;
@Inject
private ConcreteGreetingClient client;
@Before
public void setup()
{
server = ApplicationContext.run(EmbeddedServer.class);
client = server.getApplicationContext().getBean(ConcreteGreetingClient.class);
}
@After
public void cleanup()
{
server.stop();
}
@Test
public void testGreeting() {
assertEquals(client.greet("Mike"), "Hello Mike");

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