Trying to set a destination boolean property based on source byte[] presence?

568 views
Skip to first unread message

Ninju

unread,
Apr 16, 2013, 3:24:36 PM4/16/13
to model...@googlegroups.com
Hello all,

I am trying the following:

public class UserInfoMapper extends PropertyMap<User, UserInfo> {
    @Override
    protected void configure() {
        Converter<byte[], Boolean> isBytesNotNull = new Converter<byte[], Boolean>() {
            @Override
            public Boolean convert(MappingContext<byte[], Boolean> context) {
                return context.getSource() != null;
            }
        };
        using(isBytesNotNull).map().setPhotoAvailable(source.getPhoto());
        skip().setPhoto(null);
    }
}

But I get the following compile error:

java: method setPhotoAvailable in class com.asics.model.UserInfo cannot be applied to given types;
  required: boolean
  found: byte[]
  reason: actual argument byte[] cannot be converted to boolean by method invocation conversion

source.getPhoto() returns a byte[] while setPhotoAvailable(boolean) expects boolean

What is the right way to do this?

Thanks!


Luis Cruz

unread,
Apr 16, 2013, 3:44:54 PM4/16/13
to model...@googlegroups.com

   Hi, try this:

using(isBytesNotNull).map(source.getPhoto()).setPhotoAvailable(null);




--
You received this message because you are subscribed to the Google Groups "modelmapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modelmapper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ninju

unread,
Apr 16, 2013, 4:04:02 PM4/16/13
to model...@googlegroups.com
Close...because my setPhotoAvailable(...) takes a primitive boolean and not an object Boolean I had to 'cast' the null

        using(isBytesNotNull).map(source.getPhoto()).setPhotoAvailable(Boolean.parseBoolean(null));

But after that it worked like a charm!!

Just curious...would it possible to the generalize the converter to work with any source type.  Would the following work?

        Converter<?, Boolean> isSourceNotNull = new Converter<?, Boolean>() {
            @Override
            public Boolean convert(MappingContext<?, Boolean> context) {
                return context.getSource() != null;
            }
        };

And then I can reuse this converter in many places!!

Jonathan Halterman

unread,
Apr 16, 2013, 11:14:32 PM4/16/13
to model...@googlegroups.com
Hey Ninju,


On Tuesday, April 16, 2013 1:04:02 PM UTC-7, Ninju wrote:
Close...because my setPhotoAvailable(...) takes a primitive boolean and not an object Boolean I had to 'cast' the null

        using(isBytesNotNull).map(source.getPhoto()).setPhotoAvailable(Boolean.parseBoolean(null));

I would think:

using(isBytesNotNull).map(source.getPhoto()).setPhotoAvailable(false);

..would work as well (for simplicity). Basically the false value is ignored since the real value is being provided by your converter. 
 

But after that it worked like a charm!!

Just curious...would it possible to the generalize the converter to work with any source type.  Would the following work?

        Converter<?, Boolean> isSourceNotNull = new Converter<?, Boolean>() {
            @Override
            public Boolean convert(MappingContext<?, Boolean> context) {
                return context.getSource() != null;
            }
        };

It depends how you register that converter for use. If you use ModelMapper.addConverter, I think you'll get an exception since the source type is not defined. You should be able to reuse your converter if you set it against individual TypeMaps:

modelMapper.createTypeMap(Source.class, Destination.class).setConverter(myConverter);

Cheers,
Jonathan

Jonathan Halterman

unread,
Apr 16, 2013, 11:17:47 PM4/16/13
to model...@googlegroups.com
Hey Ninju,

There is one more ModelMapper abstraction that you might want to check out as an alternative to using a Converter here: Conditions. Ex:

when(Conditions.notNull).map().setPhotoAvailable(source.getPhoto());

Cheers,
Jonathan
Reply all
Reply to author
Forward
0 new messages