Designing Intelligent Constants - Java Language Basics

Java examples for Language Basics:enum

Introduction

An enum type can have state and behavior like any other class.

Demo Code


import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

enum FieldType {/*ww w  .ja  va2s  .c  om*/

    PASSWORD(FieldType.passwordFieldName) {

        @Override
        public boolean validate(String fieldValue) {
            return fieldValue != null && fieldValue.length()>2;
        }
    },

    EMAIL_ADDRESS(FieldType.emailFieldName) {


        @Override
        public boolean validate(String fieldValue) {
            return fieldValue != null && fieldValue.length()>4;    
        }
    },

    PHONE_NUMBER(FieldType.phoneFieldName) {

        @Override
        public boolean validate(String fieldValue) {
            return fieldValue != null && fieldValue.length()>6;
        }
    },

    SOCIAL_SECURITY_NUMBER(FieldType.ssnFieldName) {

        @Override
        public boolean validate(String fieldValue) {
            return fieldValue != null && fieldValue.length()>8;
        }

    };  // End of enum constants definition

    private String fieldName;
    
    static final String passwordFieldName = "password";
    static final String emailFieldName = "email";
    static final String phoneFieldName = "phone";
    static final String ssnFieldName = "ssn";

    private FieldType(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getFieldName() {
        return this.fieldName;
    }

    abstract boolean validate(String fieldValue);
    
    private static final Map<String, FieldType> nameToFieldTypeMap = new HashMap<>();

    static {
        for (FieldType field : FieldType.values()) {
            nameToFieldTypeMap.put(field.getFieldName(), field);
        }
    }

    public static FieldType lookup(String fieldName) {
        return nameToFieldTypeMap.get(fieldName.toLowerCase());
    }

    private static void printValid(FieldType field, String fieldValue, boolean valid) {
        System.out.println(field.getFieldName() + 
                           "(\"" + fieldValue + "\") valid: " + valid);
    }

    public static void main(String... args) {
                
        String fieldName = FieldType.passwordFieldName;
        String fieldValue = "1Cxy9";  
        FieldType field = lookup(fieldName);
        printValid(field, fieldValue, field.validate(fieldValue));

        fieldName = FieldType.phoneFieldName;
        fieldValue = "1-800-555-1234";
        field = lookup(fieldName);
        printValid(field, fieldValue, field.validate(fieldValue));

        fieldName = FieldType.emailFieldName;
        fieldValue = "john@doe";  // invalid - missing .<tld>
        field = lookup(fieldName);
        printValid(field, fieldValue, field.validate(fieldValue));

        fieldName = FieldType.ssnFieldName;
        fieldValue = "111-11-1111";  // valid
        field = lookup(fieldName);
        printValid(field, fieldValue, field.validate(fieldValue));
    }
}

Related Tutorials