gpt4 book ai didi

java - Apache Commons DiffBuilder

转载 作者:行者123 更新时间:2023-12-02 11:41:25 26 4
gpt4 key购买 nike

我试图找出两个对象之间的差异,但里面有实例、实例列表、其他类的实例。根据差异,我正在生成一个增量文件,该文件将用于更新数据库。

我使用 JSoniter 将 JSON 与对象进行匹配,结果如下所示:

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 有一个 Rooms 对象的实例,该实例有一个 Room 对象列表。

DiffBuilder 使用 Accommodation 内的字符串变量,但是当涉及到 Rooms 和 Room 的实例时,它只是声明它们是不同的(出于测试目的,我确保它们是相同的),

DiffBuilder 有办法这样工作吗?或者,如果有人有其他建议,请放弃。

(注意:这些类位于单独的文件中。仅出于显示目的,它们是这样的)

最佳答案

我知道有点晚了,但我有同样的情况并做了解决方法,房间有一个房间列表,您正在比较列表而不是单个 Room对象

DiffBuilder db = new DiffBuilder(this, rooms, ToStringStyle.DEFAULT_STYLE)
.append("Room", this.Room, rooms.Room);

相反,您需要迭代列表并比较每个 Room 对象

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();

注意:考虑 List<Room>大小两个对象都是相同的

关于java - Apache Commons DiffBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48521426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com