首页 > 解决方案 > 干净的代码 - 避免使用基于通用数据类型的集合进行显式类型转换

问题描述

我正在使用 Map 从电子表格中读取行并以下列方式存储其内容:

public class DocumentRow {

    private Map<Column, DocumentCell<?>> rowContents = new HashMap<>();

    private int rowNum;

    public DocumentCell<?> getValue(Column column) {
        return rowContents.get(column);
    }

    public void setValue(Column column, DocumentCell<?> value) {
        rowContents.put(column, value);
    }

    public DigitalDocument toDomainObject() {
        DomainObject domainObject = new DomainObject();
        domainObject.setTextValue((String) rowContents.get(TEXT_VALUE).getValue());
        domainObject.setNumericValue((int) rowContents.get(NUMERIC_VALUE).getValue());
        domainObject.setDateValue((LocalDate) rowContents.get(DATE_VALUE).getValue());
        return domainObject;
    }
}

public class DocumentCell<T> {
    private T value;
}

public enum Column {
    TEXT_VALUE("Text_Column_Name", DataType.STRING),
    NUMERIC_VALUE("Numeric_Column_Name", DataType.NUMBER),
    DATE_VALUE("Date_Column_Name", DataType.DATE);
}

(为简洁起见,我省略了一些明显的类)

行值提供为:

row.setValue(column, new DocumentCell<>(getDateCellValue(spreadSheetCell)));

有没有办法可以使这更干净,以便在构造域对象时不需要这些未经检查的强制转换?或者更好的设计方法?

标签: javagenericsrefactoringreusability

解决方案


enum 的问题是您不能使用泛型类型。您可以在如何使用泛型实现枚举?.

首先,我们需要使用泛型类型创建一个带有常量的“类枚举”类。

class Column<T> {
    public static final Column<String> TEXT_VALUE = new Column<>("Text_Column_Name", String.class);
    public static final Column<Number> NUMERIC_VALUE = new Column<>("Numeric_Column_Name", Number.class);
    public static final Column<Date> DATE_VALUE = new Column<>("Date_Column_Name", Date.class);

    String name;
    Class<T> clazz;

    private Column(String name, Class<T> clazz){
        this.name = name;
        this.clazz = clazz;
    }
}

这样,我们可以确保在映射中插入值以匹配Column方法的类型:

public <U> void setValue(Column<U> column, DocumentCell<U> value) {
    rowContents.put(column, value);
}

例子 :

DocumentRow row = new DocumentRow();
row.setValue(Column.TEXT_VALUE, new DocumentCell<String>("asdf"));
row.setValue(Column.TEXT_VALUE, new DocumentCell<Integer>(4)); //Don't compile, can't set an `Integer` document cell into a `Column.TEXT_VALUE`

现在,我们确定插入的值 forColumn.TEXT_VALUE将持有 a String,并且对于每个Column常量都相同。

由于我们在插入过程中对类型进行了保险,因此我们可以脏并DocumentCell<?>从映射中转换为相同的类型Column

public <U> U getValue(Column<U> column) {
    @SuppressWarnings("unchecked")
    DocumentCell<U> doc = (DocumentCell<U>) rowContents.get(column);
    return doc.getValue(column);
}

结果的一个小例子:

String s = row.getValue(Column.TEXT_VALUE);
Integer i = row.getValue(Column.TEXT_VALUE); //DON'T COMPILE : `row.getValue` will return a value of the type define by `Column`, here a `String`

完整的使用示例:

DocumentRow row = new DocumentRow();
row.setValue(Column.TEXT_VALUE, new DocumentCell<>("asdf"));
row.setValue(Column.NUMERIC_VALUE, new DocumentCell<>(4));
row.setValue(Column.DATE_VALUE, new DocumentCell<>(new Date()));

String s = row.getValue(Column.TEXT_VALUE);
Number i = row.getValue(Column.NUMERIC_VALUE);
Date d = row.getValue(Column.DATE_VALUE);

请注意,在上一个示例中,我没有提供 for 的类型DocumentCell,编译器知道它将使用与Column之前相同的 a 参数。

你可以在这个 ideone项目上找到完整的代码。


当然,我们可以删除“类枚举”部分并Column根据需要初始化实例。我们需要做的就是将构造函数设置为可见(至少不是私有的),我们可以创建新的“列类型映射”

Column<LocalDateTime> colDate = new Column<>("A new Date", LocalDateTime.class);
row.setValue(colDate , new DocumentCell<>(LocalDateTime.now()));

推荐阅读