Java Enum

Java enum can be really tricky like this:

package org.kd.examples;

public class EnumEx
{

private enum Column {
A(“a”,1)
{
public void execute(String value)
{
setExpression(“This is ” + A.title+ ” with ” + value);
};
},

B(“b”,2)
{
public void execute(String value)
{
setExpression(“This is ” + B.title+ ” with ” + value);
};
},

C(“c”,3)
{
public void execute(String value)
{
setExpression(“This is ” + C.title+ ” with ” + value);
};
};

public abstract void execute(String value);

private String title;
private int index;
private String expression;

public void setExpression(String value)
{
expression = value;
}
Column(String str, int i)
{
title = str;
index = i;
}

public String getExpression(){ return expression;}
}

public static Column getColumn(int i)
{
for(Column c : Column.values())
{
if(c.index == i)
return c;
}
return null;
}

public static void main(String argv[])
{
StringBuilder desc = new StringBuilder();

for(int i= 1 ; i < 4; i++)
{
Column c = getColumn(i);
c.execute(String.valueOf(i +4));
}

for(Column c: Column.values())
System.out.println(c.getExpression());
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s