gpt4 book ai didi

java集合: get objects are modified,添加和删除?

转载 作者:行者123 更新时间:2023-12-02 00:47:40 25 4
gpt4 key购买 nike

是否有这样一个集合实现可以让我们知道与特定点相比新添加、修改或删除了哪些对象?

我想使用这样的集合来保存从数据库加载的对象,并将其绑定(bind)到用户界面,以便用户可以向其中添加新对象,删除其中的项目或修改某些项目。当用户单击保存按钮时,我需要将更改保存到数据库,因此我需要知道更改的对象。

<小时/>

这是我自己的解决方案,但我不确定它是否很糟糕,请给我一些建议。

界面:

import java.util.Collection;

/**
* @author ggfan@amarsoft
*
*/
public interface DataObjectsMonitor {

/**
* take a snapshot for comparing
*/
public void snapshot();

/**
*
* @return Objects that are modified comparing to those ones before {@link #snapshot()} last called
*/
public Collection<?> getmodifiedObjects();

/**
*
* @return Objects that are deleted comparing to those ones before {@link #snapshot()} last called
*/
public Collection<?> getDeletedObjects();

/**
*
* @return Objects that are added comparing to those ones before {@link #snapshot()} last called
*/
public Collection<?> getAddedObjects();
}

模型类必须从这样的抽象类扩展:

public abstract class DataObject {

public abstract int dataHashCode();

}

实现类:

public class DataObjectListMonitor<T extends DataObject> extends ArrayList<T> implements DataObjectsMonitor {

private static final long serialVersionUID = 1L;

private Map<T, Integer> oldVersion = new HashMap<T, Integer>();

public void snapshot() {
oldVersion.clear();
for(T t : this){
oldVersion.put(t, new Integer(t.dataHashCode()));
}
}

public Collection<T> getmodifiedObjects() {
ArrayList<T> modified = new ArrayList<T>();
for(T t : oldVersion.keySet()){
if(this.contains(t) && t.dataHashCode() != oldVersion.get(t)){
modified.add(t);
}
}
return modified;
}

public Collection<T> getDeletedObjects() {
ArrayList<T> deleted = new ArrayList<T>();
for(T t : oldVersion.keySet()){
if(!this.contains(t)){
deleted.add(t);
}
}
return deleted;
}

public Collection<T> getAddedObjects() {
ArrayList<T> added = new ArrayList<T>();
for(T t : this){
if(!oldVersion.keySet().contains(t)){
added.add(t);
}
}
return added;
}

}

测试:

public class Model extends DataObject {
private String id;

private String name;

public String toString() {
return "Model [id=" + id + ", name=" + name + "]";
}

public Model(String id, String name) {
super();
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int dataHashCode() {
int dataHashCode = 0;
if(id != null){
dataHashCode += id.hashCode();
}
if(name != null){
dataHashCode += name.hashCode();
}
return dataHashCode;
}

public static void main(String[] args){
DataObjectListMonitor<Model> data = new DataObjectListMonitor<Model>();
Model m1 = new Model("m1", "model 1");
Model m2 = new Model("m2", "model 2");
Model m3 = new Model("m3", "model 3");
Model m4 = new Model("m4", "model 4");
Model m5 = new Model("m5", "model 5");
data.add(m1);
data.add(m2);
data.add(m3);
data.add(m4);
data.add(m5);
data.snapshot();
Model m6 = new Model("m6", "model 6");
data.add(m6);
m3.setName("model 3 changed");
m3.setName("model 3");
data.remove(m5);
m1.setName("model 1 chaned");
for(Model m : data.getAddedObjects()){
System.out.println("added : " + m);
}

for(Model m : data.getDeletedObjects()){
System.out.println("deleted : " + m);
}

for(Model m : data.getmodifiedObjects()){
System.out.println("modified : " + m);
}
}

}

输出:

added : Model [id=m6, name=model 6]
deleted : Model [id=m5, name=model 5]
modified : Model [id=m1, name=model 1 chaned]

编辑:使用 hashCode 是完全错误的,但也许我们可以使用 MD5 或 CRC32。

最佳答案

您可以实现一个自定义集合,该集合可以轻松跟踪和记录添加、替换和删除操作,但跟踪和记录对集合项的修改是完全不可能的。

总体思路:实现List接口(interface),添加一个委托(delegate)(一个真正的列表)并将所有方法调用委托(delegate)给内部列表(或set、map、queue...):

public class LoggingList implements List {
private List delegate = new ArrayList();
private List<ChangeEvent> history = new ArrayList<ChangeEvent>();

// ...

@Override
public boolean add(Object item) {
boolean success = delegate.add(item);
if (success) {
history.add(new ChangeEvent(Type.ADD, item));
}
return success;
}
}

但是 - 如果有人在添加项目后使用对 item 的引用来修改其状态,列表无法跟踪。

关于java集合: get objects are modified,添加和删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6516905/

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