Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

ordinary

[effective_java] item36, item 37 본문

카테고리 없음

[effective_java] item36, item 37

extra_ 2023. 2. 12. 19:23

item36 : 비트 필드 대신 EnumSet을 사용하라

item37 : ordinal 인덱싱 대신 EnumMap을 사용하라


 


item36 : 비트 필드 대신 EnumSet을 사용하라


 

- 비트 필드를 사용한 열거형 class

public class Text {

    public static final int A_TYPE = 1 << 0;
    public static final int B_TYPE = 1 << 1;
    public static final int C_TYPE = 1 << 2;
    public static final int D_TYPE = 1 << 3;


}

위 코드에서는 << 쉬프트 연산자를 사용해서 int를 표현했다. 

값은 ABCD 순서대로 1, 2, 4, 8이 나오는데 자세한 내용은 비트내용만 따로 정리해 두는 것이 좋겠다.

 

 

- 비트 연산자

  •  비트별 OR을 사용해 여러 상수를 하나의 집합으로 모을 수 있다.
  • 하나의 class안에서 중복되지 않음을 보장하므로, Enum을 사용하지 않았으므로 이전에 확인했던 것 처럼 정수 열거 함수의 단점을 그대로 가져온다.
  • 또한 비트값이 그대로 출력되면 모든 원소를 순회해야하는데 미리 예측하여 타입 선택을 해야함 - 이건 열거타입의 단점이기도 하다.
  • http://underpop.online.fr/j/java/hardcore-java/hardcorejv-chp-7-sect-2.html 
 

Bit Fields - Java - Languages - Programming - Computers

Bit Fields A bit field is a memory-saving tool that was developed early in the history of computer science. It saves memory by combining several true-false options into one byte. For example, a car may have several options that are either present or not pr

underpop.online.fr

설명이 잘 되어있다.

 



- EnumSet을 사용한 코드

package effectivejava.chapter6.item36;

import java.util.*;

// EnumSet - a modern replacement for bit fields (Page 170)
public class Text {
    public enum Style {BOLD, ITALIC, UNDERLINE, STRIKETHROUGH}

    // Any Set could be passed in, but EnumSet is clearly best
    public void applyStyles(Set<Style> styles) {
        System.out.printf("Applying styles %s to text%n",
                Objects.requireNonNull(styles));
    }

    // Sample use
    public static void main(String[] args) {
        Text text = new Text();
        text.applyStyles(EnumSet.of(Style.BOLD, Style.ITALIC));
    }
}

 

  • EnumSet이 처리해 줄 수 있는 of메서드를 사용해서 정적팩터리 기능을 효율적으로 사용한다.
  • 파라미터를 set으로 받아줌으로써 '좀 특이한 클라이언트'가 다른 Set구현체를 넘기더라도 처리할 수 있게 한다.

 

 




item37 : ordinal 인덱싱 대신 EnumMap을 사용하라

 



- ordinal 메서드는 배열이나 리스트에서 인덱스를 얻을 때 사용된다.

 

 

 

- ordinal값을 배열의 인덱스로 사용하려고 하는 코드

class Plant {
    enum LifeCycle { ANNUAL, PERENNIAL, BIENNIAL }

    final String name;
    final LifeCycle lifeCycle;

    Plant(String name, LifeCycle lifeCycle) {
        this.name = name;
        this.lifeCycle = lifeCycle;
    }

    @Override public String toString() {
        return name;
    }

    public static void main(String[] args) {
        Plant[] garden = {
                new Plant("Basil",    LifeCycle.ANNUAL),
                new Plant("Carroway", LifeCycle.BIENNIAL),
                new Plant("Dill",     LifeCycle.ANNUAL),
                new Plant("Lavendar", LifeCycle.PERENNIAL),
                new Plant("Parsley",  LifeCycle.BIENNIAL),
                new Plant("Rosemary", LifeCycle.PERENNIAL)
        };


    Set<Plant>[] plantsByLifeCycleArr = (Set<Plant>[]) new Set[Plant.LifeCycle.values().length];
    for (int i = 0; i < plantsByLifeCycleArr.length; i++) {
		plantsByLifeCycleArr[i] = new HashSet<>();
    }
    for (Plant p : garden) {
		plantsByLifeCycleArr[p.lifeCycle.ordinal()].add(p);
    }


    for (int i = 0; i < plantsByLifeCycleArr.length; i++) {
            System.out.printf("%s: %s%n",
                    Plant.LifeCycle.values()[i], plantsByLifeCycleArr[i]);
    }
        
    }   
}

 

  •  위 코드에서 의도한 것은 PLANT의 LIFECYCLE별 결과를 나타내고 싶었던 것
    • 결과
      ANNUAL: [Dill, Basil]
      PERENNIAL: [Rosemary, Lavendar]
      BIENNIAL: [Carroway, Parsley]
  • 동작은 잘 하지만 배열과 제네릭이 호환되지 않아 비검사 형변환을 해야하고
  •  

 

 

 

 

제네릭과 배열은 호환되지 않으므로 비검사 형변환을 수행해야한다.
- 비검사 형변환의 말처럼 검사없이 형변환을 수행하게되면 타입안전하지 않은 동작을 수행하게 될수있다. ~~~

 

 

 

 

- EnumMap을 사용한 데이터와 열거타입 매핑 코드

Map<Plant.LifeCycle, Set<Plant>> plantsByLifeCycle = new EnumMap<>(Plant.LifeCycle.class);

for (Plant.LifeCycle lc : Plant.LifeCycle.values()) {
	plantsByLifeCycle.put(lc, new HashSet<>());
}

for (Plant p : garden) {
	plantsByLifeCycle.get(p.lifeCycle).add(p);
}

System.out.println(plantsByLifeCycle);

 

  • Map< Enum, Set<> > 요런 형태로 가게 되면 Plant.LifeCycle에서 키값을 받을때 오류가 날 가능성이 사라짐
  • 열거타입 자체로 출력용 문자열을 리턴하게 만들 수 있다.

 

 

- Stream을 사용한 코드

System.out.println(Arrays.stream(garden)
        .collect(groupingBy(p -> p.lifeCycle)));

 

  • 스트림을 사용하면 맵 구현체를 사용하게 되므로 EnumMap의 성능이점이 사라질 수 있다.

 

 

 

- Stream을 사용한 코드2

System.out.println(Arrays.stream(garden)
        .collect(groupingBy(p -> p.lifeCycle,
                () -> new EnumMap<>(LifeCycle.class), toSet())));

 


- 최적화

 

public enum Phase {
    SOLID, LIQUID, GAS;
    public enum Transition {
        MELT(SOLID, LIQUID), FREEZE(LIQUID, SOLID),
        BOIL(LIQUID, GAS), CONDENSE(GAS, LIQUID),
        SUBLIME(SOLID, GAS), DEPOSIT(GAS, SOLID);

        private final Phase from;
        private final Phase to;
        Transition(Phase from, Phase to) {
            this.from = from;
            this.to = to;
        }

        private static final Map<Phase, Map<Phase, Transition>> m = 
                Stream.of(values())
                        .collect(groupingBy(t -> t.from, 
                                () -> new EnumMap<>(Phase.class),
                                toMap(t -> t.to, t -> t, (x, y) -> y, () -> new EnumMap<>(Phase.class)))
                        );
                        
        /**
        * 이전상태 -> 이후 상태의 transition을 구현하기 위해 'Collector'의 사용이 두번 들어갔다. (책의 번역체는 '수집기'라고 했다')
        * toMap은 점층적 팩터리를 사용하기 위한 맵 팩터리의 선언만 했다.
        */
        

        public static Transition from(Phase from, Phase to) {
            return m.get(from).get(to);
        }
    }

    public static void main(String[] args) {
        for (Phase src : Phase.values()) {
            for (Phase dst : Phase.values()) {
                Transition transition = Transition.from(src, dst);
                if (transition != null) {
                    System.out.printf("%s to %s : %s %n", src, dst, transition);

                }
            }
        }
    }
}
  • ordinal대신 EnumMap을 사용한 케이스인데
  • 사실 이렇게 까지 ordinal을 사용해야 할 아이디어조차 생각한 적 없는 나로서는 EnumMap이 당연히 좋아보인다.

p.222에서는 “대부분의 프로그래머는 이 메서드 (ordinal)를 쓸 일이 없다. 이 메서드는 EnumSet과 EnumMap 같이 열거타입 기반의 범용 자료구조에 쓸 목적으로 설계되었다”. 따라서 이런 용도가 아니라면 ordinal 메서드는 절대 사용하지 말자.

고 했다.

 

  • Enum 안에서 함수를 재정의하고 구하는 경우가 많은가보다. 한번 예시를 구체적으로 생각해서 구현해봐야겠다.


- 기존 상태에서 새로운 Phase하나만  추가한 코드

public enum Phase {
 
	SOLID, LIQUID, GAS, 
    PLASMA; // 추가됨
	public enum Transition {
		MELT(SOLID, LIQUID), FREEZE(LIQUID, SOLID),
		BOIL(LIQUID, GAS), CONDENSE(GAS, LIQUID),
		SUBLIME(SOLID, GAS), DEPOSIT(GAS, SOLID),
		IONIZE(GAS, PLASMA), DEIONIZE(PLASMA, GAS); // 추가된것을 추가함
    }   
}