Geeks With Blogs
Tex-blog Mobile and other stuff

Few weeks ago while preparing for my SCJP exam I reed about enum constructs in Java 5.0. While I was reading how many features it provides I was wondering if I will ever really need it. As my primary programming language is C++, enums are simple tools to group constants of same category and benefit from some type safety. Java in version 5.0 adds among others, ability to specify constructor that will individually initialize each enum element, and also allow to add variables that can be accessed individually by each enumeration element. Each enumeration element behaves as if it was a single class instance.

Recently I was modeling system for parsing rfc2445 - ICalendar files. I was looking for flexible and easy to grow method to represent each component (VTODO, VEVENT - its like appointments in Outlook), component's properties (DESCRIPTION, DTSTART) and parameters (ie. CN - common name). I didn't want to add lots of new code when adding implementation for new property parsing. The idea that I come up with was to use new enum features with reflection.

Reading ICalendar files requires parsing lines of the .ics files - called content lines. Each content line, possesses name that identifies what component or property it represents. For example:

DTSTART;VALUE=DATE:20070502

DTSTART is name of property, VALUE=DATA is parameters, and all right of ‘=’ sign is property value.

Lets look at how to represent association of property names with classes that will know how to parse content lines with such name. Model for property parameters is similar. Following enumeration associates property names with their classes:

static public enum ComponentPropertyType {      

    // Descriptive Component Properties
    ATTACH("ATTACH", AttachComponentProperty.class),    
   
CATEGORIES("CATEGORIES", CategoriesComponentProperty.class),     
   
CLASS("CLASS", ClassComponentProperty.class),
    COMMENT("COMMENT", CommentComponentProperty.class)

    // ... many more
    ;
   
    Type(String p_name, Class p_typeClass) {
        name = p_name;
        typeClass = p_typeClass;
    }
    Type(String p_name) {
        name = p_name;
        typeClass = XUnknownComponentProperty.class;
    }
       
    String name;
    String getName() { return name; }
       
    Class typeClass;
    Class getTypeClass() { return typeClass; }
}

Attach ComponentProperty is defined as follows:

interface IPropertyBase {
    void init(string name, string params, string value);// More operations …
}

class AttachComponentProperty implements IPropertyBase {

    public void init(string name, string params, string value)  {
        // Parsing of params and value. Params parsing can be done in general way.
        // Value parsing is more specific for each property.
    }
}

Now when .ics file is being parsed, ie. with the use of regular expressions, name for properties will be found. Using those names, appropriate enumeration elements will be found and associated with them class will be available. When class is known it is easy to write:
 

// .. parsing content lines and matching names with enumeration elements
ComponentPropertyType foundPropType = ComponentPropertyType. ATTACH;
IPropertyBase newProperty = (IPropertyBase) foundPropType.getTypeClass().newInstance();
newProperty.init(name, params, value);

After writing parsing code, now it is easy to add parsing for new properties or parameters.

My other approach used SableCC, compiler compiler, but I found it to be much to complicated. Actually I am not sure if this is the best way to approach problem, I am still using it in prototype version and trying to find its weak sides.

Posted on Sunday, July 1, 2007 1:07 PM | Back to top


Comments on this post: Java 5.0 enum enhancements are cool

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Martinez | Powered by: GeeksWithBlogs.net