I have a use case where I want to map objects to a ByteBuffer for transmission....
@Mapper
public static interface ByteBufferMapper {
public static ByteBufferMapper INSTANCE = Mappers.getMapper(ByteBufferMapper.class);
default byte toByte(ByteBuffer buffer) {
byte b = buffer.get();
return b;
}
}
public static class Dto {
public byte b;
public byte bb;
...
}
@Mapper(uses = ByteBufferMapper.class)
public static interface DtoMapper {
public static DtoMapper INSTANCE = Mappers.getMapper(DtoMapper.class);
@Mapping(source = "buffer", target = "bb")
@Mapping(source = "buffer", target = "b")
Dto byteBufferToDto(ByteBuffer buffer);
}
public static void main( String[] args ) {
ByteBuffer buffer = ByteBuffer.allocate(2).put((byte) 0xFF).put((byte) 0x00).flip();
System.out.println(DtoMapper.INSTANCE.byteBufferToDto(buffer));
}
Is there a way I can control MapStructs mapping order so the b variable gets populated with the 0xFF and bb gets populated with the 0x00 value?
Yes you can by means of @Mapping.dependsOn.
Like this:
@Mappings({
@Mapping(target = "surName", source = "lastName", dependsOn = "middleName"),
@Mapping(target = "middleName", dependsOn = "givenName"),
@Mapping(target = "givenName", source = "firstName")
})
AddressDto addressToDto(Address address);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With