INTRODUCTION
There are many new features are introduced as part of Java 1.5 SDK. They are
- Enum
- Varargs
- Autoboxing
- Generics
ENUMS
Advantages
- Unlike using an array of Strings, we can use enums as case labels.
- Enums in java are type safe.
- If you are using lower version than java 7, then you can use enum in the Switch statement.
- We can easily add additional fields to the enum in java.
- Enum in java uses its own namespace.
- We can compare enums easily with ==.
- Enums in java helps in reducing the bugs in our code
Example 1
First, create an enum DaySlogan.
public enum DaySlogan {
SUNDAY("Jolly Day!!!"), MONDAY("MON"), TUESDAY("TUE"), WEDNESDAY("WED"), THURSDAY("THU"), FRIDAY("FRI"), SATURDAY(
"SAT");
private String _slogan;
private DaySlogan(String slogan) {
_slogan = slogan;
}
public String getSlogan() {
return _slogan;
}
}
|
Create another enum Days.
public enum Days {
SUNDAY(1, "Sunday", DaySlogan.SUNDAY), MONDAY(2, "Monday", DaySlogan.MONDAY), TUESDAY(3, "Tuesday", DaySlogan.TUESDAY), WEDNESDAY(
4, "Wednesday", DaySlogan.WEDNESDAY), THURSDAY(5, "Thrusday", DaySlogan.THURSDAY), FRIDAY(6, "Friday",
DaySlogan.FRIDAY), SATURDAY(7, "Saturday", DaySlogan.SATURDAY);
private int _dayId;
private String _dayValue;
private DaySlogan _daysSlogan;
private Days(int dayId, String dayValue, DaySlogan daysSlogan) {
_dayId = dayId;
_dayValue = dayValue;
_daysSlogan = daysSlogan;
}
public int getDayId() {
return _dayId;
}
public String getDayValue() {
return _dayValue;
}
public DaySlogan getDaysSlogan() {
return _daysSlogan;
}
public static Days getDayById(int dayId) {
Days day = null;
for (Days d : Days.values()) {
if (d.getDayId() == dayId) {
day = d;
break;
}
}
return day;
}
public static Days getDayByValue(String dayValue) {
Days day = null;
for (Days d : Days.values()) {
if (d.getDayValue() == dayValue) {
day = d;
break;
}
}
return day;
}
public static Days getDay(int dayId) {
Days day = null;
switch (dayId) {
case 1:
day = SUNDAY;
break;
case 2:
day = MONDAY;
break;
case 3:
day = TUESDAY;
break;
case 4:
day = WEDNESDAY;
break;
case 5:
day = THURSDAY;
break;
case 6:
day = FRIDAY;
break;
case 7:
day = SATURDAY;
break;
}
return day;
}
}
|
Now test both enums.
public class EnumTest {
public static void main(String[] args) {
System.out.println("Value of Monday By Id: " + Days.MONDAY.getDayId());
System.out.println("Value of Monday By Value: " + Days.MONDAY.getDayValue());
System.out.println("Value of Monday By Value(Monday): " + Days.getDayByValue("Monday"));
System.out.println("Day of 2 by Id: " + Days.getDayById(2));
System.out.println("Day of 2: " + Days.getDay(2));
System.out.println("Days Slogan for Sunday: " + Days.SUNDAY.getDaysSlogan().getSlogan());
if (Days.valueOf("FRIDAY") == Days.FRIDAY) {
System.out.println("Day is: " + Days.FRIDAY);
}
}
}
|
Output
Value of Monday By Id: 2
Value of Monday By Value: Monday
Value of Monday By Value(Monday): MONDAY
Day of 2 by Id: MONDAY
Day of 2: MONDAY
Days Slogan for Sunday: Jolly Day!!!
Day is: FRIDAY
|
Example 2
Create an enum Months
public enum Months {
JANUARY("January"), FEBRUARY("February"), MARCH("March"), APRIL("April"), MAY("May");
String _monthValue;
Months(String monthValue) {
_monthValue = monthValue;
}
public String getMonthValue() {
return _monthValue;
}
public static Months getValueOfMonth(String month) {
Months monthObj = null;
for (Months months : Months.values()) {
if (months.getMonthValue().equals(month)) {
monthObj = months;
break;
}
}
return monthObj;
}
/** Convert a enum into Bitmask. */
public static long mask(Months months) {
return 1L << months.ordinal();
}
/** Returns a bit mask with all types set. */
public static long mask() {
return (1L << Months.values().length) - 1;
}
/** Determines if a type is part of a bitmask. */
public static boolean maskContains(long mask, Months month) {
return (mask << (1L << month.ordinal())) != 0;
}
/** Returns string array of purchase option value from bit mask value. */
public static String[] unMask(long purchaseOpt) {
List<String> options = new ArrayList<String>();
for (Months opt : Months.values()) {
if (Months.maskContains(purchaseOpt, opt)) {
options.add(opt.getMonthValue());
}
}
return options.toArray(new String[options.size()]);
}
}
|
Now test enums.
public class MonthsTest {
public static void main(String[] args) {
System.out.println("Value of JANUARY: " + Months.JANUARY.getMonthValue());
System.out.println("Value of January: " + Months.getValueOfMonth("January"));
System.out.println("Ordinal for JANUARY : " + (Months.JANUARY.ordinal()));
System.out.println("Ordinal for FEBRUARY : " + (Months.FEBRUARY.ordinal()));
System.out.println("Ordinal for MARCH : " + (Months.MARCH.ordinal()));
System.out.println("Ordinal for APRIL : " + (Months.APRIL.ordinal()));
System.out.println("Ordinal for MAY : " + (Months.MAY.ordinal()));
System.out.println("Mask of JANUARY: " + Months.mask(Months.JANUARY));
System.out.println("Mask of FEBRUARY: " + Months.mask(Months.FEBRUARY));
System.out.println("Mask of MARCH: " + Months.mask(Months.MARCH));
System.out.println("Mask of APRIL: " + Months.mask(Months.APRIL));
System.out.println("Mask of MAY: " + Months.mask(Months.MAY));
System.out.println("Total Mask value: " + Months.mask());
System.out.println("Mask Contains JANUARY: " + Months.maskContains(2, Months.FEBRUARY));
System.out.println("Mask Contains JANUARY: " + Months.maskContains(16, Months.APRIL));
String[] masked = Months.unMask(Months.mask(Months.MARCH));
for (String string : masked) {
System.out.println("Unmasked: " + string);
}
}
}
|
Output
Value of JANUARY: January
Value of January: JANUARY
Ordinal for JANUARY : 0
Ordinal for FEBRUARY : 1
Ordinal for MARCH : 2
Ordinal for APRIL : 3
Ordinal for MAY : 4
Mask of JANUARY: 1
Mask of FEBRUARY: 2
Mask of MARCH: 4
Mask of APRIL: 8
Mask of MAY: 16
Total Mask value: 31
Mask Contains JANUARY: true
Mask Contains JANUARY: true
Unmasked: January
Unmasked: February
Unmasked: March
Unmasked: April
Unmasked: May
|
No comments :
Post a Comment