Reputation: 11
I am trying to find the difference between two objects, but have instances, of list of instances, of other Classes inside. From the differences, I am generating a delta file which will be used to update a DB.
I have used JSoniter to match the JSON to objects, which has result in stuff like below:
public class Accommodation implements Diffable<Accommodation> {
public String HotelCode;
public String HotelName;
public String SupplierCruising;
public String SupplierHeadline;
public Rooms Rooms;
@Override
public DiffResult diff(Accommodation accommodation) {
DiffBuilder db = new DiffBuilder(this, accommodation, ToStringStyle.SHORT_PREFIX_STYLE)
.append("HotelCode", this.HotelCode, accommodation.HotelCode)
.append("HotelName", this.HotelName, accommodation.HotelName)
.append("SupplierCruising", this.SupplierCruising, accommodation.SupplierCruising)
.append("SupplierHeadline", this.SupplierHeadline, accommodation.SupplierHeadline)
.append("Rooms", this.Rooms, accommodation.Rooms);
return db.build();
}
}
public class Rooms implements Diffable<Rooms>{
public List<Room> Room;
@Override
public DiffResult diff(Rooms rooms) {
DiffBuilder db = new DiffBuilder(this, rooms, ToStringStyle.DEFAULT_STYLE)
.append("Room", this.Room, rooms.Room);
return db.build();
}
}
public class Room implements Diffable<Room>{
public String RoomCode;
public String RoomAvailability;
public String RoomName;
public String MaxOccupancy;
public List<String> SupplementPrice;
@Override
public DiffResult diff (Room room) {
DiffBuilder db = new DiffBuilder(this, room, ToStringStyle.SHORT_PREFIX_STYLE)
.append("RoomCode", this.RoomCode, room.RoomCode)
.append("RoomAvailability", this.RoomAvailability, room.RoomAvailability)
.append("RoomName", this.RoomName, room.RoomName)
.append("MaxOccupancy", this.MaxOccupancy, room.MaxOccupancy)
.append("SupplementPrice", this.SupplementPrice, room.SupplementPrice);
return db.build();
}
Tour has a instance of Rooms object, which has a List of Room objects.
The DiffBuilder works with the String variables inside Accommodation but when it comes to the instance of Rooms, and then Room, it just states they are different (For testing purposes, I ensured they are the same),
Is there a way for DiffBuilder to work this way? Or if anyone has other suggestions, fire away.
(Note: The classes are in separate files. Just for display purposes they are like that)
Upvotes: 1
Views: 9839
Reputation: 39
I know it's kind of late but I have the same scenario and did a workaround,
The Rooms have a list of Room and you are comparing the list and not the indivual Room
object
DiffBuilder db = new DiffBuilder(this, rooms, ToStringStyle.DEFAULT_STYLE)
.append("Room", this.Room, rooms.Room);
Instead, you need to iterate over the list and compare each Room object
DiffBuilder db = new DiffBuilder(this, rooms, ToStringStyle.DEFAULT_STYLE);
for(int i =0; i< Room.size(); i++) {
db.append("room", this.Rooms.Room.get(i).diff(rooms.Room.get(i)));
}
return db.build();
NOTE: Considering the size of List<Room>
is same for both the objects
Upvotes: 2