Tuesday, November 5, 2013

Java Enum Example

Here is an example of Enum in Java
public class EnumExample {
    public static void main(String []args){
        
        System.out.println(SearchCriteriaComparisonExpression.equal);
        System.out.println(SearchCriteriaComparisonExpression.equal.sqlExpression());
        
        for(SearchCriteriaComparisonExpression value: SearchCriteriaComparisonExpression.values()){
            System.out.println(value.name()+" | "+value);
            
        }
        System.out.println();
    }

    
}
enum SearchCriteriaComparisonExpression {

    /**
     *
     */    
    equal("equal") , // equal (=)    
    notequal("notequal"), // Not equal (<>)
    greaterthan("gt"), // Greater Than (>)
    greaterthanorequal("ge"), // Greater Than or equal (>=)
    lessthan("lt"), // // Less Than (<)
    lessthanorequal("le"), // Less Than or equal (<=)       
    isnull("isnull"), // IS NULL
    isnotnull("isnotnull"); // IS NOT NULL
    
    private String expression;
   
    private SearchCriteriaComparisonExpression(String expression) {
        this.expression = expression;       
    }
       
    @Override
    public String toString() {        
        return this.expression.toLowerCase();
    }

    public String sqlExpression() {
        String value = "";
        if (this.compareTo(equal) == 0) {
            value = "=";
        } else if (this.compareTo(notequal) == 0) {
            value = "<>";
        } else if (this.compareTo(greaterthan) == 0) {
            value = ">";
        } else if (this.compareTo(greaterthanorequal) == 0) {
            value = ">=";
        } else if (this.compareTo(lessthan) == 0) {
            value = "<";
        } else if (this.compareTo(lessthanorequal) == 0) {
            value = "<=";
        } else if (this.compareTo(isnull) == 0) {
            value = "IS NULL";
        } else if (this.compareTo(isnotnull) == 0) {
            value = "IS NOT NULL";
        } 
        return value;
    }
}

No comments:

Post a Comment