Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Scratch {
- final static List<List<Integer>> samples = Arrays.asList(
- Arrays.<Integer>asList(null, null),
- Arrays.asList(1, null),
- Arrays.asList(null, 10),
- Arrays.asList(1, 10)
- );
- final static List<Integer> both_notnull_visited = new ArrayList<Integer>(1);
- final static List<Integer> at_least_one_null_visited = new ArrayList<Integer>(1);
- public static void main(String[] args) {
- at_least_one_null_visited.add(0);
- both_notnull_visited.add(0);
- asIfPresentOrElseNestedIfPresent();
- try {
- 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)";
- assert both_notnull_visited.get(0) == 1 : "both non-null case not visited";
- } catch (AssertionError error) {
- System.err.println(error);
- }
- at_least_one_null_visited.set(0, 0);
- both_notnull_visited.set(0, 0);
- asIfPresentOrElseNested();
- try {
- 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)";
- assert both_notnull_visited.get(0) == 1 : "both non-null case not visited";
- } catch (AssertionError error) {
- System.err.println(error);
- }
- at_least_one_null_visited.set(0, 0);
- both_notnull_visited.set(0, 0);
- asKonjugatedNegation();
- 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)";
- assert both_notnull_visited.get(0) == 1 : "both non-null case not visited";
- at_least_one_null_visited.set(0, 0);
- both_notnull_visited.set(0, 0);
- asIfPresentOrElseNestedGoodcase();
- 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)";
- assert both_notnull_visited.get(0) == 1 : "both non-null case not visited";
- }
- public static void asIfPresentOrElseNestedIfPresent() {
- for (List<Integer> entry : samples) {
- final Optional<Integer> optVon = Optional.ofNullable(entry.get(0));
- final Optional<Integer> optBis = Optional.ofNullable(entry.get(1));
- optVon.ifPresentOrElse(von -> optBis.ifPresent(
- bis -> {
- both_notnull_visited.set(0, 1);
- }),
- () -> {
- int x = at_least_one_null_visited.get(0);
- at_least_one_null_visited.set(0, ++x);
- }
- );
- }
- }
- public static void asIfPresentOrElseNested() {
- for (List<Integer> entry : samples) {
- final Optional<Integer> optVon = Optional.ofNullable(entry.get(0));
- final Optional<Integer> optBis = Optional.ofNullable(entry.get(1));
- optVon.ifPresentOrElse(von -> optBis.ifPresentOrElse(
- bis -> {
- both_notnull_visited.set(0, 1);
- },
- () -> {
- int x = at_least_one_null_visited.get(0);
- at_least_one_null_visited.set(0, ++x);
- }),
- () -> {}
- );
- }
- }
- /**
- * This is the only equivalent solution to if (.. != null && .. != null), but clearly breaks DRY and is way more
- * verbose
- */
- public static void asIfPresentOrElseNestedGoodcase() {
- for (List<Integer> entry : samples) {
- final Optional<Integer> optVon = Optional.ofNullable(entry.get(0));
- final Optional<Integer> optBis = Optional.ofNullable(entry.get(1));
- optVon.ifPresentOrElse(von -> optBis.ifPresentOrElse(
- bis -> {
- both_notnull_visited.set(0, 1);
- },
- () -> {
- int x = at_least_one_null_visited.get(0);
- at_least_one_null_visited.set(0, ++x);
- }),
- () -> {
- int x = at_least_one_null_visited.get(0);
- at_least_one_null_visited.set(0, ++x);
- }
- );
- }
- }
- public static void asKonjugatedNegation() {
- for (List<Integer> entry : samples) {
- final Integer optVon = entry.get(0);
- final Integer optBis = entry.get(1);
- if (optVon != null && optBis != null) {
- both_notnull_visited.set(0, 1);
- } else {
- int x = at_least_one_null_visited.get(0);
- at_least_one_null_visited.set(0, ++x);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement