How to pass the definition of an enumeration through a function and access to its members there in java?

I am using mongodb and for data verification I am trying to use java enumerations. I have defined the database schema in enum and am trying to pass this enumeration and actual data through the validationFunction function.

The definition of an enumeration is given below.

//Enum definition
public enum Type {
  STRING, OBJECT, INTEGER    
}

public enum Existence {
  REQUIRED, OPTIONAL
}

public enum Adress {
  HOUSE_NO("houseNo", Type.STRING, Existence.REQUIRED),
  STREET_NO("streetNo", Type.STRING, Existence.REQUIRED),
  LANDMARK("landmark", Type.STRING, Existence.OPTIONAL)
      public enum employeeSchema {
  NAME("name", Type.STRING, Existence.REQUIRED),
  AGE("age", Type.INTEGER, Existence.Optional),
  ADDRESS("address", Type.OBJECT, Existence.REQUIRED)

  String text;
  Type valueType;
  Existence exist;
  Address(String text, Type valueType, Existence exist) {
    this.text = text;
    this.valueType = valueType;
    this.exist = exist;
  }
}

public enum employeeSchema {
  NAME("name", Type.STRING, Existence.REQUIRED),
  AGE("age", Type.INTEGER, Existence.Optional),
  ADDRESS("address", Type.OBJECT, Existence.REQUIRED, Address)

  String text;
  Type valueType;
  Existence exist;
  employeeSchema(String text, Type valueType, Existence exist) {
    this.text = text;
    this.valueType = valueType;
    this.exist = exist;
  }
  employeeSchema(String text, Type valueType, Existence exist, Enum schema) {
    this.text = text;
    this.valueType = valueType;
    this.exist = exist;
    this.schema = schema;
  }
}

Now I want to pass employeeSchema through a function to validate the data.

public boolean validateData(JsonNode data, Enum schema){
  //Want to iterate the enum here. Get the corresponding field from the data and will check if the field has type required and if its null it will return false. Again if the field would be object this function would be called with respective schema.
}

So, the problem is that I want to pass enum through a function, but I need to have a common return type, because when I assemble it into an Enum type, it does not have the corresponding enum values.

For example, if I pass employeeSchema and execute employeeSchema.text, it says that the Enum type has no text.

I hope my problem is clear. Thank you in advance.

+4
2

:

  • Type schema.

  • Field :

Field:

public interface Field {
    String getText();
    Type getType();
    Existence getExistence();
}

Type:

public static final class Type {
    // predefined primitive types
    public static final Type INTEGER = new Type(Integer.class);
    public static final Type STRING = new Type(String.class);

    private final Class<?> clazz;

    private Type(Class<?> clazz) {
        this.clazz = clazz;
    }

    // Object types are created by this constructor     
    public static Type of(Class<? extends Field> fieldClass) {
        return new Type(fieldClass);
    }

    @Override
    public int hashCode() {
        return clazz.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null || getClass() != obj.getClass())
            return false;
        return clazz.equals(((Type) obj).clazz);
    }

    public Class<?> getValueClass() {
        return clazz;
    }

    // retrieve all possible Fields for given object type
    public Field[] values() {
        if(Field.class.isAssignableFrom(clazz)) {
            return clazz.asSubclass(Field.class).getEnumConstants();
        }
        return null;
    }
}

:

public enum Address implements Field {
    HOUSE_NO("houseNo", Type.STRING, Existence.REQUIRED), 
    STREET_NO("streetNo", Type.STRING, Existence.REQUIRED), 
    LANDMARK("landmark", Type.STRING, Existence.OPTIONAL);

    String text;
    Type valueType;
    Existence exist;

    @Override
    public String getText() {
        return text;
    }

    @Override
    public Type getType() {
        return valueType;
    }

    @Override
    public Existence getExistence() {
        return exist;
    }

    Address(String text, Type valueType, Existence exist) {
        this.text = text;
        this.valueType = valueType;
        this.exist = exist;
    }
}

public enum employeeSchema implements Field {
    NAME("name", Type.STRING, Existence.REQUIRED), 
    AGE("age", Type.INTEGER, Existence.OPTIONAL), 
    ADDRESS("address", Type.of(Address.class), Existence.REQUIRED);

    String text;
    Type valueType;
    Existence exist;

    employeeSchema(String text, Type valueType, Existence exist) {
        this.text = text;
        this.valueType = valueType;
        this.exist = exist;
    }

    @Override
    public String getText() {
        return text;
    }

    @Override
    public Type getType() {
        return valueType;
    }

    @Override
    public Existence getExistence() {
        return exist;
    }
}

, validateData Type :

public boolean validateData(JsonNode data, Type type){
    Field[] values = type.values();
    if(values != null) {
        for(Field field : values) {
           ... // use field.getText()/field.getType()/etc. to validate
           // probably it ok to call recursively here
           // validateData(data.get(field.getText()), field.getType());
        }
    } else {
        Class<?> clazz = type.getValueClass();
        // clazz is a simple type like Integer or String
    }
}
+1

Enum ; NAME, . , , - :

public boolean validateData(JsonNode data, Class<? extends Enum> schema) {
  for (Enum<?> e : schema.getEnumConstants()) {
    ...
  }
}

validateData(data, Adress.class);
0

All Articles