Guest User

Untitled

a guest
May 20th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. package com.gdn.rnd.codingguideline;
  2.  
  3. import lombok.AllArgsConstructor;
  4. import lombok.Builder;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;
  7. import org.junit.Test;
  8.  
  9. import java.util.Optional;
  10.  
  11. /**
  12. * @author Eko Kurniawan Khannedy
  13. */
  14. public class OptionalTest {
  15.  
  16. private Customer customer;
  17.  
  18. @Test
  19. public void nameUsingIf() {
  20. String name = customer.getName();
  21. if (name == null) {
  22. name = "";
  23. }
  24.  
  25. System.out.println(name);
  26. }
  27.  
  28. @Test
  29. public void nameUsingOptional() {
  30. String name = Optional.ofNullable(customer.getName())
  31. .orElse("");
  32.  
  33. System.out.println(name);
  34. }
  35.  
  36. @Test
  37. public void dbLogicUsingIf() {
  38.  
  39. Customer user = findCustomerById("id");
  40. if (user == null) {
  41. user = createNewUser("id");
  42. }
  43.  
  44. System.out.println(user);
  45. }
  46.  
  47. @Test
  48. public void dbLogicUsingOptional() {
  49. Customer user = Optional.ofNullable(findCustomerById("id"))
  50. .orElse(createNewUser("id"));
  51.  
  52. System.out.println(user);
  53. }
  54.  
  55. @Test
  56. public void nameUpperCaseUsingIf() {
  57. String nameUpper = customer.getName();
  58. if (nameUpper != null) {
  59. nameUpper = nameUpper.toUpperCase();
  60. } else {
  61. nameUpper = "";
  62. }
  63.  
  64. System.out.println(nameUpper);
  65. }
  66.  
  67. @Test
  68. public void nameUpperUsingOptional() {
  69. String nameUpper = Optional.ofNullable(customer.getName())
  70. .map(String::toUpperCase)
  71. .orElse("");
  72.  
  73. System.out.println(nameUpper);
  74. }
  75.  
  76. @Test
  77. public void countryUsingIf() {
  78. String country = "Indonesia";
  79. if (customer.getAddress() != null) {
  80. if (customer.getAddress().getCountry() != null) {
  81. country = customer.getAddress().getCountry();
  82. }
  83. }
  84.  
  85. System.out.println(country);
  86. }
  87.  
  88. @Test
  89. public void countryUsingOption() {
  90. String country = Optional.ofNullable(customer.getAddress())
  91. .map(Customer.Address::getCountry)
  92. .orElse("Indonesia");
  93.  
  94. System.out.println(country);
  95. }
  96.  
  97. @Test
  98. public void nameExceptionUsingIf() {
  99. String name = customer.getName();
  100. if (name == null) {
  101. throw new IllegalArgumentException("Name is null");
  102. }
  103.  
  104. System.out.println(name);
  105. }
  106.  
  107. @Test
  108. public void nameExceptionUsingOption() {
  109. String name = Optional.ofNullable(customer.getName())
  110. .orElseThrow(() -> new IllegalArgumentException("Name is null"));
  111.  
  112. System.out.println(name);
  113. }
  114.  
  115. @Test
  116. public void countryDoSomeThingUsingIf() {
  117. if (customer.getAddress() != null) {
  118. if (customer.getAddress().getCountry() != null) {
  119.  
  120. // do something
  121. System.out.println(customer.getAddress().getCountry());
  122.  
  123. }
  124. }
  125. }
  126.  
  127. @Test
  128. public void countryDoSomethingUsingOption() {
  129. Optional.ofNullable(customer.getAddress())
  130. .map(Customer.Address::getCountry)
  131. .ifPresent(country -> {
  132. // do something
  133. System.out.println(customer.getAddress().getCountry());
  134. });
  135. }
  136.  
  137. @Test
  138. public void nestedObjectUsingIf() {
  139.  
  140. Long cashBalance = 0L;
  141.  
  142. if (customer.getWallet() != null) {
  143. if (customer.getWallet().getBalance() != null) {
  144. cashBalance = customer.getWallet().getBalance().getCashBalance();
  145. }
  146. }
  147.  
  148. System.out.println(cashBalance);
  149. }
  150.  
  151. @Test
  152. public void nestedObjectUsingOption() {
  153. Long cashBalance = Optional.ofNullable(customer.getWallet())
  154. .map(Customer.Wallet::getBalance)
  155. .map(Customer.WalletBalance::getCashBalance)
  156. .orElse(0L);
  157.  
  158. System.out.println(cashBalance);
  159. }
  160.  
  161. @Test
  162. public void filterWithIf() {
  163.  
  164. Long bonus = 0L;
  165. if (Customer.Type.PREMIUM.equals(customer.getType())) {
  166. if (customer.getWallet() != null) {
  167. if (customer.getWallet().getBalance() != null) {
  168. bonus = customer.getWallet().getBalance().getCashBalance() * 10 / 100;
  169. }
  170. }
  171. }
  172.  
  173. System.out.println(bonus);
  174. }
  175.  
  176. @Test
  177. public void filterWithOptional() {
  178. Long bonus = Optional.of(customer)
  179. .filter(value -> Customer.Type.PREMIUM.equals(value.getType()))
  180. .map(Customer::getWallet)
  181. .map(Customer.Wallet::getBalance)
  182. .map(balance -> balance.getCashBalance() * 10 / 100)
  183. .orElse(0L);
  184.  
  185. System.out.println(bonus);
  186. }
  187.  
  188. private Customer createNewUser(String id) {
  189. return null;
  190. }
  191.  
  192. private Customer findCustomerById(String id) {
  193. return null;
  194. }
  195.  
  196. @Builder
  197. @AllArgsConstructor
  198. @NoArgsConstructor
  199. @Data
  200. public static class Customer {
  201.  
  202. private String name;
  203.  
  204. private Address address;
  205.  
  206. private Wallet wallet;
  207.  
  208. private Type type;
  209.  
  210. public static enum Type {
  211. GOLD, PREMIUM, CLASSIC
  212. }
  213.  
  214. @Builder
  215. @AllArgsConstructor
  216. @NoArgsConstructor
  217. @Data
  218. public static class Address {
  219.  
  220. private String street;
  221.  
  222. private String city;
  223.  
  224. private String country;
  225.  
  226. }
  227.  
  228. @Builder
  229. @AllArgsConstructor
  230. @NoArgsConstructor
  231. @Data
  232. public static class Wallet {
  233.  
  234. private Long totalBalance;
  235.  
  236. private WalletBalance balance;
  237.  
  238. }
  239.  
  240. @Builder
  241. @AllArgsConstructor
  242. @NoArgsConstructor
  243. @Data
  244. public static class WalletBalance {
  245.  
  246. private Long refundBalance;
  247.  
  248. private Long nonCashBalance;
  249.  
  250. private Long cashBalance;
  251.  
  252. }
  253.  
  254. }
  255.  
  256. }
Add Comment
Please, Sign In to add comment