简体   繁体   中英

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. However, when I am trying to copy the unmodifiableList through Orika mapping, it fails by throwing below exception:

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:

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:

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?

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:

Add a freeze method to your objects. 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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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