Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gdn.rnd.codingguideline;
- import lombok.AllArgsConstructor;
- import lombok.Builder;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import org.junit.Test;
- import java.util.Optional;
- /**
- * @author Eko Kurniawan Khannedy
- */
- public class OptionalTest {
- private Customer customer;
- @Test
- public void nameUsingIf() {
- String name = customer.getName();
- if (name == null) {
- name = "";
- }
- System.out.println(name);
- }
- @Test
- public void nameUsingOptional() {
- String name = Optional.ofNullable(customer.getName())
- .orElse("");
- System.out.println(name);
- }
- @Test
- public void dbLogicUsingIf() {
- Customer user = findCustomerById("id");
- if (user == null) {
- user = createNewUser("id");
- }
- System.out.println(user);
- }
- @Test
- public void dbLogicUsingOptional() {
- Customer user = Optional.ofNullable(findCustomerById("id"))
- .orElse(createNewUser("id"));
- System.out.println(user);
- }
- @Test
- public void nameUpperCaseUsingIf() {
- String nameUpper = customer.getName();
- if (nameUpper != null) {
- nameUpper = nameUpper.toUpperCase();
- } else {
- nameUpper = "";
- }
- System.out.println(nameUpper);
- }
- @Test
- public void nameUpperUsingOptional() {
- String nameUpper = Optional.ofNullable(customer.getName())
- .map(String::toUpperCase)
- .orElse("");
- System.out.println(nameUpper);
- }
- @Test
- public void countryUsingIf() {
- String country = "Indonesia";
- if (customer.getAddress() != null) {
- if (customer.getAddress().getCountry() != null) {
- country = customer.getAddress().getCountry();
- }
- }
- System.out.println(country);
- }
- @Test
- public void countryUsingOption() {
- String country = Optional.ofNullable(customer.getAddress())
- .map(Customer.Address::getCountry)
- .orElse("Indonesia");
- System.out.println(country);
- }
- @Test
- public void nameExceptionUsingIf() {
- String name = customer.getName();
- if (name == null) {
- throw new IllegalArgumentException("Name is null");
- }
- System.out.println(name);
- }
- @Test
- public void nameExceptionUsingOption() {
- String name = Optional.ofNullable(customer.getName())
- .orElseThrow(() -> new IllegalArgumentException("Name is null"));
- System.out.println(name);
- }
- @Test
- public void countryDoSomeThingUsingIf() {
- if (customer.getAddress() != null) {
- if (customer.getAddress().getCountry() != null) {
- // do something
- System.out.println(customer.getAddress().getCountry());
- }
- }
- }
- @Test
- public void countryDoSomethingUsingOption() {
- Optional.ofNullable(customer.getAddress())
- .map(Customer.Address::getCountry)
- .ifPresent(country -> {
- // do something
- System.out.println(customer.getAddress().getCountry());
- });
- }
- @Test
- public void nestedObjectUsingIf() {
- Long cashBalance = 0L;
- if (customer.getWallet() != null) {
- if (customer.getWallet().getBalance() != null) {
- cashBalance = customer.getWallet().getBalance().getCashBalance();
- }
- }
- System.out.println(cashBalance);
- }
- @Test
- public void nestedObjectUsingOption() {
- Long cashBalance = Optional.ofNullable(customer.getWallet())
- .map(Customer.Wallet::getBalance)
- .map(Customer.WalletBalance::getCashBalance)
- .orElse(0L);
- System.out.println(cashBalance);
- }
- @Test
- public void filterWithIf() {
- Long bonus = 0L;
- if (Customer.Type.PREMIUM.equals(customer.getType())) {
- if (customer.getWallet() != null) {
- if (customer.getWallet().getBalance() != null) {
- bonus = customer.getWallet().getBalance().getCashBalance() * 10 / 100;
- }
- }
- }
- System.out.println(bonus);
- }
- @Test
- public void filterWithOptional() {
- Long bonus = Optional.of(customer)
- .filter(value -> Customer.Type.PREMIUM.equals(value.getType()))
- .map(Customer::getWallet)
- .map(Customer.Wallet::getBalance)
- .map(balance -> balance.getCashBalance() * 10 / 100)
- .orElse(0L);
- System.out.println(bonus);
- }
- private Customer createNewUser(String id) {
- return null;
- }
- private Customer findCustomerById(String id) {
- return null;
- }
- @Builder
- @AllArgsConstructor
- @NoArgsConstructor
- @Data
- public static class Customer {
- private String name;
- private Address address;
- private Wallet wallet;
- private Type type;
- public static enum Type {
- GOLD, PREMIUM, CLASSIC
- }
- @Builder
- @AllArgsConstructor
- @NoArgsConstructor
- @Data
- public static class Address {
- private String street;
- private String city;
- private String country;
- }
- @Builder
- @AllArgsConstructor
- @NoArgsConstructor
- @Data
- public static class Wallet {
- private Long totalBalance;
- private WalletBalance balance;
- }
- @Builder
- @AllArgsConstructor
- @NoArgsConstructor
- @Data
- public static class WalletBalance {
- private Long refundBalance;
- private Long nonCashBalance;
- private Long cashBalance;
- }
- }
- }
Add Comment
Please, Sign In to add comment