Home > Software design >  How to override a value of a single field in an object without copying the entire objects values
How to override a value of a single field in an object without copying the entire objects values

Time:05-19

I want to replace a certain field deep inside an object of objects.

The value I need to replace is in the Voucher object and it is located from

CarBookingRequest>CarSegment>RentalPayentPref>Voucher>type

At the moment, this is my code

//generate new voucher type for SV
            String currencyCode = originalRequest.getCarSegment().getCarTotalPrice().getCurrencyCode();
            String amount = originalRequest.getCarSegment().getCarTotalPrice().getAmount();
            String carTotalPrice = currencyCode   amount;

            //get Old Voucher values and re-assign type to carTotalPrice
            Voucher voucher = new Voucher();
            voucher.setBillingNumber(originalRequest.getCarSegment().getRentalPaymentPref().getVoucher().getBillingNumber());
            voucher.setFormat(originalRequest.getCarSegment().getRentalPaymentPref().getVoucher().getFormat());
            voucher.setType(carTotalPrice);

            RentalPaymentPref rentalPaymentPref = new RentalPaymentPref();
            rentalPaymentPref.setVoucher(voucher);

            CarSegment carSegment = new CarSegment();
            carSegment.setRentalPaymentPref(rentalPaymentPref);

            originalRequest.setCarSegment(carSegment);

How do I do it without removing the existing values from all of this other objects as afterall, this objects above the voucher has their own values that I dont need to change but needs to be retained. As this values are already assigned to variable that is being referenced on another code. This class is being referenced close to this below in the code.

CarBookingRequest newRequesst = originalRequestBody; 

I tried manually instantiating all of the high level classes and just overwriting the values I need, but it also removes the existing values from other params that doesnt need changing.

Here is my sample model class:

CarbookingRequest.class

 public class CarBookingRequest {
           
        private CarSegment carSegment;
        private List<Remark> remarks;
        private String test;
    
    }

CarSegment.class

public class CarSegment {
       
    private String billingNumber;
    private RentalPayentPref rental;
    private String test;

}

RentalPayentPref.class

public class RentalPayentPref {
       
    private Voucher Voucher;
    private String test;

}

Voucher.class

public class Voucher {
       
    private String amount;
    private String type;

}

CodePudding user response:

Why are you creating new objects, when you can get the existing objects from getter methods. You provided getter methods only in your answer, and not in the question. You can use them as follows:

originalRequest
  .getCarSegment()
  .getRentalPaymentPref()
  .getVoucher()
  .setType(newValue);

CodePudding user response:

Just realized I could do this with this single line of code.

Optional.ofNullable(originalRequest)
                    .map(CarBookingRequest::getCarSegment)
                    .map(CarSegment::getRentalPaymentPref)
                    .map(RentalPaymentPref::getVoucher)
                    .ifPresent(voucher -> voucher.setType(carTotalPrice));

My Issue is resolved now. Though the other answer I marked as answer above would prolly be the better and shorter version of this as there is no need for maps already and the existing values is still retained.

  • Related