Moved the code to spring-5-reactive-2
This commit is contained in:
@@ -1,12 +1,16 @@
|
|||||||
|
|
||||||
The "REST with Spring" Classes
|
The Courses
|
||||||
==============================
|
==============================
|
||||||
|
|
||||||
Here's the Master Class of REST With Spring (along with the newly announced Boot 2 material): <br/>
|
|
||||||
**[>> THE REST WITH SPRING - MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
|
|
||||||
|
|
||||||
And here's the Master Class of Learn Spring Security: <br/>
|
Here's the new "Learn Spring" course: <br/>
|
||||||
**[>> LEARN SPRING SECURITY - MASTER CLASS](http://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
|
**[>> LEARN SPRING - THE MASTER CLASS](https://www.baeldung.com/learn-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=ls#master-class)**
|
||||||
|
|
||||||
|
Here's the Master Class of "REST With Spring" (along with the new announced Boot 2 material): <br/>
|
||||||
|
**[>> THE REST WITH SPRING - MASTER CLASS](https://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
|
||||||
|
|
||||||
|
And here's the Master Class of "Learn Spring Security": <br/>
|
||||||
|
**[>> LEARN SPRING SECURITY - MASTER CLASS](https://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -15,7 +19,7 @@ Java and Spring Tutorials
|
|||||||
|
|
||||||
This project is **a collection of small and focused tutorials** - each covering a single and well defined area of development in the Java ecosystem.
|
This project is **a collection of small and focused tutorials** - each covering a single and well defined area of development in the Java ecosystem.
|
||||||
A strong focus of these is, of course, the Spring Framework - Spring, Spring Boot and Spring Security.
|
A strong focus of these is, of course, the Spring Framework - Spring, Spring Boot and Spring Security.
|
||||||
In additional to Spring, the following technologies are in focus: `core Java`, `Jackson`, `HttpClient`, `Guava`.
|
In additional to Spring, the modules here are covering a number of aspects in Java.
|
||||||
|
|
||||||
|
|
||||||
Building the project
|
Building the project
|
||||||
@@ -32,8 +36,15 @@ Running a Spring Boot module
|
|||||||
====================
|
====================
|
||||||
To run a Spring Boot module run the command: `mvn spring-boot:run` in the module directory
|
To run a Spring Boot module run the command: `mvn spring-boot:run` in the module directory
|
||||||
|
|
||||||
#Running Tests
|
|
||||||
|
|
||||||
|
Working with the IDE
|
||||||
|
====================
|
||||||
|
This repo contains a large number of modules.
|
||||||
|
When you're working with an individual module, there's no need to import all of them (or build all of them) - you can simply import that particular module in either Eclipse or IntelliJ.
|
||||||
|
|
||||||
|
|
||||||
|
Running Tests
|
||||||
|
=============
|
||||||
The command `mvn clean install` will run the unit tests in a module.
|
The command `mvn clean install` will run the unit tests in a module.
|
||||||
To run the integration tests, use the command `mvn clean install -Pintegration-lite-first`
|
To run the integration tests, use the command `mvn clean install -Pintegration-lite-first`
|
||||||
|
|
||||||
|
|||||||
@@ -6,4 +6,4 @@
|
|||||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
- [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 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)
|
- [Checking if a Java Graph has a Cycle](https://www.baeldung.com/java-graph-has-a-cycle)
|
||||||
- [A Guide to the Folding Technique](https://www.baeldung.com/folding-hashing-technique)
|
- [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique)
|
||||||
|
|||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.algorithms.stringsortingbynumber;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
public final class NaturalOrderComparators {
|
||||||
|
|
||||||
|
private static final String DIGIT_AND_DECIMAL_REGEX = "[^\\d.]";
|
||||||
|
|
||||||
|
private NaturalOrderComparators() {
|
||||||
|
throw new AssertionError("Let's keep this static");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Comparator<String> createNaturalOrderRegexComparator() {
|
||||||
|
return Comparator.comparingDouble(NaturalOrderComparators::parseStringToNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double parseStringToNumber(String input){
|
||||||
|
|
||||||
|
final String digitsOnly = input.replaceAll(DIGIT_AND_DECIMAL_REGEX, "");
|
||||||
|
|
||||||
|
if("".equals(digitsOnly)) return 0;
|
||||||
|
|
||||||
|
try{
|
||||||
|
return Double.parseDouble(digitsOnly);
|
||||||
|
}catch (NumberFormatException nfe){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+79
@@ -0,0 +1,79 @@
|
|||||||
|
package com.baeldung.algorithms.stringsortingbynumber;
|
||||||
|
|
||||||
|
import com.baeldung.algorithms.stringsortingbynumber.NaturalOrderComparators;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class NaturalOrderComparatorsUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSimpleStringsContainingIntsAndDoubles_whenSortedByRegex_checkSortingCorrect() {
|
||||||
|
|
||||||
|
List<String> testStrings = Arrays.asList("a1", "b3", "c4", "d2.2", "d2.4", "d2.3d");
|
||||||
|
|
||||||
|
testStrings.sort(NaturalOrderComparators.createNaturalOrderRegexComparator());
|
||||||
|
|
||||||
|
List<String> expected = Arrays.asList("a1", "d2.2", "d2.3d", "d2.4", "b3", "c4");
|
||||||
|
|
||||||
|
assertEquals(expected, testStrings);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSimpleStringsContainingIntsAndDoublesWithAnInvalidNumber_whenSortedByRegex_checkSortingCorrect() {
|
||||||
|
|
||||||
|
List<String> testStrings = Arrays.asList("a1", "b3", "c4", "d2.2", "d2.4", "d2.3.3d");
|
||||||
|
|
||||||
|
testStrings.sort(NaturalOrderComparators.createNaturalOrderRegexComparator());
|
||||||
|
|
||||||
|
List<String> expected = Arrays.asList("d2.3.3d", "a1", "d2.2", "d2.4", "b3", "c4");
|
||||||
|
|
||||||
|
assertEquals(expected, testStrings);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAllForseenProblems_whenSortedByRegex_checkSortingCorrect() {
|
||||||
|
|
||||||
|
List<String> testStrings = Arrays.asList("a1", "b3", "c4", "d2.2", "d2.f4", "d2.3.3d");
|
||||||
|
|
||||||
|
testStrings.sort(NaturalOrderComparators.createNaturalOrderRegexComparator());
|
||||||
|
|
||||||
|
List<String> expected = Arrays.asList("d2.3.3d", "a1", "d2.2", "d2.f4", "b3", "c4");
|
||||||
|
|
||||||
|
assertEquals(expected, testStrings);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenComplexStringsContainingSeparatedNumbers_whenSortedByRegex_checkNumbersCondensedAndSorted() {
|
||||||
|
|
||||||
|
List<String> testStrings = Arrays.asList("a1b2c5", "b3ght3.2", "something65.thensomething5"); //125, 33.2, 65.5
|
||||||
|
|
||||||
|
List<String> expected = Arrays.asList("b3ght3.2", "something65.thensomething5", "a1b2c5" );
|
||||||
|
|
||||||
|
testStrings.sort(NaturalOrderComparators.createNaturalOrderRegexComparator());
|
||||||
|
|
||||||
|
assertEquals(expected, testStrings);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenStringsNotContainingNumbers_whenSortedByRegex_checkOrderNotChanged() {
|
||||||
|
|
||||||
|
List<String> testStrings = Arrays.asList("a", "c", "d", "e");
|
||||||
|
List<String> expected = new ArrayList<>(testStrings);
|
||||||
|
|
||||||
|
testStrings.sort(NaturalOrderComparators.createNaturalOrderRegexComparator());
|
||||||
|
|
||||||
|
assertEquals(expected, testStrings);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.algorithms.shellsort;
|
||||||
|
|
||||||
|
public class ShellSort {
|
||||||
|
|
||||||
|
public static void sort(int arrayToSort[]) {
|
||||||
|
int n = arrayToSort.length;
|
||||||
|
|
||||||
|
for (int gap = n / 2; gap > 0; gap /= 2) {
|
||||||
|
for (int i = gap; i < n; i++) {
|
||||||
|
int key = arrayToSort[i];
|
||||||
|
int j = i;
|
||||||
|
while (j >= gap && arrayToSort[j - gap] > key) {
|
||||||
|
arrayToSort[j] = arrayToSort[j - gap];
|
||||||
|
j -= gap;
|
||||||
|
}
|
||||||
|
arrayToSort[j] = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.algorithms.shellsort;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ShellSortUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUnsortedArray_whenShellSort_thenSortedAsc() {
|
||||||
|
int[] input = {41, 15, 82, 5, 65, 19, 32, 43, 8};
|
||||||
|
ShellSort.sort(input);
|
||||||
|
int[] expected = {5, 8, 15, 19, 32, 41, 43, 65, 82};
|
||||||
|
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-7
@@ -18,12 +18,6 @@
|
|||||||
<groupId>org.axonframework</groupId>
|
<groupId>org.axonframework</groupId>
|
||||||
<artifactId>axon-spring-boot-starter</artifactId>
|
<artifactId>axon-spring-boot-starter</artifactId>
|
||||||
<version>${axon.version}</version>
|
<version>${axon.version}</version>
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>org.axonframework</groupId>
|
|
||||||
<artifactId>axon-server-connector</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -58,7 +52,7 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<axon.version>4.0.3</axon.version>
|
<axon.version>4.1.2</axon.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -13,6 +13,7 @@ import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
|
|||||||
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
|
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
|
||||||
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
|
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
|
||||||
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
|
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
|
||||||
|
import com.baeldung.axon.coreapi.exceptions.UnconfirmedOrderException;
|
||||||
|
|
||||||
@Aggregate
|
@Aggregate
|
||||||
public class OrderAggregate {
|
public class OrderAggregate {
|
||||||
@@ -34,7 +35,7 @@ public class OrderAggregate {
|
|||||||
@CommandHandler
|
@CommandHandler
|
||||||
public void handle(ShipOrderCommand command) {
|
public void handle(ShipOrderCommand command) {
|
||||||
if (!orderConfirmed) {
|
if (!orderConfirmed) {
|
||||||
throw new IllegalStateException("Cannot ship an order which has not been confirmed yet.");
|
throw new UnconfirmedOrderException();
|
||||||
}
|
}
|
||||||
|
|
||||||
apply(new OrderShippedEvent(orderId));
|
apply(new OrderShippedEvent(orderId));
|
||||||
@@ -43,12 +44,12 @@ public class OrderAggregate {
|
|||||||
@EventSourcingHandler
|
@EventSourcingHandler
|
||||||
public void on(OrderPlacedEvent event) {
|
public void on(OrderPlacedEvent event) {
|
||||||
this.orderId = event.getOrderId();
|
this.orderId = event.getOrderId();
|
||||||
orderConfirmed = false;
|
this.orderConfirmed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventSourcingHandler
|
@EventSourcingHandler
|
||||||
public void on(OrderConfirmedEvent event) {
|
public void on(OrderConfirmedEvent event) {
|
||||||
orderConfirmed = true;
|
this.orderConfirmed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected OrderAggregate() {
|
protected OrderAggregate() {
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.axon.coreapi.exceptions;
|
||||||
|
|
||||||
|
public class UnconfirmedOrderException extends IllegalStateException {
|
||||||
|
|
||||||
|
public UnconfirmedOrderException() {
|
||||||
|
super("Cannot ship an order which has not been confirmed yet.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.axonframework.config.ProcessingGroup;
|
||||||
import org.axonframework.eventhandling.EventHandler;
|
import org.axonframework.eventhandling.EventHandler;
|
||||||
import org.axonframework.queryhandling.QueryHandler;
|
import org.axonframework.queryhandling.QueryHandler;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -16,6 +17,7 @@ import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
|
|||||||
import com.baeldung.axon.coreapi.queries.OrderedProduct;
|
import com.baeldung.axon.coreapi.queries.OrderedProduct;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@ProcessingGroup("ordered-products")
|
||||||
public class OrderedProductsEventHandler {
|
public class OrderedProductsEventHandler {
|
||||||
|
|
||||||
private final Map<String, OrderedProduct> orderedProducts = new HashMap<>();
|
private final Map<String, OrderedProduct> orderedProducts = new HashMap<>();
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
spring.application.name=Order Management Service
|
||||||
@@ -2,6 +2,7 @@ package com.baeldung.axon.commandmodel;
|
|||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import com.baeldung.axon.coreapi.exceptions.UnconfirmedOrderException;
|
||||||
import org.axonframework.test.aggregate.AggregateTestFixture;
|
import org.axonframework.test.aggregate.AggregateTestFixture;
|
||||||
import org.axonframework.test.aggregate.FixtureConfiguration;
|
import org.axonframework.test.aggregate.FixtureConfiguration;
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
@@ -41,12 +42,12 @@ public class OrderAggregateUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowIllegalStateException() {
|
public void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowUnconfirmedOrderException() {
|
||||||
String orderId = UUID.randomUUID().toString();
|
String orderId = UUID.randomUUID().toString();
|
||||||
String product = "Deluxe Chair";
|
String product = "Deluxe Chair";
|
||||||
fixture.given(new OrderPlacedEvent(orderId, product))
|
fixture.given(new OrderPlacedEvent(orderId, product))
|
||||||
.when(new ShipOrderCommand(orderId))
|
.when(new ShipOrderCommand(orderId))
|
||||||
.expectException(IllegalStateException.class);
|
.expectException(UnconfirmedOrderException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -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())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,4 @@
|
|||||||
|
## Relevant Articles:
|
||||||
|
|
||||||
|
|
||||||
|
- [String API Updates in Java 12](https://www.baeldung.com/java12-string-api)
|
||||||
+34
@@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung.java_8_features.groupingby;
|
||||||
|
|
||||||
|
public class Tuple {
|
||||||
|
|
||||||
|
private BlogPostType type;
|
||||||
|
private String author;
|
||||||
|
|
||||||
|
public Tuple(BlogPostType type, String author) {
|
||||||
|
super();
|
||||||
|
this.type = type;
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BlogPostType getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(BlogPostType type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthor(String author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Tuple [type=" + type + ", author=" + author + ", getType()=" + getType() + ", getAuthor()=" + getAuthor() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
@@ -259,4 +259,15 @@ public class OptionalUnitTest {
|
|||||||
LOG.debug("Getting default value...");
|
LOG.debug("Getting default value...");
|
||||||
return "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());
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
*.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
|
||||||
|
backup-pom.xml
|
||||||
|
/bin/
|
||||||
|
/temp
|
||||||
|
|
||||||
|
#IntelliJ specific
|
||||||
|
.idea/
|
||||||
|
*.iml
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<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</groupId>
|
||||||
|
<artifactId>core-java-arrays-2</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-arrays-2</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- test scoped -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj-core.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-arrays-2</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
|
||||||
|
<!-- util -->
|
||||||
|
<commons-lang3.version>3.9</commons-lang3.version>
|
||||||
|
<!-- testing -->
|
||||||
|
<assertj-core.version>3.10.0</assertj-core.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
package com.baeldung.array.looping;
|
||||||
|
|
||||||
|
public class LoopDiagonally {
|
||||||
|
|
||||||
|
|
||||||
|
public String loopDiagonally(String[][] twoDArray) {
|
||||||
|
|
||||||
|
int length = twoDArray.length;
|
||||||
|
int diagonalLines = (length + length) - 1;
|
||||||
|
int itemsInDiagonal = 0;
|
||||||
|
int midPoint = (diagonalLines / 2) + 1;
|
||||||
|
StringBuilder output = new StringBuilder();
|
||||||
|
|
||||||
|
for (int i = 1; i <= diagonalLines; i++) {
|
||||||
|
|
||||||
|
StringBuilder items = new StringBuilder();
|
||||||
|
int rowIndex;
|
||||||
|
int columnIndex;
|
||||||
|
|
||||||
|
if (i <= midPoint) {
|
||||||
|
itemsInDiagonal++;
|
||||||
|
for (int j = 0; j < itemsInDiagonal; j++) {
|
||||||
|
rowIndex = (i - j) - 1;
|
||||||
|
columnIndex = j;
|
||||||
|
items.append(twoDArray[rowIndex][columnIndex]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
itemsInDiagonal--;
|
||||||
|
for (int j = 0; j < itemsInDiagonal; j++) {
|
||||||
|
rowIndex = (length - 1) - j;
|
||||||
|
columnIndex = (i - length) + j;
|
||||||
|
items.append(twoDArray[rowIndex][columnIndex]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i != diagonalLines) {
|
||||||
|
output.append(items).append(" ");
|
||||||
|
} else {
|
||||||
|
output.append(items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.array.looping;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class LoopDiagonallyUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void twoArrayIsLoopedDiagonallyAsExpected() {
|
||||||
|
|
||||||
|
LoopDiagonally loopDiagonally = new LoopDiagonally();
|
||||||
|
String[][] twoDArray = {{"a", "b", "c"},
|
||||||
|
{"d", "e", "f"},
|
||||||
|
{"g", "h", "i"}};
|
||||||
|
|
||||||
|
String output = loopDiagonally.loopDiagonally(twoDArray);
|
||||||
|
assertEquals("a db gec hf i", output);
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
-22
@@ -2,7 +2,10 @@ package com.baeldung.array;
|
|||||||
|
|
||||||
import com.baeldung.arraycopy.model.Employee;
|
import com.baeldung.arraycopy.model.Employee;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
public class SortedArrayChecker {
|
public class SortedArrayChecker {
|
||||||
|
|
||||||
boolean isSorted(int[] array, int length) {
|
boolean isSorted(int[] array, int length) {
|
||||||
if (array == null || length < 2)
|
if (array == null || length < 2)
|
||||||
return true;
|
return true;
|
||||||
@@ -22,7 +25,7 @@ public class SortedArrayChecker {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isSorted(String[] array, int length) {
|
boolean isSorted(Comparable[] array, int length) {
|
||||||
if (array == null || length < 2)
|
if (array == null || length < 2)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@@ -32,40 +35,31 @@ public class SortedArrayChecker {
|
|||||||
return isSorted(array, length - 1);
|
return isSorted(array, length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isSorted(String[] array) {
|
boolean isSorted(Comparable[] 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) {
|
for (int i = 0; i < array.length - 1; ++i) {
|
||||||
if (array[i].getName().compareTo(array[i + 1].getName()) > 0)
|
if (array[i].compareTo(array[i + 1]) > 0)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isSortedByAge(Employee[] array) {
|
boolean isSorted(Object[] array, Comparator comparator) {
|
||||||
for (int i = 0; i < array.length - 1; ++i) {
|
for (int i = 0; i < array.length - 1; ++i) {
|
||||||
if (array[i].getAge() > (array[i + 1].getAge()))
|
if (comparator.compare(array[i], (array[i + 1])) > 0)
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
boolean isSorted(Object[] array, Comparator comparator, int length) {
|
||||||
}
|
|
||||||
|
|
||||||
boolean isSortedByAge(Employee[] array, int length) {
|
|
||||||
if (array == null || length < 2)
|
if (array == null || length < 2)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (array[length - 2].getAge() > array[length - 1].getAge())
|
if (comparator.compare(array[length - 2], array[length - 1]) > 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return isSortedByAge(array, length - 1);
|
return isSorted(array, comparator, length - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-12
@@ -4,32 +4,33 @@ import com.baeldung.arraycopy.model.Employee;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
class SortedArrayCheckerUnitTest {
|
public class SortedArrayCheckerUnitTest {
|
||||||
|
|
||||||
private static final int[] INTEGER_SORTED = {1, 3, 5, 7, 9};
|
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 int[] INTEGER_NOT_SORTED = {1, 3, 11, 7};
|
||||||
|
|
||||||
private static final String[] STRING_SORTED = {"abc", "cde", "fgh"};
|
private static final String[] STRING_SORTED = {"abc", "cde", "fgh"};
|
||||||
private static final String[] STRING_NOT_SORTED = {"abc", "fgh", "cde", "ijk"};
|
private static final String[] STRING_NOT_SORTED = {"abc", "fgh", "cde", "ijk"};
|
||||||
|
|
||||||
private final Employee[] EMPLOYEES_SORTED_BY_NAME = {
|
private static final Employee[] EMPLOYEES_SORTED_BY_NAME = {
|
||||||
new Employee(1, "Carlos", 26),
|
new Employee(1, "Carlos", 26),
|
||||||
new Employee(2, "Daniel", 31),
|
new Employee(2, "Daniel", 31),
|
||||||
new Employee(3, "Marta", 27)};
|
new Employee(3, "Marta", 27)};
|
||||||
|
|
||||||
private final Employee[] EMPLOYEES_NOT_SORTED_BY_NAME = {
|
private static final Employee[] EMPLOYEES_NOT_SORTED_BY_NAME = {
|
||||||
new Employee(1, "Daniel", 31),
|
new Employee(1, "Daniel", 31),
|
||||||
new Employee(2, "Carlos", 26),
|
new Employee(2, "Carlos", 26),
|
||||||
new Employee(3, "Marta", 27)};
|
new Employee(3, "Marta", 27)};
|
||||||
|
|
||||||
private final Employee[] EMPLOYEES_SORTED_BY_AGE = {
|
private static final Employee[] EMPLOYEES_SORTED_BY_AGE = {
|
||||||
new Employee(1, "Carlos", 26),
|
new Employee(1, "Carlos", 26),
|
||||||
new Employee(2, "Marta", 27),
|
new Employee(2, "Marta", 27),
|
||||||
new Employee(3, "Daniel", 31)};
|
new Employee(3, "Daniel", 31)};
|
||||||
|
|
||||||
private final Employee[] EMPLOYEES_NOT_SORTED_BY_AGE = {
|
private static final Employee[] EMPLOYEES_NOT_SORTED_BY_AGE = {
|
||||||
new Employee(1, "Marta", 27),
|
new Employee(1, "Marta", 27),
|
||||||
new Employee(2, "Carlos", 26),
|
new Employee(2, "Carlos", 26),
|
||||||
new Employee(3, "Daniel", 31)};
|
new Employee(3, "Daniel", 31)};
|
||||||
@@ -61,13 +62,18 @@ class SortedArrayCheckerUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmployeeArray_thenReturnIfItIsSortedOrNot() {
|
public void givenEmployeeArray_thenReturnIfItIsSortedOrNot() {
|
||||||
assertThat(sortedArrayChecker.isSortedByName(EMPLOYEES_SORTED_BY_NAME)).isEqualTo(true);
|
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_SORTED_BY_NAME, Comparator.comparing(Employee::getName))).isEqualTo(true);
|
||||||
assertThat(sortedArrayChecker.isSortedByName(EMPLOYEES_NOT_SORTED_BY_NAME)).isEqualTo(false);
|
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_NOT_SORTED_BY_NAME, Comparator.comparing(Employee::getName))).isEqualTo(false);
|
||||||
|
|
||||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_SORTED_BY_AGE)).isEqualTo(true);
|
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge))).isEqualTo(true);
|
||||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_NOT_SORTED_BY_AGE)).isEqualTo(false);
|
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_NOT_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge))).isEqualTo(false);
|
||||||
|
|
||||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_SORTED_BY_AGE, EMPLOYEES_SORTED_BY_AGE.length)).isEqualTo(true);
|
assertThat(sortedArrayChecker
|
||||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_NOT_SORTED_BY_AGE, EMPLOYEES_NOT_SORTED_BY_AGE.length)).isEqualTo(false);
|
.isSorted(EMPLOYEES_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge), EMPLOYEES_SORTED_BY_AGE.length))
|
||||||
|
.isEqualTo(true);
|
||||||
|
assertThat(sortedArrayChecker
|
||||||
|
.isSorted(EMPLOYEES_NOT_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge), EMPLOYEES_NOT_SORTED_BY_AGE.length))
|
||||||
|
.isEqualTo(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+1
-1
@@ -42,7 +42,7 @@ public class CoreJavaCollectionsUnitTest {
|
|||||||
@Test(expected = UnsupportedOperationException.class)
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
public final void givenUsingGuavaBuilder_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
|
public final void givenUsingGuavaBuilder_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
|
||||||
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
|
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
|
||||||
final ImmutableList<Object> unmodifiableList = ImmutableList.builder().addAll(list).build();
|
final ImmutableList<String> unmodifiableList = ImmutableList.<String>builder().addAll(list).build();
|
||||||
unmodifiableList.add("four");
|
unmodifiableList.add("four");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,3 +17,4 @@
|
|||||||
- [Runnable vs. Callable in Java](http://www.baeldung.com/java-runnable-callable)
|
- [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)
|
- [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 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)
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
## Relevant Articles:
|
||||||
|
|
||||||
|
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user