1

考虑以下代码片段:

public interface FieldEnum {
    public String getEnumName();
}

public enum InvoiceStatus implements FieldEnum {
    UNCHECKED("unchecked"),
    ERROR("error"),
    OK("ok");

    private final String enumName;

    private InvoiceStatus(final String enumName) {
        this.enumName = enumName;
    }

    @Override
    public String getEnumName() {
        return enumName;
    }
}

private InvoiceBean(final Integer invoiceId, final Integer businessPartnerId, final String invoiceNumber, final Date invoiceDate, final BigDecimal priceExclVAT, final BigDecimal VAT, final BigDecimal priceInclVAT, final BigDecimal paymentDiscount, final InvoiceStatus status) {
    this.invoiceId = invoiceId;
    this.businessPartnerId = businessPartnerId;
    this.invoiceNumber = invoiceNumber;
    this.invoiceDate = invoiceDate;
    this.priceExclVAT = priceExclVAT;
    this.VAT = VAT;
    this.priceInclVAT = priceInclVAT;
    this.paymentDiscount = paymentDiscount;
    this.status = status;
}

public InvoiceBean(final ResultSet rs) throws SQLException {
    this(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getDate(4), rs.getBigDecimal(5), rs.getBigDecimal(6), rs.getBigDecimal(7), rs.getBigDecimal(8), EnumConverter.convert(rs.getString(9), InvoiceStatus.values()));
}

下面的代码安全吗?

public class EnumConverter {
    public static <T extends FieldEnum> T convert(String enumName, T[] enumValues) {
        for (T enumValue : enumValues) {
            if (enumName.equals(enumValue.getEnumName())) {
                return enumValue;
            }
        }
        throw new IllegalStateException("orm.enums.EnumConverter.convert: No suitable enum has been found. enumName = " + enumName + " fieldEnums = " + enumValues);
    }
}

或者使用它会有什么好处?(注意添加Class<T> clazz参数)

public class EnumConverter {
    public static <T extends FieldEnum> T convert(String enumName, T[] enumValues, Class<T> clazz) {
        for (T enumValue : enumValues) {
            if (enumName.equals(enumValue.getEnumName())) {
                return enumValue;
            }
        }
        throw new IllegalStateException("orm.enums.EnumConverter.convert: No suitable enum has been found. enumName = " + enumName + " fieldEnums = " + enumValues);
    }
}

EnumConverter.convert(rs.getString(9), InvoiceStatus.values(), InvoiceStatus.class)然后通过当然调用它。

问候。

4

4 回答 4

8

一个简单的答案是忘记 EnumConverter 而是使用InvoiceStatus.valueOf(rs.getString(9));

于 2013-07-23T12:35:37.697 回答
3

泛型使用第一个T类型参数的类类型。添加新的Class<T>不会增加任何安全性。事实上,您现在要求您知道将哪个类放入参数列表中,这可能很麻烦。

于 2013-07-23T12:34:38.650 回答
0

不需要额外的参数 - 编译器将能够从传递的数组中确定类型 T,因此无需显式将 Class 作为参数。

于 2013-07-23T12:35:09.903 回答
0

我建议使用类似的东西:

public enum InvoiceStatus implements FieldEnum {
    UNCHECKED("unchecked"),
    ERROR("error"),
    OK("ok");

    private static final Map<String, InvoiceStatus> invoiceStatusMap = new HashMap<>(values().length);

    static{
        for (InvoiceStatus  invoiceStatus : values()) {
            invoiceStatusMap.put(invoiceStatus.enumName, invoiceStatus);
        }
    }    
    private final String enumName;

    private InvoiceStatus(final String enumName) {
        this.enumName = enumName;
    }

    @Override
    public String getEnumName() {
        return enumName;
    }

    public static InvoiceStatus getInvoiceStatus(String enumName){
        if (invoiceStatusMap.contains(enumName)){
           return invoiceStatusMap.get(enumName);
        } else {
            throw new IllegalArgumentException("No suitable enum has been found. enumName = " + enumName + " fieldEnums = " + enumValues);
        }
    }
}
于 2013-07-23T12:40:50.787 回答