java enumMap

import java.util.EnumMap;
import java.util.Map;

public enum Phase {
	SOLID,LIQUID,GAS;
	public enum Transition{
		MELT,FREEZE,BOIL,CONDENSE,SUBLIME,DEPOSIT;
		// Rows indexed by src-ordinal , cols by dst-ordinal
		private static final Transition[][] TRANSITIONS={
			{null,MELT,SUBLIME},
			{FREEZE,null,BOIL},
			{DEPOSIT,CONDENSE,null}
		};
		
		//Returns the phase transition from one phase to another
		public static Transition from(Phase src,Phase dst){
			return TRANSITIONS[src.ordinal()][dst.ordinal()];
		}
		
	}
}

enum Phase2 {
	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 Phase2 src;
		private final Phase2 dst;
		
		private Transition(Phase2 src,Phase2 dst) {
			this.src=src;
			this.dst=dst;
		}
		private static final Map<Phase2,Map<Phase2, Transition>> m=new EnumMap<Phase2, Map<Phase2,Transition>>(Phase2.class);
		
		static{
			for(Phase2 p:Phase2.values()){
				m.put(p, new EnumMap<Phase2, Phase2.Transition>(Phase2.class));
			}
			for(Transition trans:Transition.values()){
				m.get(trans.src).put(trans.dst, trans);
			}
		}
		
		public static Transition from(Phase src,Phase dst){
			return m.get(src).get(dst);
		}
	}
}

你可能感兴趣的:(java,enumMap)