Question

I am facing a very strange problem in object state updation, what I need to is I need to check if the room code matches put some data in the object and update it into a new blank List object of the HotelRoom object.

The actual problem occurs when I have multiple Search Criteria Object for similar room to book so what the following code is doing that it is updating the following object

List<HotelRoom> rooms = newHotel.getRooms();

When I check at the end I get all the objects with the similar criteria in the following list.

List<HotelRoom> hotelRooms = new ArrayList<HotelRoom>(0);

Say if the first criteria is 2 Adults 0 Children, Lead Traveller say Mr. X for first room, and second room criteria is 2 Adults 2 Children, Lead Travller say Mr. Y, when I check the object again I get both the objects same in the above list object i.e. Both objects with 2 Adults 2 Children and Lead Traveller Mr. Y.

private void enrichObject(ProductDTO productDTO, HttpServletRequest request) {
    String selectedRoomCode = request.getParameter("selectedRoomCode");
    SearchRequest searchRequest = sessionInterface.getSearchObjectFromSession(request);
    if(searchRequest != null){
        if(productDTO.getHotel() != null && productDTO.getHotel().getRooms() != null && !productDTO.getHotel().getRooms().isEmpty()){
            List<HotelRoom> hotelRooms = new ArrayList<HotelRoom>(0);
            HotelDTO newHotel = productDTO.getHotel();
            List<HotelRoom> rooms = newHotel.getRooms();
            int i = 0;
            for(SearchCriteria criteria : searchRequest.getCriterias()){
                for(HotelRoom room : rooms){
                    HotelRoom newRoom = room;
                    if(room.getProviderRoomId().equals(selectedRoomCode)){

                        String travellerTitle = request.getParameter("travellerTitle" +i);
                        String travellerFirstName = request.getParameter("travellerFirstName" + i);
                        String travellerLastName = request.getParameter("travellerLastName" + i);

                        TravellerDTO travellerDTO = new TravellerDTO();
                        travellerDTO.setLeadTraveller(true);
                        travellerDTO.setTitle(PERSONTITLE.valueOf(travellerTitle));
                        travellerDTO.setFirstName(travellerFirstName);
                        travellerDTO.setLastName(travellerLastName);

                        hotelRooms.add(newRoom);
                        sessionInterface.setLeadTravellerObject(request, travellerDTO);
                        i++;
                        break;
                    }
                }
            }
            productDTO.getHotel().setRooms(hotelRooms);
        }   
    }
}

So finally my problem is why the object which is enriched inside the inner loop updates the global object as well, which overwrites all my existing objects here as well.

List<HotelRoom> hotelRooms = new ArrayList<HotelRoom>(0);
Was it helpful?

Solution

In Java all the variables are references, so when you do

HotelRoom newRoom = room;

you are actually just copying the memory address from room and it is saved in newRoom, so, when you change something in newRoom you are also changing room because both variables point to the same address.

You can add a copy() method (or just implement the clone() method) in the HotelRoom class

public HotelRoom copy() {
    Room newRoom = new Room();
    newRoom.member0 = this.member0;
    ...
    newRoom.membern = this.membern;
    return newRoom;
}

which create a new instance and copy all the members, so, you can safely do HotelRoom newRoom = room.copy() and now you will have two variables with the same members but in different memory address, so newRoom won't change the room object. Be careful, if one of your members is another object, you will need to copy (or clone) that member as well

OTHER TIPS

HotelRoom newRoom = room; 

You are working on a reference to that room after this assignment. You should deep copy the object, then work on the cloned one. You can use clone(). You should override clone according to your object structure. You can follow the guidelines in here.

I think this is where you nare facing the issue: HotelRoom newRoom = room;

Try HotelRoom newRoom = new HotelRoom(); then do newRoom = room;

if this doesnt work try populating newRoom using getter setters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top