How to pass enum as an argument in a method in java?

后端 未结 6 1106
走了就别回头了
走了就别回头了 2021-01-03 18:47
public class Enumvalues{

    enum courseList {
        JAVA,
        C,
        PYTHON,
        PERL
    }

    enum generalInformation {
        NAME,
        AGE,         


        
6条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 19:04

    TestEnumMain.java:

    package enumSample;
    
    import org.apache.commons.lang3.EnumUtils; 
    
        public static void main(String[] args) {
            boolean result= isValidEnum(DummyEnum.class, "APPLE"); 
            System.out.println("result= " + result); 
            boolean result2= isValidEnum2(DummyEnum.class, "COF"); 
            System.out.println("result2= " + result2); 
            //boolean result3= isValidEnum2(A_Different_Enum.class, "COF");  //works
            //System.out.println("result3= " + result3); 
    
        }
    
        public static> boolean isValidEnum2(Class enumClass, String enumName) {
    
            System.out.println("enumClass.getEnumConstants().length= " + enumClass.getEnumConstants().length); 
            System.out.println("enumClass.getEnumConstants()[0]= " + enumClass.getEnumConstants()[0]); 
            System.out.println("enumClass.getEnumConstants()[1]= " + enumClass.getEnumConstants()[1].name()); 
            System.out.println("enumName.valueOf(enumClass)= "+ enumName.valueOf(enumClass));
    
            if(enumName== null) {
                return false; 
            } else {
                try {
                    if(enumClass.isAssignableFrom(DummyEnum.class)) { /*Checks if enumClass is an instance of DummyEnum.class*/
                        for (int i=0; i!= enumClass.getEnumConstants().length; i++) {
                            System.out.println("(DummyEnum) enumClass.getEnumConstants()["+i+"]).code()= "+ ((DummyEnum) enumClass.getEnumConstants()[i]).code());          
                            if(enumName.equals(((DummyEnum) enumClass.getEnumConstants()[i]).code())) {
                                return true; 
                            }
                        }                   
                        return false; 
                    } else {
                        Enum.valueOf(enumClass, enumName); 
                        return true; 
                    }
                } catch (IllegalArgumentException var3){
                    return false; 
                }
            }
    
        }
    
        //Original Method from apache.commons.long3.EnumUtils: 
        public static> boolean isValidEnum(Class enumClass, String enumName) {
            if(enumName== null) {
                return false; 
            } else {
                try {
                    Enum.valueOf(enumClass, enumName); 
                    return true; 
                } catch (IllegalArgumentException var3){
                    return false; 
                }
            }
        }
    }
    

    DummyEnum.java:

    package enumSample;
    
    public enum DummyEnum {
        APPLE("APP"), 
        GOOGLE("GOOG"), 
        CAPITAL_ONE("COF");
    
        private String code; 
        DummyEnum(String code) {this.code= code;}
        public String code() {return code;}
    }
    

提交回复
热议问题