commit
dfa2c562f0
|
@ -1,4 +1,5 @@
|
|||
*/bin/*
|
||||
bin/
|
||||
|
||||
*.class
|
||||
|
||||
|
@ -21,6 +22,9 @@
|
|||
*.iws
|
||||
out/
|
||||
|
||||
# VSCode
|
||||
.vscode/
|
||||
|
||||
# Mac
|
||||
.DS_Store
|
||||
|
||||
|
@ -77,4 +81,5 @@ apache-avro/src/main/java/com/baeldung/avro/model/
|
|||
jta/transaction-logs/
|
||||
software-security/sql-injection-samples/derby.log
|
||||
spring-soap/src/main/java/com/baeldung/springsoap/gen/
|
||||
/report-*.json
|
||||
/report-*.json
|
||||
transaction.log
|
|
@ -42,7 +42,7 @@
|
|||
<dependency>
|
||||
<groupId>com.github.dpaukov</groupId>
|
||||
<artifactId>combinatoricslib3</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<version>${combinatoricslib3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -83,6 +83,7 @@
|
|||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
<guava.version>27.0.1-jre</guava.version>
|
||||
<combinatoricslib3.version>3.3.0</combinatoricslib3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -5,3 +5,5 @@
|
|||
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
|
||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
||||
- [Checking If a List Is Sorted in Java](https://www.baeldung.com/java-check-if-list-sorted)
|
||||
- [Checking if a Java Graph has a Cycle](https://www.baeldung.com/java-graph-has-a-cycle)
|
||||
- [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique)
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.10</version>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
@ -3,37 +3,28 @@
|
|||
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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.3.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>org.baeldung.examples.olingo2</groupId>
|
||||
<artifactId>olingo2-sample</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>olingo2-sample</name>
|
||||
<description>Sample Olingo 2 Project</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<olingo2.version>2.0.11</olingo2.version>
|
||||
</properties>
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jersey</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
|
@ -90,4 +81,8 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<olingo2.version>2.0.11</olingo2.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class Olingo2SampleApplicationTests {
|
||||
public class Olingo2SampleApplicationUnitTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
|
@ -7,6 +7,13 @@
|
|||
<version>0.0.1</version>
|
||||
<name>apache-pulsar</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.pulsar</groupId>
|
||||
|
|
2
apache-pulsar/src/main/java/com/baeldung/ConsumerTest.java → apache-pulsar/src/main/java/com/baeldung/ConsumerUnitTest.java
Executable file → Normal file
2
apache-pulsar/src/main/java/com/baeldung/ConsumerTest.java → apache-pulsar/src/main/java/com/baeldung/ConsumerUnitTest.java
Executable file → Normal file
|
@ -7,7 +7,7 @@ import org.apache.pulsar.client.api.Message;
|
|||
import org.apache.pulsar.client.api.PulsarClient;
|
||||
import org.apache.pulsar.client.api.SubscriptionType;
|
||||
|
||||
public class ConsumerTest {
|
||||
public class ConsumerUnitTest {
|
||||
|
||||
private static final String SERVICE_URL = "pulsar://localhost:6650";
|
||||
private static final String TOPIC_NAME = "test-topic";
|
2
apache-pulsar/src/main/java/com/baeldung/ProducerTest.java → apache-pulsar/src/main/java/com/baeldung/ProducerUnitTest.java
Executable file → Normal file
2
apache-pulsar/src/main/java/com/baeldung/ProducerTest.java → apache-pulsar/src/main/java/com/baeldung/ProducerUnitTest.java
Executable file → Normal file
|
@ -11,7 +11,7 @@ import org.apache.pulsar.client.api.PulsarClientException;
|
|||
import java.io.IOException;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class ProducerTest {
|
||||
public class ProducerUnitTest {
|
||||
|
||||
private static final String SERVICE_URL = "pulsar://localhost:6650";
|
||||
private static final String TOPIC_NAME = "test-topic";
|
|
@ -10,7 +10,7 @@ import org.apache.pulsar.client.api.SubscriptionType;
|
|||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class ExclusiveSubscriptionTest {
|
||||
public class ExclusiveSubscriptionUnitTest {
|
||||
private static final String SERVICE_URL = "pulsar://localhost:6650";
|
||||
private static final String TOPIC_NAME = "test-topic";
|
||||
private static final String SUBSCRIPTION_NAME = "test-subscription";
|
|
@ -11,7 +11,7 @@ import org.apache.pulsar.client.api.SubscriptionType;
|
|||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class FailoverSubscriptionTest {
|
||||
public class FailoverSubscriptionUnitTest {
|
||||
private static final String SERVICE_URL = "pulsar://localhost:6650";
|
||||
private static final String TOPIC_NAME = "failover-subscription-test-topic";
|
||||
private static final String SUBSCRIPTION_NAME = "test-subscription";
|
|
@ -49,6 +49,7 @@
|
|||
<version>${com.datastax.spark.spark-cassandra-connector-java.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -78,6 +79,7 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<org.apache.spark.spark-core.version>2.3.0</org.apache.spark.spark-core.version>
|
||||
<org.apache.spark.spark-sql.version>2.3.0</org.apache.spark.spark-sql.version>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
### Relevant Articles:
|
||||
- [Introduction to AutoValue](http://www.baeldung.com/introduction-to-autovalue)
|
||||
- [Introduction to AutoFactory](http://www.baeldung.com/autofactory)
|
||||
- [Google AutoService](https://www.baeldung.com/google-autoservice)
|
||||
|
|
|
@ -112,7 +112,6 @@
|
|||
</repositories>
|
||||
|
||||
<properties>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
<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>
|
||||
|
|
|
@ -65,7 +65,6 @@
|
|||
<properties>
|
||||
<main.class>com.baeldung.bootique.App</main.class>
|
||||
<bootique-bom.version>0.23</bootique-bom.version>
|
||||
|
||||
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -6,4 +6,5 @@
|
|||
- [Template Engines in Groovy](https://www.baeldung.com/groovy-template-engines)
|
||||
- [Groovy def Keyword](https://www.baeldung.com/groovy-def-keyword)
|
||||
- [Pattern Matching in Strings in Groovy](https://www.baeldung.com/groovy-pattern-matching)
|
||||
- [Working with XML in Groovy](https://www.baeldung.com/groovy-xml)
|
||||
- [Working with XML in Groovy](https://www.baeldung.com/groovy-xml)
|
||||
- [Integrating Groovy into Java Applications](https://www.baeldung.com/groovy-java-applications)
|
||||
|
|
|
@ -157,27 +157,24 @@
|
|||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>bintray</id>
|
||||
<name>Groovy Bintray</name>
|
||||
<url>https://dl.bintray.com/groovy/maven</url>
|
||||
<releases>
|
||||
<!-- avoid automatic updates -->
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<pluginRepository>
|
||||
<id>bintray</id>
|
||||
<name>Groovy Bintray</name>
|
||||
<url>https://dl.bintray.com/groovy/maven</url>
|
||||
<releases>
|
||||
<!-- avoid automatic updates -->
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
<java.version>1.8</java.version>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<logback.version>1.2.3</logback.version>
|
||||
<groovy.version>2.5.7</groovy.version>
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.concatenate
|
||||
|
||||
class Wonder {
|
||||
|
||||
String numOfWonder = 'seven'
|
||||
|
||||
String operator_plus() {
|
||||
return 'The ' + numOfWonder + ' wonders of the world'
|
||||
}
|
||||
|
||||
String operator_left() {
|
||||
return 'The ' << numOfWonder << ' wonders of ' << 'the world'
|
||||
}
|
||||
|
||||
String interpolation_one() {
|
||||
return "The $numOfWonder wonders of the world"
|
||||
|
||||
}
|
||||
|
||||
String interpolation_two() {
|
||||
return "The ${numOfWonder} wonders of the world"
|
||||
}
|
||||
|
||||
String multilineString() {
|
||||
return """
|
||||
There are $numOfWonder wonders of the world.
|
||||
Can you name them all?
|
||||
1. The Great Pyramid of Giza
|
||||
2. Hanging Gardens of Babylon
|
||||
3. Colossus of Rhode
|
||||
4. Lighthouse of Alexendra
|
||||
5. Temple of Artemis
|
||||
6. Status of Zeus at Olympia
|
||||
7. Mausoleum at Halicarnassus
|
||||
"""
|
||||
}
|
||||
|
||||
String method_concat() {
|
||||
return 'The '.concat(numOfWonder).concat(' wonders of the world')
|
||||
|
||||
}
|
||||
|
||||
String method_builder() {
|
||||
return new StringBuilder()
|
||||
.append('The ').append(numOfWonder).append(' wonders of the world')
|
||||
}
|
||||
|
||||
String method_buffer() {
|
||||
return new StringBuffer()
|
||||
.append('The ').append(numOfWonder).append(' wonders of the world')
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package com.baeldung.concatenate
|
||||
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.*
|
||||
|
||||
class WonderUnitTest {
|
||||
|
||||
static final String EXPECTED_SINGLE_LINE = "The seven wonders of the world"
|
||||
|
||||
Wonder wonder
|
||||
|
||||
@Before
|
||||
void before() {
|
||||
wonder = new Wonder()
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingOperatorPlus_thenConcatCorrectly() {
|
||||
assertEquals(EXPECTED_SINGLE_LINE, wonder.operator_plus())
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingOperatorLeft_thenConcatCorrectly() {
|
||||
assertEquals(EXPECTED_SINGLE_LINE, wonder.operator_left())
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingInterpolationOne_thenConcatCorrectly() {
|
||||
assertEquals(EXPECTED_SINGLE_LINE, wonder.interpolation_one())
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingInterpolationTwo_thenConcatCorrectly() {
|
||||
assertEquals(EXPECTED_SINGLE_LINE, wonder.interpolation_two())
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingMultiline_thenConcatCorrectly() {
|
||||
def expectedMultiline = """
|
||||
There are seven wonders of the world.
|
||||
Can you name them all?
|
||||
1. The Great Pyramid of Giza
|
||||
2. Hanging Gardens of Babylon
|
||||
3. Colossus of Rhode
|
||||
4. Lighthouse of Alexendra
|
||||
5. Temple of Artemis
|
||||
6. Status of Zeus at Olympia
|
||||
7. Mausoleum at Halicarnassus
|
||||
"""
|
||||
assertEquals(expectedMultiline, wonder.multilineString())
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingMethodConcat_thenConcatCorrectly() {
|
||||
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_concat())
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingMethodBuilder_thenConcatCorrectly() {
|
||||
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_builder())
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingMethodBuffer_thenConcatCorrectly() {
|
||||
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_buffer())
|
||||
}
|
||||
}
|
|
@ -3,3 +3,4 @@
|
|||
- [Multi-Module Maven Application with Java Modules](https://www.baeldung.com/maven-multi-module-project-java-jpms)
|
||||
- [Guide to Java FileChannel](https://www.baeldung.com/java-filechannel)
|
||||
- [Understanding the NumberFormatException in Java](https://www.baeldung.com/java-number-format-exception)
|
||||
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)
|
||||
|
|
|
@ -5,3 +5,4 @@
|
|||
- [Guide to Java 10](http://www.baeldung.com/java-10-overview)
|
||||
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
|
||||
- [Deep Dive Into the New Java JIT Compiler – Graal](https://www.baeldung.com/graal-java-jit-compiler)
|
||||
- [Copying Sets in Java](https://www.baeldung.com/java-copy-sets)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [Negate a Predicate Method Reference with Java 11](https://www.baeldung.com/java-negate-predicate-method-reference)
|
|
@ -0,0 +1,3 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [String API Updates in Java 12](https://www.baeldung.com/java12-string-api)
|
|
@ -6,3 +6,4 @@
|
|||
- [Anonymous Classes in Java](http://www.baeldung.com/)
|
||||
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
|
||||
- [Run JAR Application With Command Line Arguments](https://www.baeldung.com/java-run-jar-with-arguments)
|
||||
- [Java 8 Stream skip() vs limit()](https://www.baeldung.com/java-stream-skip-vs-limit)
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
<name>core-java-8-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
|
@ -17,19 +16,18 @@
|
|||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<icu.version>64.2</icu.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ibm.icu</groupId>
|
||||
<artifactId>icu4j</artifactId>
|
||||
<version>${icu.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -44,6 +42,13 @@
|
|||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<icu.version>64.2</icu.version>
|
||||
<assertj.version>3.12.2</assertj.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,164 @@
|
|||
package com.baeldung.bifunction;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BiFunctionalInterfacesUnitTest {
|
||||
@Test
|
||||
public void givenStreamValues_whenMappedToNewValues() {
|
||||
List<String> mapped = Stream.of("hello", "world")
|
||||
.map(word -> word + "!")
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(mapped).containsExactly("hello!", "world!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStreamValues_whenReducedWithPrefixingOperation() {
|
||||
String result = Stream.of("hello", "world")
|
||||
.reduce("", (a, b) -> b + "-" + a);
|
||||
|
||||
assertThat(result).isEqualTo("world-hello-");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStreamValues_whenReducedWithPrefixingLambda_thenHasNoTrailingDash() {
|
||||
String result = Stream.of("hello", "world")
|
||||
.reduce("", (a, b) -> combineWithoutTrailingDash(a, b));
|
||||
|
||||
assertThat(result).isEqualTo("world-hello");
|
||||
}
|
||||
|
||||
private String combineWithoutTrailingDash(String a, String b) {
|
||||
if (a.isEmpty()) {
|
||||
return b;
|
||||
}
|
||||
return b + "-" + a;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStreamValues_whenReducedWithPrefixingMethodReference_thenHasNoTrailingDash() {
|
||||
String result = Stream.of("hello", "world")
|
||||
.reduce("", this::combineWithoutTrailingDash);
|
||||
|
||||
assertThat(result).isEqualTo("world-hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenCombined() {
|
||||
List<String> list1 = Arrays.asList("a", "b", "c");
|
||||
List<Integer> list2 = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> result = new ArrayList<>();
|
||||
for (int i=0; i < list1.size(); i++) {
|
||||
result.add(list1.get(i) + list2.get(i));
|
||||
}
|
||||
|
||||
assertThat(result).containsExactly("a1", "b2", "c3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenCombinedWithGeneralPurposeCombiner() {
|
||||
List<String> list1 = Arrays.asList("a", "b", "c");
|
||||
List<Integer> list2 = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> result = listCombiner(list1, list2, (a, b) -> a + b);
|
||||
|
||||
assertThat(result).containsExactly("a1", "b2", "c3");
|
||||
}
|
||||
|
||||
private static <T, U, R> List<R> listCombiner(List<T> list1,
|
||||
List<U> list2,
|
||||
BiFunction<T, U, R> combiner) {
|
||||
List<R> result = new ArrayList<>();
|
||||
for (int i = 0; i < list1.size(); i++) {
|
||||
result.add(combiner.apply(list1.get(i), list2.get(i)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenComparedWithCombiningFunction() {
|
||||
List<Double> list1 = Arrays.asList(1.0d, 2.1d, 3.3d);
|
||||
List<Float> list2 = Arrays.asList(0.1f, 0.2f, 4f);
|
||||
|
||||
// algorithm to determine if the value in list1 > value in list 2
|
||||
List<Boolean> result = listCombiner(list1, list2, (a, b) -> a > b);
|
||||
|
||||
assertThat(result).containsExactly(true, true, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenComparedWithCombiningFunctionByMethodReference() {
|
||||
List<Double> list1 = Arrays.asList(1.0d, 2.1d, 3.3d);
|
||||
List<Float> list2 = Arrays.asList(0.1f, 0.2f, 4f);
|
||||
|
||||
// algorithm to determine if the value in list1 > value in list 2
|
||||
List<Boolean> result = listCombiner(list1, list2, this::firstIsGreaterThanSecond);
|
||||
|
||||
assertThat(result).containsExactly(true, true, false);
|
||||
}
|
||||
|
||||
private boolean firstIsGreaterThanSecond(Double a, Float b) {
|
||||
return a > b;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenComparedForEqualityByCombiningFunction() {
|
||||
List<Float> list1 = Arrays.asList(0.1f, 0.2f, 4f);
|
||||
List<Float> list2 = Arrays.asList(0.1f, 0.2f, 4f);
|
||||
|
||||
List<Boolean> result = listCombiner(list1, list2, (a, b) -> a.equals(b));
|
||||
|
||||
assertThat(result).containsExactly(true, true, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenComparedForEqualityByCombiningFunctionWithMethodReference() {
|
||||
List<Float> list1 = Arrays.asList(0.1f, 0.2f, 4f);
|
||||
List<Float> list2 = Arrays.asList(0.1f, 0.2f, 4f);
|
||||
|
||||
List<Boolean> result = listCombiner(list1, list2, Float::equals);
|
||||
|
||||
assertThat(result).containsExactly(true, true, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenComparedWithCombiningFunctionWithCompareTo() {
|
||||
List<Double> list1 = Arrays.asList(1.0d, 2.1d, 3.3d);
|
||||
List<Double> list2 = Arrays.asList(0.1d, 0.2d, 4d);
|
||||
|
||||
List<Integer> result = listCombiner(list1, list2, Double::compareTo);
|
||||
|
||||
assertThat(result).containsExactly(1, 1, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to to pass in a lambda or method reference and then
|
||||
* get access to the BiFunction it is meant to become
|
||||
*/
|
||||
private static <T, U, R> BiFunction<T, U, R> asBiFunction(BiFunction<T, U, R> function) {
|
||||
return function;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenComparedWithCombiningFunctionWithComposedBiFunction() {
|
||||
List<Double> list1 = Arrays.asList(1.0d, 2.1d, 3.3d);
|
||||
List<Double> list2 = Arrays.asList(0.1d, 0.2d, 4d);
|
||||
|
||||
List<Boolean> result = listCombiner(list1, list2,
|
||||
asBiFunction(Double::compareTo)
|
||||
.andThen(i -> i > 0));
|
||||
|
||||
assertThat(result).containsExactly(true, true, false);
|
||||
}
|
||||
}
|
|
@ -40,3 +40,4 @@
|
|||
- [Java 8 Predicate Chain](https://www.baeldung.com/java-predicate-chain)
|
||||
- [Method References in Java](https://www.baeldung.com/java-method-references)
|
||||
- [Creating a Custom Annotation in Java](https://www.baeldung.com/java-custom-annotation)
|
||||
- [The Difference Between Collection.stream().forEach() and Collection.forEach()](https://www.baeldung.com/java-collection-stream-foreach)
|
||||
|
|
|
@ -175,7 +175,6 @@
|
|||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
|
|
|
@ -259,4 +259,15 @@ public class OptionalUnitTest {
|
|||
LOG.debug("Getting default value...");
|
||||
return "Default Value";
|
||||
}
|
||||
|
||||
// Uncomment code when code base is compatiable with Java 11
|
||||
// @Test
|
||||
// public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected() {
|
||||
// Optional<String> opt = Optional.of("Baeldung");
|
||||
// assertFalse(opt.isEmpty());
|
||||
//
|
||||
// opt = Optional.ofNullable(null);
|
||||
// assertTrue(opt.isEmpty());
|
||||
// }
|
||||
|
||||
}
|
|
@ -389,7 +389,7 @@
|
|||
<properties>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
|
||||
<jmh-core.version>1.19</jmh-core.version>
|
||||
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class JaggedArray {
|
||||
|
||||
int[][] shortHandFormInitialization() {
|
||||
int[][] jaggedArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
return jaggedArr;
|
||||
}
|
||||
|
||||
int[][] declarationAndThenInitialization() {
|
||||
int[][] jaggedArr = new int[3][];
|
||||
jaggedArr[0] = new int[] { 1, 2 };
|
||||
jaggedArr[1] = new int[] { 3, 4, 5 };
|
||||
jaggedArr[2] = new int[] { 6, 7, 8, 9 };
|
||||
return jaggedArr;
|
||||
}
|
||||
|
||||
int[][] declarationAndThenInitializationUsingUserInputs() {
|
||||
int[][] jaggedArr = new int[3][];
|
||||
jaggedArr[0] = new int[2];
|
||||
jaggedArr[1] = new int[3];
|
||||
jaggedArr[2] = new int[4];
|
||||
initializeElements(jaggedArr);
|
||||
return jaggedArr;
|
||||
}
|
||||
|
||||
void initializeElements(int[][] jaggedArr) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
for (int outer = 0; outer < jaggedArr.length; outer++) {
|
||||
for (int inner = 0; inner < jaggedArr[outer].length; inner++) {
|
||||
jaggedArr[outer][inner] = sc.nextInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printElements(int[][] jaggedArr) {
|
||||
for (int index = 0; index < jaggedArr.length; index++) {
|
||||
System.out.println(Arrays.toString(jaggedArr[index]));
|
||||
}
|
||||
}
|
||||
|
||||
int[] getElementAtGivenIndex(int[][] jaggedArr, int index) {
|
||||
return jaggedArr[index];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class MultiDimensionalArray {
|
||||
|
||||
int[][] shortHandFormInitialization() {
|
||||
int[][] multiDimensionalArray = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
return multiDimensionalArray;
|
||||
}
|
||||
|
||||
int[][] declarationAndThenInitialization() {
|
||||
int[][] multiDimensionalArray = new int[3][];
|
||||
multiDimensionalArray[0] = new int[] { 1, 2 };
|
||||
multiDimensionalArray[1] = new int[] { 3, 4, 5 };
|
||||
multiDimensionalArray[2] = new int[] { 6, 7, 8, 9 };
|
||||
return multiDimensionalArray;
|
||||
}
|
||||
|
||||
int[][] declarationAndThenInitializationUsingUserInputs() {
|
||||
int[][] multiDimensionalArray = new int[3][];
|
||||
multiDimensionalArray[0] = new int[2];
|
||||
multiDimensionalArray[1] = new int[3];
|
||||
multiDimensionalArray[2] = new int[4];
|
||||
initializeElements(multiDimensionalArray);
|
||||
return multiDimensionalArray;
|
||||
}
|
||||
|
||||
void initializeElements(int[][] multiDimensionalArray) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
for (int outer = 0; outer < multiDimensionalArray.length; outer++) {
|
||||
for (int inner = 0; inner < multiDimensionalArray[outer].length; inner++) {
|
||||
multiDimensionalArray[outer][inner] = sc.nextInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initialize2DArray(int[][] multiDimensionalArray) {
|
||||
for (int[] array : multiDimensionalArray) {
|
||||
Arrays.fill(array, 7);
|
||||
}
|
||||
}
|
||||
|
||||
void printElements(int[][] multiDimensionalArray) {
|
||||
for (int index = 0; index < multiDimensionalArray.length; index++) {
|
||||
System.out.println(Arrays.toString(multiDimensionalArray[index]));
|
||||
}
|
||||
}
|
||||
|
||||
int[] getElementAtGivenIndex(int[][] multiDimensionalArray, int index) {
|
||||
return multiDimensionalArray[index];
|
||||
}
|
||||
|
||||
int[] findLengthOfElements(int[][] multiDimensionalArray) {
|
||||
int[] arrayOfLengths = new int[multiDimensionalArray.length];
|
||||
for (int i = 0; i < multiDimensionalArray.length; i++) {
|
||||
arrayOfLengths[i] = multiDimensionalArray[i].length;
|
||||
}
|
||||
return arrayOfLengths;
|
||||
}
|
||||
|
||||
Integer[] findLengthOfElements(Integer[][] multiDimensionalArray) {
|
||||
return Arrays.stream(multiDimensionalArray)
|
||||
.map(array -> array.length)
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
|
||||
int[][] copy2DArray(int[][] arrayOfArrays) {
|
||||
int[][] copied2DArray = new int[arrayOfArrays.length][];
|
||||
for (int i = 0; i < arrayOfArrays.length; i++) {
|
||||
int[] array = arrayOfArrays[i];
|
||||
copied2DArray[i] = Arrays.copyOf(array, array.length);
|
||||
}
|
||||
return copied2DArray;
|
||||
}
|
||||
|
||||
Integer[][] copy2DArray(Integer[][] arrayOfArrays) {
|
||||
return Arrays.stream(arrayOfArrays)
|
||||
.map(array -> Arrays.copyOf(array, array.length))
|
||||
.toArray(Integer[][]::new);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
public class RemoveElementFromAnArray {
|
||||
|
||||
public int[] removeAnElementWithAGivenIndex(int[] array, int index) {
|
||||
return ArrayUtils.remove(array, index);
|
||||
}
|
||||
|
||||
public int[] removeAllElementsWithGivenIndices(int[] array, int... indicies) {
|
||||
return ArrayUtils.removeAll(array, indicies);
|
||||
}
|
||||
|
||||
public int[] removeFirstOccurrenceOfGivenElement(int[] array, int element) {
|
||||
return ArrayUtils.removeElement(array, element);
|
||||
}
|
||||
|
||||
public int[] removeAllGivenElements(int[] array, int... elements) {
|
||||
return ArrayUtils.removeElements(array, elements);
|
||||
}
|
||||
|
||||
public int[] removeAllOccurrencesOfAGivenElement(int[] array, int element) {
|
||||
return ArrayUtils.removeAllOccurences(array, element);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import com.baeldung.arraycopy.model.Employee;
|
||||
|
||||
public class SortedArrayChecker {
|
||||
boolean isSorted(int[] array, int length) {
|
||||
if (array == null || length < 2)
|
||||
return true;
|
||||
|
||||
if (array[length - 2] > array[length - 1])
|
||||
return false;
|
||||
|
||||
return isSorted(array, length - 1);
|
||||
}
|
||||
|
||||
boolean isSorted(int[] array) {
|
||||
for (int i = 0; i < array.length - 1; i++) {
|
||||
if (array[i] > array[i + 1])
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isSorted(String[] array, int length) {
|
||||
if (array == null || length < 2)
|
||||
return true;
|
||||
|
||||
if (array[length - 2].compareTo(array[length - 1]) > 0)
|
||||
return false;
|
||||
|
||||
return isSorted(array, length - 1);
|
||||
}
|
||||
|
||||
boolean isSorted(String[] array) {
|
||||
for (int i = 0; i < array.length - 1; ++i) {
|
||||
if (array[i].compareTo(array[i + 1]) > 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isSortedByName(Employee[] array) {
|
||||
for (int i = 0; i < array.length - 1; ++i) {
|
||||
if (array[i].getName().compareTo(array[i + 1].getName()) > 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isSortedByAge(Employee[] array) {
|
||||
for (int i = 0; i < array.length - 1; ++i) {
|
||||
if (array[i].getAge() > (array[i + 1].getAge()))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isSortedByAge(Employee[] array, int length) {
|
||||
if (array == null || length < 2)
|
||||
return true;
|
||||
|
||||
if (array[length - 2].getAge() > array[length - 1].getAge())
|
||||
return false;
|
||||
|
||||
return isSortedByAge(array, length - 1);
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ public class Employee implements Serializable {
|
|||
private static final long serialVersionUID = -2454619097207585825L;
|
||||
private int id;
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Employee() {
|
||||
}
|
||||
|
@ -15,10 +16,24 @@ public class Employee implements Serializable {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
public Employee(int id, String name, int age) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JaggedArrayUnitTest {
|
||||
|
||||
private JaggedArray obj = new JaggedArray();
|
||||
|
||||
@Test
|
||||
public void whenInitializedUsingShortHandForm_thenCorrect() {
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.shortHandFormInitialization());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitializedWithDeclarationAndThenInitalization_thenCorrect() {
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.declarationAndThenInitialization());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitializedWithDeclarationAndThenInitalizationUsingUserInputs_thenCorrect() {
|
||||
InputStream is = new ByteArrayInputStream("1 2 3 4 5 6 7 8 9".getBytes());
|
||||
System.setIn(is);
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.declarationAndThenInitializationUsingUserInputs());
|
||||
System.setIn(System.in);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJaggedArrayAndAnIndex_thenReturnArrayAtGivenIndex() {
|
||||
int[][] jaggedArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(new int[] { 1, 2 }, obj.getElementAtGivenIndex(jaggedArr, 0));
|
||||
assertArrayEquals(new int[] { 3, 4, 5 }, obj.getElementAtGivenIndex(jaggedArr, 1));
|
||||
assertArrayEquals(new int[] { 6, 7, 8, 9 }, obj.getElementAtGivenIndex(jaggedArr, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJaggedArray_whenUsingArraysAPI_thenVerifyPrintedElements() {
|
||||
int[][] jaggedArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(outContent));
|
||||
obj.printElements(jaggedArr);
|
||||
assertEquals("[1, 2][3, 4, 5][6, 7, 8, 9]", outContent.toString().replace("\r", "").replace("\n", ""));
|
||||
System.setOut(System.out);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MultiDimensionalArrayUnitTest {
|
||||
|
||||
private MultiDimensionalArray obj = new MultiDimensionalArray();
|
||||
|
||||
@Test
|
||||
public void whenInitializedUsingShortHandForm_thenCorrect() {
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.shortHandFormInitialization());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitializedWithDeclarationAndThenInitalization_thenCorrect() {
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.declarationAndThenInitialization());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitializedWithDeclarationAndThenInitalizationUsingUserInputs_thenCorrect() {
|
||||
InputStream is = new ByteArrayInputStream("1 2 3 4 5 6 7 8 9".getBytes());
|
||||
System.setIn(is);
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.declarationAndThenInitializationUsingUserInputs());
|
||||
System.setIn(System.in);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArrayAndAnIndex_thenReturnArrayAtGivenIndex() {
|
||||
int[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(new int[] { 1, 2 }, obj.getElementAtGivenIndex(multiDimensionalArr, 0));
|
||||
assertArrayEquals(new int[] { 3, 4, 5 }, obj.getElementAtGivenIndex(multiDimensionalArr, 1));
|
||||
assertArrayEquals(new int[] { 6, 7, 8, 9 }, obj.getElementAtGivenIndex(multiDimensionalArr, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingArraysAPI_thenVerifyPrintedElements() {
|
||||
int[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(outContent));
|
||||
obj.printElements(multiDimensionalArr);
|
||||
assertEquals("[1, 2][3, 4, 5][6, 7, 8, 9]", outContent.toString().replace("\r", "").replace("\n", ""));
|
||||
System.setOut(System.out);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingArraysFill_thenVerifyInitialize2DArray() {
|
||||
int[][] multiDimensionalArr = new int[3][];
|
||||
multiDimensionalArr[0] = new int[2];
|
||||
multiDimensionalArr[1] = new int[3];
|
||||
multiDimensionalArr[2] = new int[4];
|
||||
obj.initialize2DArray(multiDimensionalArr);
|
||||
assertArrayEquals(new int[][] {{7,7}, {7,7,7}, {7,7,7,7}}, multiDimensionalArr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingIteration_thenVerifyFindLengthOfElements() {
|
||||
int[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(new int[]{2,3,4}, obj.findLengthOfElements(multiDimensionalArr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingArraysStream_thenVerifyFindLengthOfElements() {
|
||||
Integer[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(new Integer[]{2,3,4}, obj.findLengthOfElements(multiDimensionalArr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingArraysCopyOf_thenVerifyCopy2DArray() {
|
||||
int[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(multiDimensionalArr, obj.copy2DArray(multiDimensionalArr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingArraysStream_thenVerifyCopy2DArray() {
|
||||
Integer[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(multiDimensionalArr, obj.copy2DArray(multiDimensionalArr));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RemoveElementFromAnArrayUnitTest {
|
||||
|
||||
private final RemoveElementFromAnArray sut = new RemoveElementFromAnArray();
|
||||
private final int[] inputArray = new int[] { 40, 10, 20, 30, 40, 50 };
|
||||
|
||||
@Test
|
||||
void testRemoveAnElementWithAGivenIndex() {
|
||||
int index = 2;
|
||||
int[] modifiedArray = sut.removeAnElementWithAGivenIndex(inputArray, index);
|
||||
|
||||
assertFalse(ArrayUtils.contains(modifiedArray, inputArray[index]));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveAllElementsWithGivenIndices() {
|
||||
int first = 0;
|
||||
int last = inputArray.length - 1;
|
||||
int[] modifiedArray = sut.removeAllElementsWithGivenIndices(inputArray, first, last);
|
||||
|
||||
assertFalse(ArrayUtils.contains(modifiedArray, inputArray[first]) && ArrayUtils.contains(modifiedArray, inputArray[last]));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveElement_WhenArrayIsNull_ThrowsIndexOutOfBoundEx() {
|
||||
int index = 2;
|
||||
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> {
|
||||
sut.removeAnElementWithAGivenIndex(null, index);
|
||||
});
|
||||
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> {
|
||||
sut.removeAllElementsWithGivenIndices(null, index);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveFirstOccurrenceOfGivenElement() {
|
||||
int element = 40;
|
||||
int[] modifiedArray = sut.removeFirstOccurrenceOfGivenElement(inputArray, element);
|
||||
|
||||
int indexInInputArray = ArrayUtils.indexOf(inputArray, element);
|
||||
int indexInModifiedArray = ArrayUtils.indexOf(modifiedArray, element);
|
||||
assertFalse(indexInInputArray == indexInModifiedArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveAllGivenElements() {
|
||||
int duplicateElement = 40;
|
||||
int[] elements = new int[] { duplicateElement, 10, 50 };
|
||||
int[] modifiedArray = sut.removeAllGivenElements(inputArray, elements);
|
||||
|
||||
assertTrue(ArrayUtils.contains(modifiedArray, duplicateElement));
|
||||
assertFalse(ArrayUtils.contains(modifiedArray, elements[1]));
|
||||
assertFalse(ArrayUtils.contains(modifiedArray, elements[2]));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveAllOccurrencesOfAGivenElement() {
|
||||
int element = 40;
|
||||
int[] modifiedArray = sut.removeAllOccurrencesOfAGivenElement(inputArray, element);
|
||||
|
||||
assertFalse(ArrayUtils.contains(modifiedArray, element));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveElement_WhenArrayIsNull_ReturnsNull() {
|
||||
int element = 20;
|
||||
|
||||
assertEquals(null, sut.removeFirstOccurrenceOfGivenElement(null, element));
|
||||
assertEquals(null, sut.removeAllGivenElements(null, element));
|
||||
assertEquals(null, sut.removeAllOccurrencesOfAGivenElement(null, element));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import com.baeldung.arraycopy.model.Employee;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class SortedArrayCheckerUnitTest {
|
||||
|
||||
private static final int[] INTEGER_SORTED = {1, 3, 5, 7, 9};
|
||||
private static final int[] INTEGER_NOT_SORTED = {1, 3, 11, 7};
|
||||
|
||||
private static final String[] STRING_SORTED = {"abc", "cde", "fgh"};
|
||||
private static final String[] STRING_NOT_SORTED = {"abc", "fgh", "cde", "ijk"};
|
||||
|
||||
private final Employee[] EMPLOYEES_SORTED_BY_NAME = {
|
||||
new Employee(1, "Carlos", 26),
|
||||
new Employee(2, "Daniel", 31),
|
||||
new Employee(3, "Marta", 27)};
|
||||
|
||||
private final Employee[] EMPLOYEES_NOT_SORTED_BY_NAME = {
|
||||
new Employee(1, "Daniel", 31),
|
||||
new Employee(2, "Carlos", 26),
|
||||
new Employee(3, "Marta", 27)};
|
||||
|
||||
private final Employee[] EMPLOYEES_SORTED_BY_AGE = {
|
||||
new Employee(1, "Carlos", 26),
|
||||
new Employee(2, "Marta", 27),
|
||||
new Employee(3, "Daniel", 31)};
|
||||
|
||||
private final Employee[] EMPLOYEES_NOT_SORTED_BY_AGE = {
|
||||
new Employee(1, "Marta", 27),
|
||||
new Employee(2, "Carlos", 26),
|
||||
new Employee(3, "Daniel", 31)};
|
||||
|
||||
private SortedArrayChecker sortedArrayChecker;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
sortedArrayChecker = new SortedArrayChecker();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_thenReturnIfItIsSortedOrNot() {
|
||||
assertThat(sortedArrayChecker.isSorted(INTEGER_SORTED)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(INTEGER_NOT_SORTED)).isEqualTo(false);
|
||||
|
||||
assertThat(sortedArrayChecker.isSorted(INTEGER_SORTED, INTEGER_SORTED.length)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(INTEGER_NOT_SORTED, INTEGER_NOT_SORTED.length)).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringArray_thenReturnIfItIsSortedOrNot() {
|
||||
assertThat(sortedArrayChecker.isSorted(STRING_SORTED)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(STRING_NOT_SORTED)).isEqualTo(false);
|
||||
|
||||
assertThat(sortedArrayChecker.isSorted(STRING_SORTED, STRING_SORTED.length)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(STRING_NOT_SORTED, STRING_NOT_SORTED.length)).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_thenReturnIfItIsSortedOrNot() {
|
||||
assertThat(sortedArrayChecker.isSortedByName(EMPLOYEES_SORTED_BY_NAME)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSortedByName(EMPLOYEES_NOT_SORTED_BY_NAME)).isEqualTo(false);
|
||||
|
||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_SORTED_BY_AGE)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_NOT_SORTED_BY_AGE)).isEqualTo(false);
|
||||
|
||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_SORTED_BY_AGE, EMPLOYEES_SORTED_BY_AGE.length)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_NOT_SORTED_BY_AGE, EMPLOYEES_NOT_SORTED_BY_AGE.length)).isEqualTo(false);
|
||||
}
|
||||
}
|
|
@ -10,3 +10,4 @@
|
|||
- [Initializing HashSet at the Time of Construction](http://www.baeldung.com/java-initialize-hashset)
|
||||
- [Guide to EnumSet](https://www.baeldung.com/java-enumset)
|
||||
- [Set Operations in Java](https://www.baeldung.com/java-set-operations)
|
||||
- [Copying Sets in Java](https://www.baeldung.com/java-copy-sets)
|
||||
|
|
|
@ -14,30 +14,31 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>${commons-lang.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-collections4.version>4.3</commons-collections4.version>
|
||||
<guava.version>27.1-jre</guava.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -72,7 +72,6 @@
|
|||
<properties>
|
||||
<!-- util -->
|
||||
<guava.version>21.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
|
|
|
@ -17,3 +17,4 @@
|
|||
- [Runnable vs. Callable in Java](http://www.baeldung.com/java-runnable-callable)
|
||||
- [What is Thread-Safety and How to Achieve it?](https://www.baeldung.com/java-thread-safety)
|
||||
- [How to Start a Thread in Java](https://www.baeldung.com/java-start-thread)
|
||||
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
|
||||
|
|
|
@ -45,8 +45,6 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
|
|
|
@ -62,7 +62,6 @@
|
|||
<properties>
|
||||
<!-- util -->
|
||||
<guava.version>21.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)
|
|
@ -14,15 +14,11 @@
|
|||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -32,4 +28,8 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -248,7 +248,6 @@
|
|||
<properties>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<bouncycastle.version>1.55</bouncycastle.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
|
|
|
@ -11,6 +11,14 @@
|
|||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.servicemodule</groupId>
|
||||
<artifactId>servicemodule</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -19,12 +27,4 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.servicemodule</groupId>
|
||||
<artifactId>servicemodule</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -2,17 +2,18 @@
|
|||
<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">
|
||||
<parent>
|
||||
<artifactId>com.baeldung.decoupling-pattern2</artifactId>
|
||||
<groupId>decoupling-pattern2</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.consumermodule</groupId>
|
||||
<artifactId>consumermodule</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<artifactId>com.baeldung.decoupling-pattern2</artifactId>
|
||||
<groupId>decoupling-pattern2</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.servicemodule</groupId>
|
||||
|
|
|
@ -35,7 +35,6 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [Why Do Local Variables Used in Lambdas Have to Be Final or Effectively Final?](https://www.baeldung.com/java-lambda-effectively-final-local-variables)
|
|
@ -6,3 +6,4 @@
|
|||
- [Generic Constructors in Java](https://www.baeldung.com/java-generic-constructors)
|
||||
- [Cannot Reference “X” Before Supertype Constructor Has Been Called](https://www.baeldung.com/java-cannot-reference-x-before-supertype-constructor-error)
|
||||
- [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes)
|
||||
- [Raw Types in Java](https://www.baeldung.com/raw-types-java)
|
||||
|
|
|
@ -75,13 +75,7 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- marshalling -->
|
||||
<jackson.version>2.8.5</jackson.version>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<equalsverifier.version>3.0.3</equalsverifier.version>
|
||||
|
|
|
@ -75,9 +75,6 @@
|
|||
|
||||
<properties>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
|
||||
<javax.mail.version>1.5.0-b01</javax.mail.version>
|
||||
|
||||
|
|
|
@ -42,8 +42,6 @@
|
|||
|
||||
<properties>
|
||||
<javax.mail.version>1.5.0-b01</javax.mail.version>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<springframework.spring-web.version>4.3.4.RELEASE</springframework.spring-web.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [Determine File Creating Date in Java](https://www.baeldung.com/file-creation-date-java)
|
||||
- [Determine File Creating Date in Java](https://www.baeldung.com/java-file-creation-date)
|
||||
|
|
|
@ -8,9 +8,9 @@
|
|||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
</project>
|
|
@ -2,4 +2,5 @@
|
|||
|
||||
## Core Java Optional
|
||||
|
||||
### Relevant Articles:
|
||||
### Relevant Articles:
|
||||
- [Java Optional as Return Type](https://www.baeldung.com/java-optional-return)
|
||||
|
|
|
@ -1,22 +1,14 @@
|
|||
<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>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>core-java-optional</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<hibernate.core.version>5.4.0.Final</hibernate.core.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<jackson.databind.version>2.9.8</jackson.databind.version>
|
||||
</properties>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-optional</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -27,12 +19,12 @@
|
|||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.databind.version}</version>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -50,4 +42,11 @@
|
|||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<hibernate.core.version>5.4.0.Final</hibernate.core.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -68,7 +68,6 @@
|
|||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
|
||||
|
|
|
@ -23,11 +23,4 @@
|
|||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -43,7 +43,6 @@
|
|||
<properties>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||
<bouncycastle.version>1.60</bouncycastle.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.random;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.DoubleStream;
|
||||
|
||||
public interface SecureRandomDemo {
|
||||
|
||||
public static void generateSecureRandomValues() {
|
||||
SecureRandom sr = new SecureRandom();
|
||||
|
||||
int randomInt = sr.nextInt();
|
||||
long randomLong = sr.nextLong();
|
||||
float randomFloat = sr.nextFloat();
|
||||
double randomDouble = sr.nextDouble();
|
||||
boolean randomBoolean = sr.nextBoolean();
|
||||
|
||||
IntStream randomIntStream = sr.ints();
|
||||
LongStream randomLongStream = sr.longs();
|
||||
DoubleStream randomDoubleStream = sr.doubles();
|
||||
|
||||
byte[] values = new byte[124];
|
||||
sr.nextBytes(values);
|
||||
}
|
||||
|
||||
public static SecureRandom getSecureRandomForAlgorithm(String algorithm) throws NoSuchAlgorithmException {
|
||||
if (algorithm == null || algorithm.isEmpty()) {
|
||||
return new SecureRandom();
|
||||
}
|
||||
|
||||
return SecureRandom.getInstance(algorithm);
|
||||
}
|
||||
|
||||
}
|
|
@ -264,7 +264,6 @@
|
|||
|
||||
<!-- util -->
|
||||
<guava.version>23.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<bouncycastle.version>1.55</bouncycastle.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
|
@ -114,7 +113,7 @@
|
|||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<!-- instrumentation -->
|
||||
<dependency>
|
||||
|
@ -454,8 +453,6 @@
|
|||
<gson.version>2.8.2</gson.version>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<decimal4j.version>1.0.3</decimal4j.version>
|
||||
<unix4j.version>0.4</unix4j.version>
|
||||
|
@ -472,7 +469,6 @@
|
|||
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||
|
||||
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<esapi.version>2.1.0.1</esapi.version>
|
||||
<jmh-core.version>1.19</jmh-core.version>
|
||||
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.daomodule</groupId>
|
||||
<artifactId>daomodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.multimodulemavenproject</groupId>
|
||||
<artifactId>multimodulemavenproject</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<groupId>com.baeldung.daomodule</groupId>
|
||||
<artifactId>daomodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.entitymodule</groupId>
|
||||
<artifactId>entitymodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.multimodulemavenproject</groupId>
|
||||
<artifactId>multimodulemavenproject</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<groupId>com.baeldung.entitymodule</groupId>
|
||||
<artifactId>entitymodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.mainappmodule</groupId>
|
||||
<artifactId>mainappmodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.multimodulemavenproject</groupId>
|
||||
<artifactId>multimodulemavenproject</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<groupId>com.baeldung.mainappmodule</groupId>
|
||||
<artifactId>mainappmodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
|
@ -14,18 +14,25 @@
|
|||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>entitymodule</module>
|
||||
<module>daomodule</module>
|
||||
<module>userdaomodule</module>
|
||||
<module>mainappmodule</module>
|
||||
</modules>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.12.2</version>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -49,14 +56,8 @@
|
|||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<modules>
|
||||
<module>entitymodule</module>
|
||||
<module>daomodule</module>
|
||||
<module>userdaomodule</module>
|
||||
<module>mainappmodule</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<assertj-core.version>3.12.2</assertj-core.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -1,24 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.userdaomodule</groupId>
|
||||
<artifactId>userdaomodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.multimodulemavenproject</groupId>
|
||||
<artifactId>multimodulemavenproject</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<groupId>com.baeldung.userdaomodule</groupId>
|
||||
<artifactId>userdaomodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -33,6 +25,15 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>9</maven.compiler.source>
|
||||
<maven.compiler.target>9</maven.compiler.target>
|
||||
|
|
|
@ -18,9 +18,10 @@
|
|||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.25</version>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>pre-jpms</finalName>
|
||||
<plugins>
|
||||
|
@ -65,9 +66,9 @@
|
|||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -88,7 +88,7 @@
|
|||
|
||||
<properties>
|
||||
<kotlin.version>1.3.30</kotlin.version>
|
||||
<junit.jupiter.version>5.4.2</junit.jupiter.version>
|
||||
<junit-jupiter.version>5.4.2</junit-jupiter.version>
|
||||
<mockito.version>2.27.0</mockito.version>
|
||||
<byte-buddy.version>1.9.12</byte-buddy.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.kittinunf.fuel</groupId>
|
||||
|
@ -76,11 +76,9 @@
|
|||
|
||||
<properties>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||
<junit.platform.version>1.1.1</junit.platform.version>
|
||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<fuel.version>1.15.0</fuel.version>
|
||||
<kovenant.version>3.3.0</kovenant.version>
|
||||
<injekt-core.version>1.16.1</injekt-core.version>
|
||||
|
|
|
@ -69,7 +69,6 @@
|
|||
<properties>
|
||||
<couchbase.client.version>2.5.0</couchbase.client.version>
|
||||
<spring-framework.version>4.3.5.RELEASE</spring-framework.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -7,6 +7,12 @@
|
|||
<name>custom-pmd</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
|
@ -115,7 +115,6 @@
|
|||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<disruptor.version>3.3.6</disruptor.version>
|
||||
<!-- testing -->
|
||||
<testng.version>6.10</testng.version>
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<dozer.version>5.5.1</dozer.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -8,6 +8,13 @@
|
|||
<version>1.0-SNAPSHOT</version>
|
||||
<name>flyway-cdi-extension</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.enterprise</groupId>
|
||||
|
@ -51,6 +58,5 @@
|
|||
<flyway-core.version>5.1.4</flyway-core.version>
|
||||
<tomcat-jdbc.version>8.5.33</tomcat-jdbc.version>
|
||||
<javax.annotation-api.version>1.3.2</javax.annotation-api.version>
|
||||
<h2.version>1.4.197</h2.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -64,7 +64,6 @@
|
|||
<!-- marshalling -->
|
||||
<gson.version>2.8.0</gson.version>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<joda-time.version>2.9.6</joda-time.version>
|
||||
</properties>
|
||||
|
|
|
@ -55,7 +55,6 @@
|
|||
<properties>
|
||||
<!-- util -->
|
||||
<guava.version>24.0-jre</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
|
||||
<!-- testing -->
|
||||
|
|
|
@ -49,7 +49,6 @@
|
|||
<properties>
|
||||
<!-- util -->
|
||||
<guava.version>24.0-jre</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
|
|
|
@ -297,7 +297,6 @@
|
|||
<properties>
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<httpasyncclient.version>4.1.4</httpasyncclient.version>
|
||||
<!-- testing -->
|
||||
|
@ -305,7 +304,6 @@
|
|||
<httpcore.version>4.4.11</httpcore.version>
|
||||
<httpclient.version>4.5.8</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
|
||||
<!-- maven plugins -->
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -120,7 +120,6 @@
|
|||
<properties>
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<httpasyncclient.version>4.1.4</httpasyncclient.version>
|
||||
<!-- testing -->
|
||||
|
|
|
@ -65,7 +65,6 @@
|
|||
<hystrix-core.version>1.5.8</hystrix-core.version>
|
||||
<rxjava-core.version>0.20.7</rxjava-core.version>
|
||||
<!-- maven plugins -->
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
||||
<hystrix-metrics-event-stream.version>1.5.8</hystrix-metrics-event-stream.version>
|
||||
<hystrix-dashboard.version>1.5.8</hystrix-dashboard.version>
|
||||
|
|
|
@ -9,4 +9,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Mapping Multiple JSON Fields to a Single Java Field](https://www.baeldung.com/json-multiple-fields-single-java-field)
|
||||
- [How to Process YAML with Jackson](https://www.baeldung.com/jackson-yaml)
|
||||
- [Working with Tree Model Nodes in Jackson](https://www.baeldung.com/jackson-json-node-tree-model)
|
||||
|
||||
- [Converting JSON to CSV in Java](https://www.baeldung.com/java-converting-json-to-csv)
|
||||
|
|
|
@ -26,21 +26,21 @@
|
|||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-yaml</artifactId>
|
||||
<version>2.9.8</version>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- CSV -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-csv</artifactId>
|
||||
<version>2.9.8</version>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Allow use of LocalDate -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>2.9.8</version>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,126 @@
|
|||
package com.baeldung.jackson.json.compare;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.NumericNode;
|
||||
import com.fasterxml.jackson.databind.node.TextNode;
|
||||
|
||||
public class JsonCompareUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoSameJsonDataObjects_whenCompared_thenAreEqual() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
String s1 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\", \"age\": 34 }}";
|
||||
String s2 = "{\"employee\": {\"id\": \"1212\",\"age\": 34, \"fullName\": \"John Miles\" }}";
|
||||
|
||||
JsonNode actualObj1 = mapper.readTree(s1);
|
||||
JsonNode actualObj2 = mapper.readTree(s2);
|
||||
|
||||
assertEquals(actualObj1, actualObj2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSameNestedJsonDataObjects_whenCompared_thenEqual() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
String s1 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\",\"age\": 34, \"contact\":{\"email\": \"john@xyz.com\",\"phone\": \"9999999999\"} }}";
|
||||
String s2 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\",\"age\": 34, \"contact\":{\"email\": \"john@xyz.com\",\"phone\": \"9999999999\"} }}";
|
||||
|
||||
JsonNode actualObj1 = mapper.readTree(s1);
|
||||
JsonNode actualObj2 = mapper.readTree(s2);
|
||||
|
||||
assertEquals(actualObj1, actualObj2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSameListJsonDataObjects_whenCompared_thenEqual() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
String s1 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\",\"age\": 34, \"skills\":[\"Java\", \"C++\", \"Python\"] }}";
|
||||
String s2 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\",\"age\": 34, \"skills\":[\"Java\", \"C++\", \"Python\"] }}";
|
||||
|
||||
JsonNode actualObj1 = mapper.readTree(s1);
|
||||
JsonNode actualObj2 = mapper.readTree(s2);
|
||||
|
||||
assertEquals(actualObj1, actualObj2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoJsonDataObjects_whenComparedUsingCustomNumericNodeComparator_thenEqual() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
String s1 = "{\"name\": \"John\",\"score\":5.0}";
|
||||
String s2 = "{\"name\": \"John\",\"score\":5}";
|
||||
JsonNode actualObj1 = mapper.readTree(s1);
|
||||
JsonNode actualObj2 = mapper.readTree(s2);
|
||||
|
||||
NumericNodeComparator cmp = new NumericNodeComparator();
|
||||
|
||||
assertNotEquals(actualObj1, actualObj2);
|
||||
assertTrue(actualObj1.equals(cmp, actualObj2));
|
||||
|
||||
}
|
||||
|
||||
public class NumericNodeComparator implements Comparator<JsonNode> {
|
||||
@Override
|
||||
public int compare(JsonNode o1, JsonNode o2) {
|
||||
if (o1.equals(o2)) {
|
||||
return 0;
|
||||
}
|
||||
if ((o1 instanceof NumericNode) && (o2 instanceof NumericNode)) {
|
||||
Double d1 = ((NumericNode) o1).asDouble();
|
||||
Double d2 = ((NumericNode) o2).asDouble();
|
||||
if (d1.compareTo(d2) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoJsonDataObjects_whenComparedUsingCustomTextNodeComparator_thenEqual() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
String s1 = "{\"name\": \"JOHN\",\"score\":5}";
|
||||
String s2 = "{\"name\": \"John\",\"score\":5}";
|
||||
JsonNode actualObj1 = mapper.readTree(s1);
|
||||
JsonNode actualObj2 = mapper.readTree(s2);
|
||||
|
||||
TextNodeComparator cmp = new TextNodeComparator();
|
||||
|
||||
assertNotEquals(actualObj1, actualObj2);
|
||||
assertTrue(actualObj1.equals(cmp, actualObj2));
|
||||
|
||||
}
|
||||
|
||||
public class TextNodeComparator implements Comparator<JsonNode> {
|
||||
@Override
|
||||
public int compare(JsonNode o1, JsonNode o2) {
|
||||
if (o1.equals(o2)) {
|
||||
return 0;
|
||||
}
|
||||
if ((o1 instanceof TextNode) && (o2 instanceof TextNode)) {
|
||||
String s1 = ((TextNode) o1).asText();
|
||||
String s2 = ((TextNode) o2).asText();
|
||||
if (s1.equalsIgnoreCase(s2)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -118,7 +118,6 @@
|
|||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.8</commons-lang3.version>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<commons-collections4.version>4.2</commons-collections4.version>
|
||||
|
|
|
@ -118,7 +118,6 @@
|
|||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.8</commons-lang3.version>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<commons-collections4.version>4.2</commons-collections4.version>
|
||||
|
|
|
@ -10,4 +10,7 @@
|
|||
- [Converting a List to String in Java](http://www.baeldung.com/java-list-to-string)
|
||||
- [How to Convert List to Map in Java](http://www.baeldung.com/java-list-to-map)
|
||||
- [Array to String Conversions](https://www.baeldung.com/java-array-to-string)
|
||||
- [Converting a Collection to ArrayList in Java](https://www.baeldung.com/java-convert-collection-arraylist)
|
||||
- [Converting a Collection to ArrayList in Java](https://www.baeldung.com/java-convert-collection-arraylist)
|
||||
- [Java 8 Collectors toMap](https://www.baeldung.com/java-collectors-tomap)
|
||||
- [Converting Iterable to Collection in Java](https://www.baeldung.com/java-iterable-to-collection)
|
||||
- [Converting Iterator to List](https://www.baeldung.com/java-convert-iterator-to-list)
|
||||
|
|
|
@ -33,7 +33,6 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.java.collections;
|
||||
package com.baeldung.java.collections;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.junit.Assert.assertThat;
|
|
@ -54,7 +54,6 @@
|
|||
<trove4j.version>3.0.2</trove4j.version>
|
||||
<fastutil.version>8.1.0</fastutil.version>
|
||||
<colt.version>1.2.0</colt.version>
|
||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -44,7 +44,6 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
<dependency>
|
||||
<groupId>commons-validator</groupId>
|
||||
<artifactId>commons-validator</artifactId>
|
||||
<version>1.6</version>
|
||||
<version>${commons-validator.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
|
@ -63,6 +63,7 @@
|
|||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<commons-validator.version>1.6</commons-validator.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
|
|
|
@ -74,8 +74,6 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
|
|
|
@ -72,8 +72,6 @@
|
|||
<javaee-version>8.0</javaee-version>
|
||||
<liberty-maven-plugin.version>2.3</liberty-maven-plugin.version>
|
||||
<openliberty-runtime.version>18.0.0.1</openliberty-runtime.version>
|
||||
|
||||
<maven-war-plugin.version>3.2.2</maven-war-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
<dependency>
|
||||
<groupId>com.github.dpaukov</groupId>
|
||||
<artifactId>combinatoricslib3</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<version>${combinatoricslib3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -63,6 +63,7 @@
|
|||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
<guava.version>27.0.1-jre</guava.version>
|
||||
<combinatoricslib3.version>3.3.0</combinatoricslib3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,26 @@
|
|||
*.class
|
||||
|
||||
0.*
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
.resourceCache
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# Files generated by integration tests
|
||||
*.txt
|
||||
backup-pom.xml
|
||||
/bin/
|
||||
/temp
|
||||
|
||||
#IntelliJ specific
|
||||
.idea/
|
||||
*.iml
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue