Skip to main content

How to create cloned object using Java



I have an experience with updating a user profile with checking unique email and phone number. In that case I want to get my previous profile details and want to set new updated object details. But after I set the new object details to my retrieved previous object, my previous object setting with new data. So I have a way to again recall the previous data object using a database call. 

But if you prefer to maintain a performance based app, you can reduce the database call using cloning the objects. Here actually we create the same object as old and new two objects. 

First I will show how we can achieve using Java 8 version.

Think if you have two classes Profile and DomainEntity. Here actually DomainEntity used for extend generic objects like created date, updated date etc.

@Getter
@Setter
public class DomainEntity {

    private Date createdDate;

    private Date updatedDate;

    private boolean deleted;
}

@Getter
@Setter
public class Profile extends DomainEntity {

    private String firstName;

    private String lastName;

    private String address;

    private Integer age;
}

With these classes you can see I used domain entity to share my generic objects to my profile class.

So, here is the trick goes. I will create a constructor and pass same object as a parameter to that constructor. I do the same thing for both child and parent classes.

Profile(Profile profile) {
        if (profile != null) {
            this.firstName = profile.firstName;
            this.lastName = profile.lastName;
            this.address = profile.address;
            this.age = profile.age;
        }
 }

for the domain entity class, I used the same way.

So, the final class with be looks like below.

@Getter
@Setter
public class Profile extends DomainEntity {

    private String firstName;

    private String lastName;

    private String address;

    private Integer age;

    Profile(Profile profile) {
        super(profile);
        if (profile != null) {
            this.firstName = profile.firstName;
            this.lastName = profile.lastName;
            this.address = profile.address;
            this.age = profile.age;
        }
    }
}


@Getter
@Setter
public class DomainEntity {

    private Date createdDate = new Date();

    private Date updatedDate = new Date();

    private boolean deleted;

    DomainEntity(DomainEntity entity) {
        if (entity != null) {
            this.createdDate = entity.createdDate;
            this.updatedDate = entity.updatedDate;
            this.deleted = entity.deleted;
        }
    }
}

Lets see how we can use this objects in our codebase.

Profile profile = this.profileRepository.findById(id);

// copy the profile object
Profile previousProfile = new Profile(profile);

// update the existing profile object
profile.setFirstName("New First Name");
profile.setLastName("New Last Name");

Now you can get the old object details from the previousProfile and new object details details using profile object.

This is how we create clone objects in Java. There is another way we can use to clone objects. 

Profile profile = SerializationUtils.clone(profile);

This utility might not available with Java 8 and old versions. But the latest java versions should available this inbuilt method.

I think you will get an idea about how to cloning the Java objects. 

Have a nice day you all.....







Comments

Popular posts from this blog

Building a Resilient Upsert in Spring Boot

I got this idea when I was working on a project that deals with a third party API. Actually my requirement was to update the database everytime if there is any new change with my third party API. So, it comes like this. I had a project that needed to sync the country list from a third party API. It is very simple as you know and the countries are not going to change in future. It is most likely the same country list. But the issue comes now. Sometimes they remove some countries from their list because they stop supporting that specific country. (Think the API is not providing a webhook). So, now what can I do? Can I use the same country list and throw errors from my system everytime. No, I had to think about a solution to update the country list every time. When I was thinking about a way to update the list, I found a better and efficient way to solve this problem. So, I will explain the steps I followed to solve this issue. Before that you can ask why you can’t keep the country list r...

Better Way to Make a Shared Data Grid Using Material UI and React

Sometime ago I have a requirement to make a shared data grid that can use inside different components in the react. I tried with different scenarios but couldn’t figure out a better way to make a shared data grid with different options. My requirement is also gather with Typescript and Material UI version 5.0. So I used their latest Material data grid free release(version 6). When we plan to use v6 data grid, It is bit different than version 5. So I like to show how I fulfill my requirements and how I made my components in a better way. Ok let’s go. First we need to have latest react version and Material UI version up to 5. Then in here I’m planning to work on Material UI x-data-grid version 6. In here I’m using Typescript to show the coding examples. Ok, Let’s create our shared data grid components like below. I will explain about some properties I have used inside this data grid Material UI component. This is my basic setup of data grid component. import React from 'react'...