Pages

Wednesday, February 11, 2015

A day for Enumeration perhaps

I had written a post about how to implement a Switch Case using String in past. I had to do a similar thing now and felt like it time I dig more about Enum Classes and objects.

I understand that an object definition using the Enum keyword is similar to a class definition and can be put in its own file. And that the Enum objects by default will extend java.lang.Enum implicitly

An enum type is a special kind of Java class used to define collections of constants, methods etc. It can hold if statements, switch statements, fields, methods, iterations etc. Following is a simple example of my test with an Enum Object and its results

Enum Object definition

package com.oz.core;

public enum Level {
      HIGH(3), // calls constructor with value 3
      MEDIUM(2), // calls constructor with value 2
      LOW(1) // calls constructor with value 1
      ; // semicolon needed when fields / methods follow

      private final int levelCode;

      Level(int levelCode) {
            this.levelCode = levelCode;
      }

      public int getLevelCode() {
            return this.levelCode;
      }
}

Implementation class definintion

package com.oz.core;

public class EnumerationSample {

      public static void main(String args[]) {

            EnumerationSample self = new EnumerationSample();
            self.caseWithStringArgs("medium");
            self.caseWithStringArgs("high");
            self.caseWithStringArgs("critical");
           
            Level level = Level.valueOf("LOW");
            System.out.println("level="+level);
            System.out.println("level.compareTo(Level.valueOf(\"HIGH\"))="+level.compareTo(Level.valueOf("HIGH")));
            System.out.println("level.compareTo(Level.valueOf(\"LOW\"))="+level.compareTo(Level.valueOf("LOW")));
            System.out.println("level.compareTo(level.HIGH)="+level.compareTo(level.HIGH));
            System.out.println("level.compareTo(level.LOW)="+level.compareTo(level.LOW));
            System.out.println("level.getLevelCode()="+level.getLevelCode());
            System.out.println("level.hashCode()="+level.hashCode());
            System.out.println("level.name()="+level.name());
            System.out.println("level.ordinal()="+level.ordinal());
            System.out.println("level.toString()="+level.toString());
            System.out.println("level.equals(Level.LOW)="+level.equals(Level.LOW));
            System.out.println("level.equals(Level.MEDIUM)="+level.equals(Level.MEDIUM));
            System.out.println("level.equals(Level.valueOf(\"LOW\"))="+level.equals(Level.valueOf("LOW")));
            System.out.println("level.equals(Level.valueOf(\"MEDIUM\"))="+level.equals(Level.valueOf("MEDIUM")));
            System.out.println("level.getClass()="+level.getClass());
            System.out.println("level.getDeclaringClass()="+level.getDeclaringClass());
           
            for (Level lvl : level.values())    {
                  System.out.println("lvl="+lvl);
            }
           
            /*System.out.println("level="+Level.valueOf(arg0, arg1));
            System.out.println("level="+level.notify());
            System.out.println("level="+level.notifyAll());
            System.out.println("level="+level.wait());
            System.out.println("level="+level.wait(arg0, arg1));*/           
           
      }
     
      String caseWithStringArgs(String arg)     {
           
            String evaluatedString = "Not in list";
            try   {
                  Level level = Level.valueOf(arg.toUpperCase());
                 
                  switch (level) {
                  case HIGH:
                        evaluatedString = "case statement for High";
                        break;

                  case MEDIUM:
                        evaluatedString = "case statement for Medium";
                        break;

                  case LOW:
                        evaluatedString = "case statement for Low";
                        break;

                  default:
                        break;
                  }
            } catch     (IllegalArgumentException exp)      {
                 
            } finally   {
                  System.out.println(evaluatedString);
                  return evaluatedString;
            }          
      }
}

Following are the set of obtained console statements

case statement for Medium
case statement for High
Not in list
level=LOW
level.compareTo(Level.valueOf("HIGH"))=2  //HIGH=3, LOW=1, so HIGH-LOW=2
level.compareTo(Level.valueOf("LOW"))=0   //LOW=1, LOW=1, so LOW-LOW=2
level.compareTo(level.HIGH)=2             //HIGH=3, LOW=1, so HIGH-LOW=2
level.compareTo(level.LOW)=0              //LOW=1, LOW=1, so LOW-LOW=2
level.getLevelCode()=1                    //LOW=1
level.hashCode()=650651336
level.name()=LOW                          //name
level.ordinal()=2                         //ordinal = 2 - returns its position in its enum declaration
level.toString()=LOW
level.equals(Level.LOW)=true
level.equals(Level.MEDIUM)=false
level.equals(Level.valueOf("LOW"))=true
level.equals(Level.valueOf("MEDIUM"))=false
level.getClass()=class com.oz.core.Level
level.getDeclaringClass()=class com.oz.core.Level     //Two enum constants e1 and e2 are of the same enum type if and only if e1.getDeclaringClass() == e2.getDeclaringClass().
lvl=HIGH          //printed because of the iteration statement with in enum
lvl=MEDIUM        //printed because of the iteration statement with in enum
lvl=LOW           //printed because of the iteration statement with in enum


Hope this helps

No comments:

Post a Comment