【问题标题】:SuperCSV Dozer deep-mapping map with enum and subclasses?带有枚举和子类的 SuperCSV Dozer 深度映射图?
【发布时间】:2013-02-14 20:45:27
【问题描述】:

我正在尝试使用 SuperCSV 和 Dozer 是否可以做到这一点,或者我是否应该恢复到 Map 解析。我有一个具有 Map 成员字段的 POJO。值得庆幸的是,在 CSV 解析期间,我知道应该构建 MyInterface 的哪个特定子类,并且 MyEnum 的值也是静态的。但是我将如何在列映射中设置所有这些?谢谢!

目前,我的单元处理器具有这种结构,并且我正在使用 CsvMapReader。

private static final CellProcessor[] CELL_PROCESSORS = new CellProcessor[] {
        new NotNull(new Trim(new StrRegEx("^\\d{10,}$"))),  // phone1
        new Optional(new Trim(new StrRegEx("^\\d{10,}$"))), // phone2
        new Optional(new Trim(new StrRegEx("^\\d{10,}$"))), // phone3
        new Optional(new Trim()),                           // callVar1
        new Optional(new Trim()),                           // callVar2
        new Optional(new Trim()),                           // callVar3
        new Optional(new Trim()),                           // callVar4
        new Optional(new Trim()),                           // callVar5
        new Optional(new Trim()),                           // callVar6
        new Optional(new Trim()),                           // callVar7
        new Optional(new Trim()),                           // callVar8
        new Optional(new Trim()),                           // callVar9
        new Optional(new Trim()),                           // callVar10
};

private Contact mapRowToContact(Map<String, Object> row) {
    Contact contact = new Contact();

    MyPhoneContactMethodData methodData = new MyPhoneContactMethodData();

    List<Phone> phones = new ArrayList<>();
    Phone phone = new Phone();
    phone.setPhoneNumber((String)row.get("phone1"));
    phones.add(phone);
    phone = new Phone();
    phone.setPhoneNumber((String)row.get("phone2"));
    if (phone.getPhoneNumber() != null) {
        phones.add(phone);
    }
    phone = new Phone();
    phone.setPhoneNumber((String)row.get("phone3"));
    if (phone.getPhoneNumber() != null) {
        phones.add(phone);
    }
    methodData.setPhones(phones);

    List<String> callVars = new ArrayList<>();
    callVars.add((String)row.get("callVar1"));
    callVars.add((String)row.get("callVar2"));
    callVars.add((String)row.get("callVar3"));
    callVars.add((String)row.get("callVar4"));
    callVars.add((String)row.get("callVar5"));
    callVars.add((String)row.get("callVar6"));
    callVars.add((String)row.get("callVar7"));
    callVars.add((String)row.get("callVar8"));
    callVars.add((String)row.get("callVar9"));
    callVars.add((String)row.get("callVar10"));
    methodData.setEnterpriseCallVarData(callVars);

    Map<ContactMethod, ContactMethodData> methodDataMap = new HashMap<>();
    methodDataMap.put(ContactMethod.PHONE, methodData);
    contact.setContactMethodData(methodDataMap);

    return contact;
}

Contact 具有这种结构,还有许多其他不相关的字段:

public class Contact {
    private Integer id;
    private Map<ContactMethod, ContactMethodData> contactMethodData;
}

ContactMethod 是一个枚举,其值为PHONEEMAILContactMethodData是一个接口,MyPhoneContactMethodData的超类实现了这个接口。

【问题讨论】:

  • 你能发布一些示例代码吗?我是一名超级 CSV 开发人员 - 我可以提供帮助,但前提是我了解问题所在! :)

标签: map enums subclass supercsv


【解决方案1】:

感谢代码 - 现在更容易理解了 :)

您应该能够通过使用以下 bean 映射将 CSV 的每一行作为 MyPhoneContactMethodData 实例读取。请确保在阅读之前使用此信息致电 configureBeanMapping()(如 Super CSV website 所示)。

然后,您必须手动创建 Contact 并将 MyPhoneContactMethodData 添加到 contactMethodData 映射,使用 ContactMethod.PHONE 作为键(如代码的最后 3 行中所做的那样)。

final String[] beanMapping = new String[]{
    "phones[0].phoneNumber",
    "phones[1].phoneNumber",
    "phones[2].phoneNumber",
    "enterpriseCallVarData[0]",
    "enterpriseCallVarData[1]",
    "enterpriseCallVarData[2]",
    "enterpriseCallVarData[3]",
    "enterpriseCallVarData[4]",
    "enterpriseCallVarData[5]",
    "enterpriseCallVarData[6]",
    "enterpriseCallVarData[7]",
    "enterpriseCallVarData[8]",
    "enterpriseCallVarData[9]"
};

beanReader.configureBeanMapping(MyPhoneContactMethodData.class, beanMapping);

MyPhoneContactMethodData methodData;
while( (methodData = 
    beanReader.read(MyPhoneContactMethodData.class, CELL_PROCESSORS)) != null ) {
    // add to contact
}

【讨论】:

  • 谢谢。这就说得通了。无论如何,我最终可能最终还是坚持使用 CsvMapReader,因为 Contact 中的其他一些字段非常复杂,为了使事情与手头的问题保持隔离,这里没有显示这些字段。
猜你喜欢
  • 2012-01-06
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 2015-06-27
相关资源
最近更新 更多