Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Copy all values from fields in one class to another class

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 2 classes:



Cloud you tell me how to copy fields from class A to B
Is it a good idea to use Gson?  are there better solutions ?


 
Saloon Keeper
Posts: 9272
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apache-commons, BeanUtils.copyProperties( dest, src ).
 
Saloon Keeper
Posts: 13868
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on what A and B really represent and what they are used for. Tell us more about your application.

My preferred way is to just write a constructor.
 
Bronislav Bronek
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must write web service that call other web service. My web service do nothing just only works as a proxy 1-1 with outer web service.
I cannot set up output as a  response from the outer directly.
I must create my objects, fill my objects from outer response and set up as a output.

 
Carey Brown
Saloon Keeper
Posts: 9272
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Apache-commons, BeanUtils.copyProperties( dest, src ).

Read up on this. I haven't used it but the doc says it tries to match up fields to copy and will perform common type conversions if necessary. Sounds like it might be useful.
 
Stephan van Hulst
Saloon Keeper
Posts: 13868
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bronislav Bronek wrote:I cannot set up output as a  response from the outer directly.


Why not?
 
Bronislav Bronek
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a convention in the company where I am now.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bronislav Bronek wrote:I have 2 classes:



Cloud you tell me how to copy fields from class A to B
Is it a good idea to use Gson?  are there better solutions ?




It looks like you want to use JSON to convert two types from one to the other? Besides being highly inefficient, you introduce the limitations and complexities of JSON where it is not necessarily needed. In some cases it might be needed, i.e., you have a JSON-serialized version of A
If you have direct access to each type, why not write a B.from(A) or A.to(B) static method? No need for JSON if data transfer/persistence is not required.
Just curious, how does this case arise where two classes A and B are identical? What is the semantic difference? Perhaps a refactoring is in order, eliminating the need to convert between two identical types.
 
Carey Brown
Saloon Keeper
Posts: 9272
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bronislav Bronek wrote:This is a convention in the company where I am now.

What purpose does this convention serve? Obfuscation?
 
Saloon Keeper
Posts: 25466
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My go-to tool to copy data between class instances - and yes, there are lots of cases where that's desirable - is an open-source project caller Dozer.

In its simplest form, it can automatically detect and copy files by class introspection, saving not only a ton of grunt coding but also eliminating the need to modify copy code if fields are added/removed/changed.

In its more complex forms it can work on fields selectively and do other useful transfer functions.
 
Those are the largest trousers in the world! Especially when next to this ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic