Create new Object to replace values in an immutable Object

karvai

I have immutable objects as follows.

@Getter 
@Builder 
class MainDetail {
    // 5 other String fields
    private Data data;
}

@Getter 
@Builder 
class ImageUrl {
    private final String dataOne; // looking to change these 2 values
    private final String dataTwo; // if rest call returns null for these. 
}

Information to fill these up is fetched from a rest call, working fine as follows.

List<MainDetail> featureBenefits = // value from a rest response 

I wish to switch out the dataOne and dataTwo values in here if it is null for each MainDetail Object.
I can't just use a set method to do this cos it is immutable.

I end up with the following verbose way of doing it where I need to do multiple variations of the check to swap values.
I can't just check one at a time and switch cos Object becomes immutable. Can't add another if the second one is null too after that.

Is there a way to do this more elegantly, possibly via streams? Appreciate any help. Thanks.

List<MainDetail> mainDetails = new ArrayList<>();
for (MainDetail mainDetail : featureBenefits) {
    if (mainDetail.getImageUrl().getDataOne() == null && mainDetail.getImageUrl().getdataTwo() == null) {
        ImageUrl imageUrl = ImageUrl.builder()
                .dataOne("default1")
                .dataTwo("default12")
                .build();
        MainDetail detail = MainDetail.builder()
                .imageUrl(imageUrl)
                .build();
        mainDetails.add(detail);
    }
    else if (mainDetail.getImageUrl().getdataOne() == null) {
        ImageUrl imageUrl = ImageUrl.builder()
                .dataOne("default1")
                .build();
        MainDetail detail = MainDetail.builder()
                .imageUrl(imageUrl)
                .build();
        mainDetails.add(detail);
    }
    else if (mainDetail.getImageUrl().getDataTwo() == null) {
        ImageUrl imageUrl = ImageUrl.builder()
                .dataTwo("default2")
                .build();
        MainDetail detail = MainDetail.builder()
                .imageUrl(imageUrl)
                .build();
        mainDetails.add(detail);
    }
}
oleg.cherednik

What about this one:

List<MainDetail> featureBenefits = Collections.emptyList();
List<MainDetail> mainDetails = new ArrayList<>();

for (MainDetail mainDetail : featureBenefits) {
    ImageUrl imageUrl = mainDetail.getImageUrl();

    mainDetails.add(MainDetail.builder()
                              .imageUrl(ImageUrl.builder()
                                                .dataOne(Optional.ofNullable(imageUrl.getDataOne()).orElse("default1"))
                                                .dataTwo(Optional.ofNullable(imageUrl.getDataTwo()).orElse("default2"))
                                                .build())
                              .build());
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create immutable object, instantiated without new

From Dev

Create DEEP immutable object in runtime

From Dev

Best way to create new object from existing object with certain values

From Dev

Can F# update immutable bindings in a class (create a new object with the specified bindings changed?)

From Dev

Can F# update immutable bindings in a class (create a new object with the specified bindings changed?)

From Dev

How to create mutable AND immutable copy of object in JS

From Dev

Is it in some scenarios impossible to create immutable object graphs?

From Dev

Adding a new object with key value pairs in immutable object

From Dev

create new object in php

From Dev

create new object in json

From Dev

Create new object on ModelForm save, with values from related model

From Dev

How to break up an object with a string of values to create a new array of objects?

From Dev

How can I create a new object from existing values?

From Dev

Create new attributes inside a PHP object, through external values

From Dev

Is there a way to create an Immutable object in java, when the object have mutators?

From Dev

immutable.js - replace node in complex and nested object

From Dev

Create a new object in React.js from the values in a separate json object

From Dev

Create new object or reuse old object Java

From Dev

Does Object Modification Create a New Object?

From Dev

Create new object or reuse old object Java

From Dev

Create a new object by modifying an existing object

From Dev

Pass object through or Create new object?

From Dev

How to create new object based on object properties

From Dev

Create new object from existing array and object

From Dev

Appending New Values to a Durable Object

From Dev

Replace null values to empty values in a JSON OBJECT

From Dev

Object.create vs new

From Dev

Create new assoc object in $.each

From Dev

Asking the user to create a new object?

Related Related

  1. 1

    Create immutable object, instantiated without new

  2. 2

    Create DEEP immutable object in runtime

  3. 3

    Best way to create new object from existing object with certain values

  4. 4

    Can F# update immutable bindings in a class (create a new object with the specified bindings changed?)

  5. 5

    Can F# update immutable bindings in a class (create a new object with the specified bindings changed?)

  6. 6

    How to create mutable AND immutable copy of object in JS

  7. 7

    Is it in some scenarios impossible to create immutable object graphs?

  8. 8

    Adding a new object with key value pairs in immutable object

  9. 9

    create new object in php

  10. 10

    create new object in json

  11. 11

    Create new object on ModelForm save, with values from related model

  12. 12

    How to break up an object with a string of values to create a new array of objects?

  13. 13

    How can I create a new object from existing values?

  14. 14

    Create new attributes inside a PHP object, through external values

  15. 15

    Is there a way to create an Immutable object in java, when the object have mutators?

  16. 16

    immutable.js - replace node in complex and nested object

  17. 17

    Create a new object in React.js from the values in a separate json object

  18. 18

    Create new object or reuse old object Java

  19. 19

    Does Object Modification Create a New Object?

  20. 20

    Create new object or reuse old object Java

  21. 21

    Create a new object by modifying an existing object

  22. 22

    Pass object through or Create new object?

  23. 23

    How to create new object based on object properties

  24. 24

    Create new object from existing array and object

  25. 25

    Appending New Values to a Durable Object

  26. 26

    Replace null values to empty values in a JSON OBJECT

  27. 27

    Object.create vs new

  28. 28

    Create new assoc object in $.each

  29. 29

    Asking the user to create a new object?

HotTag

Archive