From 166723cf62ee1c8a133636f4e86e835aa6fd7844 Mon Sep 17 00:00:00 2001 From: catalin-burcea Date: Tue, 26 Nov 2019 12:01:07 +0200 Subject: [PATCH] [BAEL-16531] - Move all the existing articles on "optional" under java-optional --- core-java-modules/core-java-11/README.md | 1 - .../main/resources/META-INF/persistence.xml | 8 +- .../core-java-9-improvements/README.md | 1 - .../com/baeldung/java9/Java9OptionalTest.java | 85 ----------------- core-java-modules/core-java-9/README.md | 6 +- .../com/baeldung/optionals/OptionalsTest.java | 51 ---------- .../core-java-exceptions/README.md | 1 - .../core-java-optional/README.md | 13 ++- core-java-modules/core-java-optional/pom.xml | 20 ++++ .../{ => orelse}/OrElseAndOrElseGet.java | 2 +- .../OrElseAndOrElseGetBenchmarkRunner.java | 2 +- .../HandleOptionalTypeExample.java | 2 +- .../OptionalToJsonExample.java | 2 +- .../PersistOptionalTypeExample.java | 6 +- .../PersistOptionalTypeExample2.java | 4 +- .../PersistUserExample.java | 4 +- .../SerializeOptionalTypeExample.java | 2 +- .../User.java | 2 +- .../UserOptional.java | 2 +- .../UserOptionalField.java | 2 +- .../orelseoptional/OptionalUtils.java} | 18 ++-- .../EmptyStringToEmptyOptionalUnitTest.java | 15 +-- .../Java9OptionalsStreamUnitTest.java | 22 +++-- .../java9additions/Java9OptionalUnitTest.java | 95 +++++++++++++++++++ .../optional/OptionalChainingUnitTest.java | 2 +- .../optional/OptionalUnitTest.java | 6 +- .../orelse}/OrElseAndOrElseGetUnitTest.java | 4 +- .../OrElseOptionalUnitTest.java | 52 ++++++++++ core-java-modules/core-java/README.md | 1 - 29 files changed, 230 insertions(+), 201 deletions(-) delete mode 100644 core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/Java9OptionalTest.java delete mode 100644 core-java-modules/core-java-9/src/test/java/com/baeldung/optionals/OptionalsTest.java rename core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/{ => orelse}/OrElseAndOrElseGet.java (96%) rename core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/{ => orelse}/OrElseAndOrElseGetBenchmarkRunner.java (96%) rename core-java-modules/core-java-optional/src/main/java/com/baeldung/{optionalReturnType => optionalreturntype}/HandleOptionalTypeExample.java (96%) rename core-java-modules/core-java-optional/src/main/java/com/baeldung/{optionalReturnType => optionalreturntype}/OptionalToJsonExample.java (93%) rename core-java-modules/core-java-optional/src/main/java/com/baeldung/{optionalReturnType => optionalreturntype}/PersistOptionalTypeExample.java (82%) rename core-java-modules/core-java-optional/src/main/java/com/baeldung/{optionalReturnType => optionalreturntype}/PersistOptionalTypeExample2.java (85%) rename core-java-modules/core-java-optional/src/main/java/com/baeldung/{optionalReturnType => optionalreturntype}/PersistUserExample.java (84%) rename core-java-modules/core-java-optional/src/main/java/com/baeldung/{optionalReturnType => optionalreturntype}/SerializeOptionalTypeExample.java (96%) rename core-java-modules/core-java-optional/src/main/java/com/baeldung/{optionalReturnType => optionalreturntype}/User.java (92%) rename core-java-modules/core-java-optional/src/main/java/com/baeldung/{optionalReturnType => optionalreturntype}/UserOptional.java (94%) rename core-java-modules/core-java-optional/src/main/java/com/baeldung/{optionalReturnType => optionalreturntype}/UserOptionalField.java (93%) rename core-java-modules/{core-java-9/src/main/java/com/baeldung/optionals/Optionals.java => core-java-optional/src/main/java/com/baeldung/orelseoptional/OptionalUtils.java} (54%) rename core-java-modules/{core-java-11/src/test/java/com/baeldung => core-java-optional/src/test/java/com/baeldung/emptystringoptional}/EmptyStringToEmptyOptionalUnitTest.java (65%) rename core-java-modules/{core-java-9/src/test/java/com/baeldung/java9 => core-java-optional/src/test/java/com/baeldung/filterstream}/Java9OptionalsStreamUnitTest.java (76%) create mode 100644 core-java-modules/core-java-optional/src/test/java/com/baeldung/java9additions/Java9OptionalUnitTest.java rename core-java-modules/core-java-optional/src/test/java/com/baeldung/{java8 => }/optional/OptionalChainingUnitTest.java (98%) rename core-java-modules/core-java-optional/src/test/java/com/baeldung/{java8 => }/optional/OptionalUnitTest.java (97%) rename core-java-modules/core-java-optional/src/test/java/com/baeldung/{java8/optional => optional/orelse}/OrElseAndOrElseGetUnitTest.java (94%) create mode 100644 core-java-modules/core-java-optional/src/test/java/com/baeldung/orelseoptional/OrElseOptionalUnitTest.java diff --git a/core-java-modules/core-java-11/README.md b/core-java-modules/core-java-11/README.md index 514f24a4ae..93a2c6faf5 100644 --- a/core-java-modules/core-java-11/README.md +++ b/core-java-modules/core-java-11/README.md @@ -12,4 +12,3 @@ This module contains articles about Java 11 core features - [An Introduction to Epsilon GC: A No-Op Experimental Garbage Collector](https://www.baeldung.com/jvm-epsilon-gc-garbage-collector) - [Guide to jlink](https://www.baeldung.com/jlink) - [Negate a Predicate Method Reference with Java 11](https://www.baeldung.com/java-negate-predicate-method-reference) -- [Transforming an Empty String into an Empty Optional](https://www.baeldung.com/java-empty-string-to-empty-optional) diff --git a/core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml b/core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml index e8cd723ec2..c412ca0839 100644 --- a/core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml +++ b/core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml @@ -5,14 +5,14 @@ version="2.0"> Persist Optional Return Type Demo org.hibernate.jpa.HibernatePersistenceProvider - com.baeldung.optionalReturnType.User - com.baeldung.optionalReturnType.UserOptional + com.baeldung.optionalreturntype.User + com.baeldung.optionalreturntype.UserOptional true diff --git a/core-java-modules/core-java-9-improvements/README.md b/core-java-modules/core-java-9-improvements/README.md index b0baccba7f..5864170e85 100644 --- a/core-java-modules/core-java-9-improvements/README.md +++ b/core-java-modules/core-java-9-improvements/README.md @@ -5,7 +5,6 @@ This module contains articles about the improvements to core Java features intro ### Relevant Articles: - [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors) -- [Java 9 Optional API Additions](https://www.baeldung.com/java-9-optional) - [Java 9 Convenience Factory Methods for Collections](https://www.baeldung.com/java-9-collections-factory-methods) - [Java 9 Stream API Improvements](https://www.baeldung.com/java-9-stream-api) - [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new) diff --git a/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/Java9OptionalTest.java b/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/Java9OptionalTest.java deleted file mode 100644 index cd0efb028d..0000000000 --- a/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/Java9OptionalTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.baeldung.java9; - -public class Java9OptionalTest { - @Test - public void givenOptionalOfSome_whenToStream_thenShouldTreatItAsOneElementStream() { - //given - Optional value = Optional.of("a"); - - //when - List collect = value.stream().map(String::toUpperCase).collect(Collectors.toList()); - - //then - assertThat(collect).hasSameElementsAs(List.of("A")); - } - - @Test - public void givenOptionalOfNone_whenToStream_thenShouldTreatItAsZeroElementStream() { - //given - Optional value = Optional.empty(); - - //when - List collect = value.stream().map(String::toUpperCase).collect(Collectors.toList()); - - //then - assertThat(collect).isEmpty(); - } - - @Test - public void givenOptional_whenPresent_thenShouldExecuteProperCallback() { - //given - Optional value = Optional.of("properValue"); - AtomicInteger successCounter = new AtomicInteger(0); - AtomicInteger onEmptyOptionalCounter = new AtomicInteger(0); - - //when - value.ifPresentOrElse((v) -> successCounter.incrementAndGet(), onEmptyOptionalCounter::incrementAndGet); - - //then - assertThat(successCounter.get()).isEqualTo(1); - assertThat(onEmptyOptionalCounter.get()).isEqualTo(0); - } - - @Test - public void givenOptional_whenNotPresent_thenShouldExecuteProperCallback() { - //given - Optional value = Optional.empty(); - AtomicInteger successCounter = new AtomicInteger(0); - AtomicInteger onEmptyOptionalCounter = new AtomicInteger(0); - - //when - value.ifPresentOrElse((v) -> successCounter.incrementAndGet(), onEmptyOptionalCounter::incrementAndGet); - - //then - assertThat(successCounter.get()).isEqualTo(0); - assertThat(onEmptyOptionalCounter.get()).isEqualTo(1); - } - - @Test - public void givenOptional_whenPresent_thenShouldTakeAValueFromIt() { - //given - String expected = "properValue"; - Optional value = Optional.of(expected); - Optional defaultValue = Optional.of("default"); - - //when - Optional result = value.or(() -> defaultValue); - - //then - assertThat(result.get()).isEqualTo(expected); - } - - @Test - public void givenOptional_whenEmpty_thenShouldTakeAValueFromOr() { - //given - String defaultString = "default"; - Optional value = Optional.empty(); - Optional defaultValue = Optional.of(defaultString); - - //when - Optional result = value.or(() -> defaultValue); - - //then - assertThat(result.get()).isEqualTo(defaultString); - } -} \ No newline at end of file diff --git a/core-java-modules/core-java-9/README.md b/core-java-modules/core-java-9/README.md index 94f480db67..e2bea5f7e2 100644 --- a/core-java-modules/core-java-9/README.md +++ b/core-java-modules/core-java-9/README.md @@ -4,13 +4,11 @@ This module contains articles about Java 9 core features ### Relevant Articles: -- [Method Handles in Java](http://www.baeldung.com/java-method-handles) -- [Introduction to Chronicle Queue](http://www.baeldung.com/java-chronicle-queue) -- [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional) +- [Method Handles in Java](https://www.baeldung.com/java-method-handles) +- [Introduction to Chronicle Queue](https://www.baeldung.com/java-chronicle-queue) - [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range) - [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap) - [Immutable Set in Java](https://www.baeldung.com/java-immutable-set) -- [Filtering a Stream of Optionals in Java](https://www.baeldung.com/java-filter-stream-of-optional) Note: also contains part of the code for the article [How to Filter a Collection in Java](https://www.baeldung.com/java-collection-filtering). diff --git a/core-java-modules/core-java-9/src/test/java/com/baeldung/optionals/OptionalsTest.java b/core-java-modules/core-java-9/src/test/java/com/baeldung/optionals/OptionalsTest.java deleted file mode 100644 index 4e5f94c0db..0000000000 --- a/core-java-modules/core-java-9/src/test/java/com/baeldung/optionals/OptionalsTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.optionals; - -import static org.junit.Assert.assertEquals; - -import java.util.Optional; - -import org.junit.Test; - -public class OptionalsTest { - - @Test - public void givenOptional_whenEmptyValue_thenCustomMessage() { - assertEquals(Optional.of("Name not provided"), Optionals.getName(Optional.ofNullable(null))); - } - - @Test - public void givenOptional_whenValue_thenOptional() { - String name = "Filan Fisteku"; - Optional optionalString = Optional.ofNullable(name); - assertEquals(optionalString, Optionals.getName(optionalString)); - } - - @Test - public void givenOptional_whenValue_thenOptionalGeneralMethod() { - String name = "Filan Fisteku"; - String missingOptional = "Name not provided"; - Optional optionalString = Optional.ofNullable(name); - Optional fallbackOptionalString = Optional.ofNullable(missingOptional); - assertEquals(optionalString, Optionals.or(optionalString, fallbackOptionalString)); - } - - @Test - public void givenEmptyOptional_whenValue_thenOptionalGeneralMethod() { - String missingOptional = "Name not provided"; - Optional optionalString = Optional.empty(); - Optional fallbackOptionalString = Optional.ofNullable(missingOptional); - assertEquals(fallbackOptionalString, Optionals.or(optionalString, fallbackOptionalString)); - } - - @Test - public void givenGuavaOptional_whenInvoke_thenOptional() { - String name = "Filan Fisteku"; - com.google.common.base.Optional stringOptional = com.google.common.base.Optional.of(name); - assertEquals(stringOptional, Optionals.getOptionalGuavaName(stringOptional)); - } - - @Test - public void givenGuavaOptional_whenNull_thenDefaultText() { - assertEquals(com.google.common.base.Optional.of("Name not provided"), Optionals.getOptionalGuavaName(com.google.common.base.Optional.fromNullable(null))); - } -} \ No newline at end of file diff --git a/core-java-modules/core-java-exceptions/README.md b/core-java-modules/core-java-exceptions/README.md index 429474003c..63a3eb19a1 100644 --- a/core-java-modules/core-java-exceptions/README.md +++ b/core-java-modules/core-java-exceptions/README.md @@ -15,6 +15,5 @@ This module contains articles about core java exceptions - [Java – Try with Resources](https://www.baeldung.com/java-try-with-resources) - [Java Global Exception Handler](https://www.baeldung.com/java-global-exception-handler) - [Common Java Exceptions](https://www.baeldung.com/java-common-exceptions) -- [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception) - [How to Find an Exception’s Root Cause in Java](https://www.baeldung.com/java-exception-root-cause) - [Is It a Bad Practice to Catch Throwable?](https://www.baeldung.com/java-catch-throwable-bad-practice) diff --git a/core-java-modules/core-java-optional/README.md b/core-java-modules/core-java-optional/README.md index 22ac8736fa..d9d2fe813b 100644 --- a/core-java-modules/core-java-optional/README.md +++ b/core-java-modules/core-java-optional/README.md @@ -1,8 +1,13 @@ -========= +## Java Optional -## Core Java Optional +This module contains articles about Java Optional. ### Relevant Articles: - [Java Optional as Return Type](https://www.baeldung.com/java-optional-return) -- [Guide To Java 8 Optional](https://www.baeldung.com/java-optional) -- [Java Optional – orElse() vs orElseGet()](https://www.baeldung.com/java-optional-or-else-vs-or-else-get) \ No newline at end of file +- [Guide to Java 8 Optional](https://www.baeldung.com/java-optional) +- [Java Optional – orElse() vs orElseGet()](https://www.baeldung.com/java-optional-or-else-vs-or-else-get) +- [Transforming an Empty String into an Empty Optional](https://www.baeldung.com/java-empty-string-to-empty-optional) +- [Filtering a Stream of Optionals in Java](https://www.baeldung.com/java-filter-stream-of-optional) +- [Java 9 Optional API Additions](https://www.baeldung.com/java-9-optional) +- [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception) +- [Optional orElse Optional](https://www.baeldung.com/java-optional-or-else-optional) diff --git a/core-java-modules/core-java-optional/pom.xml b/core-java-modules/core-java-optional/pom.xml index 38d7565282..3c556012da 100644 --- a/core-java-modules/core-java-optional/pom.xml +++ b/core-java-modules/core-java-optional/pom.xml @@ -42,11 +42,31 @@ jmh-generator-bytecode ${jmh-generator.version} + + com.google.guava + guava + ${guava.version} + + + io.rest-assured + json-path + ${rest-assured.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + 5.4.0.Final 1.19 1.19 + 27.1-jre + 3.10.0 + 3.1.1 \ No newline at end of file diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/OrElseAndOrElseGet.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/orelse/OrElseAndOrElseGet.java similarity index 96% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/OrElseAndOrElseGet.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/orelse/OrElseAndOrElseGet.java index d4d07291d5..45b47c745e 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/OrElseAndOrElseGet.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/orelse/OrElseAndOrElseGet.java @@ -1,4 +1,4 @@ -package com.baeldung.optional; +package com.baeldung.optional.orelse; import java.util.Arrays; import java.util.List; diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/OrElseAndOrElseGetBenchmarkRunner.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/orelse/OrElseAndOrElseGetBenchmarkRunner.java similarity index 96% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/OrElseAndOrElseGetBenchmarkRunner.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/orelse/OrElseAndOrElseGetBenchmarkRunner.java index 8bf0b0b25d..1cbb8803d0 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/OrElseAndOrElseGetBenchmarkRunner.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optional/orelse/OrElseAndOrElseGetBenchmarkRunner.java @@ -1,4 +1,4 @@ -package com.baeldung.optional; +package com.baeldung.optional.orelse; import java.io.IOException; import java.util.concurrent.TimeUnit; diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/HandleOptionalTypeExample.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/HandleOptionalTypeExample.java similarity index 96% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/HandleOptionalTypeExample.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/HandleOptionalTypeExample.java index c472bab077..a280957a3c 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/HandleOptionalTypeExample.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/HandleOptionalTypeExample.java @@ -1,4 +1,4 @@ -package com.baeldung.optionalReturnType; +package com.baeldung.optionalreturntype; import java.util.HashMap; import java.util.Map; diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/OptionalToJsonExample.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/OptionalToJsonExample.java similarity index 93% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/OptionalToJsonExample.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/OptionalToJsonExample.java index b44a35fae1..1852ecbf65 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/OptionalToJsonExample.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/OptionalToJsonExample.java @@ -1,4 +1,4 @@ -package com.baeldung.optionalReturnType; +package com.baeldung.optionalreturntype; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/PersistOptionalTypeExample.java similarity index 82% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/PersistOptionalTypeExample.java index 85c96b9bc3..a0c182bb7d 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/PersistOptionalTypeExample.java @@ -1,4 +1,4 @@ -package com.baeldung.optionalReturnType; +package com.baeldung.optionalreturntype; import java.util.Optional; @@ -7,13 +7,13 @@ import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class PersistOptionalTypeExample { - static String persistenceUnit = "com.baeldung.optionalReturnType"; + static String persistenceUnit = "com.baeldung.optionalreturntype"; static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit); static EntityManager entityManager = emf.createEntityManager(); // to run this app, uncomment the follow line in META-INF/persistence.xml - // com.baeldung.optionalReturnType.UserOptionalField + // com.baeldung.optionalreturntype.UserOptionalField public static void main(String[] args) { UserOptionalField user1 = new UserOptionalField(); user1.setUserId(1l); diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample2.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/PersistOptionalTypeExample2.java similarity index 85% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample2.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/PersistOptionalTypeExample2.java index 3114e7cefb..5671266b68 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample2.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/PersistOptionalTypeExample2.java @@ -1,11 +1,11 @@ -package com.baeldung.optionalReturnType; +package com.baeldung.optionalreturntype; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class PersistOptionalTypeExample2 { - static String persistenceUnit = "com.baeldung.optionalReturnType"; + static String persistenceUnit = "com.baeldung.optionalreturntype"; static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit); static EntityManager em = emf.createEntityManager(); diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/PersistUserExample.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/PersistUserExample.java similarity index 84% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/PersistUserExample.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/PersistUserExample.java index f1284958e7..e26268d8e6 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/PersistUserExample.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/PersistUserExample.java @@ -1,11 +1,11 @@ -package com.baeldung.optionalReturnType; +package com.baeldung.optionalreturntype; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class PersistUserExample { - static String persistenceUnit = "com.baeldung.optionalReturnType"; + static String persistenceUnit = "com.baeldung.optionalreturntype"; static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit); static EntityManager em = emf.createEntityManager(); diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/SerializeOptionalTypeExample.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/SerializeOptionalTypeExample.java similarity index 96% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/SerializeOptionalTypeExample.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/SerializeOptionalTypeExample.java index d67337ad98..8566e49a21 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/SerializeOptionalTypeExample.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/SerializeOptionalTypeExample.java @@ -1,4 +1,4 @@ -package com.baeldung.optionalReturnType; +package com.baeldung.optionalreturntype; import java.io.FileOutputStream; import java.io.IOException; diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/User.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/User.java similarity index 92% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/User.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/User.java index 7aa11d78cb..b754b41787 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/User.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/User.java @@ -1,4 +1,4 @@ -package com.baeldung.optionalReturnType; +package com.baeldung.optionalreturntype; import java.io.Serializable; diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/UserOptional.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/UserOptional.java similarity index 94% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/UserOptional.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/UserOptional.java index 0138a84ab9..081553dbe5 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/UserOptional.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/UserOptional.java @@ -1,4 +1,4 @@ -package com.baeldung.optionalReturnType; +package com.baeldung.optionalreturntype; import java.io.Serializable; import java.util.Optional; diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/UserOptionalField.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/UserOptionalField.java similarity index 93% rename from core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/UserOptionalField.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/UserOptionalField.java index c02430b1ba..e8c16bfa02 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalReturnType/UserOptionalField.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionalreturntype/UserOptionalField.java @@ -1,4 +1,4 @@ -package com.baeldung.optionalReturnType; +package com.baeldung.optionalreturntype; import java.io.Serializable; import java.util.Optional; diff --git a/core-java-modules/core-java-9/src/main/java/com/baeldung/optionals/Optionals.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/orelseoptional/OptionalUtils.java similarity index 54% rename from core-java-modules/core-java-9/src/main/java/com/baeldung/optionals/Optionals.java rename to core-java-modules/core-java-optional/src/main/java/com/baeldung/orelseoptional/OptionalUtils.java index 5efa607f94..f2bf1637dc 100644 --- a/core-java-modules/core-java-9/src/main/java/com/baeldung/optionals/Optionals.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/orelseoptional/OptionalUtils.java @@ -1,20 +1,20 @@ -package com.baeldung.optionals; +package com.baeldung.orelseoptional; import java.util.Optional; -public class Optionals { +public class OptionalUtils { public static Optional or(Optional optional, Optional fallback) { return optional.isPresent() ? optional : fallback; } - public static Optional getName(Optional name) { - return name.or(() -> getCustomMessage()); - } - - public static com.google.common.base.Optional getOptionalGuavaName(com.google.common.base.Optional name) { - return name.or(getCustomMessageGuava()); - } +// public static Optional getName(Optional name) { +// return name.or(() -> getCustomMessage()); +// } +// +// public static com.google.common.base.Optional getOptionalGuavaName(com.google.common.base.Optional name) { +// return name.or(getCustomMessageGuava()); +// } private static Optional getCustomMessage() { return Optional.of("Name not provided"); diff --git a/core-java-modules/core-java-11/src/test/java/com/baeldung/EmptyStringToEmptyOptionalUnitTest.java b/core-java-modules/core-java-optional/src/test/java/com/baeldung/emptystringoptional/EmptyStringToEmptyOptionalUnitTest.java similarity index 65% rename from core-java-modules/core-java-11/src/test/java/com/baeldung/EmptyStringToEmptyOptionalUnitTest.java rename to core-java-modules/core-java-optional/src/test/java/com/baeldung/emptystringoptional/EmptyStringToEmptyOptionalUnitTest.java index cc429209d4..22e11c9a0b 100644 --- a/core-java-modules/core-java-11/src/test/java/com/baeldung/EmptyStringToEmptyOptionalUnitTest.java +++ b/core-java-modules/core-java-optional/src/test/java/com/baeldung/emptystringoptional/EmptyStringToEmptyOptionalUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.emptystringoptional; import com.google.common.base.Strings; import org.junit.Assert; @@ -16,12 +16,13 @@ public class EmptyStringToEmptyOptionalUnitTest { Assert.assertFalse(opt.isPresent()); } - @Test - public void givenEmptyString_whenFilteringOnOptionalInJava11_thenEmptyOptionalIsReturned() { - String str = ""; - Optional opt = Optional.ofNullable(str).filter(Predicate.not(String::isEmpty)); - Assert.assertFalse(opt.isPresent()); - } +// Uncomment code when code base is compatible with Java 11 +// @Test +// public void givenEmptyString_whenFilteringOnOptionalInJava11_thenEmptyOptionalIsReturned() { +// String str = ""; +// Optional opt = Optional.ofNullable(str).filter(Predicate.not(String::isEmpty)); +// Assert.assertFalse(opt.isPresent()); +// } @Test public void givenEmptyString_whenPassingResultOfEmptyToNullToOfNullable_thenEmptyOptionalIsReturned() { diff --git a/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/Java9OptionalsStreamUnitTest.java b/core-java-modules/core-java-optional/src/test/java/com/baeldung/filterstream/Java9OptionalsStreamUnitTest.java similarity index 76% rename from core-java-modules/core-java-9/src/test/java/com/baeldung/java9/Java9OptionalsStreamUnitTest.java rename to core-java-modules/core-java-optional/src/test/java/com/baeldung/filterstream/Java9OptionalsStreamUnitTest.java index 7a28a4b977..5341813164 100644 --- a/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/Java9OptionalsStreamUnitTest.java +++ b/core-java-modules/core-java-optional/src/test/java/com/baeldung/filterstream/Java9OptionalsStreamUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java9; +package com.baeldung.filterstream; import static org.junit.Assert.assertEquals; @@ -47,15 +47,17 @@ public class Java9OptionalsStreamUnitTest { assertEquals("bar", filteredList.get(1)); } - @Test - public void filterOutPresentOptionalsWithJava9() { - assertEquals(4, listOfOptionals.size()); - List filteredList = listOfOptionals.stream().flatMap(Optional::stream).collect(Collectors.toList()); - - assertEquals(2, filteredList.size()); - assertEquals("foo", filteredList.get(0)); - assertEquals("bar", filteredList.get(1)); - } +// Uncomment code when code base is compatible with Java 9 +// @Test +// public void filterOutPresentOptionalsWithJava9() { +// assertEquals(4, listOfOptionals.size()); +// +// List filteredList = listOfOptionals.stream().flatMap(Optional::stream).collect(Collectors.toList()); +// +// assertEquals(2, filteredList.size()); +// assertEquals("foo", filteredList.get(0)); +// assertEquals("bar", filteredList.get(1)); +// } } diff --git a/core-java-modules/core-java-optional/src/test/java/com/baeldung/java9additions/Java9OptionalUnitTest.java b/core-java-modules/core-java-optional/src/test/java/com/baeldung/java9additions/Java9OptionalUnitTest.java new file mode 100644 index 0000000000..ee04432e44 --- /dev/null +++ b/core-java-modules/core-java-optional/src/test/java/com/baeldung/java9additions/Java9OptionalUnitTest.java @@ -0,0 +1,95 @@ +package com.baeldung.java9additions; + +import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; + +// Uncomment code when code base is compatible with Java 9 +//public class Java9OptionalUnitTest { +// +// @Test +// public void givenOptionalOfSome_whenToStream_thenShouldTreatItAsOneElementStream() { +// //given +// Optional value = Optional.of("a"); +// +// //when +// List collect = value.stream().map(String::toUpperCase).collect(Collectors.toList()); +// +// //then +// assertThat(collect).hasSameElementsAs(List.of("A")); +// } +// +// @Test +// public void givenOptionalOfNone_whenToStream_thenShouldTreatItAsZeroElementStream() { +// //given +// Optional value = Optional.empty(); +// +// //when +// List collect = value.stream().map(String::toUpperCase).collect(Collectors.toList()); +// +// //then +// assertThat(collect).isEmpty(); +// } +// +// @Test +// public void givenOptional_whenPresent_thenShouldExecuteProperCallback() { +// //given +// Optional value = Optional.of("properValue"); +// AtomicInteger successCounter = new AtomicInteger(0); +// AtomicInteger onEmptyOptionalCounter = new AtomicInteger(0); +// +// //when +// value.ifPresentOrElse((v) -> successCounter.incrementAndGet(), onEmptyOptionalCounter::incrementAndGet); +// +// //then +// assertThat(successCounter.get()).isEqualTo(1); +// assertThat(onEmptyOptionalCounter.get()).isEqualTo(0); +// } +// +// @Test +// public void givenOptional_whenNotPresent_thenShouldExecuteProperCallback() { +// //given +// Optional value = Optional.empty(); +// AtomicInteger successCounter = new AtomicInteger(0); +// AtomicInteger onEmptyOptionalCounter = new AtomicInteger(0); +// +// //when +// value.ifPresentOrElse((v) -> successCounter.incrementAndGet(), onEmptyOptionalCounter::incrementAndGet); +// +// //then +// assertThat(successCounter.get()).isEqualTo(0); +// assertThat(onEmptyOptionalCounter.get()).isEqualTo(1); +// } +// +// @Test +// public void givenOptional_whenPresent_thenShouldTakeAValueFromIt() { +// //given +// String expected = "properValue"; +// Optional value = Optional.of(expected); +// Optional defaultValue = Optional.of("default"); +// +// //when +// Optional result = value.or(() -> defaultValue); +// +// //then +// assertThat(result.get()).isEqualTo(expected); +// } +// +// @Test +// public void givenOptional_whenEmpty_thenShouldTakeAValueFromOr() { +// //given +// String defaultString = "default"; +// Optional value = Optional.empty(); +// Optional defaultValue = Optional.of(defaultString); +// +// //when +// Optional result = value.or(() -> defaultValue); +// +// //then +// assertThat(result.get()).isEqualTo(defaultString); +// } +//} \ No newline at end of file diff --git a/core-java-modules/core-java-optional/src/test/java/com/baeldung/java8/optional/OptionalChainingUnitTest.java b/core-java-modules/core-java-optional/src/test/java/com/baeldung/optional/OptionalChainingUnitTest.java similarity index 98% rename from core-java-modules/core-java-optional/src/test/java/com/baeldung/java8/optional/OptionalChainingUnitTest.java rename to core-java-modules/core-java-optional/src/test/java/com/baeldung/optional/OptionalChainingUnitTest.java index 3e0d752bb6..9ef156501b 100644 --- a/core-java-modules/core-java-optional/src/test/java/com/baeldung/java8/optional/OptionalChainingUnitTest.java +++ b/core-java-modules/core-java-optional/src/test/java/com/baeldung/optional/OptionalChainingUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java8.optional; +package com.baeldung.optional; import org.junit.Before; import org.junit.Test; diff --git a/core-java-modules/core-java-optional/src/test/java/com/baeldung/java8/optional/OptionalUnitTest.java b/core-java-modules/core-java-optional/src/test/java/com/baeldung/optional/OptionalUnitTest.java similarity index 97% rename from core-java-modules/core-java-optional/src/test/java/com/baeldung/java8/optional/OptionalUnitTest.java rename to core-java-modules/core-java-optional/src/test/java/com/baeldung/optional/OptionalUnitTest.java index bf594610bb..e5c4319bf7 100644 --- a/core-java-modules/core-java-optional/src/test/java/com/baeldung/java8/optional/OptionalUnitTest.java +++ b/core-java-modules/core-java-optional/src/test/java/com/baeldung/optional/OptionalUnitTest.java @@ -1,7 +1,5 @@ -package com.baeldung.java8.optional; +package com.baeldung.optional; -import com.baeldung.optional.Modem; -import com.baeldung.optional.Person; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -260,7 +258,7 @@ public class OptionalUnitTest { return "Default Value"; } -// Uncomment code when code base is compatiable with Java 11 +// Uncomment code when code base is compatible with Java 11 // @Test // public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected() { // Optional opt = Optional.of("Baeldung"); diff --git a/core-java-modules/core-java-optional/src/test/java/com/baeldung/java8/optional/OrElseAndOrElseGetUnitTest.java b/core-java-modules/core-java-optional/src/test/java/com/baeldung/optional/orelse/OrElseAndOrElseGetUnitTest.java similarity index 94% rename from core-java-modules/core-java-optional/src/test/java/com/baeldung/java8/optional/OrElseAndOrElseGetUnitTest.java rename to core-java-modules/core-java-optional/src/test/java/com/baeldung/optional/orelse/OrElseAndOrElseGetUnitTest.java index c8a631d9f0..8f175c30eb 100644 --- a/core-java-modules/core-java-optional/src/test/java/com/baeldung/java8/optional/OrElseAndOrElseGetUnitTest.java +++ b/core-java-modules/core-java-optional/src/test/java/com/baeldung/optional/orelse/OrElseAndOrElseGetUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java8.optional; +package com.baeldung.optional.orelse; import org.junit.Test; import org.slf4j.Logger; @@ -6,8 +6,6 @@ import org.slf4j.LoggerFactory; import static org.junit.Assert.*; -import com.baeldung.optional.OrElseAndOrElseGet; - public class OrElseAndOrElseGetUnitTest { private OrElseAndOrElseGet orElsevsOrElseGet = new OrElseAndOrElseGet(); diff --git a/core-java-modules/core-java-optional/src/test/java/com/baeldung/orelseoptional/OrElseOptionalUnitTest.java b/core-java-modules/core-java-optional/src/test/java/com/baeldung/orelseoptional/OrElseOptionalUnitTest.java new file mode 100644 index 0000000000..91aebbeebd --- /dev/null +++ b/core-java-modules/core-java-optional/src/test/java/com/baeldung/orelseoptional/OrElseOptionalUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.orelseoptional; + +import static org.junit.Assert.assertEquals; + +import java.util.Optional; + +import org.junit.Test; + +public class OrElseOptionalUnitTest { + + @Test + public void givenOptional_whenValue_thenOptionalGeneralMethod() { + String name = "Filan Fisteku"; + String missingOptional = "Name not provided"; + Optional optionalString = Optional.ofNullable(name); + Optional fallbackOptionalString = Optional.ofNullable(missingOptional); + assertEquals(optionalString, OptionalUtils.or(optionalString, fallbackOptionalString)); + } + + @Test + public void givenEmptyOptional_whenValue_thenOptionalGeneralMethod() { + String missingOptional = "Name not provided"; + Optional optionalString = Optional.empty(); + Optional fallbackOptionalString = Optional.ofNullable(missingOptional); + assertEquals(fallbackOptionalString, OptionalUtils.or(optionalString, fallbackOptionalString)); + } + +// Uncomment code when code base is compatible with Java 9 +// @Test +// public void givenOptional_whenEmptyValue_thenCustomMessage() { +// assertEquals(Optional.of("Name not provided"), OptionalUtils.getName(Optional.ofNullable(null))); +// } +// +// @Test +// public void givenOptional_whenValue_thenOptional() { +// String name = "Filan Fisteku"; +// Optional optionalString = Optional.ofNullable(name); +// assertEquals(optionalString, OptionalUtils.getName(optionalString)); +// } +// +// @Test +// public void givenGuavaOptional_whenInvoke_thenOptional() { +// String name = "Filan Fisteku"; +// com.google.common.base.Optional stringOptional = com.google.common.base.Optional.of(name); +// assertEquals(stringOptional, OptionalUtils.getOptionalGuavaName(stringOptional)); +// } +// +// @Test +// public void givenGuavaOptional_whenNull_thenDefaultText() { +// assertEquals(com.google.common.base.Optional.of("Name not provided"), OptionalUtils.getOptionalGuavaName(com.google.common.base.Optional.fromNullable(null))); +// } +} \ No newline at end of file diff --git a/core-java-modules/core-java/README.md b/core-java-modules/core-java/README.md index dd5e7dc14e..c7ae946ab5 100644 --- a/core-java-modules/core-java/README.md +++ b/core-java-modules/core-java/README.md @@ -23,7 +23,6 @@ - [Java Global Exception Handler](http://www.baeldung.com/java-global-exception-handler) - [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object) - [Common Java Exceptions](http://www.baeldung.com/java-common-exceptions) -- [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception) - [Merging java.util.Properties Objects](https://www.baeldung.com/java-merging-properties) - [Java – Try with Resources](https://www.baeldung.com/java-try-with-resources) - [Guide to Character Encoding](https://www.baeldung.com/java-char-encoding)