[자바] Null 체크 (2) (feat. Optional 적용)

손경이·2023년 11월 8일
0

자바

목록 보기
14/17

2023.11.08 [테킷 백엔드 장희성 강사님 강의]

Null 체크(feat. Optional 적용)

  • 실무에서는 Null 체크 지옥이 자주 발생한다.
  • [자바] Null 체크 (1) (feat. Optional 적용)에서는 각각의 클래스들의 필드에 타입을 Optional로 감싸줬다.
  • 이번에는 클래스들은 가만히 놔두고 main메서드 안에서 Optional을 활용하는 방법을 알아보자

Optional 여러가지 장치 적용

  • .map, .ifPresentOrElse
package com.ll.date20231107;

import java.util.Optional;

public class NullCheck2 {
    public static void main(String[] args) {
        // Not Optional
        System.out.println("== Not Optional ==");
        PersonNull2 person1 = new PersonNull2();
        boolean isHandNull = true;

        if (person1 != null)
            if (person1.getLeftArm() != null) {
                if (person1.getLeftArm().getHand() != null) {
                    isHandNull = false;
                    person1.getLeftArm().getHand().grab(); // 왼팔에 달린 왼손으로 잡기
                }
        }

        if (isHandNull) {
            System.out.println("hand is null");
        }

        // Optional, v1
        System.out.println("== Optional, v1 ==");
        Optional<PersonNull2> person2 = Optional.ofNullable(null);

        // map은 안에 구성요소를 실행, Optional은 person2안에 객체가 있으면 실행한다.
        person2
                .map(p -> p.getLeftArm()) // Arm2 Optional로 바뀜, 결과가 있으면 실행, person2 안에 있는 **Person 객체**를 꺼내서 getLeftArm()을 실행,
                .map(arm -> arm.getHand()) // Hand2 Optional로 바뀜, 결과가 있으면 실행, **arm 객체**를 꺼내서 getHand()를 실행, 없으면 실행안됨.
                .ifPresentOrElse(
                        hand -> hand.grab(), // 결과가 있으면 실행
                        () -> System.out.println("hand is null") // 첫번째 p, 두번째 arm, 세번째 hand가 null 이면 실행
                );

        // Optional, v2
        System.out.println("== Optional, v2 ==");
        Optional<PersonNull2> person3 = Optional.ofNullable(new PersonNull2());

        person3
                .map(PersonNull2::getLeftArm) // person2 안에 있는 Person 객체를 꺼내서 getLeftArm()을 실행,
                .map(Arm2::getHand) // arm 객체를 꺼내서 getHand()를 실행, 없으면 실행안됨.
                .ifPresentOrElse(
                        Hand2::grab,
                        () -> System.out.println("hand is null")
                );
    }
}

class PersonNull2 {
    private Arm2 leftArm;
    private Arm2 rightArm;

    public PersonNull2() {
        this.leftArm = new Arm2(); // 왼팔
        this.rightArm = new Arm2(); // 오른팔
    }

    public Arm2 getLeftArm() {
        return leftArm;
    }

    public Arm2 getRightArm() {
        return rightArm;
    }
}

class Arm2 {
    private Hand2 hand; // 손
    private Shoulder2 shoulder; // 어깨

    public Arm2() {
        this.shoulder = new Shoulder2();
        this.hand = new Hand2();
    }

    public Hand2 getHand() {
        return hand;
    }

    public Shoulder2 getShoulder() {
        return shoulder;
    }
}

class Shoulder2 {

}

class Hand2 {
    // 손의 집는 기능
    public void grab() {
        System.out.println("Grabbing");
    }
}

0개의 댓글