我有一个类的变量为旧数据和新数据。
的例子:
class Person{
String newAddress;
int newMobileNumber;
String newOfficeId;
// many fields like this (atleast 15 fields)
String oldAddress;
int oldMobileNumber;
String oldOfficeId;
// many fields like this (atleast 15 fields)
//getters and setters of all the fields.
}
我正在做的是点击按钮存储旧数据和新数据在一个表中包含与字段相同名称的列(用于跟踪旧数据)
但是如果所有的oldFields都等于newFields我想避免避免数据库操作
其中一种方法是使用多个if条件。像这样,if(oldAddress.equals(newAddress)){
flag = true;
}
if(oldMobileNumber.equals(newMobileNumber)){
flag = true;
}
所以我需要很多这样的if()
,我不觉得这个解决方案那么好。我怎样才能做得更好呢?
您还可以在Person
类中丢弃所有这些双值,并仅创建一个Person变量,该变量仅用于存储旧值。你可以在setter方法中更新旧的值。为了检查是否有任何值改变,你可以覆盖equals方法,并比较当前对象和Person类中的olvValues
变量。
由于这种方式,如果你在某个时候向你的Person类添加变量,你将安全自己一些额外的工作。
可以是这样的
public class Person{
String address;
int mobileNumber;
String officeId;
// many fields like this (atleast 15 fields)
private Person oldValues;
public Person(String address, int mobileNumber, String officeId) {
this.address = address;
this.mobileNumber = mobileNumber;
this.officeId = officeId;
oldValues = new Person(this);
}
public Person(Person p) {
this.address = p.address;
this.mobileNumber = p.mobileNumber;
this.officeId = p.officeId;
}
// Your method that checks if any value did change.
public void checkIfValuesChanged() {
if(this.equals(oldValues)) {
// Nothing changed
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + mobileNumber;
result = prime * result + ((officeId == null) ? 0 : officeId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if(!(obj instanceof Person)) return false;
Person other = (Person) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (mobileNumber != other.mobileNumber)
return false;
if (officeId == null) {
if (other.officeId != null)
return false;
} else if (!officeId.equals(other.officeId))
return false;
return true;
}
// Your setter methods do save the old values in the oldValues Person object
public void setAddress(String address) {
oldValues.address = this.address;
this.address = address;
}
}
您可以使用来自Guava的ComparisonChain类来简化样板代码。在你的例子中,In会像这样:
return ComparisonChain.start()
.compare(newAddress, oldAddress)
.compare(newMobileNumber, oldMobileNumber)
...
.result() == 0;
尽管我强烈建议你像Kevin Esche建议的那样去掉复制粘贴。比较链在这种情况下也很方便。
UPD请注意,如果您的类的成员可以是null
,那么简单的oldAddress.equals(newAddress)
将不够,因为NullPointerException
。如果你不想依赖于Guava,你可以使用object# equals方法来简化繁琐的null检查。
您只需要在为您的类重写的Object#equals
方法中添加一次所有这些if
语句。
你可以让它在大多数ide中自动为你起草。
您可能还想在此过程中重写Object#hashCode
。
- 右键单击你的类
- 点击
Source
->Generate hashCode() and equals()
然后通过调用equals
来比较两个Person
实例。
我建议定义一个类Contact,并使用标准的equals方法比较旧联系人和新联系人
import org.junit.Test;
public class MyTest {
@Test
public void myTest() {
Contact oldContact= new Contact("A",1,"A");
Contact newContact= new Contact("A",1,"A");
System.out.println(oldContact.equals(newContact));
}
}
class Contact{
String newAddress;
int newMobileNumber;
String newOfficeId;
public Contact(String newAddress, int newMobileNumber, String newOfficeId) {
super();
this.newAddress = newAddress;
this.newMobileNumber = newMobileNumber;
this.newOfficeId = newOfficeId;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Contact other = (Contact) obj;
if (newAddress == null) {
if (other.newAddress != null)
return false;
} else if (!newAddress.equals(other.newAddress))
return false;
if (newMobileNumber != other.newMobileNumber)
return false;
if (newOfficeId == null) {
if (other.newOfficeId != null)
return false;
} else if (!newOfficeId.equals(other.newOfficeId))
return false;
return true;
}
}
class Person{
Contact newContact;
Contact oldContact;
public Person(Contact newContact, Contact oldContact) {
super();
this.newContact = newContact;
this.oldContact = oldContact;
}
}