package com.rensanning.enumtest;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import com.rensanning.enumtest.EnumBean.ConstructableEnums;
import com.rensanning.enumtest.EnumBean.EnumWithInterface;
import com.rensanning.enumtest.EnumBean.EnumsWithMethod;
import com.rensanning.enumtest.EnumBean.NumericEnums;
public class Main {
public static void main(String[] args) {
testEnumBasics();
testEnumCollection();
testEnumConversions();
}
private static void testEnumBasics() {
// ----------------------------------------
System.out.println("printing NumericEnums.values():");
for (NumericEnums e : NumericEnums.values()) {
System.out.print(e.toString() + "\t");
}//ONE TWO THREE FOUR FIVE
System.out.println();
NumericEnums o = NumericEnums.ONE;
if(o == NumericEnums.ONE){
System.out.println("enum in java can be compared using ==");
}
System.out.println(NumericEnums.valueOf("ONE").equals(NumericEnums.ONE));//true
try {
NumericEnums.valueOf("one");
} catch (IllegalArgumentException e) {
System.out.println("one is not exist!");
}
NumericEnums one = NumericEnums.ONE;
switch(one) {
case ONE:
System.out.println("ONE");
break;
}
// ----------------------------------------
System.out.println("printing ConstructableEnums.values():");
for (ConstructableEnums e : ConstructableEnums.values()) {
System.out.print(e.toString() + "\t");
}//ABC DEF GHI JKL
System.out.println();
System.out.println(ConstructableEnums.valueOf("DEF").equals(ConstructableEnums.DEF));//true
System.out.println(ConstructableEnums.DEF.getContent());//num=[20], name=[def]
// ----------------------------------------
System.out.println(EnumsWithMethod.MORNING.hello("foo")); //Good Morning, foo. zzz...
System.out.println(EnumsWithMethod.AFTERNOON.hello("bar"));//Good Afternoon, bar. tea or coffee ?
System.out.println(EnumsWithMethod.EVENING.hello("baz"));//Good Evening, baz. sleep, sleep.
System.out.println(EnumsWithMethod.MORNING.getGreeting());//Good Morning
System.out.println(EnumsWithMethod.AFTERNOON.getGreeting());//Good Afternoon, but sleepy...
System.out.println(EnumsWithMethod.EVENING.getGreeting());//Good Evening
// ----------------------------------------
System.out.println(EnumWithInterface.ME_THEN_YOU.sayHello("abc"));//I'm foo, you're abc.
System.out.println(EnumWithInterface.YOU_THEN_ME.sayHello("def"));//You're def, I'm bar.
}
private static void testEnumCollection() {
// EnumMap----------------------------------------
Map<NumericEnums, String> numerics = new EnumMap<NumericEnums, String>(NumericEnums.class);
numerics.put(NumericEnums.ONE, "one");
numerics.put(NumericEnums.TWO, "two");
numerics.put(NumericEnums.THREE, "three");
numerics.put(NumericEnums.FOUR, "four");
numerics.put(NumericEnums.FIVE, "five");
System.out.println(numerics.get(NumericEnums.FOUR));//four
// EnumSet----------------------------------------
Set<NumericEnums> s1 = EnumSet.of(NumericEnums.TWO, NumericEnums.THREE);
System.out.println(s1.contains(NumericEnums.TWO));//true
System.out.println(s1.contains(NumericEnums.FOUR));//false
Set<NumericEnums> s2 = EnumSet.noneOf(NumericEnums.class);
System.out.println(s2.size());//0
Set<NumericEnums> s3 = EnumSet.range(NumericEnums.TWO, NumericEnums.FOUR);
System.out.println(s3.contains(NumericEnums.ONE));//false
System.out.println(s3.contains(NumericEnums.TWO));//true
System.out.println(s3.contains(NumericEnums.THREE));//true
System.out.println(s3.contains(NumericEnums.FOUR));//true
System.out.println(s3.contains(NumericEnums.FIVE));//false
}
private static void testEnumConversions() {
// int->Enum
System.out.println(NumericEnums.values()[0].equals(NumericEnums.ONE));//true
// String->Enum
System.out.println(NumericEnums.valueOf("ONE").equals(NumericEnums.ONE));//true
// Enum->int----------------------------------------
System.out.println(NumericEnums.ONE.ordinal());//0
System.out.println(NumericEnums.TWO.ordinal());//1
System.out.println(NumericEnums.THREE.ordinal());//2
System.out.println(NumericEnums.FOUR.ordinal());//3
System.out.println(NumericEnums.FIVE.ordinal());//4
// Enum->String----------------------------------------
System.out.println(NumericEnums.ONE.name());//ONE
System.out.println(NumericEnums.TWO.name());//TWO
System.out.println(NumericEnums.THREE.name());//THREE
System.out.println(NumericEnums.FOUR.name());//FOUR
System.out.println(NumericEnums.FIVE.name());//FIVE
System.out.println(NumericEnums.ONE.toString());//ONE
System.out.println(NumericEnums.TWO.toString());//TWO
System.out.println(NumericEnums.THREE.toString());//THREE
System.out.println(NumericEnums.FOUR.toString());//FOUR
System.out.println(NumericEnums.FIVE.toString());//FIVE
}
}