From 87d66ba879da4b35c72bbf2b7b6ad9b23232cd68 Mon Sep 17 00:00:00 2001 From: anghelleonard Date: Thu, 6 Aug 2020 16:29:58 +0300 Subject: [PATCH 01/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f781169..0419ea5f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Java Coding Problems -Java Coding Problems +Java Coding Problems This is the code repository for [Java Coding Problems ](https://www.packtpub.com/programming/java-coding-problems?utm_source=github&utm_medium=repository&utm_campaign=), published by Packt. From ddd3fbe08f3d3dc20759bfb15c8fbffc618105dc Mon Sep 17 00:00:00 2001 From: AnghelLeonard Date: Sun, 30 Aug 2020 17:08:10 +0300 Subject: [PATCH 02/14] Arrays, collections and data structures --- .../BONUS_2_ConvertIterableToList/README.md | 23 +++++ .../BONUS_2_ConvertIterableToList/pom.xml | 14 +++ .../java/modern/challenge/Converters.java | 97 +++++++++++++++++++ .../modern/challenge/MainApplication.java | 19 ++++ 4 files changed, 153 insertions(+) create mode 100644 Chapter05/BONUS_2_ConvertIterableToList/README.md create mode 100644 Chapter05/BONUS_2_ConvertIterableToList/pom.xml create mode 100644 Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/Converters.java create mode 100644 Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/MainApplication.java diff --git a/Chapter05/BONUS_2_ConvertIterableToList/README.md b/Chapter05/BONUS_2_ConvertIterableToList/README.md new file mode 100644 index 00000000..910eb521 --- /dev/null +++ b/Chapter05/BONUS_2_ConvertIterableToList/README.md @@ -0,0 +1,23 @@ +**[How To Efficiently Chunk A Java List](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/ChunkList)** + +If you prefer to read it as a blog-post containing the relevant snippets of code then check this post + +**Description:** Is a common scenario to have a big `List` and to need to chunk it in multiple smaller `List` of a given size. For example, if we want to employ a concurrent batch implementation we need to give each thread a sublist of items. Chunking a list can be done via Google Guava, `Lists.partition(List list, int size)` [method](https://guava.dev/releases/22.0/api/docs/com/google/common/collect/Lists.html#partition-java.util.List-int-) or Apache Commons Collections, `ListUtils.partition(List list, int size)` [method](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/ListUtils.html#partition(java.util.List,%20int)). But, it can be implemented in plain Java as well. This application exposes 6 ways to do it. The trade-off is between the speed of implementation and speed of execution. For example, while the implementation relying on grouping collectors is not performing very well, it is quite simple and fast to write it. + +**Key points:** +- the fastest execution is provided by `Chunk.java` class which relies on the built-in `List.subList()` method + +**Time-performance trend graphic for chunking 500, 1_000_000, 10_000_000 and 20_000_000 items in lists of 5 items:**\ +![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/ChunkList/head-to-head.png) + +----------------------------------------------------------------------------------------------------------------------- + + +
If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.
+

+
+

+
+ +----------------------------------------------------------------------------------------------------------------------- + diff --git a/Chapter05/BONUS_2_ConvertIterableToList/pom.xml b/Chapter05/BONUS_2_ConvertIterableToList/pom.xml new file mode 100644 index 00000000..1230c71e --- /dev/null +++ b/Chapter05/BONUS_2_ConvertIterableToList/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + com.app + BONUS_1_ChunkList + 1.0-SNAPSHOT + jar + + UTF-8 + 13 + 13 + + BONUS_1_ChunkList + \ No newline at end of file diff --git a/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/Converters.java b/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/Converters.java new file mode 100644 index 00000000..8fb4844d --- /dev/null +++ b/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/Converters.java @@ -0,0 +1,97 @@ +package modern.challenge; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +public class Converters { + + private Converters() { + throw new AssertionError("Cannot be instantiatied"); + } + + public static List iterableToList1(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result = new ArrayList<>(); + iterable.forEach(result::add); + + return result; + } + + public static List iterableToList2(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result = StreamSupport.stream(iterable.spliterator(), false) + .collect(Collectors.toList()); + + return result; + } + + public static List iterableToList3(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result = new ArrayList<>(); + iterable.iterator().forEachRemaining(result::add); + + return result; + } + + public static List iterableToList4(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result + = StreamSupport.stream(Spliterators. + spliteratorUnknownSize(iterable.iterator(), Spliterator.ORDERED), false) + .collect(Collectors.toList()); + + return result; + } + + public static List iterableToList5(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result = new ArrayList<>(); + for (T elem : iterable) { + result.add(elem); + } + + return result; + } + + public static List iterableToList6(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result = new ArrayList<>(); + Iterator iterator = iterable.iterator(); + while (iterator.hasNext()) { + result.add(iterator.next()); + } + + return result; + } + +} diff --git a/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/MainApplication.java b/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/MainApplication.java new file mode 100644 index 00000000..a6252ed2 --- /dev/null +++ b/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/MainApplication.java @@ -0,0 +1,19 @@ +package modern.challenge; + +import java.util.Arrays; + +public class MainApplication { + + public static void main(String[] args) { + + // let's consider the next Iterable + Iterable iterable = Arrays.asList("ana", "george", "mark"); + + System.out.println("iterableToList1(): " + Converters.iterableToList1(iterable)); + System.out.println("iterableToList2(): " + Converters.iterableToList2(iterable)); + System.out.println("iterableToList3(): " + Converters.iterableToList3(iterable)); + System.out.println("iterableToList4(): " + Converters.iterableToList4(iterable)); + System.out.println("iterableToList5(): " + Converters.iterableToList5(iterable)); + System.out.println("iterableToList6(): " + Converters.iterableToList6(iterable)); + } +} From d89310b7389aba87c019a32d861ad2738cea2df7 Mon Sep 17 00:00:00 2001 From: anghelleonard Date: Sun, 30 Aug 2020 17:09:50 +0300 Subject: [PATCH 03/14] Arrays, collections and data structures --- .../BONUS_2_ConvertIterableToList/README.md | 25 ++----------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/Chapter05/BONUS_2_ConvertIterableToList/README.md b/Chapter05/BONUS_2_ConvertIterableToList/README.md index 910eb521..5d7d1527 100644 --- a/Chapter05/BONUS_2_ConvertIterableToList/README.md +++ b/Chapter05/BONUS_2_ConvertIterableToList/README.md @@ -1,23 +1,2 @@ -**[How To Efficiently Chunk A Java List](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/ChunkList)** - -If you prefer to read it as a blog-post containing the relevant snippets of code then check this post - -**Description:** Is a common scenario to have a big `List` and to need to chunk it in multiple smaller `List` of a given size. For example, if we want to employ a concurrent batch implementation we need to give each thread a sublist of items. Chunking a list can be done via Google Guava, `Lists.partition(List list, int size)` [method](https://guava.dev/releases/22.0/api/docs/com/google/common/collect/Lists.html#partition-java.util.List-int-) or Apache Commons Collections, `ListUtils.partition(List list, int size)` [method](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/ListUtils.html#partition(java.util.List,%20int)). But, it can be implemented in plain Java as well. This application exposes 6 ways to do it. The trade-off is between the speed of implementation and speed of execution. For example, while the implementation relying on grouping collectors is not performing very well, it is quite simple and fast to write it. - -**Key points:** -- the fastest execution is provided by `Chunk.java` class which relies on the built-in `List.subList()` method - -**Time-performance trend graphic for chunking 500, 1_000_000, 10_000_000 and 20_000_000 items in lists of 5 items:**\ -![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/ChunkList/head-to-head.png) - ------------------------------------------------------------------------------------------------------------------------ - - -
If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.
-

-
-

-
- ------------------------------------------------------------------------------------------------------------------------ - +# Converting `Iterable` to `List` +Write a program that converts an `Iterable` to `List`. From bc72f7a27b65dd8e87724c2365b36ec5ab9f1ea1 Mon Sep 17 00:00:00 2001 From: AnghelLeonard Date: Fri, 30 Oct 2020 18:34:51 +0200 Subject: [PATCH 04/14] Convert List into Map> --- .../BONUS_3_ConvertListVtoMapKListV/README.md | 23 ++++++++++ .../BONUS_3_ConvertListVtoMapKListV/pom.xml | 14 ++++++ .../java/modern/challenge/Converters.java | 46 +++++++++++++++++++ .../modern/challenge/MainApplication.java | 26 +++++++++++ 4 files changed, 109 insertions(+) create mode 100644 Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md create mode 100644 Chapter05/BONUS_3_ConvertListVtoMapKListV/pom.xml create mode 100644 Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/Converters.java create mode 100644 Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/MainApplication.java diff --git a/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md b/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md new file mode 100644 index 00000000..910eb521 --- /dev/null +++ b/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md @@ -0,0 +1,23 @@ +**[How To Efficiently Chunk A Java List](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/ChunkList)** + +If you prefer to read it as a blog-post containing the relevant snippets of code then check this post + +**Description:** Is a common scenario to have a big `List` and to need to chunk it in multiple smaller `List` of a given size. For example, if we want to employ a concurrent batch implementation we need to give each thread a sublist of items. Chunking a list can be done via Google Guava, `Lists.partition(List list, int size)` [method](https://guava.dev/releases/22.0/api/docs/com/google/common/collect/Lists.html#partition-java.util.List-int-) or Apache Commons Collections, `ListUtils.partition(List list, int size)` [method](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/ListUtils.html#partition(java.util.List,%20int)). But, it can be implemented in plain Java as well. This application exposes 6 ways to do it. The trade-off is between the speed of implementation and speed of execution. For example, while the implementation relying on grouping collectors is not performing very well, it is quite simple and fast to write it. + +**Key points:** +- the fastest execution is provided by `Chunk.java` class which relies on the built-in `List.subList()` method + +**Time-performance trend graphic for chunking 500, 1_000_000, 10_000_000 and 20_000_000 items in lists of 5 items:**\ +![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/ChunkList/head-to-head.png) + +----------------------------------------------------------------------------------------------------------------------- + + +
If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.
+

+
+

+
+ +----------------------------------------------------------------------------------------------------------------------- + diff --git a/Chapter05/BONUS_3_ConvertListVtoMapKListV/pom.xml b/Chapter05/BONUS_3_ConvertListVtoMapKListV/pom.xml new file mode 100644 index 00000000..bac0d2a9 --- /dev/null +++ b/Chapter05/BONUS_3_ConvertListVtoMapKListV/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + com.app + BONUS_3_ConvertListVtoMapKListV + 1.0-SNAPSHOT + jar + + UTF-8 + 13 + 13 + + BONUS_3_ConvertListVtoMapKListV + \ No newline at end of file diff --git a/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/Converters.java b/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/Converters.java new file mode 100644 index 00000000..562f7762 --- /dev/null +++ b/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/Converters.java @@ -0,0 +1,46 @@ +package modern.challenge; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +public class Converters { + + private Converters() { + throw new AssertionError("Cannot be instantiatied"); + } + + public static Map> toMap(List list) { + + if (list == null || list.isEmpty()) { + return Collections.emptyMap(); + } + + return list.stream().collect( + Collectors.groupingBy(String::length, + HashMap::new, Collectors.toCollection(ArrayList::new)) + ); + } + + @SuppressWarnings("unchecked") + public static , M extends Map> M toMap( + List list, Function c, Supplier ms, Supplier cs) { + + if (list == null || c == null || ms == null || cs == null + || list.isEmpty()) { + + throw new IllegalArgumentException("Non of the arguments can be null or empty"); + } + + return list.stream().collect( + Collectors.groupingBy(c, ms, Collectors.toCollection(cs)) + ); + } + +} \ No newline at end of file diff --git a/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/MainApplication.java b/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/MainApplication.java new file mode 100644 index 00000000..e8f5dc42 --- /dev/null +++ b/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/MainApplication.java @@ -0,0 +1,26 @@ +package modern.challenge; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; + +public class MainApplication { + + public static void main(String[] args) { + + /* Convert List into Map> */ + // consider this list + List names + = List.of("joana", "mark", "adela", "leo", "stan", "marius", "kely"); + + HashMap> result1 + = Converters.toMap(names, String::length, HashMap::new, ArrayList::new); + System.out.println("HashMap>: " + result1); + + LinkedHashMap> result2 + = Converters.toMap(names, String::length, LinkedHashMap::new, LinkedList::new); + System.out.println("LinkedHashMap>: " + result2); + } +} From a5ae945eb5b18c6392435404a531f9ebab079319 Mon Sep 17 00:00:00 2001 From: anghelleonard Date: Fri, 30 Oct 2020 18:36:51 +0200 Subject: [PATCH 05/14] Convert List into Map> --- .../BONUS_3_ConvertListVtoMapKListV/README.md | 24 ++----------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md b/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md index 910eb521..4c05b4bb 100644 --- a/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md +++ b/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md @@ -1,23 +1,3 @@ -**[How To Efficiently Chunk A Java List](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/ChunkList)** - -If you prefer to read it as a blog-post containing the relevant snippets of code then check this post - -**Description:** Is a common scenario to have a big `List` and to need to chunk it in multiple smaller `List` of a given size. For example, if we want to employ a concurrent batch implementation we need to give each thread a sublist of items. Chunking a list can be done via Google Guava, `Lists.partition(List list, int size)` [method](https://guava.dev/releases/22.0/api/docs/com/google/common/collect/Lists.html#partition-java.util.List-int-) or Apache Commons Collections, `ListUtils.partition(List list, int size)` [method](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/ListUtils.html#partition(java.util.List,%20int)). But, it can be implemented in plain Java as well. This application exposes 6 ways to do it. The trade-off is between the speed of implementation and speed of execution. For example, while the implementation relying on grouping collectors is not performing very well, it is quite simple and fast to write it. - -**Key points:** -- the fastest execution is provided by `Chunk.java` class which relies on the built-in `List.subList()` method - -**Time-performance trend graphic for chunking 500, 1_000_000, 10_000_000 and 20_000_000 items in lists of 5 items:**\ -![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/ChunkList/head-to-head.png) - ------------------------------------------------------------------------------------------------------------------------ - - -
If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.
-

-
-

-
- ------------------------------------------------------------------------------------------------------------------------ +**[Convert `List` into `Map>`]** +Write a program that converts `List` into `Map>`. From d0dbb28565becce9968dd39bd70211d67758e989 Mon Sep 17 00:00:00 2001 From: anghelleonard Date: Fri, 30 Oct 2020 18:37:29 +0200 Subject: [PATCH 06/14] Convert List into Map> --- Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md b/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md index 4c05b4bb..9e830873 100644 --- a/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md +++ b/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md @@ -1,3 +1,3 @@ -**[Convert `List` into `Map>`]** +# Convert `List` into `Map>` Write a program that converts `List` into `Map>`. From e4b16875acf67091dd0db34a2e33a953f370b3c0 Mon Sep 17 00:00:00 2001 From: AnghelLeonard Date: Fri, 30 Oct 2020 18:38:56 +0200 Subject: [PATCH 07/14] Arrays, collections and data structures --- Chapter05/BONUS_2_ConvertIterableToList/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Chapter05/BONUS_2_ConvertIterableToList/pom.xml b/Chapter05/BONUS_2_ConvertIterableToList/pom.xml index 1230c71e..ef363031 100644 --- a/Chapter05/BONUS_2_ConvertIterableToList/pom.xml +++ b/Chapter05/BONUS_2_ConvertIterableToList/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.app - BONUS_1_ChunkList + BONUS_2_ConvertIterableToList 1.0-SNAPSHOT jar @@ -10,5 +10,5 @@ 13 13 - BONUS_1_ChunkList + BONUS_2_ConvertIterableToList \ No newline at end of file From 21a1169cd2742559b586a972fa0d31e6f106cae2 Mon Sep 17 00:00:00 2001 From: anghelleonard Date: Fri, 30 Oct 2020 18:39:37 +0200 Subject: [PATCH 08/14] Arrays, collections and data structures --- Chapter05/BONUS_2_ConvertIterableToList/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter05/BONUS_2_ConvertIterableToList/README.md b/Chapter05/BONUS_2_ConvertIterableToList/README.md index 5d7d1527..cf84b634 100644 --- a/Chapter05/BONUS_2_ConvertIterableToList/README.md +++ b/Chapter05/BONUS_2_ConvertIterableToList/README.md @@ -1,2 +1,2 @@ # Converting `Iterable` to `List` -Write a program that converts an `Iterable` to `List`. +Write a program that converts an `Iterable` to `List`. From deeece53d2a34562eebf4394e2c8efe0c2480bd4 Mon Sep 17 00:00:00 2001 From: Packt-ITService <62882280+Packt-ITService@users.noreply.github.com> Date: Mon, 14 Dec 2020 16:41:17 +0000 Subject: [PATCH 09/14] add $5 campaign --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 0419ea5f..dd423e8f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +## $5 Tech Unlocked 2021! +[Buy and download this Book for only $5 on PacktPub.com](https://www.packtpub.com/product/java-coding-problems/9781789801415) +----- +*If you have read this book, please leave a review on [Amazon.com](https://www.amazon.com/gp/product/1789801419). Potential readers can then use your unbiased opinion to help them make purchase decisions. Thank you. The $5 campaign runs from __December 15th 2020__ to __January 13th 2021.__* + # Java Coding Problems Java Coding Problems From 6fce08373b63b7e4a657c57c4b8b6904fe41b350 Mon Sep 17 00:00:00 2001 From: Packt-ITService <62882280+Packt-ITService@users.noreply.github.com> Date: Wed, 16 Dec 2020 06:08:40 +0000 Subject: [PATCH 10/14] add $5 campaign From 469355afd84a0229e781d7d13b3ffd336ed99417 Mon Sep 17 00:00:00 2001 From: anghelleonard Date: Thu, 31 Dec 2020 16:42:55 +0200 Subject: [PATCH 11/14] Convert between Date and YearMonth --- .../BONUS_1_ConvertYearMonthToDate/README.md | 2 ++ .../BONUS_1_ConvertYearMonthToDate/pom.xml | 14 ++++++++ .../java/modern/challenge/Converters.java | 33 +++++++++++++++++++ .../modern/challenge/MainApplication.java | 13 ++++++++ 4 files changed, 62 insertions(+) create mode 100644 Chapter03/BONUS_1_ConvertYearMonthToDate/README.md create mode 100644 Chapter03/BONUS_1_ConvertYearMonthToDate/pom.xml create mode 100644 Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/Converters.java create mode 100644 Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/MainApplication.java diff --git a/Chapter03/BONUS_1_ConvertYearMonthToDate/README.md b/Chapter03/BONUS_1_ConvertYearMonthToDate/README.md new file mode 100644 index 00000000..cf84b634 --- /dev/null +++ b/Chapter03/BONUS_1_ConvertYearMonthToDate/README.md @@ -0,0 +1,2 @@ +# Converting `Iterable` to `List` +Write a program that converts an `Iterable` to `List`. diff --git a/Chapter03/BONUS_1_ConvertYearMonthToDate/pom.xml b/Chapter03/BONUS_1_ConvertYearMonthToDate/pom.xml new file mode 100644 index 00000000..fd4c512d --- /dev/null +++ b/Chapter03/BONUS_1_ConvertYearMonthToDate/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + com.app + BONUS_1_ConvertYearMonthToDate + 1.0-SNAPSHOT + jar + + UTF-8 + 13 + 13 + + BONUS_1_ConvertYearMonthToDate + \ No newline at end of file diff --git a/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/Converters.java b/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/Converters.java new file mode 100644 index 00000000..29ccc9c0 --- /dev/null +++ b/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/Converters.java @@ -0,0 +1,33 @@ +package modern.challenge; + +import java.time.YearMonth; +import java.time.ZoneId; +import java.util.Date; + +public class Converters { + + private Converters() { + throw new AssertionError("Cannot be instantiatied"); + } + + public static YearMonth toYearMonth(Date date) { + + if (date == null) { + throw new IllegalArgumentException("The given date cannot be null"); + } + + return YearMonth.from(date.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate()); + } + + public static Date toDate(YearMonth ym) { + + if (ym == null) { + throw new IllegalArgumentException("The given year-month cannot be null"); + } + + return Date.from(ym.atDay(1) + .atStartOfDay(ZoneId.systemDefault()).toInstant()); + } +} diff --git a/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/MainApplication.java b/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/MainApplication.java new file mode 100644 index 00000000..f4b019e3 --- /dev/null +++ b/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/MainApplication.java @@ -0,0 +1,13 @@ +package modern.challenge; + +import java.time.YearMonth; +import java.util.Date; + +public class MainApplication { + + public static void main(String[] args) { + + System.out.println("Date to YearMonth: " + Converters.toYearMonth(new Date())); + System.out.println("YearMonth to Date: " + Converters.toDate(YearMonth.now())); + } +} From 0f4eb9f9681f047dd30e228d536c0fcb53ee6af1 Mon Sep 17 00:00:00 2001 From: anghelleonard Date: Thu, 31 Dec 2020 16:44:22 +0200 Subject: [PATCH 12/14] Convert between Date and YearMonth --- Chapter03/BONUS_1_ConvertYearMonthToDate/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Chapter03/BONUS_1_ConvertYearMonthToDate/README.md b/Chapter03/BONUS_1_ConvertYearMonthToDate/README.md index cf84b634..d654d971 100644 --- a/Chapter03/BONUS_1_ConvertYearMonthToDate/README.md +++ b/Chapter03/BONUS_1_ConvertYearMonthToDate/README.md @@ -1,2 +1,2 @@ -# Converting `Iterable` to `List` -Write a program that converts an `Iterable` to `List`. +# Converting `Date` to `YearMonth` +Write a program that converts an `Date` to `YearMonth` and vice-versa. From 66bc05a5c1ba7419b22e61473a90f00be3d2ced2 Mon Sep 17 00:00:00 2001 From: anghelleonard Date: Thu, 31 Dec 2020 19:03:37 +0200 Subject: [PATCH 13/14] Get current project root directory --- .../README.md | 2 ++ .../pom.xml | 14 +++++++++++ .../modern/challenge/MainApplication.java | 10 ++++++++ .../src/main/java/modern/challenge/Roots.java | 23 +++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md create mode 100644 Chapter06/BONUS_1_GetCurrentProjectRootDirectory/pom.xml create mode 100644 Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/MainApplication.java create mode 100644 Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/Roots.java diff --git a/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md new file mode 100644 index 00000000..d654d971 --- /dev/null +++ b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md @@ -0,0 +1,2 @@ +# Converting `Date` to `YearMonth` +Write a program that converts an `Date` to `YearMonth` and vice-versa. diff --git a/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/pom.xml b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/pom.xml new file mode 100644 index 00000000..db2d1fc8 --- /dev/null +++ b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + com.app + BONUS_1_GetCurrentProjectRootDirectory + 1.0-SNAPSHOT + jar + + UTF-8 + 13 + 13 + + BONUS_1_GetCurrentProjectRootDirectory + \ No newline at end of file diff --git a/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/MainApplication.java b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/MainApplication.java new file mode 100644 index 00000000..dca8ec4e --- /dev/null +++ b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/MainApplication.java @@ -0,0 +1,10 @@ +package modern.challenge; + +public class MainApplication { + + public static void main(String[] args) { + + System.out.println("The root directory of this project is:\n" + + Roots.getCurrentProjectRootDirectory()); + } +} diff --git a/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/Roots.java b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/Roots.java new file mode 100644 index 00000000..82e892eb --- /dev/null +++ b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/Roots.java @@ -0,0 +1,23 @@ +package modern.challenge; + +import java.nio.file.Path; +import java.nio.file.Paths; + +public class Roots { + + private Roots() { + throw new AssertionError("Cannot be instantiatied"); + } + + public static String getCurrentProjectRootDirectory() { + + String userDirectory = System.getProperty("user.dir"); + Path rootDirectory = Paths.get(".").normalize().toAbsolutePath(); + + if (rootDirectory.startsWith(userDirectory)) { + return rootDirectory.toString(); + } else { + throw new RuntimeException("Cannot find the current project root directory"); + } + } +} From 0d378eed2cf79e836bdba0d1b7ac58832870a667 Mon Sep 17 00:00:00 2001 From: anghelleonard Date: Thu, 31 Dec 2020 19:04:41 +0200 Subject: [PATCH 14/14] Get current project root directory --- Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md index d654d971..ae04a606 100644 --- a/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md +++ b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md @@ -1,2 +1,2 @@ -# Converting `Date` to `YearMonth` -Write a program that converts an `Date` to `YearMonth` and vice-versa. +# Current directory +Write a program that return the current project root directory