gpt4 book ai didi

java - Enum 类中的 Enum 变量

转载 作者:行者123 更新时间:2023-12-01 16:35:39 26 4
gpt4 key购买 nike

请帮助找到以下情况的最佳方法:

有一个表,大约有 20 列。
每列都有自己的短名称、全名称和类型(数字或字符串)。
每个列类型都可以有自己的运算符 - 例如,字符串 - 包含、等于;数字 - 更多、更少、==、!=
每个操作符都可以有自己的描述。

我必须拥有 Table 类的对象,并且能够查看其所有列,查看每列的短名称和全名,根据列类型使用运算符。

我正在尝试使用枚举,但我不知道如何将特定列连接到特定类型。
例如,如何将“Id”列连接到“StringType”,将“Services”列连接到“NumberType”。请您帮忙。

class Table{

public enum Column {
Id("id", "ID number"),
Services("serv", "Services");

private final String shortName;
private final String fullName;

Column(String shortName, String fullName) {
this.shortName = shortName;
this.fullName = fullName;
}

public String getShortName() {
return shortName;
}

public String getFullName() {
return fullName;
}
}


public enum StringType{
contains("String contain another string"),
equals("String equal a string");

private final String placeholder;

StringType(String fullName) {
this.placeholder = fullName;
}

public String getPlaceholder() {
return placeholder;
}
}

public enum NumberType{
more("value that is more than input"),
less("value that is less than input");

private final String placeholder;

NumberType(String fullName) {
this.placeholder = fullName;
}

public String getPlaceholder() {
return placeholder;
}
}

}

最佳答案

与任何其他类一样,枚举类型可以实现接口(interface)。您可以利用这一点来发挥自己的优势:

public interface DataType {
// Deliberately empty. This is a marker interface.
}

public enum StringType
implements DataType {
// ...
}

public enum NumberType
implements DataType {
// ...
}

public enum Column {
Id("id", "ID number", StringType.class),
Services("serv", "Services", NumberType.class);

private final String shortName;
private final String fullName;
private final Class<? extends DataType> type;

Column(String shortName, String fullName, Class<? extends DataType> type) {
this.shortName = shortName;
this.fullName = fullName;
this.type = type;
}

// ...
}

如果您打算实际使用它们来比较数据,您可以向 DataType 接口(interface)添加方法:

public interface DataType<T> {
Class<T> getDataClass();

BiPredicate<? super T, ? super T> getTest();

default boolean test(T value1, T value2) {
return getTest().test(value1, value2);
}

default boolean testObjects(Object value1, Object value2) {
Class<T> type = getDataClass();
return test(type.cast(value1), type.cast(value2));
}
}

public enum StringType
implements DataType<String> {
contains("String contain another string", String::contains),
equals("String equal a string", Object::equals);

private final String placeholder;
private final BiPredicate<? super String, ? super String> test;

StringType(String fullName,
BiPredicate<? super String, ? super String> test) {
this.placeholder = fullName;
this.test = test;
}

public String getPlaceholder() {
return placeholder;
}

@Override
public BiPredicate<? super String, ? super String> getTest() {
return test;
}

@Override
public Class<String> getDataClass() {
return String.class;
}
}

public enum NumberType
implements DataType<Number> {
more("value that is more than input",
(n1, n2) -> n1.doubleValue() > n2.doubleValue()),
less("value that is less than input",
(n1, n2) -> n1.doubleValue() < n2.doubleValue());

private final String placeholder;
private final BiPredicate<? super Number, ? super Number> test;

NumberType(String fullName,
BiPredicate<? super Number, ? super Number> test) {
this.placeholder = fullName;
this.test = test;
}

public String getPlaceholder() {
return placeholder;
}

@Override
public BiPredicate<? super Number, ? super Number> getTest() {
return test;
}

@Override
public Class<Number> getDataClass() {
return Number.class;
}
}

public enum Column {
Id("id", "ID number", StringType.class),
Services("serv", "Services", NumberType.class);

private final String shortName;
private final String fullName;
private final Class<? extends DataType<?>> type;

Column(String shortName, String fullName, Class<? extends DataType<?>> type) {
this.shortName = shortName;
this.fullName = fullName;
this.type = type;
}

// ...
}

关于java - Enum 类中的 Enum 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61953120/

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