1. enum YesNo 


import java.util.HashMap;
import java.util.Map;

/**
 * "Y" or "N"
 *  
 * 
 */
public enum YesNo {
	// static & final
	YES("Y"),
	NO("N");
	// TODO 대소문자 구분 없음. 

	private static final Map typesByValue = new HashMap();

	static {
		for (YesNo type : YesNo.values()) {
			typesByValue.put(type.value, type);
		}
	}

	private String value;

	private YesNo(String value) {
		this.value = value;
	}

	public static YesNo forValue(String value) {
		YesNo type = typesByValue.get(value);
		if (type == null) {
			throw new IllegalArgumentException();
		}
		return type;
	}

	/**
	 * 1 or 0 으로 변환된값 리턴 
	 * 
	 */
	public int dbValue() {
		return this == YES ? 1 : 0;
	}
}


2. 사용예


	// 사용 예
	String sFavorite_yn =  "Y";
	YesNo mFav = YesNo.forValue(sFavorite_yn);
	int nFav = mFav.dbValue();  // 0 or 1


1. enum GnbMenu


package mytest.java.test;

import java.util.HashMap;
import java.util.Map;


public enum GnbMenu {
	HOME(1){

		@Override
		String fragmentClass() {
			return "Home.class";
		}

		@Override
		int gnbMenuID() {
			return 100;
		}
		
	},
	EPG(2){

		@Override
		String fragmentClass() {
			return "EPG.class";
		}

		@Override
		int gnbMenuID() {
			return 200;
		}
		
	},
	VOD(3){

		@Override
		String fragmentClass() {
			return "VOD.class";
		}

		@Override
		int gnbMenuID() {
			return 300;
		}
		
	},
	MY(4){

		@Override
		String fragmentClass() {
			return "My.class";
		}

		@Override
		int gnbMenuID() {
			return 400;
		}
		
	}
	;
	
	private int value;
	GnbMenu(int value){	// 생성자
		this.value = value;
	}
	int value(){
		return value;
	}
	
	private static final Map typesByValue = new HashMap();
	
	static{
		for (GnbMenu type : GnbMenu.values()) {
			typesByValue.put(type.value, type);
		}
	}
	
	public static GnbMenu forValue(int value){
		GnbMenu type = typesByValue.get(value);
		if(type == null){
			throw new IllegalArgumentException();
		}
		return type;
	}
	
	abstract String fragmentClass();
	abstract int gnbMenuID(); 
}



2. 사용예


	// 사용 예
	int nMenu = GnbMenu.HOME.gnbMenuID();
	String sMenu = GnbMenu.HOME.fragmentClass();
	System.out.println(nMenu+", "+ sMenu);
		
	GnbMenu menu = GnbMenu.forValue(1);
	System.out.println(menu.fragmentClass());


enum 정리 참고


+ Recent posts