From fbf171d8961a3f2f47a845b77a8e60dadff8d8e6 Mon Sep 17 00:00:00 2001 From: javahongxi Date: Sat, 17 Aug 2019 11:34:11 +0800 Subject: [PATCH] OptionalTest --- .../test/java/org/hongxi/java/Address.java | 14 ++++++ .../src/test/java/org/hongxi/java/User.java | 14 ++++++ .../org/hongxi/java/util/OptionalTest.java | 45 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 whatsmars-common/src/test/java/org/hongxi/java/Address.java create mode 100644 whatsmars-common/src/test/java/org/hongxi/java/User.java create mode 100644 whatsmars-common/src/test/java/org/hongxi/java/util/OptionalTest.java diff --git a/whatsmars-common/src/test/java/org/hongxi/java/Address.java b/whatsmars-common/src/test/java/org/hongxi/java/Address.java new file mode 100644 index 00000000..ce778799 --- /dev/null +++ b/whatsmars-common/src/test/java/org/hongxi/java/Address.java @@ -0,0 +1,14 @@ +package org.hongxi.java; + +import lombok.Data; + +/** + * Created by shenhongxi on 2019/1/22. + */ +@Data +public class Address { + + private String province; + + private String city; +} diff --git a/whatsmars-common/src/test/java/org/hongxi/java/User.java b/whatsmars-common/src/test/java/org/hongxi/java/User.java new file mode 100644 index 00000000..bbefc5e4 --- /dev/null +++ b/whatsmars-common/src/test/java/org/hongxi/java/User.java @@ -0,0 +1,14 @@ +package org.hongxi.java; + +import lombok.Data; + +/** + * Created by shenhongxi on 2019/1/22. + */ +@Data +public class User { + + private String name; + + private Address address; +} diff --git a/whatsmars-common/src/test/java/org/hongxi/java/util/OptionalTest.java b/whatsmars-common/src/test/java/org/hongxi/java/util/OptionalTest.java new file mode 100644 index 00000000..9b0510c8 --- /dev/null +++ b/whatsmars-common/src/test/java/org/hongxi/java/util/OptionalTest.java @@ -0,0 +1,45 @@ +package org.hongxi.java.util; + +import org.hongxi.java.Address; +import org.hongxi.java.User; + +import java.util.Optional; + +/** + * Created by shenhongxi on 2019/1/22. + */ +public class OptionalTest { + + public static void main(String[] args) { + Optional emptyOpt = Optional.empty(); + System.out.println(emptyOpt.isPresent()); + System.out.println(emptyOpt.orElse("a")); + System.out.println(emptyOpt.isPresent()); + System.out.println(emptyOpt.orElseGet(() -> { + String s1 = "a"; + String s2 = "b"; + return s1 + s2; + })); + + String s = "Hello"; + Optional notNullOpt = Optional.of(s); + if (notNullOpt.isPresent()) { + System.out.println(notNullOpt.get()); + } + notNullOpt = notNullOpt.filter(s1 -> s1.contains("xyz")); + System.out.println(notNullOpt.isPresent()); + } + + public static String getName(User user) { + return Optional.ofNullable(user) + .map(User::getName) + .orElse("Unknown"); + } + + public static String getCity(User user) throws IllegalArgumentException { + return Optional.ofNullable(user) + .map(User::getAddress) + .map(Address::getCity) + .orElseThrow(() -> new IllegalArgumentException("params cant not be null")); + } +}