Typesafe Enums

In 1.5.0 release the enumeration type has been added. you can create the enumeration like other language C,C++ etc.

For example

     enum days {MON, Tues, Wed, Thu, Fri,SaT,Sun } ;

In java enum is not as the other language. In java enum type has some methods. Each enum type contains a static methos values() that returns an array containing all the values of the enum type in the same sequesnce in which they are declared. You can use this method to extract all the value using for-each-loop.

import java.util.*;
public class EnumDemo
{
    enum Days {MON, TUES, WED, THU, FRI,SAT,SUN } ;
    //enum type should not be local variable
    Days days;
    public void print()
    {
        //Iterateing
        System.out.println("Iterating throughout");
        for(Days val:days.values())
        {
            System.out.println("  "+val);
        }
        //Iterating from WED to SAT
        System.out.println("Iterating from WED to SAT");
        for(Days val: EnumSet.range(Days.WED, Days.SAT))
        {
            System.out.println("  "+val);
        }
    }
    public static void main(String ar[])
    {
        EnumDemo e=new EnumDemo();
        e.print();
    }

}
/
 Iterating throughout
  MON
  TUES
  WED
  THU
  FRI
  SAT
  SUN
Iterating from WED to SAT
  WED
  THU
  FRI
  SAT
 */

Adding data and behavior

You can add data and behavior into the enum type. The method can be invoked by enum type variable and can work as the normal methods. The data into the enum type can be stored.

public class EnumDemo2
{
    public enum Item {
            CD (43, 23),
            FLOPPY (24,6),
            KEYBOARD (5,200),
            DVDROM (6,1203),
            MONITOR (1,3400),
            CPU (56, 6007),
            MOUSE (825, 207),
            SPEAKER (1, 900),
            CABINET (12, 730);
    
            private final int pieces;
            private final int price;
            Item(int pieces, int price) {
                this.pieces = pieces;
                this.price = price;
            }
            public int pieces()   { return pieces; }
            public int price() { return price; }
    
            public int totalAmount(){
                return  (price * pieces);
            }
        
    }

    Item item;
    public void print()
    {
        //int total=item.totalAmount();
        for(Item val:item.values())
        {
            System.out.println(val+"\t"+val.totalAmount());
        }

    }
    public static void main(String ar[])
    {
        EnumDemo2 e=new EnumDemo2();
        e.print();
    }

}
/
 CD      989
FLOPPY  144
KEYBOARD        1000
DVDROM  7218
MONITOR 3400
CPU     336392
MOUSE   170775
SPEAKER 900
CABINET 8760
 */


There is the anther facility to create the enumeration of methods. You can declare the method abstract and can implements method for each enum constant to take different action. Each enumeration constant then works for the different behavior.


public class EnumDemo3
{
    public enum Calculation
    {
      PLUS{ double calculate(double x, double y) { return x + y; } },
      MINUS  { double calculate(double x, double y) { return x - y; } },
      MULTIPLY  { double calculate(double x, double y) { return x * y; }},
      DIVIDE { double calculate(double x, double y) { return x / y; } };
      abstract double calculate(double x, double y);
    }
    public void print()
    {
        double x = 100.98;
        double y = 98.09;
        for (Calculation op : Calculation.values())
            System.out.println(op+" \t "+op.calculate(x, y));
    }
    public static void main(String ar[])
    {
        EnumDemo3 e=new EnumDemo3();
        e.print();
    }

}

OUTPUT
PLUS     199.07
MINUS    2.8900000000000006
MULTIPLY         9905.128200000001
DIVIDE   1.0294627383015598

Two classes added into the java.util package EnumSet and EnumMap. EnumSet is a high-performance Set implementation for enums. EnumMap is a high-performance Map implementation for use with enum keys, internally implemented as an array.

No comments:

Popular Posts