관리 메뉴

HAMA 블로그

자바 enum 정리 본문

Java

자바 enum 정리

[하마] 이승현 (wowlsh93@gmail.com) 2015. 9. 1. 11:38

사용법 1  :   클래스의 멤버로 사용 


public enum Currency {PENNY, NICKLE, DIME, QUARTER};

Currency coin = Currency.PENNY;

coin = 1; //compilation error


사용법 2  :   클래스의 멤버로 사용 (값을 지정) 


public enum Currency {PENNY(1), NICKLE(5), DIME(10), QUARTER(25)};


사용법 3  :   Switch 문의 인자로 사용  


Currency usCoin = Currency.DIME;
    switch (usCoin) {
            case PENNY:
                    System.out.println("Penny coin");
                    break;
            case NICKLE:
                    System.out.println("Nickle coin");
                    break;
            case DIME:
                    System.out.println("Dime coin");
                    break;
            case QUARTER:
                    System.out.println("Quarter coin");
    }


사용법 4  :   enum 안에 정의된 상수들은 final 이라, == 으로 비교가능 


Currency usCoin = Currency.DIME;
    if(usCoin == Currency.DIME){
       System.out.println("enum in java can be"+
               "compared using ==");
    }


사용법 5  :   enum 안에 정의된 상수들은 final 이라, == 으로 비교가능 


사용법 6  :  메소드 확장하기 


public enum SizeEnum {


  SMALL("S"), MEDIUM("M"), LARGE("L");

  // Fields

  private String mAbbreviation;

  // Constructor

  private SizeEnum(String abbreviation) {

   mAbbreviation = abbreviation;

  }

   

  // Methods

  public String getAbbreviation() { return mAbbreviation; }

  public void setAbbreviation(String abbreviation) { mAbbreviation = abbreviation; }

 

}


사용법 7  :  Enum 클래스로 사용하기 


public enum FontStyle {
    NORMAL, BOLD, ITALIC, UNDERLINE;
 
    FontStyle() {
    }
}

인라인 클래스로 사용  ( sample.FontStyle.NORMAL  접근) 

public enum SampleClass {
    public enum FontStyle { NORMAL, BOLD, ITALIC, UNDERLINE }
    ...
}


사용법 7  :  EnumSet 사용하기

import java.util.EnumSet;

import java.util.Set; /** * Simple Java Program to demonstrate how to use EnumSet. * It has some interesting use cases and it's specialized collection for * Enumeration types. Using Enum with EnumSet will give you far better * performance than using Enum with HashSet, or LinkedHashSet. * * @author Javin Paul */ public class EnumSetDemo { private enum Color { RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255); private int r; private int g; private int b; private Color(int r, int g, int b) { this.r = r; this.g = g; this.b = b; } public int getR() { return r; } public int getG() { return g; } public int getB() { return b; } } public static void main(String args[]) { // this will draw line in yellow color EnumSet<Color> yellow = EnumSet.of(Color.RED, Color.GREEN); drawLine(yellow); // RED + GREEN + BLUE = WHITE EnumSet<Color> white = EnumSet.of(Color.RED, Color.GREEN, Color.BLUE); drawLine(white); // RED + BLUE = PINK EnumSet<Color> pink = EnumSet.of(Color.RED, Color.BLUE); drawLine(pink); } public static void drawLine(Set<Color> colors) { System.out.println("Requested Colors to draw lines : " + colors); for (Color c : colors) { System.out.println("drawing line in color : " + c); } } } Output: Requested Colors to draw lines : [RED, GREEN] drawing line in color : RED drawing line in color : GREEN Requested Colors to draw lines : [RED, GREEN, BLUE] drawing line in color : RED drawing line in color : GREEN drawing line in color : BLUE Requested Colors to draw lines : [RED, BLUE] drawing line in color : RED drawing line in color : BLUE



사용법 8  :  EnumSet 으로 비트필드 대체하기 

package resolver;

public class IntEnumPatternExample {

    public static final int STYLE_BOLD          = 1 << 0; // 1
    public static final int STYLE_ITALIC        = 1 << 1; // 2
    public static final int STYLE_UNDERLINE     = 1 << 2; // 4
    public static final int STYLE_STRIKETHROUGH = 1 << 3; // 8

    public static void main(String[] args) {
        final IntEnumPatternResolver resolver = new IntEnumPatternResolver();
        resolver.enableAll(STYLE_BOLD, STYLE_ITALIC, STYLE_STRIKETHROUGH, STYLE_UNDERLINE);
        resolver.disable(STYLE_STRIKETHROUGH);
        resolver.toggle(STYLE_UNDERLINE);
        print(resolver);
    }

    private static void print(IntEnumPatternResolver resolver) {
        assert resolver.isEnabled(STYLE_BOLD) == true;
        assert resolver.isEnabled(STYLE_ITALIC) == true;
        assert resolver.isEnabled(STYLE_UNDERLINE) == false;
        assert resolver.isEnabled(STYLE_STRIKETHROUGH) == false;

        System.out.println("STYLE_BOLD: " + resolver.isEnabled(STYLE_BOLD));
        System.out.println("STYLE_ITALIC: " + resolver.isEnabled(STYLE_ITALIC));
        System.out.println("STYLE_UNDERLINE: " + resolver.isEnabled(STYLE_UNDERLINE));
        System.out.println("STYLE_STRIKETHROUGH: " + resolver.isEnabled(STYLE_STRIKETHROUGH));
    }

}
package resolver;

import java.util.EnumSet;

public class EnumPatternExample {

    public enum Style {
        BOLD, ITALIC, UNDERLINE, STRIKETHROUGH
    }

    public static void main(String[] args) {
        final EnumSet<Style> styles = EnumSet.noneOf(Style.class);
        styles.addAll(EnumSet.range(Style.BOLD, Style.STRIKETHROUGH)); // enable all constants
        styles.removeAll(EnumSet.of(Style.UNDERLINE, Style.STRIKETHROUGH)); // disable a couple
        assert EnumSet.of(Style.BOLD, Style.ITALIC).equals(styles); // check set contents are correct
        System.out.println(styles);
    }

}

http://claude-martin.ch/enumbitset/  완전한 EnumBitSet 라이브러리 


사용법 8  :  EnumMap 사용하기 


enum Importance {
Low, Medium, High, Critical
}


EnumMap<Importance, String> enumMap = new EnumMap<>(Importance.class);


enumMap.put(Importance.Low, "=Low");
enumMap.put(Importance.High, "=High");


String value1 = enumMap.get(Importance.Low);
String value2 = enumMap.get(Importance.High);


package program;
import java.util.EnumMap;
public class Program {
    // 1. Create an Enum.
    enum Importance {
Low, Medium, High, Critical
    }
    public static void main(String[] args) {
        // 2. Create an EnumMap.
        EnumMap<Importance, String> enumMap = new EnumMap<>(Importance.class);
        // 3. PUT values into the map.
        enumMap.put(Importance.Low, "=Low");
        enumMap.put(Importance.High, "=High");
        // 4. Get values from the map.
        String value1 = enumMap.get(Importance.Low);
        String value2 = enumMap.get(Importance.High);
        System.out.println(value1);
        System.out.println(value2);
    }
}



Comments