You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by "David Bosschaert (JIRA)" <ji...@apache.org> on 2016/09/08 00:35:20 UTC

[jira] [Commented] (FELIX-5335) Addition of copy() method to API

    [ https://issues.apache.org/jira/browse/FELIX-5335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15472264#comment-15472264 ] 

David Bosschaert commented on FELIX-5335:
-----------------------------------------

The problem with adding a copy() method that does the same as convert(x).to(x.getClass()) is that the name would indicate more restrictive behaviour than the convert() name.

For example, if you call convert("hello").to(String.class) it will still produce "hello" but it will not be a copy since Strings are immutable, not copying is not needed. So copy("hello") would do the same, and violate what people would associate with the word copy.

Another example is where you call convert(Arrays.asList(1,2,3)).to(List.class). This will produce a new List instance with the same content as the original, but it will not be an exact copy as Arrays.asList(1,2,3) returns an instance of Arrays$ArrayList which is a private type that the converter cannot instantiate. So what is returned will still be a List but just a different implementation of it, which for practical purposes is probably not a problem at all, but it's not really an exact copy.

Therefore I think it's better to *not* add the copy() and properly document how the convert() method behaves - that in certain cases it returns a new object that can be owned and further modified by the caller. In the end what you get will be usable, but it's not always an exact copy.

[~dleangen], I hope you appreciate these thoughts. If you think it's ok to not add the copy() please close this bug.

> Addition of copy() method to API
> --------------------------------
>
>                 Key: FELIX-5335
>                 URL: https://issues.apache.org/jira/browse/FELIX-5335
>             Project: Felix
>          Issue Type: New Feature
>          Components: Converter
>            Reporter: David Leangen
>
> Just to keep a record of this somewhere... here is the request I posted to the mail list.
> ----
> bq. Is there a simple way to transform a DTO into an immutable value object?
> To put my question into context: on the bndtools list, I asked this question, and got this response Peter K.:
> bq.Not in enRoute and not in the new spec that was inspired by this service. In general you pass copies so they can do whatever they like with them.
> Fine. But I am not seeing a simple way of creating such copy. Is there a “clone” method of some sort, or do I just convert an object to another object of its own type? Like this:
>  MyDTO copy = cnv.convert( dto ).to( MyDTO.class );
> If making copies is indeed how this service is intended to be used, it would be nice to have a more explicit method for this, maybe:
>  MyDTO copy = cnv.copy( dto );



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)