From 49acba206ccdc2ccd15cf0a5782d847105a0fd0e Mon Sep 17 00:00:00 2001 From: lor6 Date: Thu, 31 Aug 2017 13:06:37 +0300 Subject: [PATCH] optional examples (#2537) --- guest/core-java-9/pom.xml | 27 +++ .../main/java/com/stackify/optional/User.java | 28 +++ .../com/stackify/optional/OptionalTest.java | 41 +++++ guest/core-java/pom.xml | 36 ++++ .../java/com/stackify/optional/Address.java | 40 +++++ .../java/com/stackify/optional/Country.java | 29 +++ .../main/java/com/stackify/optional/User.java | 51 ++++++ .../stackify/optional/chaining/Address.java | 38 ++++ .../stackify/optional/chaining/Country.java | 28 +++ .../com/stackify/optional/chaining/User.java | 50 ++++++ guest/core-java/src/main/resources/log4j2.xml | 13 ++ .../com/stackify/optional/OptionalTest.java | 166 ++++++++++++++++++ .../chaining/OptionalChainingTest.java | 36 ++++ 13 files changed, 583 insertions(+) create mode 100644 guest/core-java-9/pom.xml create mode 100644 guest/core-java-9/src/main/java/com/stackify/optional/User.java create mode 100644 guest/core-java-9/src/test/java/com/stackify/optional/OptionalTest.java create mode 100644 guest/core-java/pom.xml create mode 100644 guest/core-java/src/main/java/com/stackify/optional/Address.java create mode 100644 guest/core-java/src/main/java/com/stackify/optional/Country.java create mode 100644 guest/core-java/src/main/java/com/stackify/optional/User.java create mode 100644 guest/core-java/src/main/java/com/stackify/optional/chaining/Address.java create mode 100644 guest/core-java/src/main/java/com/stackify/optional/chaining/Country.java create mode 100644 guest/core-java/src/main/java/com/stackify/optional/chaining/User.java create mode 100644 guest/core-java/src/main/resources/log4j2.xml create mode 100644 guest/core-java/src/test/java/com/stackify/optional/OptionalTest.java create mode 100644 guest/core-java/src/test/java/com/stackify/optional/chaining/OptionalChainingTest.java diff --git a/guest/core-java-9/pom.xml b/guest/core-java-9/pom.xml new file mode 100644 index 0000000000..d588df4abc --- /dev/null +++ b/guest/core-java-9/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + com.stackify + core-java-9 + 0.0.1-SNAPSHOT + + + + junit + junit + 4.12 + + + + + + + maven-compiler-plugin + 3.6.2 + + 1.9 + 1.9 + + + + + \ No newline at end of file diff --git a/guest/core-java-9/src/main/java/com/stackify/optional/User.java b/guest/core-java-9/src/main/java/com/stackify/optional/User.java new file mode 100644 index 0000000000..903e75f6f6 --- /dev/null +++ b/guest/core-java-9/src/main/java/com/stackify/optional/User.java @@ -0,0 +1,28 @@ +package com.stackify.optional; + +public class User { + private String email; + private String password; + + public User(String email, String password) { + super(); + this.email = email; + this.password = password; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/guest/core-java-9/src/test/java/com/stackify/optional/OptionalTest.java b/guest/core-java-9/src/test/java/com/stackify/optional/OptionalTest.java new file mode 100644 index 0000000000..4c3503b811 --- /dev/null +++ b/guest/core-java-9/src/test/java/com/stackify/optional/OptionalTest.java @@ -0,0 +1,41 @@ +package com.stackify.optional; + +import org.junit.Test; +import java.util.Optional; +import java.util.List; + +import static org.junit.Assert.*; +import java.util.stream.Collectors; + +public class OptionalTest { + + private User user; + + @Test + public void whenEmptyOptional_thenGetValueFromOr() { + User result = Optional.ofNullable(user) + .or( () -> Optional.of(new User("default","1234"))).get(); + + assertEquals(result.getEmail(), "default"); + } + + @Test + public void whenIfPresentOrElse_thenOk() { + Optional.ofNullable(user) + .ifPresentOrElse( u -> System.out.println("User is:" + u.getEmail()), () -> System.out.println("User not found")); + } + + @Test + public void whenGetStream_thenOk() { + User user = new User("john@gmail.com", "1234"); + List emails = Optional.ofNullable(user) + .stream() + .filter(u -> u.getEmail() != null && u.getEmail().contains("@")) + .map( u -> u.getEmail()) + .collect(Collectors.toList()); + + assertTrue(emails.size() == 1); + assertEquals(emails.get(0), user.getEmail()); + } + +} diff --git a/guest/core-java/pom.xml b/guest/core-java/pom.xml new file mode 100644 index 0000000000..548d5c663b --- /dev/null +++ b/guest/core-java/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + com.stackify + core-java + 0.0.1-SNAPSHOT + + + + junit + junit + 4.12 + + + org.apache.logging.log4j + log4j-core + ${log4j2.version} + + + + + + + maven-compiler-plugin + 3.5 + + 1.8 + 1.8 + + + + + + 2.8.2 + + \ No newline at end of file diff --git a/guest/core-java/src/main/java/com/stackify/optional/Address.java b/guest/core-java/src/main/java/com/stackify/optional/Address.java new file mode 100644 index 0000000000..f54c999920 --- /dev/null +++ b/guest/core-java/src/main/java/com/stackify/optional/Address.java @@ -0,0 +1,40 @@ +package com.stackify.optional; + +public class Address { + private String addressLine; + private String city; + private Country country; + + public Address(String addressLine, String city, Country country) { + super(); + this.addressLine = addressLine; + this.city = city; + this.country = country; + } + + public String getAddressLine() { + return addressLine; + } + + public void setAddressLine(String addressLine) { + this.addressLine = addressLine; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public Country getCountry() { + return country; + } + + public void setCountry(Country country) { + this.country = country; + } + + +} diff --git a/guest/core-java/src/main/java/com/stackify/optional/Country.java b/guest/core-java/src/main/java/com/stackify/optional/Country.java new file mode 100644 index 0000000000..bf97ef7b6d --- /dev/null +++ b/guest/core-java/src/main/java/com/stackify/optional/Country.java @@ -0,0 +1,29 @@ +package com.stackify.optional; + +public class Country { + private String name; + private String isocode; + + public Country(String name, String isocode) { + super(); + this.name = name; + this.isocode = isocode; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIsocode() { + return isocode; + } + + public void setIsocode(String isocode) { + this.isocode = isocode; + } + +} diff --git a/guest/core-java/src/main/java/com/stackify/optional/User.java b/guest/core-java/src/main/java/com/stackify/optional/User.java new file mode 100644 index 0000000000..910f2605dd --- /dev/null +++ b/guest/core-java/src/main/java/com/stackify/optional/User.java @@ -0,0 +1,51 @@ +package com.stackify.optional; + +import java.util.Optional; + +public class User { + private String email; + private String password; + + private Address address; + + private String position; + + public User(String email, String password) { + super(); + this.email = email; + this.password = password; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public Optional getPosition() { + return Optional.ofNullable(position); + } + + public void setPosition(String position) { + this.position = position; + } + +} diff --git a/guest/core-java/src/main/java/com/stackify/optional/chaining/Address.java b/guest/core-java/src/main/java/com/stackify/optional/chaining/Address.java new file mode 100644 index 0000000000..1e10c67c39 --- /dev/null +++ b/guest/core-java/src/main/java/com/stackify/optional/chaining/Address.java @@ -0,0 +1,38 @@ +package com.stackify.optional.chaining; + +import java.util.Optional; + +public class Address { + private String addressLine; + private String city; + private Country country; + + public Address(String addressLine, String city) { + this.addressLine = addressLine; + this.city = city; + } + + public String getAddressLine() { + return addressLine; + } + + public void setAddressLine(String addressLine) { + this.addressLine = addressLine; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public Optional getCountry() { + return Optional.ofNullable(country); + } + + public void setCountry(Country country) { + this.country = country; + } +} diff --git a/guest/core-java/src/main/java/com/stackify/optional/chaining/Country.java b/guest/core-java/src/main/java/com/stackify/optional/chaining/Country.java new file mode 100644 index 0000000000..89e85a9b43 --- /dev/null +++ b/guest/core-java/src/main/java/com/stackify/optional/chaining/Country.java @@ -0,0 +1,28 @@ +package com.stackify.optional.chaining; + +public class Country { + private String name; + private String isocode; + + public Country(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIsocode() { + return isocode; + } + + public void setIsocode(String isocode) { + this.isocode = isocode; + } + + +} \ No newline at end of file diff --git a/guest/core-java/src/main/java/com/stackify/optional/chaining/User.java b/guest/core-java/src/main/java/com/stackify/optional/chaining/User.java new file mode 100644 index 0000000000..07eb398abe --- /dev/null +++ b/guest/core-java/src/main/java/com/stackify/optional/chaining/User.java @@ -0,0 +1,50 @@ +package com.stackify.optional.chaining; + +import java.util.Optional; + +public class User { + private String email; + private String password; + + private Address address; + + private String position; + + public User(String email, String password) { + this.email = email; + this.password = password; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Optional
getAddress() { + return Optional.ofNullable(address); + } + + public void setAddress(Address address) { + this.address = address; + } + + public Optional getPosition() { + return Optional.ofNullable(position); + } + + public void setPosition(String position) { + this.position = position; + } + +} diff --git a/guest/core-java/src/main/resources/log4j2.xml b/guest/core-java/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..a67aae6aa6 --- /dev/null +++ b/guest/core-java/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/guest/core-java/src/test/java/com/stackify/optional/OptionalTest.java b/guest/core-java/src/test/java/com/stackify/optional/OptionalTest.java new file mode 100644 index 0000000000..c6e6ddb15d --- /dev/null +++ b/guest/core-java/src/test/java/com/stackify/optional/OptionalTest.java @@ -0,0 +1,166 @@ +package com.stackify.optional; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Optional; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Test; + +public class OptionalTest { + + private User user; + + private Logger logger = LogManager.getLogger(OptionalTest.class); + + @Test(expected = NullPointerException.class) + public void testNull() { + String isocode = user.getAddress().getCountry().getIsocode().toUpperCase(); + } + + @Test + public void test() { + + if (user != null) { + Address address = user.getAddress(); + if (address != null) { + Country country = address.getCountry(); + if (country != null) { + String isocode = country.getIsocode(); + if (isocode != null) { + isocode = isocode.toUpperCase(); + } + } + } + } + + } + + @Test(expected = NoSuchElementException.class) + public void whenCreateEmptyOptional_thenNull() { + Optional emptyOpt = Optional.empty(); + emptyOpt.get(); + } + + @Test(expected = NullPointerException.class) + public void whenCreateOfEmptyOptional_thenNullPointerException() { + Optional opt = Optional.of(user); + } + + @Test + public void whenCreateOfNullableOptional_thenOk() { + String name = "John"; + Optional opt = Optional.ofNullable(name); + assertEquals("John", opt.get()); + } + + @Test + public void whenCheckIsPresent_thenOk() { + user = new User("john@gmail.com", "1234"); + Optional opt = Optional.ofNullable(user); + assertTrue(opt.isPresent()); + + assertEquals(user.getEmail(), opt.get().getEmail()); + } + + @Test + public void whenCheckIfPresent_thenOk() { + user = new User("john@gmail.com", "1234"); + Optional opt = Optional.ofNullable(user); + assertTrue(opt.isPresent()); + + opt.ifPresent(u -> assertEquals(user.getEmail(), u.getEmail())); + } + + @Test + public void whenEmptyValue_thenReturnDefault() { + User user = null; + User user2 = new User("anna@gmail.com", "1234"); + User result = Optional.ofNullable(user).orElse(user2); + + assertEquals("anna@gmail.com", result.getEmail()); + } + + @Test + public void whenValueNotNull_thenIgnoreDefault() { + User user = new User("john@gmail.com", "1234"); + User user2 = new User("anna@gmail.com", "1234"); + User result = Optional.ofNullable(user).orElse(user2); + + assertEquals("john@gmail.com", result.getEmail()); + } + + @Test + public void whenSetDefaultOrElseGet_thenOk() { + User user = null; + User user2 = new User("anna@gmail.com", "1234"); + User result = Optional.ofNullable(user).orElseGet(() -> user2); + + assertEquals("anna@gmail.com", result.getEmail()); + } + + @Test + public void givenPresentValue_whenCompare_thenOk() { + User user = new User("john@gmail.com", "1234"); + logger.info("Using orElse"); + User result = Optional.ofNullable(user).orElse(createNewUser()); + logger.info("Using orElseGet"); + User result2 = Optional.ofNullable(user).orElseGet(() -> createNewUser()); + } + + private User createNewUser() { + logger.info("Creating New User"); + return new User("extra@gmail.com", "1234"); + } + + @Test + public void givenEmptyValue_whenCompare_thenOk() { + User user = null; + logger.info("Using orElse"); + User result = Optional.ofNullable(user).orElse(createNewUser()); + logger.info("Using orElseGet"); + User result2 = Optional.ofNullable(user).orElseGet(() -> createNewUser()); + } + + @Test(expected = IllegalArgumentException.class) + public void whenThrowException_thenOk() { + User result = Optional.ofNullable(user).orElseThrow(() -> new IllegalArgumentException()); + } + + @Test + public void whenMap_thenOk() { + user = new User("anna@gmail.com", "1234"); + String email = Optional.ofNullable(user).map(u -> u.getEmail()).orElse("default@gmail.com"); + assertEquals(email, user.getEmail()); + + } + + @Test + public void whenFlatMap_thenOk() { + user = new User("anna@gmail.com", "1234"); + user.setPosition("Developer"); + String position = Optional.ofNullable(user).flatMap(u -> u.getPosition()).orElse("default"); + assertEquals(position, user.getPosition().get()); + + } + + @Test + public void whenFilter_thenOk() { + user = new User("anna@gmail.com", "1234"); + Optional result = Optional.ofNullable(user).filter(u -> u.getEmail() != null && u.getEmail().contains("@")); + + assertTrue(result.isPresent()); + } + + @Test + public void whenStream_thenOk() { + List users = new ArrayList<>(); + User user = users.stream().findFirst().orElse(new User("default", "1234")); + assertEquals(user.getEmail(), "default"); + } + +} diff --git a/guest/core-java/src/test/java/com/stackify/optional/chaining/OptionalChainingTest.java b/guest/core-java/src/test/java/com/stackify/optional/chaining/OptionalChainingTest.java new file mode 100644 index 0000000000..660497c367 --- /dev/null +++ b/guest/core-java/src/test/java/com/stackify/optional/chaining/OptionalChainingTest.java @@ -0,0 +1,36 @@ +package com.stackify.optional.chaining; + +import static org.junit.Assert.*; + +import java.util.Optional; + +import org.junit.Test; + +public class OptionalChainingTest { + + @Test + public void whenChaining_thenOk() { + User user = new User("anna@gmail.com", "1234"); + + String result = Optional.ofNullable(user) + .flatMap(u -> u.getAddress()) + .flatMap(a -> a.getCountry()) + .map(c -> c.getIsocode()) + .orElse("default"); + + assertEquals(result, "default"); + } + + @Test + public void whenChainingWithMethodReferences_thenOk() { + User user = new User("anna@gmail.com", "1234"); + + String result = Optional.ofNullable(user) + .flatMap(User::getAddress) + .flatMap(Address::getCountry) + .map(Country::getIsocode) + .orElse("default"); + + assertEquals(result, "default"); + } +}