Merge branch 'eugenp:master' into master
This commit is contained in:
commit
24834c5f50
|
@ -5,7 +5,7 @@ title: '[ISSUE] '
|
||||||
---
|
---
|
||||||
|
|
||||||
**Article and Module Links**
|
**Article and Module Links**
|
||||||
A link to the affected article and the affected module. The link to the module is the one in the "over on GitHub" standard phrase.
|
A link to the affected article and the affected module. You can find the link to the module in the Conclusion section in the "on Github" standard phase.
|
||||||
|
|
||||||
**Describe the Issue**
|
**Describe the Issue**
|
||||||
A clear and concise description of what the issue is.
|
A clear and concise description of what the issue is.
|
||||||
|
@ -30,9 +30,3 @@ If applicable, add screenshots to help explain your problem.
|
||||||
|
|
||||||
**Additional Context**
|
**Additional Context**
|
||||||
Add any other context about the issue here.
|
Add any other context about the issue here.
|
||||||
|
|
||||||
Note that, unfortunately, we can only help with issues that are specifically and directly related to the article - not with your own, custom application.
|
|
||||||
|
|
||||||
StackOverflow is a great place to ask more general questions.
|
|
||||||
|
|
||||||
That's primarily because we get a large number of questions and - while we do try to go through as much as everything and help wherever we can, we can't really get back to all of them.
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
- [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)
|
- [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)
|
- [bootstrap-server in Kafka Configuration](https://www.baeldung.com/java-kafka-bootstrap-server)
|
||||||
|
- [Introduction to Apache Kafka](https://www.baeldung.com/apache-kafka)
|
||||||
|
|
|
@ -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\"}";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
## Relevant Articles
|
||||||
|
- [How To Convert Excel Data Into List Of Java Objects](https://www.baeldung.com/java-convert-excel-data-into-list)
|
||||||
|
- [Expand Columns with Apache POI](https://www.baeldung.com/java-apache-poi-expand-columns)
|
|
@ -0,0 +1,2 @@
|
||||||
|
/target/
|
||||||
|
.idea/
|
|
@ -0,0 +1,7 @@
|
||||||
|
## AWS DYNAMODB
|
||||||
|
|
||||||
|
This module contains articles about AWS DynamoDB
|
||||||
|
|
||||||
|
### Relevant articles
|
||||||
|
- [Integration Testing with a Local DynamoDB Instance](https://www.baeldung.com/dynamodb-local-integration-tests)
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
<?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>aws-dynamodb</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>aws-dynamodb</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>aws-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-java-sdk</artifactId>
|
||||||
|
<version>${aws-java-sdk.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>DynamoDBLocal</artifactId>
|
||||||
|
<version>${dynamodblocal.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>${commons-io.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>${gson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>${maven-shade-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
|
<version>${maven-plugins-version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>copy</id>
|
||||||
|
<phase>compile</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>copy-dependencies</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<includeScope></includeScope>
|
||||||
|
<includeTypes>so,dll,dylib</includeTypes>
|
||||||
|
<outputDirectory>native-libs</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<gson.version>2.8.0</gson.version>
|
||||||
|
<dynamodblocal.version>1.21.1</dynamodblocal.version>
|
||||||
|
<maven-plugins-version>3.1.1</maven-plugins-version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -49,10 +49,10 @@ public class ProductInfoRepositoryIntegrationTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setupClass() {
|
public static void setupClass() {
|
||||||
Properties testProperties = loadFromFileInClasspath("test.properties")
|
Properties testProperties = loadFromFileInClasspath("test.properties")
|
||||||
.filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY)))
|
.filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY)))
|
||||||
.filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY)))
|
.filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY)))
|
||||||
.filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT)))
|
.filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT)))
|
||||||
.orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values"));
|
.orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values"));
|
||||||
|
|
||||||
String amazonAWSAccessKey = testProperties.getProperty(AWS_ACCESSKEY);
|
String amazonAWSAccessKey = testProperties.getProperty(AWS_ACCESSKEY);
|
||||||
String amazonAWSSecretKey = testProperties.getProperty(AWS_SECRETKEY);
|
String amazonAWSSecretKey = testProperties.getProperty(AWS_SECRETKEY);
|
|
@ -16,31 +16,9 @@
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.amazonaws</groupId>
|
<groupId>software.amazon.awssdk</groupId>
|
||||||
<artifactId>aws-java-sdk</artifactId>
|
<artifactId>aws-sdk-java</artifactId>
|
||||||
<version>${aws-java-sdk.version}</version>
|
<version>${aws-java-sdk-v2.version}</version>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.amazonaws</groupId>
|
|
||||||
<artifactId>aws-lambda-java-core</artifactId>
|
|
||||||
<version>${aws-lambda-java-core.version}</version>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.amazonaws</groupId>
|
|
||||||
<artifactId>aws-lambda-java-events</artifactId>
|
|
||||||
<version>${aws-lambda-java-events.version}</version>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
|
@ -52,12 +30,6 @@
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>${gson.version}</version>
|
<version>${gson.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.amazonaws</groupId>
|
|
||||||
<artifactId>DynamoDBLocal</artifactId>
|
|
||||||
<version>${dynamodblocal.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -101,8 +73,6 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
|
|
||||||
<aws-lambda-java-core.version>1.1.0</aws-lambda-java-core.version>
|
|
||||||
<gson.version>2.8.0</gson.version>
|
<gson.version>2.8.0</gson.version>
|
||||||
<dynamodblocal.version>1.21.1</dynamodblocal.version>
|
<dynamodblocal.version>1.21.1</dynamodblocal.version>
|
||||||
<commons-codec-version>1.10.L001</commons-codec-version>
|
<commons-codec-version>1.10.L001</commons-codec-version>
|
||||||
|
|
|
@ -2,136 +2,148 @@ package com.baeldung.ec2;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import com.amazonaws.auth.AWSCredentials;
|
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
|
||||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
import software.amazon.awssdk.regions.Region;
|
||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||||
import com.amazonaws.regions.Regions;
|
import software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressRequest;
|
||||||
import com.amazonaws.services.ec2.AmazonEC2;
|
import software.amazon.awssdk.services.ec2.model.CreateKeyPairRequest;
|
||||||
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
|
import software.amazon.awssdk.services.ec2.model.CreateKeyPairResponse;
|
||||||
import com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest;
|
import software.amazon.awssdk.services.ec2.model.CreateSecurityGroupRequest;
|
||||||
import com.amazonaws.services.ec2.model.CreateKeyPairRequest;
|
import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest;
|
||||||
import com.amazonaws.services.ec2.model.CreateKeyPairResult;
|
import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse;
|
||||||
import com.amazonaws.services.ec2.model.CreateSecurityGroupRequest;
|
import software.amazon.awssdk.services.ec2.model.DescribeKeyPairsRequest;
|
||||||
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
|
import software.amazon.awssdk.services.ec2.model.DescribeKeyPairsResponse;
|
||||||
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
|
import software.amazon.awssdk.services.ec2.model.IpPermission;
|
||||||
import com.amazonaws.services.ec2.model.DescribeKeyPairsRequest;
|
import software.amazon.awssdk.services.ec2.model.IpRange;
|
||||||
import com.amazonaws.services.ec2.model.DescribeKeyPairsResult;
|
import software.amazon.awssdk.services.ec2.model.MonitorInstancesRequest;
|
||||||
import com.amazonaws.services.ec2.model.IpPermission;
|
import software.amazon.awssdk.services.ec2.model.RebootInstancesRequest;
|
||||||
import com.amazonaws.services.ec2.model.IpRange;
|
import software.amazon.awssdk.services.ec2.model.RunInstancesRequest;
|
||||||
import com.amazonaws.services.ec2.model.MonitorInstancesRequest;
|
import software.amazon.awssdk.services.ec2.model.RunInstancesResponse;
|
||||||
import com.amazonaws.services.ec2.model.RebootInstancesRequest;
|
import software.amazon.awssdk.services.ec2.model.StartInstancesRequest;
|
||||||
import com.amazonaws.services.ec2.model.RunInstancesRequest;
|
import software.amazon.awssdk.services.ec2.model.StartInstancesResponse;
|
||||||
import com.amazonaws.services.ec2.model.StartInstancesRequest;
|
import software.amazon.awssdk.services.ec2.model.StopInstancesRequest;
|
||||||
import com.amazonaws.services.ec2.model.StopInstancesRequest;
|
import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesRequest;
|
||||||
import com.amazonaws.services.ec2.model.UnmonitorInstancesRequest;
|
|
||||||
|
|
||||||
public class EC2Application {
|
public class EC2Application {
|
||||||
|
|
||||||
private static final AWSCredentials credentials;
|
|
||||||
|
|
||||||
static {
|
|
||||||
// put your accesskey and secretkey here
|
|
||||||
credentials = new BasicAWSCredentials(
|
|
||||||
"<AWS accesskey>",
|
|
||||||
"<AWS secretkey>"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// Set up the client
|
// Set up the client
|
||||||
AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
|
Ec2Client ec2Client = Ec2Client.builder()
|
||||||
.withCredentials(new AWSStaticCredentialsProvider(credentials))
|
.credentialsProvider(ProfileCredentialsProvider.create("default"))
|
||||||
.withRegion(Regions.US_EAST_1)
|
.region(Region.US_EAST_1)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Create a security group
|
// Create a security group
|
||||||
CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest().withGroupName("BaeldungSecurityGroup")
|
CreateSecurityGroupRequest createSecurityGroupRequest = CreateSecurityGroupRequest.builder()
|
||||||
.withDescription("Baeldung Security Group");
|
.groupName("BaeldungSecurityGroup")
|
||||||
|
.description("Baeldung Security Group")
|
||||||
|
.build();
|
||||||
|
|
||||||
ec2Client.createSecurityGroup(createSecurityGroupRequest);
|
ec2Client.createSecurityGroup(createSecurityGroupRequest);
|
||||||
|
|
||||||
// Allow HTTP and SSH traffic
|
// Allow HTTP and SSH traffic
|
||||||
IpRange ipRange1 = new IpRange().withCidrIp("0.0.0.0/0");
|
IpRange ipRange1 = IpRange.builder()
|
||||||
|
.cidrIp("0.0.0.0/0")
|
||||||
|
.build();
|
||||||
|
|
||||||
IpPermission ipPermission1 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 }))
|
IpPermission ipPermission1 = IpPermission.builder()
|
||||||
.withIpProtocol("tcp")
|
.ipRanges(Arrays.asList(ipRange1))
|
||||||
.withFromPort(80)
|
.ipProtocol("tcp")
|
||||||
.withToPort(80);
|
.fromPort(80)
|
||||||
|
.toPort(80)
|
||||||
|
.build();
|
||||||
|
|
||||||
IpPermission ipPermission2 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 }))
|
IpPermission ipPermission2 = IpPermission.builder()
|
||||||
.withIpProtocol("tcp")
|
.ipRanges(Arrays.asList(ipRange1))
|
||||||
.withFromPort(22)
|
.ipProtocol("tcp")
|
||||||
.withToPort(22);
|
.fromPort(22)
|
||||||
|
.toPort(22)
|
||||||
|
.build();
|
||||||
|
|
||||||
AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest()
|
AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = AuthorizeSecurityGroupIngressRequest
|
||||||
.withGroupName("BaeldungSecurityGroup")
|
.builder()
|
||||||
.withIpPermissions(ipPermission1, ipPermission2);
|
.groupName("BaeldungSecurityGroup")
|
||||||
|
.ipPermissions(ipPermission1, ipPermission2)
|
||||||
|
.build();
|
||||||
|
|
||||||
ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);
|
ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);
|
||||||
|
|
||||||
// Create KeyPair
|
// Create KeyPair
|
||||||
CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest()
|
CreateKeyPairRequest createKeyPairRequest = CreateKeyPairRequest.builder()
|
||||||
.withKeyName("baeldung-key-pair");
|
.keyName("baeldung-key-pair")
|
||||||
CreateKeyPairResult createKeyPairResult = ec2Client.createKeyPair(createKeyPairRequest);
|
.build();
|
||||||
String privateKey = createKeyPairResult
|
|
||||||
.getKeyPair()
|
CreateKeyPairResponse createKeyPairResponse = ec2Client.createKeyPair(createKeyPairRequest);
|
||||||
.getKeyMaterial(); // make sure you keep it, the private key, Amazon doesn't store the private key
|
String privateKey = createKeyPairResponse.keyMaterial();
|
||||||
|
// make sure you keep it, the private key, Amazon doesn't store the private key
|
||||||
|
|
||||||
// See what key-pairs you've got
|
// See what key-pairs you've got
|
||||||
DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
|
DescribeKeyPairsRequest describeKeyPairsRequest = DescribeKeyPairsRequest.builder()
|
||||||
DescribeKeyPairsResult describeKeyPairsResult = ec2Client.describeKeyPairs(describeKeyPairsRequest);
|
.build();
|
||||||
|
DescribeKeyPairsResponse describeKeyPairsResponse = ec2Client.describeKeyPairs(describeKeyPairsRequest);
|
||||||
|
|
||||||
// Launch an Amazon Instance
|
// Launch an Amazon Instance
|
||||||
RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-97785bed") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html | https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/usingsharedamis-finding.html
|
RunInstancesRequest runInstancesRequest = RunInstancesRequest.builder()
|
||||||
.withInstanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
|
.imageId("ami-97785bed")
|
||||||
.withMinCount(1)
|
.instanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
|
||||||
.withMaxCount(1)
|
.minCount(1)
|
||||||
.withKeyName("baeldung-key-pair") // optional - if not present, can't connect to instance
|
.maxCount(1)
|
||||||
.withSecurityGroups("BaeldungSecurityGroup");
|
.keyName("baeldung-key-pair") // optional - if not present, can't connect to instance
|
||||||
|
.securityGroups("BaeldungSecurityGroup")
|
||||||
|
.build();
|
||||||
|
|
||||||
String yourInstanceId = ec2Client.runInstances(runInstancesRequest).getReservation().getInstances().get(0).getInstanceId();
|
RunInstancesResponse runInstancesResponse = ec2Client.runInstances(runInstancesRequest);
|
||||||
|
String yourInstanceId = runInstancesResponse.instances().get(0).instanceId();
|
||||||
|
|
||||||
// Start an Instance
|
// Start an Instance
|
||||||
StartInstancesRequest startInstancesRequest = new StartInstancesRequest()
|
StartInstancesRequest startInstancesRequest = StartInstancesRequest.builder()
|
||||||
.withInstanceIds(yourInstanceId);
|
.instanceIds(yourInstanceId)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
StartInstancesResponse startInstancesResponse = ec2Client.startInstances(startInstancesRequest);
|
||||||
|
|
||||||
ec2Client.startInstances(startInstancesRequest);
|
|
||||||
|
|
||||||
// Monitor Instances
|
// Monitor Instances
|
||||||
MonitorInstancesRequest monitorInstancesRequest = new MonitorInstancesRequest()
|
MonitorInstancesRequest monitorInstancesRequest = MonitorInstancesRequest.builder()
|
||||||
.withInstanceIds(yourInstanceId);
|
.instanceIds(yourInstanceId)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
ec2Client.monitorInstances(monitorInstancesRequest);
|
ec2Client.monitorInstances(monitorInstancesRequest);
|
||||||
|
|
||||||
UnmonitorInstancesRequest unmonitorInstancesRequest = new UnmonitorInstancesRequest()
|
UnmonitorInstancesRequest unmonitorInstancesRequest = UnmonitorInstancesRequest.builder()
|
||||||
.withInstanceIds(yourInstanceId);
|
.instanceIds(yourInstanceId)
|
||||||
|
.build();
|
||||||
|
|
||||||
ec2Client.unmonitorInstances(unmonitorInstancesRequest);
|
ec2Client.unmonitorInstances(unmonitorInstancesRequest);
|
||||||
|
|
||||||
// Reboot an Instance
|
// Reboot an Instance
|
||||||
|
RebootInstancesRequest rebootInstancesRequest = RebootInstancesRequest.builder()
|
||||||
RebootInstancesRequest rebootInstancesRequest = new RebootInstancesRequest()
|
.instanceIds(yourInstanceId)
|
||||||
.withInstanceIds(yourInstanceId);
|
.build();
|
||||||
|
|
||||||
ec2Client.rebootInstances(rebootInstancesRequest);
|
ec2Client.rebootInstances(rebootInstancesRequest);
|
||||||
|
|
||||||
// Stop an Instance
|
// Stop an Instance
|
||||||
StopInstancesRequest stopInstancesRequest = new StopInstancesRequest()
|
StopInstancesRequest stopInstancesRequest = StopInstancesRequest.builder()
|
||||||
.withInstanceIds(yourInstanceId);
|
.instanceIds(yourInstanceId)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
ec2Client.stopInstances(stopInstancesRequest)
|
ec2Client.stopInstances(stopInstancesRequest)
|
||||||
.getStoppingInstances()
|
.stoppingInstances()
|
||||||
.get(0)
|
.get(0)
|
||||||
.getPreviousState()
|
.previousState()
|
||||||
.getName();
|
.name();
|
||||||
|
|
||||||
// Describe an Instance
|
// Describe an Instance
|
||||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
|
DescribeInstancesRequest describeInstancesRequest = DescribeInstancesRequest.builder().build();
|
||||||
DescribeInstancesResult response = ec2Client.describeInstances(describeInstancesRequest);
|
DescribeInstancesResponse response = ec2Client.describeInstances(describeInstancesRequest);
|
||||||
System.out.println(response.getReservations()
|
System.out.println(response.reservations()
|
||||||
.get(0)
|
.get(0)
|
||||||
.getInstances()
|
.instances()
|
||||||
.get(0)
|
.get(0)
|
||||||
.getKernelId());
|
.kernelId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,5 @@
|
||||||
package com.baeldung.rds;
|
package com.baeldung.rds;
|
||||||
|
|
||||||
import com.amazonaws.auth.AWSCredentialsProvider;
|
|
||||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
|
||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
|
||||||
import com.amazonaws.regions.Regions;
|
|
||||||
import com.amazonaws.services.rds.AmazonRDS;
|
|
||||||
import com.amazonaws.services.rds.AmazonRDSClientBuilder;
|
|
||||||
import com.amazonaws.services.rds.model.*;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
@ -16,12 +8,22 @@ import java.util.Properties;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
|
||||||
|
import software.amazon.awssdk.regions.Region;
|
||||||
|
import software.amazon.awssdk.services.rds.RdsClient;
|
||||||
|
import software.amazon.awssdk.services.rds.model.CreateDbInstanceRequest;
|
||||||
|
import software.amazon.awssdk.services.rds.model.CreateDbInstanceResponse;
|
||||||
|
import software.amazon.awssdk.services.rds.model.DBInstance;
|
||||||
|
import software.amazon.awssdk.services.rds.model.DeleteDbInstanceRequest;
|
||||||
|
import software.amazon.awssdk.services.rds.model.DeleteDbInstanceResponse;
|
||||||
|
import software.amazon.awssdk.services.rds.model.DescribeDbInstancesResponse;
|
||||||
|
import software.amazon.awssdk.services.rds.model.Endpoint;
|
||||||
|
|
||||||
public class AWSRDSService {
|
public class AWSRDSService {
|
||||||
|
|
||||||
|
|
||||||
final static Logger logger = Logger.getLogger(AWSRDSService.class.getName());
|
final static Logger logger = Logger.getLogger(AWSRDSService.class.getName());
|
||||||
private AWSCredentialsProvider credentials;
|
private RdsClient rdsClient;
|
||||||
private AmazonRDS amazonRDS;
|
|
||||||
private String db_username;
|
private String db_username;
|
||||||
private String db_password;
|
private String db_password;
|
||||||
private String db_database;
|
private String db_database;
|
||||||
|
@ -34,22 +36,17 @@ public class AWSRDSService {
|
||||||
* **/
|
* **/
|
||||||
public AWSRDSService() throws IOException {
|
public AWSRDSService() throws IOException {
|
||||||
//Init RDS client with credentials and region.
|
//Init RDS client with credentials and region.
|
||||||
credentials = new
|
|
||||||
AWSStaticCredentialsProvider(new
|
|
||||||
BasicAWSCredentials("<ACCESS_KEY>",
|
|
||||||
"<SECRET_KEY>"));
|
|
||||||
amazonRDS = AmazonRDSClientBuilder.standard().withCredentials(credentials)
|
|
||||||
.withRegion(Regions.AP_SOUTHEAST_2).build();
|
|
||||||
Properties prop = new Properties();
|
Properties prop = new Properties();
|
||||||
InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties");
|
InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties");
|
||||||
prop.load(input);
|
prop.load(input);
|
||||||
db_username = prop.getProperty("db_username");
|
db_username = prop.getProperty("db_username");
|
||||||
db_password = prop.getProperty("db_password");
|
db_password = prop.getProperty("db_password");
|
||||||
db_database = prop.getProperty("db_database");
|
db_database = prop.getProperty("db_database");
|
||||||
}
|
|
||||||
|
|
||||||
public AWSRDSService(AmazonRDS amazonRDS){
|
rdsClient = RdsClient.builder()
|
||||||
this.amazonRDS = amazonRDS;
|
.region(Region.AP_SOUTHEAST_2)
|
||||||
|
.credentialsProvider(ProfileCredentialsProvider.create("default"))
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -60,29 +57,29 @@ public class AWSRDSService {
|
||||||
public String launchInstance() {
|
public String launchInstance() {
|
||||||
|
|
||||||
String identifier = "";
|
String identifier = "";
|
||||||
CreateDBInstanceRequest request = new CreateDBInstanceRequest();
|
CreateDbInstanceRequest instanceRequest = CreateDbInstanceRequest.builder()
|
||||||
// RDS instance name
|
.dbInstanceIdentifier("Sydney")
|
||||||
request.setDBInstanceIdentifier("Sydney");
|
.engine("postgres")
|
||||||
request.setEngine("postgres");
|
.multiAZ(false)
|
||||||
request.setMultiAZ(false);
|
.masterUsername(db_username)
|
||||||
request.setMasterUsername(db_username);
|
.masterUserPassword(db_password)
|
||||||
request.setMasterUserPassword(db_password);
|
.dbName(db_database)
|
||||||
request.setDBName(db_database);
|
.storageType("gp2")
|
||||||
request.setStorageType("gp2");
|
.allocatedStorage(10)
|
||||||
request.setAllocatedStorage(10);
|
.build();
|
||||||
|
|
||||||
DBInstance instance = amazonRDS.createDBInstance(request);
|
CreateDbInstanceResponse createDbInstanceResponse = rdsClient.createDBInstance(instanceRequest);
|
||||||
|
|
||||||
// Information about the new RDS instance
|
// Information about the new RDS instance
|
||||||
identifier = instance.getDBInstanceIdentifier();
|
identifier = createDbInstanceResponse.dbInstance().dbInstanceIdentifier();
|
||||||
String status = instance.getDBInstanceStatus();
|
String status = createDbInstanceResponse.dbInstance().dbInstanceStatus();
|
||||||
Endpoint endpoint = instance.getEndpoint();
|
Endpoint endpoint = createDbInstanceResponse.dbInstance().endpoint();
|
||||||
String endpoint_url = "Endpoint URL not available yet.";
|
String endpointUrl = "Endpoint URL not available yet.";
|
||||||
if (endpoint != null) {
|
if (endpoint != null) {
|
||||||
endpoint_url = endpoint.toString();
|
endpointUrl = endpoint.toString();
|
||||||
}
|
}
|
||||||
logger.info(identifier + "\t" + status);
|
logger.info(identifier + "\t" + status);
|
||||||
logger.info(endpoint_url);
|
logger.info(endpointUrl);
|
||||||
|
|
||||||
return identifier;
|
return identifier;
|
||||||
|
|
||||||
|
@ -90,44 +87,44 @@ public class AWSRDSService {
|
||||||
|
|
||||||
// Describe DB instances
|
// Describe DB instances
|
||||||
public void listInstances() {
|
public void listInstances() {
|
||||||
DescribeDBInstancesResult result = amazonRDS.describeDBInstances();
|
DescribeDbInstancesResponse response = rdsClient.describeDBInstances();
|
||||||
List<DBInstance> instances = result.getDBInstances();
|
List<DBInstance> instances = response.dbInstances();
|
||||||
for (DBInstance instance : instances) {
|
for (DBInstance instance : instances) {
|
||||||
// Information about each RDS instance
|
// Information about each RDS instance
|
||||||
String identifier = instance.getDBInstanceIdentifier();
|
String identifier = instance.dbInstanceIdentifier();
|
||||||
String engine = instance.getEngine();
|
String engine = instance.engine();
|
||||||
String status = instance.getDBInstanceStatus();
|
String status = instance.dbInstanceStatus();
|
||||||
Endpoint endpoint = instance.getEndpoint();
|
Endpoint endpoint = instance.endpoint();
|
||||||
String endpoint_url = "Endpoint URL not available yet.";
|
String endpointUrl = "Endpoint URL not available yet.";
|
||||||
if (endpoint != null) {
|
if (endpoint != null) {
|
||||||
endpoint_url = endpoint.toString();
|
endpointUrl = endpoint.toString();
|
||||||
}
|
}
|
||||||
logger.info(identifier + "\t" + engine + "\t" + status);
|
logger.info(identifier + "\t" + engine + "\t" + status);
|
||||||
logger.info("\t" + endpoint_url);
|
logger.info("\t" + endpointUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Delete RDS instance
|
//Delete RDS instance
|
||||||
public void terminateInstance(String identifier) {
|
public void terminateInstance(String identifier) {
|
||||||
|
|
||||||
DeleteDBInstanceRequest request = new DeleteDBInstanceRequest();
|
DeleteDbInstanceRequest request = DeleteDbInstanceRequest.builder()
|
||||||
request.setDBInstanceIdentifier(identifier);
|
.dbInstanceIdentifier(identifier)
|
||||||
request.setSkipFinalSnapshot(true);
|
.skipFinalSnapshot(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
// Delete the RDS instance
|
// Delete the RDS instance
|
||||||
DBInstance instance = amazonRDS.deleteDBInstance(request);
|
DeleteDbInstanceResponse response = rdsClient.deleteDBInstance(request);
|
||||||
|
|
||||||
// Information about the RDS instance being deleted
|
// Information about the RDS instance being deleted
|
||||||
String status = instance.getDBInstanceStatus();
|
String status = response.dbInstance().dbInstanceStatus();
|
||||||
Endpoint endpoint = instance.getEndpoint();
|
Endpoint endpoint = response.dbInstance().endpoint();
|
||||||
String endpoint_url = "Endpoint URL not available yet.";
|
String endpointUrl = "Endpoint URL not available yet.";
|
||||||
if (endpoint != null) {
|
if (endpoint != null) {
|
||||||
endpoint_url = endpoint.toString();
|
endpointUrl = endpoint.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(identifier + "\t" + status);
|
logger.info(identifier + "\t" + status);
|
||||||
logger.info(endpoint_url);
|
logger.info(endpointUrl);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,140 +5,190 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.amazonaws.auth.AWSCredentials;
|
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
|
||||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
import software.amazon.awssdk.regions.Region;
|
||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
import software.amazon.awssdk.services.sqs.SqsClient;
|
||||||
import com.amazonaws.regions.Regions;
|
import software.amazon.awssdk.services.sqs.model.CreateQueueRequest;
|
||||||
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
|
import software.amazon.awssdk.services.sqs.model.DeleteMessageRequest;
|
||||||
import com.amazonaws.services.sqs.model.CreateQueueRequest;
|
import software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest;
|
||||||
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
|
import software.amazon.awssdk.services.sqs.model.GetQueueAttributesResponse;
|
||||||
import com.amazonaws.services.sqs.model.GetQueueAttributesRequest;
|
import software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest;
|
||||||
import com.amazonaws.services.sqs.model.GetQueueAttributesResult;
|
import software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse;
|
||||||
import com.amazonaws.services.sqs.model.MessageAttributeValue;
|
import software.amazon.awssdk.services.sqs.model.Message;
|
||||||
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
|
import software.amazon.awssdk.services.sqs.model.MessageAttributeValue;
|
||||||
import com.amazonaws.services.sqs.model.SendMessageBatchRequest;
|
import software.amazon.awssdk.services.sqs.model.QueueAttributeName;
|
||||||
import com.amazonaws.services.sqs.model.SendMessageRequest;
|
import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest;
|
||||||
import com.amazonaws.services.sqs.model.SetQueueAttributesRequest;
|
import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest;
|
||||||
import com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry;
|
import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequestEntry;
|
||||||
import com.amazonaws.services.sqs.model.Message;
|
import software.amazon.awssdk.services.sqs.model.SendMessageRequest;
|
||||||
import com.amazonaws.services.sqs.AmazonSQS;
|
import software.amazon.awssdk.services.sqs.model.SetQueueAttributesRequest;
|
||||||
|
|
||||||
public class SQSApplication {
|
public class SQSApplication {
|
||||||
|
|
||||||
private static final AWSCredentials credentials;
|
private static final String STANDARD_QUEUE_NAME = "baeldung-queue";
|
||||||
|
private static final String FIFO_QUEUE_NAME = "baeldung-queue.fifo";
|
||||||
static {
|
private static final String DEAD_LETTER_QUEUE_NAME = "baeldung-dead-letter-queue";
|
||||||
// put your accesskey and secretkey here
|
|
||||||
credentials = new BasicAWSCredentials(
|
|
||||||
"<AWS accesskey>",
|
|
||||||
"<AWS secretkey>"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// Set up the client
|
// Set up the client
|
||||||
AmazonSQS sqs = AmazonSQSClientBuilder.standard()
|
SqsClient sqsClient = SqsClient.builder()
|
||||||
.withCredentials(new AWSStaticCredentialsProvider(credentials))
|
.region(Region.US_EAST_1)
|
||||||
.withRegion(Regions.US_EAST_1)
|
.credentialsProvider(ProfileCredentialsProvider.create())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Create a standard queue
|
// Create a standard queue
|
||||||
|
CreateQueueRequest createStandardQueueRequest = CreateQueueRequest.builder()
|
||||||
|
.queueName(STANDARD_QUEUE_NAME)
|
||||||
|
.build();
|
||||||
|
|
||||||
CreateQueueRequest createStandardQueueRequest = new CreateQueueRequest("baeldung-queue");
|
sqsClient.createQueue(createStandardQueueRequest);
|
||||||
String standardQueueUrl = sqs.createQueue(createStandardQueueRequest)
|
|
||||||
.getQueueUrl();
|
System.out.println("\nGet queue url");
|
||||||
|
|
||||||
|
GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder()
|
||||||
|
.queueName(STANDARD_QUEUE_NAME)
|
||||||
|
.build());
|
||||||
|
String standardQueueUrl = getQueueUrlResponse.queueUrl();
|
||||||
|
|
||||||
System.out.println(standardQueueUrl);
|
System.out.println(standardQueueUrl);
|
||||||
|
|
||||||
// Create a fifo queue
|
// Create a fifo queue
|
||||||
|
Map<QueueAttributeName, String> queueAttributes = new HashMap<>();
|
||||||
|
queueAttributes.put(QueueAttributeName.FIFO_QUEUE, "true");
|
||||||
|
queueAttributes.put(QueueAttributeName.CONTENT_BASED_DEDUPLICATION, "true");
|
||||||
|
|
||||||
Map<String, String> queueAttributes = new HashMap<String, String>();
|
CreateQueueRequest createFifoQueueRequest = CreateQueueRequest.builder()
|
||||||
queueAttributes.put("FifoQueue", "true");
|
.queueName(FIFO_QUEUE_NAME)
|
||||||
queueAttributes.put("ContentBasedDeduplication", "true");
|
.attributes(queueAttributes)
|
||||||
|
.build();
|
||||||
|
|
||||||
CreateQueueRequest createFifoQueueRequest = new CreateQueueRequest("baeldung-queue.fifo").withAttributes(queueAttributes);
|
sqsClient.createQueue(createFifoQueueRequest);
|
||||||
String fifoQueueUrl = sqs.createQueue(createFifoQueueRequest)
|
|
||||||
.getQueueUrl();
|
GetQueueUrlResponse getFifoQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder()
|
||||||
|
.queueName(FIFO_QUEUE_NAME)
|
||||||
|
.build());
|
||||||
|
|
||||||
|
String fifoQueueUrl = getFifoQueueUrlResponse.queueUrl();
|
||||||
|
|
||||||
System.out.println(fifoQueueUrl);
|
System.out.println(fifoQueueUrl);
|
||||||
|
|
||||||
// Set up a dead letter queue
|
// Set up a dead letter queue
|
||||||
|
CreateQueueRequest createDeadLetterQueueRequest = CreateQueueRequest.builder()
|
||||||
|
.queueName(DEAD_LETTER_QUEUE_NAME)
|
||||||
|
.build();
|
||||||
|
|
||||||
String deadLetterQueueUrl = sqs.createQueue("baeldung-dead-letter-queue")
|
String deadLetterQueueUrl = sqsClient.createQueue(createDeadLetterQueueRequest)
|
||||||
.getQueueUrl();
|
.queueUrl();
|
||||||
|
|
||||||
GetQueueAttributesResult deadLetterQueueAttributes = sqs.getQueueAttributes(new GetQueueAttributesRequest(deadLetterQueueUrl).withAttributeNames("QueueArn"));
|
GetQueueAttributesRequest getQueueAttributesRequest = GetQueueAttributesRequest.builder()
|
||||||
|
.queueUrl(deadLetterQueueUrl)
|
||||||
|
.attributeNames(QueueAttributeName.QUEUE_ARN)
|
||||||
|
.build();
|
||||||
|
|
||||||
String deadLetterQueueARN = deadLetterQueueAttributes.getAttributes()
|
GetQueueAttributesResponse deadLetterQueueAttributes = sqsClient.getQueueAttributes(getQueueAttributesRequest);
|
||||||
|
|
||||||
|
String deadLetterQueueARN = deadLetterQueueAttributes.attributes()
|
||||||
.get("QueueArn");
|
.get("QueueArn");
|
||||||
|
|
||||||
SetQueueAttributesRequest queueAttributesRequest = new SetQueueAttributesRequest().withQueueUrl(standardQueueUrl)
|
Map<QueueAttributeName, String> attributes = new HashMap<>();
|
||||||
.addAttributesEntry("RedrivePolicy", "{\"maxReceiveCount\":\"2\", " + "\"deadLetterTargetArn\":\"" + deadLetterQueueARN + "\"}");
|
attributes.put(QueueAttributeName.REDRIVE_POLICY, "{\"maxReceiveCount\":\"5\", \"deadLetterTargetArn\":\"" + deadLetterQueueARN + "\"}");
|
||||||
|
|
||||||
sqs.setQueueAttributes(queueAttributesRequest);
|
SetQueueAttributesRequest queueAttributesRequest = SetQueueAttributesRequest.builder()
|
||||||
|
.queueUrl(standardQueueUrl)
|
||||||
|
.attributes(attributes)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
sqsClient.setQueueAttributes(queueAttributesRequest);
|
||||||
|
|
||||||
// Send a message to a standard queue
|
// Send a message to a standard queue
|
||||||
|
|
||||||
Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
|
Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
|
||||||
|
MessageAttributeValue messageAttributeValue = MessageAttributeValue.builder()
|
||||||
|
.stringValue("This is an attribute")
|
||||||
|
.dataType("String")
|
||||||
|
.build();
|
||||||
|
|
||||||
messageAttributes.put("AttributeOne", new MessageAttributeValue().withStringValue("This is an attribute")
|
messageAttributes.put("AttributeOne", messageAttributeValue);
|
||||||
.withDataType("String"));
|
|
||||||
|
|
||||||
SendMessageRequest sendMessageStandardQueue = new SendMessageRequest().withQueueUrl(standardQueueUrl)
|
SendMessageRequest sendMessageStandardQueue = SendMessageRequest.builder()
|
||||||
.withMessageBody("A simple message.")
|
.queueUrl(standardQueueUrl)
|
||||||
.withDelaySeconds(30) // Message will arrive in the queue after 30 seconds. We can use this only in standard queues
|
.messageBody("A simple message.")
|
||||||
.withMessageAttributes(messageAttributes);
|
.delaySeconds(30) // Message will arrive in the queue after 30 seconds. We can use this only in standard queues
|
||||||
|
.messageAttributes(messageAttributes)
|
||||||
|
.build();
|
||||||
|
|
||||||
sqs.sendMessage(sendMessageStandardQueue);
|
sqsClient.sendMessage(sendMessageStandardQueue);
|
||||||
|
|
||||||
// Send a message to a fifo queue
|
// Send a message to a fifo queue
|
||||||
|
|
||||||
SendMessageRequest sendMessageFifoQueue = new SendMessageRequest().withQueueUrl(fifoQueueUrl)
|
SendMessageRequest sendMessageFifoQueue = SendMessageRequest.builder()
|
||||||
.withMessageBody("FIFO Queue")
|
.queueUrl(fifoQueueUrl)
|
||||||
.withMessageGroupId("baeldung-group-1")
|
.messageBody("FIFO Queue")
|
||||||
.withMessageAttributes(messageAttributes);
|
.messageGroupId("baeldung-group-1")
|
||||||
|
.messageAttributes(messageAttributes)
|
||||||
|
.build();
|
||||||
|
|
||||||
sqs.sendMessage(sendMessageFifoQueue);
|
sqsClient.sendMessage(sendMessageFifoQueue);
|
||||||
|
|
||||||
// Send multiple messages
|
// Send multiple messages
|
||||||
|
|
||||||
List<SendMessageBatchRequestEntry> messageEntries = new ArrayList<>();
|
List<SendMessageBatchRequestEntry> messageEntries = new ArrayList<>();
|
||||||
messageEntries.add(new SendMessageBatchRequestEntry().withId("id-1")
|
SendMessageBatchRequestEntry messageBatchRequestEntry1 = SendMessageBatchRequestEntry.builder()
|
||||||
.withMessageBody("batch-1")
|
.id("id-1")
|
||||||
.withMessageGroupId("baeldung-group-1"));
|
.messageBody("batch-1")
|
||||||
messageEntries.add(new SendMessageBatchRequestEntry().withId("id-2")
|
.messageGroupId("baeldung-group-1")
|
||||||
.withMessageBody("batch-2")
|
.build();
|
||||||
.withMessageGroupId("baeldung-group-1"));
|
|
||||||
|
|
||||||
SendMessageBatchRequest sendMessageBatchRequest = new SendMessageBatchRequest(fifoQueueUrl, messageEntries);
|
SendMessageBatchRequestEntry messageBatchRequestEntry2 = SendMessageBatchRequestEntry.builder()
|
||||||
sqs.sendMessageBatch(sendMessageBatchRequest);
|
.id("id-2")
|
||||||
|
.messageBody("batch-2")
|
||||||
|
.messageGroupId("baeldung-group-1")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
messageEntries.add(messageBatchRequestEntry1);
|
||||||
|
messageEntries.add(messageBatchRequestEntry2);
|
||||||
|
|
||||||
|
SendMessageBatchRequest sendMessageBatchRequest = SendMessageBatchRequest.builder()
|
||||||
|
.queueUrl(fifoQueueUrl)
|
||||||
|
.entries(messageEntries)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
sqsClient.sendMessageBatch(sendMessageBatchRequest);
|
||||||
|
|
||||||
// Read a message from a queue
|
// Read a message from a queue
|
||||||
|
|
||||||
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(fifoQueueUrl).withWaitTimeSeconds(10) // Long polling;
|
ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder()
|
||||||
.withMaxNumberOfMessages(1); // Max is 10
|
.waitTimeSeconds(10)
|
||||||
|
.maxNumberOfMessages(10)
|
||||||
|
.build();
|
||||||
|
|
||||||
List<Message> sqsMessages = sqs.receiveMessage(receiveMessageRequest)
|
List<Message> sqsMessages = sqsClient.receiveMessage(receiveMessageRequest)
|
||||||
.getMessages();
|
.messages();
|
||||||
|
|
||||||
sqsMessages.get(0)
|
sqsMessages.get(0)
|
||||||
.getAttributes();
|
.attributes();
|
||||||
sqsMessages.get(0)
|
sqsMessages.get(0)
|
||||||
.getBody();
|
.body();
|
||||||
|
|
||||||
// Delete a message from a queue
|
// Delete a message from a queue
|
||||||
|
DeleteMessageRequest deleteMessageRequest = DeleteMessageRequest.builder()
|
||||||
|
.queueUrl(fifoQueueUrl)
|
||||||
|
.receiptHandle(sqsMessages.get(0)
|
||||||
|
.receiptHandle())
|
||||||
|
.build();
|
||||||
|
|
||||||
sqs.deleteMessage(new DeleteMessageRequest().withQueueUrl(fifoQueueUrl)
|
sqsClient.deleteMessage(deleteMessageRequest);
|
||||||
.withReceiptHandle(sqsMessages.get(0)
|
|
||||||
.getReceiptHandle()));
|
|
||||||
|
|
||||||
// Monitoring
|
// Monitoring
|
||||||
GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(standardQueueUrl).withAttributeNames("All");
|
GetQueueAttributesRequest getQueueAttributesRequestForMonitoring = GetQueueAttributesRequest.builder()
|
||||||
GetQueueAttributesResult getQueueAttributesResult = sqs.getQueueAttributes(getQueueAttributesRequest);
|
.queueUrl(standardQueueUrl)
|
||||||
System.out.println(String.format("The number of messages on the queue: %s", getQueueAttributesResult.getAttributes()
|
.build();
|
||||||
|
|
||||||
|
GetQueueAttributesResponse attributesResponse = sqsClient.getQueueAttributes(getQueueAttributesRequestForMonitoring);
|
||||||
|
System.out.println(String.format("The number of messages on the queue: %s", attributesResponse.attributes()
|
||||||
.get("ApproximateNumberOfMessages")));
|
.get("ApproximateNumberOfMessages")));
|
||||||
System.out.println(String.format("The number of messages in flight: %s", getQueueAttributesResult.getAttributes()
|
System.out.println(String.format("The number of messages in flight: %s", attributesResponse.attributes()
|
||||||
.get("ApproximateNumberOfMessagesNotVisible")));
|
.get("ApproximateNumberOfMessagesNotVisible")));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,14 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>aws-modules</artifactId>
|
<artifactId>aws-modules</artifactId>
|
||||||
<name>aws-modules</name>
|
<name>aws-modules</name>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-java-sdk-dynamodb</artifactId>
|
||||||
|
<version>1.12.523</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
@ -15,6 +23,7 @@
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>aws-app-sync</module>
|
<module>aws-app-sync</module>
|
||||||
|
<module>aws-dynamodb</module>
|
||||||
<module>aws-lambda-modules</module>
|
<module>aws-lambda-modules</module>
|
||||||
<module>aws-miscellaneous</module>
|
<module>aws-miscellaneous</module>
|
||||||
<module>aws-reactive</module>
|
<module>aws-reactive</module>
|
||||||
|
@ -24,6 +33,7 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<aws-java-sdk.version>1.12.331</aws-java-sdk.version>
|
<aws-java-sdk.version>1.12.331</aws-java-sdk.version>
|
||||||
|
<aws-java-sdk-v2.version>2.20.147</aws-java-sdk-v2.version>
|
||||||
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -5,3 +5,4 @@
|
||||||
- [Collecting Stream Elements into a List in Java](https://www.baeldung.com/java-stream-to-list-collecting)
|
- [Collecting Stream Elements into a List in Java](https://www.baeldung.com/java-stream-to-list-collecting)
|
||||||
- [New Features in Java 16](https://www.baeldung.com/java-16-new-features)
|
- [New Features in Java 16](https://www.baeldung.com/java-16-new-features)
|
||||||
- [Guide to Java 8 groupingBy Collector](https://www.baeldung.com/java-groupingby-collector)
|
- [Guide to Java 8 groupingBy Collector](https://www.baeldung.com/java-groupingby-collector)
|
||||||
|
- [Value-Based Classes in Java](https://www.baeldung.com/java-value-based-classes)
|
||||||
|
|
|
@ -30,6 +30,11 @@ public final class Point {
|
||||||
return new Point(x, y, z);
|
return new Point(x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Point{" + "x=" + x + ", y=" + y + ", z=" + z + '}';
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object other) {
|
public boolean equals(Object other) {
|
||||||
if (other == null || getClass() != other.getClass())
|
if (other == null || getClass() != other.getClass())
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
package com.baeldung.value_based_class;
|
package com.baeldung.value_based_class;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ValueBasedClassUnitTest {
|
public class ValueBasedClassUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenAutoboxedAndPrimitive_whenCompared_thenReturnEquals() {
|
public void givenAutoboxedAndPrimitive_whenCompared_thenReturnEquals() {
|
||||||
int primitive_a = 125;
|
List<Integer> list = new ArrayList<>();
|
||||||
Integer obj_a = 125; // this is autoboxed
|
list.add(1); // this is autoboxed
|
||||||
Assert.assertSame(primitive_a, obj_a);
|
Assert.assertEquals(list.get(0), Integer.valueOf(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
package com.baeldung.recordproperties;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.RecordComponent;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
record Player(String name, int age, Long score) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ReadRecordPropertiesByReflectionUnitTest {
|
||||||
|
private static final Player ERIC = new Player("Eric", 28, 4242L);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingRecordComponent_thenGetExpectedResult() {
|
||||||
|
var fields = new ArrayList<Field>();
|
||||||
|
RecordComponent[] components = Player.class.getRecordComponents();
|
||||||
|
for (var comp : components) {
|
||||||
|
try {
|
||||||
|
Field field = ERIC.getClass()
|
||||||
|
.getDeclaredField(comp.getName());
|
||||||
|
field.setAccessible(true);
|
||||||
|
fields.add(field);
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
// for simplicity, error handling is skipped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(3, fields.size());
|
||||||
|
|
||||||
|
var nameField = fields.get(0);
|
||||||
|
var ageField = fields.get(1);
|
||||||
|
var scoreField = fields.get(2);
|
||||||
|
try {
|
||||||
|
assertEquals("name", nameField.getName());
|
||||||
|
assertEquals(String.class, nameField.getType());
|
||||||
|
assertEquals("Eric", nameField.get(ERIC));
|
||||||
|
|
||||||
|
assertEquals("age", ageField.getName());
|
||||||
|
assertEquals(int.class, ageField.getType());
|
||||||
|
assertEquals(28, ageField.get(ERIC));
|
||||||
|
|
||||||
|
assertEquals("score", scoreField.getName());
|
||||||
|
assertEquals(Long.class, scoreField.getType());
|
||||||
|
assertEquals(4242L, scoreField.get(ERIC));
|
||||||
|
} catch (IllegalAccessException exception) {
|
||||||
|
// for simplicity, error handling is skipped
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingClassGetDeclaredField_thenGetExpectedResult() {
|
||||||
|
// record has no public fields
|
||||||
|
assertEquals(0, Player.class.getFields().length);
|
||||||
|
|
||||||
|
var fields = new ArrayList<Field>();
|
||||||
|
for (var field : Player.class.getDeclaredFields()) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
fields.add(field);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(3, fields.size());
|
||||||
|
var nameField = fields.get(0);
|
||||||
|
var ageField = fields.get(1);
|
||||||
|
var scoreField = fields.get(2);
|
||||||
|
|
||||||
|
try {
|
||||||
|
assertEquals("name", nameField.getName());
|
||||||
|
assertEquals(String.class, nameField.getType());
|
||||||
|
assertEquals("Eric", nameField.get(ERIC));
|
||||||
|
|
||||||
|
assertEquals("age", ageField.getName());
|
||||||
|
assertEquals(int.class, ageField.getType());
|
||||||
|
assertEquals(28, ageField.get(ERIC));
|
||||||
|
|
||||||
|
assertEquals("score", scoreField.getName());
|
||||||
|
assertEquals(Long.class, scoreField.getType());
|
||||||
|
assertEquals(4242L, scoreField.get(ERIC));
|
||||||
|
} catch (IllegalAccessException ex) {
|
||||||
|
// for simplicity, error handling is skipped
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
This is a test 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,2 +1,5 @@
|
||||||
## Relevant Articles
|
## Relevant Articles
|
||||||
- [Sequenced Collections in Java 21](https://www.baeldung.com/java-21-sequenced-collections)
|
- [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)
|
||||||
|
|
|
@ -12,6 +12,12 @@
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source.version>21</maven.compiler.source.version>
|
||||||
|
<maven.compiler.target.version>21</maven.compiler.target.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
@ -20,14 +26,22 @@
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>21</source>
|
<source>21</source>
|
||||||
<target>21</target>
|
<target>21</target>
|
||||||
<compilerArgs>--enable-preview</compilerArgs>
|
<!-- Needed due to a bug with JDK 21, described here: https://issues.apache.org/jira/browse/MCOMPILER-546?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel&focusedCommentId=17767513 -->
|
||||||
|
<debug>false</debug>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>--enable-preview</arg>
|
||||||
|
</compilerArgs>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<argLine>--enable-preview</argLine>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<properties>
|
|
||||||
<maven.compiler.source.version>21</maven.compiler.source.version>
|
|
||||||
<maven.compiler.target.version>21</maven.compiler.target.version>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
</properties>
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.unnamed.variables;
|
||||||
|
|
||||||
|
public record Car<T extends Engine>(String name, String color, T engine) { }
|
||||||
|
|
||||||
|
abstract class Engine { }
|
||||||
|
|
||||||
|
class GasEngine extends Engine { }
|
||||||
|
|
||||||
|
class ElectricEngine extends Engine { }
|
||||||
|
|
||||||
|
class HybridEngine extends Engine { }
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.baeldung.unnamed.variables;
|
||||||
|
|
||||||
|
public class UnnamedPatterns {
|
||||||
|
|
||||||
|
static String getObjectsColorWithNamedPattern(Object object) {
|
||||||
|
if (object instanceof Car(String name, String color, Engine engine)) {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
return "No color!";
|
||||||
|
}
|
||||||
|
|
||||||
|
static String getObjectsColorWithUnnamedPattern(Object object) {
|
||||||
|
if (object instanceof Car(_, String color, _)) {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
return "No color!";
|
||||||
|
}
|
||||||
|
|
||||||
|
static String getObjectsColorWithSwitchAndNamedPattern(Object object) {
|
||||||
|
return switch (object) {
|
||||||
|
case Car(String name, String color, Engine engine) -> color;
|
||||||
|
default -> "No color!";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static String getObjectsColorWithSwitchAndUnnamedPattern(Object object) {
|
||||||
|
return switch (object) {
|
||||||
|
case Car(_, String color, _) -> color;
|
||||||
|
default -> "No color!";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static String getEngineTypeWithNamedPattern(Car<?> car) {
|
||||||
|
return switch (car) {
|
||||||
|
case Car(String name, String color, GasEngine engine) -> "gas";
|
||||||
|
case Car(String name, String color, ElectricEngine engine) -> "electric";
|
||||||
|
case Car(String name, String color, HybridEngine engine) -> "hybrid";
|
||||||
|
default -> "none";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static String getEngineTypeWithUnnamedPattern(Car<?> car) {
|
||||||
|
return switch (car) {
|
||||||
|
case Car(_, _, GasEngine _) -> "gas";
|
||||||
|
case Car(_, _, ElectricEngine _) -> "electric";
|
||||||
|
case Car(_, _, HybridEngine _) -> "hybrid";
|
||||||
|
default -> "none";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,134 @@
|
||||||
|
package com.baeldung.unnamed.variables;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
class Transaction implements AutoCloseable {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
System.out.println("Closed!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class UnnamedVariables {
|
||||||
|
|
||||||
|
static int countCarsOverLimitWithNamedVariable(Collection<Car<?>> cars, int limit) {
|
||||||
|
var total = 0;
|
||||||
|
var totalOverLimit = 0;
|
||||||
|
for (var car : cars) {
|
||||||
|
total++;
|
||||||
|
if (total > limit) {
|
||||||
|
totalOverLimit++;
|
||||||
|
// side effect
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return totalOverLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int countCarsOverLimitWithUnnamedVariable(Collection<Car<?>> cars, int limit) {
|
||||||
|
var total = 0;
|
||||||
|
var totalOverLimit = 0;
|
||||||
|
for (var _ : cars) {
|
||||||
|
total++;
|
||||||
|
if (total > limit) {
|
||||||
|
totalOverLimit++;
|
||||||
|
// side effect
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return totalOverLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sendNotificationToCarsWithNamedVariable(Collection<Car<?>> cars) {
|
||||||
|
sendOneTimeNotification();
|
||||||
|
for (int i = 0; i < cars.size(); i++) {
|
||||||
|
// Notify car
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sendNotificationToCarsWithUnnamedVariable(Collection<Car<?>> cars) {
|
||||||
|
for (int i = 0, _ = sendOneTimeNotification(); i < cars.size(); i++) {
|
||||||
|
// Notify car
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int sendOneTimeNotification() {
|
||||||
|
System.out.println("Sending one time notification!");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Car<?> removeThreeCarsAndReturnFirstRemovedWithNamedVariables(Queue<Car<?>> cars) {
|
||||||
|
var x = cars.poll();
|
||||||
|
var y = cars.poll();
|
||||||
|
var z = cars.poll();
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Car<?> removeThreeCarsAndReturnFirstRemovedWithUnnamedVariables(Queue<Car<?>> cars) {
|
||||||
|
var car = cars.poll();
|
||||||
|
var _ = cars.poll();
|
||||||
|
var _ = cars.poll();
|
||||||
|
return car;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handleCarExceptionWithNamedVariables(Car<?> car) {
|
||||||
|
try {
|
||||||
|
someOperationThatFails(car);
|
||||||
|
} catch (IllegalStateException ex) {
|
||||||
|
System.out.println("Got an illegal state exception for: " + car.name());
|
||||||
|
} catch (RuntimeException ex) {
|
||||||
|
System.out.println("Got a runtime exception!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handleCarExceptionWithUnnamedVariables(Car<?> car) {
|
||||||
|
try {
|
||||||
|
someOperationThatFails(car);
|
||||||
|
} catch (IllegalStateException | NumberFormatException _) {
|
||||||
|
System.out.println("Got an illegal state exception for: " + car.name());
|
||||||
|
} catch (RuntimeException _) {
|
||||||
|
System.out.println("Got a runtime exception!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void obtainTransactionAndUpdateCarWithNamedVariables(Car<?> car) {
|
||||||
|
try (var transaction = new Transaction()) {
|
||||||
|
updateCar(car);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void obtainTransactionAndUpdateCarWithUnnamedVariables(Car<?> car) {
|
||||||
|
try (var _ = new Transaction()) {
|
||||||
|
updateCar(car);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void updateCar(Car<?> car) {
|
||||||
|
// Some update logic
|
||||||
|
System.out.println("Car updated!");
|
||||||
|
}
|
||||||
|
|
||||||
|
static Map<String, List<Car<?>>> getCarsByFirstLetterWithNamedVariables(List<Car<?>> cars) {
|
||||||
|
Map<String, List<Car<?>>> carMap = new HashMap<>();
|
||||||
|
cars.forEach(car ->
|
||||||
|
carMap.computeIfAbsent(car.name().substring(0, 1), firstLetter -> new ArrayList<>()).add(car)
|
||||||
|
);
|
||||||
|
return carMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Map<String, List<Car<?>>> getCarsByFirstLetterWithUnnamedVariables(List<Car<?>> cars) {
|
||||||
|
Map<String, List<Car<?>>> carMap = new HashMap<>();
|
||||||
|
cars.forEach(car ->
|
||||||
|
carMap.computeIfAbsent(car.name().substring(0, 1), _ -> new ArrayList<>()).add(car)
|
||||||
|
);
|
||||||
|
return carMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void someOperationThatFails(Car<?> car) {
|
||||||
|
throw new IllegalStateException("Triggered exception for: " + car.name());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
void main() {
|
||||||
|
System.out.println("Hello, World!");
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.unnamedclasses;
|
||||||
|
|
||||||
|
public class HelloWorldChild extends HelloWorldSuper {
|
||||||
|
void main() {
|
||||||
|
System.out.println("Hello, World!");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.unnamedclasses;
|
||||||
|
|
||||||
|
public class HelloWorldSuper {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Hello from the superclass");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
private String getMessage() {
|
||||||
|
return "Hello, World!";
|
||||||
|
}
|
||||||
|
void main() {
|
||||||
|
System.out.println(getMessage());
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.unnamed.variables;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
class CarScenario {
|
||||||
|
|
||||||
|
protected final List<Car<?>> cars = List.of(
|
||||||
|
new Car<>("Mitsubishi", "blue", new GasEngine()),
|
||||||
|
new Car<>("Toyota", "red", new ElectricEngine()),
|
||||||
|
new Car<>("Jaguar", "white", new HybridEngine())
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.unnamed.variables;
|
||||||
|
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedPatterns.getEngineTypeWithNamedPattern;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedPatterns.getEngineTypeWithUnnamedPattern;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedPatterns.getObjectsColorWithNamedPattern;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedPatterns.getObjectsColorWithSwitchAndNamedPattern;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedPatterns.getObjectsColorWithSwitchAndUnnamedPattern;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedPatterns.getObjectsColorWithUnnamedPattern;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class UnnamedPatternsUnitTest extends CarScenario {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenExtractingColorWithNamedPatterns_thenReturnBlue() {
|
||||||
|
assertEquals("blue", getObjectsColorWithNamedPattern(cars.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenExtractingColorWithUnnamedPatterns_thenReturnBlue() {
|
||||||
|
assertEquals("blue", getObjectsColorWithUnnamedPattern(cars.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenExtractingColorWithSwitchAndNamedPatterns_thenReturnBlue() {
|
||||||
|
assertEquals("blue", getObjectsColorWithSwitchAndNamedPattern(cars.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenExtractingColorWithSwitchAndUnnamedPatterns_thenReturnBlue() {
|
||||||
|
assertEquals("blue", getObjectsColorWithSwitchAndUnnamedPattern(cars.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenExtractingEngineTypeWithNamedPatterns_thenReturnGas() {
|
||||||
|
assertEquals("gas", getEngineTypeWithNamedPattern(cars.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenExtractingEngineTypeWithUnnamedPatterns_thenReturnGas() {
|
||||||
|
assertEquals("gas", getEngineTypeWithUnnamedPattern(cars.get(0)));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,93 @@
|
||||||
|
package com.baeldung.unnamed.variables;
|
||||||
|
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.countCarsOverLimitWithNamedVariable;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.countCarsOverLimitWithUnnamedVariable;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.getCarsByFirstLetterWithNamedVariables;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.getCarsByFirstLetterWithUnnamedVariables;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.handleCarExceptionWithNamedVariables;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.handleCarExceptionWithUnnamedVariables;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.obtainTransactionAndUpdateCarWithNamedVariables;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.obtainTransactionAndUpdateCarWithUnnamedVariables;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.removeThreeCarsAndReturnFirstRemovedWithNamedVariables;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.removeThreeCarsAndReturnFirstRemovedWithUnnamedVariables;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.sendNotificationToCarsWithNamedVariable;
|
||||||
|
import static com.baeldung.unnamed.variables.UnnamedVariables.sendNotificationToCarsWithUnnamedVariable;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class UnnamedVariablesUnitTest extends CarScenario {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCountingCarsOverLimitWithNamedVariables_thenShouldReturnOne() {
|
||||||
|
assertEquals(1, countCarsOverLimitWithNamedVariable(cars, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCountingCarsOverLimitWithUnnamedVariables_thenShouldReturnOne() {
|
||||||
|
assertEquals(1, countCarsOverLimitWithUnnamedVariable(cars, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNotifyingCarsWithNamedVariables_thenShouldNotFail() {
|
||||||
|
assertDoesNotThrow(() -> sendNotificationToCarsWithNamedVariable(cars));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNotifyingCarsWithUnnamedVariables_thenShouldNotFail() {
|
||||||
|
assertDoesNotThrow(() -> sendNotificationToCarsWithUnnamedVariable(cars));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPollingCarsWithNamedVariables_thenReturnFirstOneAndEmptyQueue() {
|
||||||
|
var carQueue = new LinkedList<>(cars);
|
||||||
|
assertEquals("Mitsubishi", removeThreeCarsAndReturnFirstRemovedWithNamedVariables(carQueue).name());
|
||||||
|
assertEquals(0, carQueue.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPollingCarsWithUnnamedVariables_thenReturnFirstOneAndEmptyQueue() {
|
||||||
|
var carQueue = new LinkedList<>(cars);
|
||||||
|
assertEquals("Mitsubishi", removeThreeCarsAndReturnFirstRemovedWithUnnamedVariables(carQueue).name());
|
||||||
|
assertEquals(0, carQueue.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenHandlingExceptionWithNamedVariables_thenNoExceptionIsThrown() {
|
||||||
|
assertDoesNotThrow(() -> handleCarExceptionWithNamedVariables(cars.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenHandlingExceptionWithUnnamedVariables_thenNoExceptionIsThrown() {
|
||||||
|
assertDoesNotThrow(() -> handleCarExceptionWithUnnamedVariables(cars.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenHandlingTransactionUpdateWithNamedVariables_thenNoExceptionIsThrown() {
|
||||||
|
assertDoesNotThrow(() -> obtainTransactionAndUpdateCarWithNamedVariables(cars.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenHandlingTransactionUpdateWithUnnamedVariables_thenNoExceptionIsThrown() {
|
||||||
|
assertDoesNotThrow(() -> obtainTransactionAndUpdateCarWithUnnamedVariables(cars.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGettingCarsByFirstLetterWithNamedVariables_thenHaveThreeKeys() {
|
||||||
|
var carsByLetter = getCarsByFirstLetterWithNamedVariables(cars);
|
||||||
|
assertEquals(1, carsByLetter.get("M").size());
|
||||||
|
assertEquals(1, carsByLetter.get("T").size());
|
||||||
|
assertEquals(1, carsByLetter.get("J").size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGettingCarsByFirstLetterWithUnnamedVariables_thenHaveThreeKeys() {
|
||||||
|
var carsByLetter = getCarsByFirstLetterWithUnnamedVariables(cars);
|
||||||
|
assertEquals(1, carsByLetter.get("M").size());
|
||||||
|
assertEquals(1, carsByLetter.get("T").size());
|
||||||
|
assertEquals(1, carsByLetter.get("J").size());
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,3 +4,4 @@ This module contains articles about Java 9 streams
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [How to Break from Java Stream forEach](https://www.baeldung.com/java-break-stream-foreach)
|
- [How to Break from Java Stream forEach](https://www.baeldung.com/java-break-stream-foreach)
|
||||||
|
- [Creating Stream of Regex Matches](https://www.baeldung.com/java-stream-regex-matches)
|
||||||
|
|
|
@ -10,3 +10,4 @@ This module contains articles about arrays conversion in Java
|
||||||
- [Convert Java Array to Iterable](https://www.baeldung.com/java-array-convert-to-iterable)
|
- [Convert Java Array to Iterable](https://www.baeldung.com/java-array-convert-to-iterable)
|
||||||
- [Converting an int[] to HashSet in Java](https://www.baeldung.com/java-converting-int-array-to-hashset)
|
- [Converting an int[] to HashSet in Java](https://www.baeldung.com/java-converting-int-array-to-hashset)
|
||||||
- [Convert an ArrayList of String to a String Array in Java](https://www.baeldung.com/java-convert-string-arraylist-array)
|
- [Convert an ArrayList of String to a String Array in Java](https://www.baeldung.com/java-convert-string-arraylist-array)
|
||||||
|
- [Convert Char Array to Int Array in Java](https://www.baeldung.com/java-convert-char-int-array)
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -9,3 +9,4 @@ This module contains complete guides about arrays in Java
|
||||||
- [Guide to ArrayStoreException](https://www.baeldung.com/java-arraystoreexception)
|
- [Guide to ArrayStoreException](https://www.baeldung.com/java-arraystoreexception)
|
||||||
- [Creating a Generic Array in Java](https://www.baeldung.com/java-generic-array)
|
- [Creating a Generic Array in Java](https://www.baeldung.com/java-generic-array)
|
||||||
- [Maximum Size of Java Arrays](https://www.baeldung.com/java-arrays-max-size)
|
- [Maximum Size of Java Arrays](https://www.baeldung.com/java-arrays-max-size)
|
||||||
|
- [Merge Two Arrays and Remove Duplicates in Java](https://www.baeldung.com/java-merge-two-arrays-delete-duplicates)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
## Relevant Articles
|
||||||
|
- [Find the Middle Element of an Array in Java](https://www.baeldung.com/java-array-middle-item)
|
|
@ -14,4 +14,3 @@ This module contains articles about advanced operations on arrays in Java. They
|
||||||
- [Slicing Arrays in Java](https://www.baeldung.com/java-slicing-arrays)
|
- [Slicing Arrays in Java](https://www.baeldung.com/java-slicing-arrays)
|
||||||
- [Combining Two or More Byte Arrays](https://www.baeldung.com/java-concatenate-byte-arrays)
|
- [Combining Two or More Byte Arrays](https://www.baeldung.com/java-concatenate-byte-arrays)
|
||||||
- [Calculating the Sum of Two Arrays in Java](https://www.baeldung.com/java-sum-arrays-element-wise)
|
- [Calculating the Sum of Two Arrays in Java](https://www.baeldung.com/java-sum-arrays-element-wise)
|
||||||
- [Find the Middle Element of an Array in Java](https://www.baeldung.com/java-array-middle-item)
|
|
||||||
|
|
|
@ -5,3 +5,4 @@ This module contains articles about Java Character Class
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Character#isAlphabetic vs. Character#isLetter](https://www.baeldung.com/java-character-isletter-isalphabetic)
|
- [Character#isAlphabetic vs. Character#isLetter](https://www.baeldung.com/java-character-isletter-isalphabetic)
|
||||||
- [Difference Between Java’s “char” and “String”](https://www.baeldung.com/java-char-vs-string)
|
- [Difference Between Java’s “char” and “String”](https://www.baeldung.com/java-char-vs-string)
|
||||||
|
- [Increment Character in Java](https://www.baeldung.com/java-char-sequence)
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.incrementchar;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
class IncrementCharUnitTest {
|
||||||
|
@Test
|
||||||
|
void whenUsingForLoop_thenGenerateCharacters(){
|
||||||
|
final List<Character> allCapitalCharacters = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
|
||||||
|
List<Character> characters = new ArrayList<>();
|
||||||
|
for (char character = 'A'; character <= 'Z'; character++) {
|
||||||
|
characters.add(character);
|
||||||
|
}
|
||||||
|
Assertions.assertEquals(characters, allCapitalCharacters);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingStreams_thenGenerateCharacters() {
|
||||||
|
final List<Character> allCapitalCharacters = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
|
||||||
|
final List<Character> characters = IntStream.rangeClosed('A', 'Z').mapToObj(c -> (char) c).collect(Collectors.toList());
|
||||||
|
Assertions.assertEquals(characters, allCapitalCharacters);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,4 +5,6 @@
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Introduction to Roaring Bitmap](https://www.baeldung.com/java-roaring-bitmap-intro)
|
- [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)
|
- [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)
|
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-4)
|
||||||
|
|
|
@ -0,0 +1,126 @@
|
||||||
|
package com.baeldung.skippingfirstelement;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class SkipFirstElementExample {
|
||||||
|
|
||||||
|
private final List<String> stringList = new ArrayList<>();
|
||||||
|
private final Set<String> stringSet = new HashSet<>();
|
||||||
|
private final Map<String, String> stringMap = new HashMap<>();
|
||||||
|
|
||||||
|
public SkipFirstElementExample() {
|
||||||
|
// Initializing a List
|
||||||
|
stringList.add("Monday");
|
||||||
|
stringList.add("Tuesday");
|
||||||
|
stringList.add("Wednesday");
|
||||||
|
stringList.add("Thursday");
|
||||||
|
stringList.add("Friday");
|
||||||
|
stringList.add("Saturday");
|
||||||
|
stringList.add("Sunday");
|
||||||
|
// Initializing a Set
|
||||||
|
stringSet.add("Monday");
|
||||||
|
stringSet.add("Tuesday");
|
||||||
|
stringSet.add("Wednesday");
|
||||||
|
stringSet.add("Thursday");
|
||||||
|
stringSet.add("Friday");
|
||||||
|
stringSet.add("Saturday");
|
||||||
|
stringSet.add("Sunday");
|
||||||
|
// Initializing a Map
|
||||||
|
stringMap.put("Monday", "The day when coffee is a life support system.");
|
||||||
|
stringMap.put("Tuesday", "The day you realize that Monday's optimism was a lie.");
|
||||||
|
stringMap.put("Wednesday", "Hump Day, or as it's known, the 'Is it Friday yet?' day.");
|
||||||
|
stringMap.put("Thursday", "The day that says, 'Hold my beer, Friday is coming!'");
|
||||||
|
stringMap.put("Friday", "The golden child of the weekdays. The superhero of the workweek.");
|
||||||
|
stringMap.put("Saturday", "The day of rest? More like the day of 'What can I binge-watch next?'");
|
||||||
|
stringMap.put("Sunday", "The day before you have to adult again.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void skippingFirstElementInListWithForLoop(List<String> stringList) {
|
||||||
|
for (int i = 1; i < stringList.size(); i++) {
|
||||||
|
process(stringList.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void skippingFirstElementInListWithWhileLoop(List<String> stringList) {
|
||||||
|
final Iterator<String> iterator = stringList.iterator();
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
iterator.next();
|
||||||
|
}
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
process(iterator.next());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void skippingFirstElementInSetWithWhileLoop(Set<String> stringSet) {
|
||||||
|
final Iterator<String> iterator = stringSet.iterator();
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
iterator.next();
|
||||||
|
}
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
process(iterator.next());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void skippingFirstElementInListWithWhileLoopStoringFirstElement(List<String> stringList) {
|
||||||
|
final Iterator<String> iterator = stringList.iterator();
|
||||||
|
String firstElement = null;
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
firstElement = iterator.next();
|
||||||
|
}
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
process(iterator.next());
|
||||||
|
// additional logic using fistElement
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void skippingFirstElementInMapWithStreamSkip(Map<String, String> stringMap) {
|
||||||
|
stringMap.entrySet().stream().skip(1).forEach(this::process);
|
||||||
|
}
|
||||||
|
|
||||||
|
void skippingFirstElementInListWithSubList(List<String> stringList) {
|
||||||
|
for (final String element : stringList.subList(1, stringList.size())) {
|
||||||
|
process(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void skippingFirstElementInListWithForLoopWithAdditionalCheck(List<String> stringList) {
|
||||||
|
for (int i = 0; i < stringList.size(); i++) {
|
||||||
|
if (i == 0) {
|
||||||
|
// do something else
|
||||||
|
} else {
|
||||||
|
process(stringList.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void skippingFirstElementInListWithWhileLoopWithCounter(List<String> stringList) {
|
||||||
|
int counter = 0;
|
||||||
|
while (counter < stringList.size()) {
|
||||||
|
if (counter != 0) {
|
||||||
|
process(stringList.get(counter));
|
||||||
|
}
|
||||||
|
++counter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void skippingFirstElementInListWithReduce(List<String> stringList) {
|
||||||
|
stringList.stream().reduce((skip, element) -> {
|
||||||
|
process(element);
|
||||||
|
return element;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void process(String string) {
|
||||||
|
System.out.println(string);
|
||||||
|
}
|
||||||
|
protected void process(Entry<String, String> mapEntry) {
|
||||||
|
System.out.println(mapEntry);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,122 @@
|
||||||
|
package com.baeldung.skippingfirstelement;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
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.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
@EnabledForJreRange(min = JRE.JAVA_9)
|
||||||
|
class SkipFirstElementExampleUnitTest {
|
||||||
|
|
||||||
|
private static TestableSkipFirstElement testableSkip = new TestableSkipFirstElement();
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
testableSkip.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> listProvider() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(
|
||||||
|
List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
|
||||||
|
List.of("Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> mapProvider() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(
|
||||||
|
Map.of(
|
||||||
|
"Monday", "The day when coffee is a life support system.",
|
||||||
|
"Tuesday", "The day you realize that Monday's optimism was a lie.",
|
||||||
|
"Wednesday", "Hump Day, or as it's known, the 'Is it Friday yet?' day.",
|
||||||
|
"Thursday", "The day that says, 'Hold my beer, Friday is coming!'",
|
||||||
|
"Friday", "The golden child of the weekdays. The superhero of the workweek.",
|
||||||
|
"Saturday", "The day of rest? More like the day of 'What can I binge-watch next?'",
|
||||||
|
"Sunday", "The day before you have to adult again."
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("listProvider")
|
||||||
|
void skippingFirstElementInListWithForLoop(List<String> input, List<String> expected) {
|
||||||
|
testableSkip.skippingFirstElementInListWithForLoop(input);
|
||||||
|
List<?> actual = testableSkip.getResult();
|
||||||
|
assertEquals(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("listProvider")
|
||||||
|
void skippingFirstElementInListWithWhileLoop(List<String> input, List<String> expected) {
|
||||||
|
testableSkip.skippingFirstElementInListWithWhileLoop(input);
|
||||||
|
List<?> actual = testableSkip.getResult();
|
||||||
|
assertEquals(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("listProvider")
|
||||||
|
void skippingFirstElementInSetWithWhileLoop(List<String> input) {
|
||||||
|
testableSkip.skippingFirstElementInSetWithWhileLoop(new HashSet<>(input));
|
||||||
|
Set<?> actual = new HashSet<>(testableSkip.getResult());
|
||||||
|
assertEquals(actual.size(), input.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("listProvider")
|
||||||
|
void skippingFirstElementInListWithWhileLoopStoringFirstElement(List<String> input, List<String> expected) {
|
||||||
|
testableSkip.skippingFirstElementInListWithWhileLoopStoringFirstElement(input);
|
||||||
|
List<?> actual = testableSkip.getResult();
|
||||||
|
assertEquals(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("mapProvider")
|
||||||
|
void skippingFirstElementInMapWithStreamSkip(Map<String, String> input) {
|
||||||
|
testableSkip.skippingFirstElementInMapWithStreamSkip(input);
|
||||||
|
List<?> actual = testableSkip.getResult();
|
||||||
|
assertEquals(actual.size(), input.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("listProvider")
|
||||||
|
void skippingFirstElementInListWithSubList(List<String> input, List<String> expected) {
|
||||||
|
testableSkip.skippingFirstElementInListWithSubList(input);
|
||||||
|
List<?> actual = testableSkip.getResult();
|
||||||
|
assertEquals(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("listProvider")
|
||||||
|
void skippingFirstElementInListWithForLoopWithAdditionalCheck(List<String> input, List<String> expected) {
|
||||||
|
testableSkip.skippingFirstElementInListWithForLoopWithAdditionalCheck(input);
|
||||||
|
List<?> actual = testableSkip.getResult();
|
||||||
|
assertEquals(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("listProvider")
|
||||||
|
void skippingFirstElementInListWithWhileLoopWithCounter(List<String> input, List<String> expected) {
|
||||||
|
testableSkip.skippingFirstElementInListWithWhileLoopWithCounter(input);
|
||||||
|
List<?> actual = testableSkip.getResult();
|
||||||
|
assertEquals(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("listProvider")
|
||||||
|
void skippingFirstElementInListWithReduce(List<String> input, List<String> expected) {
|
||||||
|
testableSkip.skippingFirstElementInListWithReduce(input);
|
||||||
|
List<?> actual = testableSkip.getResult();
|
||||||
|
assertEquals(actual, expected);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.skippingfirstelement;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface TestableSkip {
|
||||||
|
|
||||||
|
void reset();
|
||||||
|
|
||||||
|
List<?> getResult();
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.baeldung.skippingfirstelement;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
public class TestableSkipFirstElement extends SkipFirstElementExample implements TestableSkip {
|
||||||
|
|
||||||
|
|
||||||
|
private List<String> processedList = new ArrayList<>();
|
||||||
|
private List<Entry<String, String>> processedEntryList = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void process(String string) {
|
||||||
|
processedList.add(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void process(Entry<String, String> stringEntry) {
|
||||||
|
processedEntryList.add(stringEntry);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
processedList.clear();
|
||||||
|
processedEntryList.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<?> getResult() {
|
||||||
|
if (!processedList.isEmpty())
|
||||||
|
return processedList;
|
||||||
|
return processedEntryList;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
## Core Java Collections ArrayList
|
||||||
|
|
||||||
|
This module contains articles about the Java ArrayList collection
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Create an ArrayList with Multiple Object Types](https://www.baeldung.com/java-arraylist-multiple-object-types)
|
|
@ -0,0 +1,32 @@
|
||||||
|
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>core-java-collections-array-list-2</artifactId>
|
||||||
|
<name>core-java-collections-array-list-2</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>${maven-compiler-plugin.source}</source>
|
||||||
|
<target>${maven-compiler-plugin.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven-compiler-plugin.source>17</maven-compiler-plugin.source>
|
||||||
|
<maven-compiler-plugin.target>17</maven-compiler-plugin.target>
|
||||||
|
</properties>
|
||||||
|
</project>
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.baeldung.list.multipleobjecttypes;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
public class AlternativeMultipeTypeList {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// List of Parent Class
|
||||||
|
ArrayList<Number> myList = new ArrayList<>();
|
||||||
|
myList.add(1.2);
|
||||||
|
myList.add(2);
|
||||||
|
myList.add(-3.5);
|
||||||
|
|
||||||
|
// List of Interface type
|
||||||
|
ArrayList<Map> diffMapList = new ArrayList<>();
|
||||||
|
diffMapList.add(new HashMap<>());
|
||||||
|
diffMapList.add(new TreeMap<>());
|
||||||
|
diffMapList.add(new LinkedHashMap<>());
|
||||||
|
|
||||||
|
// List of Custom Object
|
||||||
|
ArrayList<CustomObject> objList = new ArrayList<>();
|
||||||
|
objList.add(new CustomObject("String"));
|
||||||
|
objList.add(new CustomObject(2));
|
||||||
|
|
||||||
|
// List via Functional Interface
|
||||||
|
List<Object> dataList = new ArrayList<>();
|
||||||
|
|
||||||
|
Predicate<Object> myPredicate = inputData -> (inputData instanceof String || inputData instanceof Integer);
|
||||||
|
|
||||||
|
UserFunctionalInterface myInterface = (listObj, data) -> {
|
||||||
|
if (myPredicate.test(data))
|
||||||
|
listObj.add(data);
|
||||||
|
else
|
||||||
|
System.out.println("Skipping input as data not allowed for class: " + data.getClass()
|
||||||
|
.getSimpleName());
|
||||||
|
return listObj;
|
||||||
|
};
|
||||||
|
|
||||||
|
myInterface.addToList(dataList, Integer.valueOf(2));
|
||||||
|
myInterface.addToList(dataList, Double.valueOf(3.33));
|
||||||
|
myInterface.addToList(dataList, "String Value");
|
||||||
|
myInterface.printList(dataList);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.list.multipleobjecttypes;
|
||||||
|
|
||||||
|
public class CustomObject {
|
||||||
|
String classData;
|
||||||
|
Integer intData;
|
||||||
|
|
||||||
|
CustomObject(String classData) {
|
||||||
|
this.classData = classData;
|
||||||
|
}
|
||||||
|
|
||||||
|
CustomObject(Integer intData) {
|
||||||
|
this.intData = intData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClassData() {
|
||||||
|
return this.classData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getIntData() {
|
||||||
|
return this.intData;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.baeldung.list.multipleobjecttypes;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class MultipleObjectTypeArrayList {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
ArrayList<Object> multiTypeList = new ArrayList<>();
|
||||||
|
|
||||||
|
multiTypeList.add(Integer.valueOf(10));
|
||||||
|
multiTypeList.add(Double.valueOf(11.5));
|
||||||
|
multiTypeList.add("String Data");
|
||||||
|
multiTypeList.add(Arrays.asList(1, 2, 3));
|
||||||
|
multiTypeList.add(new CustomObject("Class Data"));
|
||||||
|
multiTypeList.add(BigInteger.valueOf(123456789));
|
||||||
|
multiTypeList.add(LocalDate.of(2023, 9, 19));
|
||||||
|
|
||||||
|
for (Object dataObj : multiTypeList) {
|
||||||
|
if (dataObj instanceof Integer intData)
|
||||||
|
System.out.println("Integer Data : " + intData);
|
||||||
|
else if (dataObj instanceof Double doubleData)
|
||||||
|
System.out.println("Double Data : " + doubleData);
|
||||||
|
else if (dataObj instanceof String stringData)
|
||||||
|
System.out.println("String Data : " + stringData);
|
||||||
|
else if (dataObj instanceof List<?> intList)
|
||||||
|
System.out.println("List Data : " + intList);
|
||||||
|
else if (dataObj instanceof CustomObject customObj)
|
||||||
|
System.out.println("CustomObject Data : " + customObj.getClassData());
|
||||||
|
else if (dataObj instanceof BigInteger bigIntData)
|
||||||
|
System.out.println("BigInteger Data : " + bigIntData);
|
||||||
|
else if (dataObj instanceof LocalDate localDate)
|
||||||
|
System.out.println("LocalDate Data : " + localDate.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.list.multipleobjecttypes;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface UserFunctionalInterface {
|
||||||
|
|
||||||
|
List<Object> addToList(List<Object> list, Object data);
|
||||||
|
|
||||||
|
default void printList(List<Object> dataList) {
|
||||||
|
for (Object data : dataList) {
|
||||||
|
if (data instanceof String stringData)
|
||||||
|
System.out.println("String Data: " + stringData);
|
||||||
|
if (data instanceof Integer intData)
|
||||||
|
System.out.println("Integer Data: " + intData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,3 +3,4 @@
|
||||||
This module contains articles about conversions among Collection types in Java.
|
This module contains articles about conversions among Collection types in Java.
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
|
- [Converting HashMap Values to an ArrayList in Java](https://www.baeldung.com/java-hashmap-arraylist)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
## Relevant Articles
|
||||||
|
- [Check if a List Contains a String Element While Ignoring Case](https://www.baeldung.com/java-list-search-case-insensitive)
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?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-list-6</artifactId>
|
||||||
|
<name>core-java-collections-list-6</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
</project>
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.lists;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class StringListCaseInsensitiveContainsUnitTest {
|
||||||
|
private final static List<String> THE_LIST = List.of("Game of Thrones", "Forrest Gump", "American Beauty", "Pretty Woman", "Catch Me If You Can");
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingContains_thenGetExpectedResult() {
|
||||||
|
assertFalse(THE_LIST.contains("catch me if you can"));
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean ignoreCaseContainsForLoop(List<String> list, String value) {
|
||||||
|
for (String e : list) {
|
||||||
|
if (value.equalsIgnoreCase(e))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingIgnoreCaseContainsForLoop_thenGetExpectedResult() {
|
||||||
|
assertTrue(ignoreCaseContainsForLoop(THE_LIST, "CATCH me if you CAN"));
|
||||||
|
assertTrue(ignoreCaseContainsForLoop(THE_LIST, "game of thrones"));
|
||||||
|
assertFalse(ignoreCaseContainsForLoop(THE_LIST, "The Godfather"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingIgnoreCaseContainsStream_thenGetExpectedResult() {
|
||||||
|
assertTrue(THE_LIST.stream()
|
||||||
|
.anyMatch(e -> e.equalsIgnoreCase("CATCH me if you CAN")));
|
||||||
|
|
||||||
|
assertTrue(THE_LIST.stream()
|
||||||
|
.anyMatch("game of thrones"::equalsIgnoreCase));
|
||||||
|
|
||||||
|
assertFalse(THE_LIST.stream()
|
||||||
|
.anyMatch("The Godfather"::equalsIgnoreCase));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,76 +0,0 @@
|
||||||
package java.com.baeldung.objecttomap;
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
import wiremock.com.fasterxml.jackson.core.type.TypeReference;
|
|
||||||
import wiremock.com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import wiremock.com.google.common.reflect.TypeToken;
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ObjectToMapUnitTest {
|
|
||||||
Employee employee = new Employee("John", 3000.0);
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenJavaObject_whenUsingReflection_thenConvertToMap() throws IllegalAccessException {
|
|
||||||
Map<String, Object> map = convertUsingReflection(employee);
|
|
||||||
Assert.assertEquals(employee.getName(), map.get("name"));
|
|
||||||
Assert.assertEquals(employee.getSalary(), map.get("salary"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> convertUsingReflection(Object object) throws IllegalAccessException {
|
|
||||||
Map<String, Object> map = new HashMap<>();
|
|
||||||
Field[] fields = object.getClass().getDeclaredFields();
|
|
||||||
|
|
||||||
for (Field field : fields) {
|
|
||||||
field.setAccessible(true);
|
|
||||||
map.put(field.getName(), field.get(object));
|
|
||||||
}
|
|
||||||
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenJavaObject_whenUsingJackson_thenConvertToMap() {
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
Map<String, Object> map = objectMapper.convertValue(employee, new TypeReference<Map<String, Object>>() {});
|
|
||||||
Assert.assertEquals(employee.getName(), map.get("name"));
|
|
||||||
Assert.assertEquals(employee.getSalary(), map.get("salary"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenJavaObject_whenUsingGson_thenConvertToMap() {
|
|
||||||
Gson gson = new Gson();
|
|
||||||
String json = gson.toJson(employee);
|
|
||||||
Map<String, Object> map = gson.fromJson(json, new TypeToken<Map<String, Object>>() {}.getType());
|
|
||||||
Assert.assertEquals(employee.getName(), map.get("name"));
|
|
||||||
Assert.assertEquals(employee.getSalary(), map.get("salary"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Employee {
|
|
||||||
private String name;
|
|
||||||
private Double salary;
|
|
||||||
|
|
||||||
public Employee(String name, Double salary) {
|
|
||||||
this.name = name;
|
|
||||||
this.salary = salary;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Double getSalary() {
|
|
||||||
return salary;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSalary(Double age) {
|
|
||||||
this.salary = salary;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,128 @@
|
||||||
|
package com.baeldung.objecttomap;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.google.common.reflect.TypeToken;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import org.junit.Test;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.SortedMap;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ObjectToMapUnitTest {
|
||||||
|
Employee employee = new Employee("John", 3000.0);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJavaObject_whenUsingReflection_thenConvertToMap() throws IllegalAccessException {
|
||||||
|
Map<String, Object> map = convertUsingReflection(employee);
|
||||||
|
assertEquals(employee.getName(), map.get("name"));
|
||||||
|
assertEquals(employee.getSalary(), map.get("salary"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> convertUsingReflection(Object object) throws IllegalAccessException {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
Field[] fields = object.getClass().getDeclaredFields();
|
||||||
|
|
||||||
|
for (Field field : fields) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
map.put(field.getName(), field.get(object));
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJavaObject_whenUsingJackson_thenConvertToMap() {
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
Map<String, Object> map = objectMapper.convertValue(employee, new TypeReference<Map<String, Object>>() {});
|
||||||
|
assertEquals(employee.getName(), map.get("name"));
|
||||||
|
assertEquals(employee.getSalary(), map.get("salary"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJavaObject_whenUsingGson_thenConvertToMap() {
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String json = gson.toJson(employee);
|
||||||
|
Map<String, Object> map = gson.fromJson(json, new TypeToken<Map<String, Object>>() {}.getType());
|
||||||
|
assertEquals(employee.getName(), map.get("name"));
|
||||||
|
assertEquals(employee.getSalary(), map.get("salary"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void given_UnsortedMap_whenSortingByValueDescending_thenValuesAreInDescendingOrder() {
|
||||||
|
Map<String, Integer> unsortedMap = new HashMap<>();
|
||||||
|
unsortedMap.put("one", 1);
|
||||||
|
unsortedMap.put("three", 3);
|
||||||
|
unsortedMap.put("five", 5);
|
||||||
|
unsortedMap.put("two", 2);
|
||||||
|
unsortedMap.put("four", 4);
|
||||||
|
|
||||||
|
Map<String, Integer> sortedMap = sortMapByValueDescending(unsortedMap);
|
||||||
|
|
||||||
|
assertEquals(5, sortedMap.size());
|
||||||
|
final Iterator<Integer> iterator = sortedMap.values().iterator();
|
||||||
|
assertEquals(5, (int) iterator.next());
|
||||||
|
assertEquals(4, (int) iterator.next());
|
||||||
|
assertEquals(3, (int) iterator.next());
|
||||||
|
assertEquals(2, (int) iterator.next());
|
||||||
|
assertEquals(1, (int) iterator.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void given_UnsortedMap_whenUsingTreeMap_thenKeysAreInDescendingOrder() {
|
||||||
|
SortedMap<String, Integer> treeMap = new TreeMap<>(Comparator.reverseOrder());
|
||||||
|
treeMap.put("one", 1);
|
||||||
|
treeMap.put("three", 3);
|
||||||
|
treeMap.put("five", 5);
|
||||||
|
treeMap.put("two", 2);
|
||||||
|
treeMap.put("four", 4);
|
||||||
|
|
||||||
|
assertEquals(5, treeMap.size());
|
||||||
|
final Iterator<String> iterator = treeMap.keySet().iterator();
|
||||||
|
assertEquals("two", iterator.next());
|
||||||
|
assertEquals("three", iterator.next());
|
||||||
|
assertEquals("one", iterator.next());
|
||||||
|
assertEquals("four", iterator.next());
|
||||||
|
assertEquals("five", iterator.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Employee {
|
||||||
|
private String name;
|
||||||
|
private Double salary;
|
||||||
|
|
||||||
|
public Employee(String name, Double salary) {
|
||||||
|
this.name = name;
|
||||||
|
this.salary = salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getSalary() {
|
||||||
|
return salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSalary(Double age) {
|
||||||
|
this.salary = salary;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <K, V extends Comparable<? super V>> Map<K, V> sortMapByValueDescending(Map<K, V> map) {
|
||||||
|
return map.entrySet().stream()
|
||||||
|
.sorted(Map.Entry.<K, V>comparingByValue().reversed())
|
||||||
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
## Relevant Articles
|
|
@ -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>
|
|
@ -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 + "}";
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,10 +12,10 @@ public class EpochTimeToLocalDateTimeConverterUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void testConvertEpochTimeToLocalDateTime() {
|
public void testConvertEpochTimeToLocalDateTime() {
|
||||||
long epochTimeMillis = 1624962431000L; // Example epoch time in milliseconds
|
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);
|
Instant instant = Instant.ofEpochMilli(epochTimeMillis);
|
||||||
ZoneId zoneId = ZoneId.systemDefault();
|
ZoneId zoneId = ZoneId.of("UTC");
|
||||||
LocalDateTime actualDateTime = instant.atZone(zoneId).toLocalDateTime();
|
LocalDateTime actualDateTime = instant.atZone(zoneId).toLocalDateTime();
|
||||||
|
|
||||||
assertEquals(expectedDateTime, actualDateTime);
|
assertEquals(expectedDateTime, actualDateTime);
|
||||||
|
|
|
@ -3,4 +3,4 @@
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
|
|
||||||
- [Introduction to Javadoc](http://www.baeldung.com/javadoc)
|
- [Introduction to Javadoc](http://www.baeldung.com/javadoc)
|
||||||
|
- [Code Snippets in Java API Documentation](https://www.baeldung.com/java-doc-code-snippets)
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.snippettag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* External code snippet showing the loop process in binary search method.
|
||||||
|
* {@snippet class="BinarySearch" region="binary"}
|
||||||
|
*
|
||||||
|
* Time Zone
|
||||||
|
* {@snippet file="application.properties" region="zone"}
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GreetingsExternalSnippet {
|
||||||
|
public void helloBinarySearch() {
|
||||||
|
System.out.println("Hi, it's great knowing that binary search uses a loop under the hood");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.snippettag;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The code below shows a full highlighted line
|
||||||
|
* {@snippet :
|
||||||
|
* public void helloBaeldung() {
|
||||||
|
* System.out.println("Hello From Team Baeldung"); // @highlight
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* highlighting a substring
|
||||||
|
* {@snippet :
|
||||||
|
* public void helloBaeldung() {
|
||||||
|
* System.out.println("Hello From Team Baeldung"); // @highlight substring="println"
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* highlighting texts on multiple lines
|
||||||
|
* {@snippet :
|
||||||
|
* public void helloBaeldung() {
|
||||||
|
* System.out.println("Hello From Team Baeldung"); // @highlight region substring="println"
|
||||||
|
* String country = "USA";
|
||||||
|
* System.out.println("Hello From Team " + country); // @end
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GreetingsHighlightTag {
|
||||||
|
public void helloBaeldung() {
|
||||||
|
System.out.println("Hello From Team Baeldung");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.snippettag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The code below shows the content of {@code helloBaeldung()} method
|
||||||
|
* {@snippet :
|
||||||
|
* public void helloBaeldung() {
|
||||||
|
* System.out.println("Hello From Team Baeldung");
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GreetingsInlineSnippet {
|
||||||
|
public void helloBaeldung() {
|
||||||
|
System.out.println("Hello From Team Baeldung");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.snippettag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Using the replace tag
|
||||||
|
* {@snippet :
|
||||||
|
* public void helloBaeldung() {
|
||||||
|
* System.out.println("Hello From Team Baeldung"); // @replace regex='".*"' replacement="..."
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* Using the link tag
|
||||||
|
* {@snippet :
|
||||||
|
* public void helloBaeldung() {
|
||||||
|
* System.out.println("Hello From Team Baeldung"); // @link substring="System.out" target="System#out"
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GreetingsReplaceAndLinkTag {
|
||||||
|
public void helloBaeldung() {
|
||||||
|
System.out.println("Hello From Team Baeldung");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
|
||||||
|
public class BinarySearch {
|
||||||
|
|
||||||
|
public int search(int[] list, int item) {
|
||||||
|
int index = Integer.MAX_VALUE;
|
||||||
|
int low = 0;
|
||||||
|
int high = list.length - 1;
|
||||||
|
// @start region="binary"
|
||||||
|
while (low <= high) {
|
||||||
|
int mid = high - low;
|
||||||
|
int guess = list[mid];
|
||||||
|
if (guess == item) {
|
||||||
|
index = mid;
|
||||||
|
break;
|
||||||
|
} else if (guess > item) {
|
||||||
|
low = mid - 1;
|
||||||
|
} else {
|
||||||
|
low = mid + 1;
|
||||||
|
}
|
||||||
|
low++;
|
||||||
|
}
|
||||||
|
// @end region="binary"
|
||||||
|
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
# @start region="zone"
|
||||||
|
local.timezone = GMT+1
|
||||||
|
local.zip = 94123
|
||||||
|
# @end region="zone"
|
|
@ -0,0 +1,2 @@
|
||||||
|
test-link*
|
||||||
|
0.*
|
|
@ -0,0 +1,8 @@
|
||||||
|
## Core Java IO
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
<?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-io-5</artifactId>
|
||||||
|
<name>core-java-io-5</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Mime Type Resolution Libraries -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.tika</groupId>
|
||||||
|
<artifactId>tika-core</artifactId>
|
||||||
|
<version>${tika.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.sf.jmimemagic</groupId>
|
||||||
|
<artifactId>jmimemagic</artifactId>
|
||||||
|
<version>${jmime-magic.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jodd</groupId>
|
||||||
|
<artifactId>jodd-util</artifactId>
|
||||||
|
<version>${jodd-util.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.j256.simplemagic</groupId>
|
||||||
|
<artifactId>simplemagic</artifactId>
|
||||||
|
<version>${simplemagic.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-io-5</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>11</source>
|
||||||
|
<target>11</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- Mime Type Libraries -->
|
||||||
|
<tika.version>2.8.0</tika.version>
|
||||||
|
<jmime-magic.version>0.1.5</jmime-magic.version>
|
||||||
|
<jodd-util.version>6.2.1</jodd-util.version>
|
||||||
|
<simplemagic.version>1.17</simplemagic.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
|
@ -11,10 +11,8 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.tika.mime.MimeTypeException;
|
import org.apache.tika.mime.MimeTypeException;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.j256.simplemagic.ContentType;
|
import com.j256.simplemagic.ContentType;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ExtensionFromMimeTypeUnitTest {
|
public class ExtensionFromMimeTypeUnitTest {
|
||||||
private static final String IMAGE_JPEG_MIME_TYPE = "image/jpeg";
|
private static final String IMAGE_JPEG_MIME_TYPE = "image/jpeg";
|
||||||
|
@ -37,14 +35,14 @@ public class ExtensionFromMimeTypeUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingMimetypesFileTypeMap_thenGetFileExtension() {
|
public void whenUsingSimpleMagic_thenGetFileExtension() {
|
||||||
List<String> expectedExtensions = Arrays.asList("jpeg", "jpg", "jpe");
|
List<String> expectedExtensions = Arrays.asList("jpeg", "jpg", "jpe");
|
||||||
String[] detectedExtensions = ContentType.fromMimeType(IMAGE_JPEG_MIME_TYPE).getFileExtensions();
|
String[] detectedExtensions = ContentType.fromMimeType(IMAGE_JPEG_MIME_TYPE).getFileExtensions();
|
||||||
assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions);
|
assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingCustomLogic_thenGetFileExtension() {
|
public void whenUsingCustomMap_thenGetFileExtension() {
|
||||||
Map<String, Set<String>> mimeExtensionsMap = new HashMap<>();
|
Map<String, Set<String>> mimeExtensionsMap = new HashMap<>();
|
||||||
List<String> expectedExtensions = Arrays.asList(".jpg", ".jpe", ".jpeg");
|
List<String> expectedExtensions = Arrays.asList(".jpg", ".jpe", ".jpeg");
|
||||||
addMimeExtensions(mimeExtensionsMap, "image/jpeg", ".jpg");
|
addMimeExtensions(mimeExtensionsMap, "image/jpeg", ".jpg");
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
A,
|
||||||
|
B,
|
||||||
|
C,
|
||||||
|
D,
|
||||||
|
E,
|
||||||
|
F
|
|
@ -0,0 +1,4 @@
|
||||||
|
A,
B,
|
||||||
|
C,
|
||||||
|
D,
E,
|
||||||
|
F
|
|
@ -7,3 +7,4 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||||
- [Get the Desktop Path in Java](https://www.baeldung.com/java-desktop-path)
|
- [Get the Desktop Path in Java](https://www.baeldung.com/java-desktop-path)
|
||||||
- [Check if a File Is Empty in Java](https://www.baeldung.com/java-check-file-empty)
|
- [Check if a File Is Empty in Java](https://www.baeldung.com/java-check-file-empty)
|
||||||
- [Converting Relative to Absolute Paths in Java](https://www.baeldung.com/java-from-relative-to-absolute-paths)
|
- [Converting Relative to Absolute Paths in Java](https://www.baeldung.com/java-from-relative-to-absolute-paths)
|
||||||
|
- [Detect EOF in Java](https://www.baeldung.com/java-file-detect-end-of-file)
|
||||||
|
|
|
@ -43,17 +43,6 @@
|
||||||
<version>${angus-activation.version}</version>
|
<version>${angus-activation.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.jodd</groupId>
|
|
||||||
<artifactId>jodd-util</artifactId>
|
|
||||||
<version>${jodd-util.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.j256.simplemagic</groupId>
|
|
||||||
<artifactId>simplemagic</artifactId>
|
|
||||||
<version>${simplemagic.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -153,8 +142,6 @@
|
||||||
<fscontext.version>4.4.2</fscontext.version>
|
<fscontext.version>4.4.2</fscontext.version>
|
||||||
<jakarta-activation-api.version>2.1.2</jakarta-activation-api.version>
|
<jakarta-activation-api.version>2.1.2</jakarta-activation-api.version>
|
||||||
<angus-activation.version>2.0.1</angus-activation.version>
|
<angus-activation.version>2.0.1</angus-activation.version>
|
||||||
<jodd-util.version>6.2.1</jodd-util.version>
|
|
||||||
<simplemagic.version>1.17</simplemagic.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -18,7 +18,11 @@
|
||||||
<artifactId>mapstruct</artifactId>
|
<artifactId>mapstruct</artifactId>
|
||||||
<version>${mapstruct.version}</version>
|
<version>${mapstruct.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.baeldung.compareobjects;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Address {
|
||||||
|
private String streetAddress;
|
||||||
|
private String city;
|
||||||
|
private String postalCode;
|
||||||
|
|
||||||
|
public Address(String streetAddress, String city, String postalCode) {
|
||||||
|
this.streetAddress = streetAddress;
|
||||||
|
this.city = city;
|
||||||
|
this.postalCode = postalCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStreetAddress() {
|
||||||
|
return streetAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPostalCode() {
|
||||||
|
return postalCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Address{" + "streetAddress='" + streetAddress + '\'' + ", city='" + city + '\'' + ", postalCode='" + postalCode + '\'' + '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o)
|
||||||
|
return true;
|
||||||
|
if (o == null || getClass() != o.getClass())
|
||||||
|
return false;
|
||||||
|
Address address = (Address) o;
|
||||||
|
return Objects.equals(streetAddress, address.streetAddress) && Objects.equals(city, address.city) && Objects.equals(postalCode, address.postalCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(streetAddress, city, postalCode);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.baeldung.compareobjects;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.builder.DiffExclude;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private int age;
|
||||||
|
private List<PhoneNumber> phoneNumbers;
|
||||||
|
@DiffExclude
|
||||||
|
private Address address;
|
||||||
|
|
||||||
|
public Person(String firstName, String lastName, int age, List<PhoneNumber> phoneNumbers, Address address) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
this.age = age;
|
||||||
|
this.phoneNumbers = phoneNumbers;
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PhoneNumber> getPhoneNumbers() {
|
||||||
|
return phoneNumbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o)
|
||||||
|
return true;
|
||||||
|
if (o == null || getClass() != o.getClass())
|
||||||
|
return false;
|
||||||
|
Person person = (Person) o;
|
||||||
|
return age == person.age && Objects.equals(firstName, person.firstName) && Objects.equals(lastName, person.lastName) && Objects.equals(phoneNumbers, person.phoneNumbers) && Objects.equals(address, person.address);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(firstName, lastName, age, phoneNumbers, address);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.compareobjects;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.builder.DiffBuilder;
|
||||||
|
import org.apache.commons.lang3.builder.DiffResult;
|
||||||
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
|
|
||||||
|
public class PersonDiffBuilder {
|
||||||
|
public static DiffResult compare(Person first, Person second) {
|
||||||
|
DiffBuilder diffBuilder = new DiffBuilder(first, second, ToStringStyle.DEFAULT_STYLE).append("person", first.getFirstName(), second.getFirstName())
|
||||||
|
.append("lastName", first.getLastName(), second.getLastName())
|
||||||
|
.append("streetAddress", first.getAddress()
|
||||||
|
.getStreetAddress(), second.getAddress()
|
||||||
|
.getStreetAddress())
|
||||||
|
.append("city", first.getAddress()
|
||||||
|
.getCity(), second.getAddress()
|
||||||
|
.getCity())
|
||||||
|
.append("postalCode", first.getAddress()
|
||||||
|
.getPostalCode(), second.getAddress()
|
||||||
|
.getPostalCode())
|
||||||
|
.append("age", first.getAge(), second.getAge());
|
||||||
|
|
||||||
|
for (int i = 0; i < first.getPhoneNumbers()
|
||||||
|
.size(); i++) {
|
||||||
|
diffBuilder.append("phoneNumbers[" + i + "].number", first.getPhoneNumbers()
|
||||||
|
.get(i)
|
||||||
|
.getNumber(), second.getPhoneNumbers()
|
||||||
|
.get(i)
|
||||||
|
.getNumber());
|
||||||
|
}
|
||||||
|
return diffBuilder.build();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.compareobjects;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.builder.DiffResult;
|
||||||
|
import org.apache.commons.lang3.builder.ReflectionDiffBuilder;
|
||||||
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
|
|
||||||
|
public class PersonReflectionDiffBuilder {
|
||||||
|
public static DiffResult compare(Person first, Person second) {
|
||||||
|
return new ReflectionDiffBuilder<>(first, second, ToStringStyle.SHORT_PREFIX_STYLE).build();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.compareobjects;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.builder.DiffExclude;
|
||||||
|
|
||||||
|
public class PhoneNumber {
|
||||||
|
private String type;
|
||||||
|
private String number;
|
||||||
|
|
||||||
|
public PhoneNumber(String type, String number) {
|
||||||
|
this.type = type;
|
||||||
|
this.number = number;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNumber() {
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "PhoneNumber{" + "type='" + type + '\'' + ", number='" + number + '\'' + '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o)
|
||||||
|
return true;
|
||||||
|
if (o == null || getClass() != o.getClass())
|
||||||
|
return false;
|
||||||
|
PhoneNumber that = (PhoneNumber) o;
|
||||||
|
return Objects.equals(number, that.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(number);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.baeldung.compareobjects;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.builder.Diff;
|
||||||
|
import org.apache.commons.lang3.builder.DiffResult;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class PersonDiffBuilderUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenTwoPeopleDifferent_whenComparingWithDiffBuilder_thenDifferencesFound() {
|
||||||
|
List<PhoneNumber> phoneNumbers1 = new ArrayList<>();
|
||||||
|
phoneNumbers1.add(new PhoneNumber("home", "123-456-7890"));
|
||||||
|
phoneNumbers1.add(new PhoneNumber("work", "987-654-3210"));
|
||||||
|
|
||||||
|
List<PhoneNumber> phoneNumbers2 = new ArrayList<>();
|
||||||
|
phoneNumbers2.add(new PhoneNumber("mobile1", "123-456-7890"));
|
||||||
|
phoneNumbers2.add(new PhoneNumber("mobile2", "987-654-3210"));
|
||||||
|
|
||||||
|
Address address1 = new Address("123 Main St", "London", "12345");
|
||||||
|
Address address2 = new Address("123 Main St", "Paris", "54321");
|
||||||
|
|
||||||
|
Person person1 = new Person("John", "Doe", 30, phoneNumbers1, address1);
|
||||||
|
Person person2 = new Person("Jane", "Smith", 28, phoneNumbers2, address2);
|
||||||
|
|
||||||
|
DiffResult<Person> diff = PersonDiffBuilder.compare(person1, person2);
|
||||||
|
for (Diff<?> d : diff.getDiffs()) {
|
||||||
|
System.out.println(d.getFieldName() + ": " + d.getLeft() + " != " + d.getRight());
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(diff.getDiffs()
|
||||||
|
.isEmpty());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.baeldung.compareobjects;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.builder.Diff;
|
||||||
|
import org.apache.commons.lang3.builder.DiffResult;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class PersonReflectionDiffBuilderUnitTest {
|
||||||
|
@Test
|
||||||
|
void givenTwoPeopleDifferent_whenComparingWithReflectionDiffBuilder_thenDifferencesFound() {
|
||||||
|
List<PhoneNumber> phoneNumbers1 = new ArrayList<>();
|
||||||
|
phoneNumbers1.add(new PhoneNumber("home", "123-456-7890"));
|
||||||
|
phoneNumbers1.add(new PhoneNumber("work", "987-654-3210"));
|
||||||
|
|
||||||
|
List<PhoneNumber> phoneNumbers2 = new ArrayList<>();
|
||||||
|
phoneNumbers2.add(new PhoneNumber("mobile1", "123-456-7890"));
|
||||||
|
phoneNumbers2.add(new PhoneNumber("mobile2", "987-654-3210"));
|
||||||
|
|
||||||
|
Address address1 = new Address("123 Main St", "London", "12345");
|
||||||
|
Address address2 = new Address("123 Main St", "Paris", "54321");
|
||||||
|
|
||||||
|
Person person1 = new Person("John", "Doe", 30, phoneNumbers1, address1);
|
||||||
|
Person person2 = new Person("Jane", "Smith", 28, phoneNumbers2, address2);
|
||||||
|
|
||||||
|
DiffResult<Person> diff = PersonReflectionDiffBuilder.compare(person1, person2);
|
||||||
|
for (Diff<?> d : diff.getDiffs()) {
|
||||||
|
System.out.println(d.getFieldName() + ": " + d.getLeft() + " != " + d.getRight());
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(diff.getDiffs()
|
||||||
|
.isEmpty());
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue