简体   繁体   English

不可修改清单的Orika对应

[英]Orika Mapping for unmodifiable List

I have got two immutable bean classes for which I am using Orika mapping to copy the values from one to other. 我有两个不可变的 bean类,正在使用Orika映射将它们从一个值复制到另一个。 However, when I am trying to copy the unmodifiableList through Orika mapping, it fails by throwing below exception: 但是,当我尝试通过Orika映射复制unmodifiableList ,由于抛出以下异常而失败:

java.lang.UnsupportedOperationException 
at ma.glasnost.orika.ExceptionUtility.newMappingException(ExceptionUtility.java:55)
at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:681)
at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:650)
at com.myproject.OrikaTest.testEmployeeMapping

I have provided the code below with which you can replicate the same issue: 我提供了以下代码,可用来复制相同的问题:

EmployeeDto class: EmployeeDto类:

public final class EmployeeDto {
    private final int id;
    private final String name;
    private final List<String> previousGrades;

    public EmployeeDto(int id, String name, List<String> previousGrades) {
        this.id = id;//validations removed
        this.name = name;//validations removed
        //Commented unmodifiableList as it does not work
        //this.previousGrades = Collections.unmodifiableList(previousGrades);
        this.previousGrades = previousGrades;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<String> getPreviousGrades() {
        //tried like this, even this does not work
        return Collections.unmodifiableList(previousGrades);
    }
}

Employee class: 员工类别:

public final class Employee {
    //ditto AS EmployeeDto
}

OrikaTest class: OrikaTest类:

public class OrikaTest {

    private static final MapperFactory mapperFactory = 
           new DefaultMapperFactory.Builder().build();

    private static final MapperFacade mapperFacade = 
             mapperFactory.getMapperFacade();

    @Test
    public void testEmployeeMapping() {
        List<String> employeeGrades = Arrays.asList("A", "B");
        Employee employee = new Employee(1234, "John", employeeGrades);

        EmployeeDto employeeDto = mapperFacade.map(employee, EmployeeDto.class);

        //tests using assertEquals
        Assert.assertEquals(employeeGrades, employeeDto.getPreviousGrades());
    }
}

I could find a link here on this subject, but it was not that clear as alternatives were not explained properly. 在这里可以找到有关此主题的链接,但是由于没有正确解释替代方法,因此不清楚。

So, can you help with an example or any workaround on how to copy the unmodifiableList through Orika mapping? 因此,您能否提供有关如何通过Orika映射复制unmodifiableList的示例或任何变通方法?

If the case is that Orika must be able to mutate the list components of the mapped objects then the following could be a work around: 如果情况是Orika必须能够更改映射对象的列表组件,则可以解决以下问题:

Add a freeze method to your objects. 向对象添加freeze方法。 When the objects are created they are in a mutable state. 创建对象时,它们处于可变状态。 After freeze has been called it is no longer possible to mutate the objects. 调用freeze后,就不再可能对对象进行突变。

It could be implemented like this: 可以这样实现:

public final class EmployeeDto {
    private final int id;
    private final String name;
    private List<String> previousGrades;

    public EmployeeDto(int id, String name, List<String> previousGrades) {
        this.id = id;//validations removed
        this.name = name;//validations removed
        this.previousGrades = previousGrades;
    }

    public void freeze() {
        previousGrades = Collections.unmodifiableList(previousGrades)
    }

    public List<String> getPreviousGrades() {
        return previousGrades;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM