Wednesday, July 27, 2011

Extending enums in Java

public enum DataStatus
{
    DECLINED_BY_USER("DECLINED-BY-USER"),
    SUCCESSFUL("SUCCESSFUL"),
    SPAM("SPAM");

    private String code;
    
    private DataStatus(String code) {
         this.code = code;
    }
    
    public String getCode() { return code; }
    
    public String toString(){ return code; } // if you want it so
    
    public static DataStatus parse( String s ){
        s = s.toUpperCase();
        if( s.equals( "SUCCESSFUL" ) )
            return SUCCESSFUL;
        if( s.equals( "DECLINED-BY-USER" ) )
            return DECLINED_BY_USER;
        if( s.equals( "SPAM" ) )
            return SPAM;
        return null;
    }
}    

No comments:

Post a Comment