gpt4 book ai didi

java - 如何通过函数传递枚举定义并在 java 中访问其成员?

转载 作者:搜寻专家 更新时间:2023-11-01 03:35:43 28 4
gpt4 key购买 nike

我正在使用 mongodb,为了进行数据验证,我正在尝试使用 java 枚举。我在枚举中定义了数据库模式,并试图通过 validationFunction 传递该枚举和实际数据。

枚举定义如下。

//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;
}
}

现在我想通过一个函数传递 employeeSchema 来验证数据。

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.
}

所以问题是,我想通过函数传递枚举,但需要具有通用返回类型,因为当我以枚举类型收集它时,它没有相应的枚举值。

例如,如果我传递 employeeSchema 并执行 employeeSchema.text 它说 Enum type does not have text。

我希望我的问题很清楚。提前致谢。

最佳答案

我对您的代码的看法如下:

  1. Type 设为一个类,并将其与 schema 字段合并。

  2. 使接口(interface)类似于Field,并使所有枚举实现它:

这是Field接口(interface):

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's 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
}
}

关于java - 如何通过函数传递枚举定义并在 java 中访问其成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32351582/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com