Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Scratch {
  4. final static List<List<Integer>> samples = Arrays.asList(
  5. Arrays.<Integer>asList(null, null),
  6. Arrays.asList(1, null),
  7. Arrays.asList(null, 10),
  8. Arrays.asList(1, 10)
  9. );
  10. final static List<Integer> both_notnull_visited = new ArrayList<Integer>(1);
  11. final static List<Integer> at_least_one_null_visited = new ArrayList<Integer>(1);
  12.  
  13. public static void main(String[] args) {
  14. at_least_one_null_visited.add(0);
  15. both_notnull_visited.add(0);
  16. asIfPresentOrElseNestedIfPresent();
  17. try {
  18. assert at_least_one_null_visited.get(0) == 3 : "at least one null cases not visited 3 times (" + at_least_one_null_visited.get(0) + "x)";
  19. assert both_notnull_visited.get(0) == 1 : "both non-null case not visited";
  20. } catch (AssertionError error) {
  21. System.err.println(error);
  22. }
  23.  
  24. at_least_one_null_visited.set(0, 0);
  25. both_notnull_visited.set(0, 0);
  26. asIfPresentOrElseNested();
  27. try {
  28. assert at_least_one_null_visited.get(0) == 3 : "at least one null cases not visited 3 times (" + at_least_one_null_visited.get(0) + "x)";
  29. assert both_notnull_visited.get(0) == 1 : "both non-null case not visited";
  30. } catch (AssertionError error) {
  31. System.err.println(error);
  32. }
  33.  
  34. at_least_one_null_visited.set(0, 0);
  35. both_notnull_visited.set(0, 0);
  36. asKonjugatedNegation();
  37. assert at_least_one_null_visited.get(0) == 3 : "at least one null cases not visited 3 times (" + at_least_one_null_visited.get(0) + "x)";
  38. assert both_notnull_visited.get(0) == 1 : "both non-null case not visited";
  39.  
  40. at_least_one_null_visited.set(0, 0);
  41. both_notnull_visited.set(0, 0);
  42. asIfPresentOrElseNestedGoodcase();
  43. assert at_least_one_null_visited.get(0) == 3 : "at least one null cases not visited 3 times (" + at_least_one_null_visited.get(0) + "x)";
  44. assert both_notnull_visited.get(0) == 1 : "both non-null case not visited";
  45. }
  46.  
  47.  
  48. public static void asIfPresentOrElseNestedIfPresent() {
  49. for (List<Integer> entry : samples) {
  50. final Optional<Integer> optVon = Optional.ofNullable(entry.get(0));
  51. final Optional<Integer> optBis = Optional.ofNullable(entry.get(1));
  52. optVon.ifPresentOrElse(von -> optBis.ifPresent(
  53. bis -> {
  54. both_notnull_visited.set(0, 1);
  55. }),
  56. () -> {
  57. int x = at_least_one_null_visited.get(0);
  58. at_least_one_null_visited.set(0, ++x);
  59. }
  60. );
  61. }
  62.  
  63. }
  64.  
  65. public static void asIfPresentOrElseNested() {
  66. for (List<Integer> entry : samples) {
  67. final Optional<Integer> optVon = Optional.ofNullable(entry.get(0));
  68. final Optional<Integer> optBis = Optional.ofNullable(entry.get(1));
  69. optVon.ifPresentOrElse(von -> optBis.ifPresentOrElse(
  70. bis -> {
  71. both_notnull_visited.set(0, 1);
  72. },
  73. () -> {
  74. int x = at_least_one_null_visited.get(0);
  75. at_least_one_null_visited.set(0, ++x);
  76. }),
  77. () -> {}
  78. );
  79. }
  80. }
  81.  
  82. /**
  83. * This is the only equivalent solution to if (.. != null && .. != null), but clearly breaks DRY and is way more
  84. * verbose
  85. */
  86. public static void asIfPresentOrElseNestedGoodcase() {
  87. for (List<Integer> entry : samples) {
  88. final Optional<Integer> optVon = Optional.ofNullable(entry.get(0));
  89. final Optional<Integer> optBis = Optional.ofNullable(entry.get(1));
  90. optVon.ifPresentOrElse(von -> optBis.ifPresentOrElse(
  91. bis -> {
  92. both_notnull_visited.set(0, 1);
  93. },
  94. () -> {
  95. int x = at_least_one_null_visited.get(0);
  96. at_least_one_null_visited.set(0, ++x);
  97. }),
  98. () -> {
  99. int x = at_least_one_null_visited.get(0);
  100. at_least_one_null_visited.set(0, ++x);
  101. }
  102. );
  103. }
  104. }
  105.  
  106. public static void asKonjugatedNegation() {
  107. for (List<Integer> entry : samples) {
  108. final Integer optVon = entry.get(0);
  109. final Integer optBis = entry.get(1);
  110.  
  111. if (optVon != null && optBis != null) {
  112. both_notnull_visited.set(0, 1);
  113. } else {
  114. int x = at_least_one_null_visited.get(0);
  115. at_least_one_null_visited.set(0, ++x);
  116. }
  117. }
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement