我有一个包含旧数据和新数据变量的类。
示例:
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.
}
我正在做的是单击按钮,将旧数据和新数据存储在一个表中,该表包含与字段名称相同的列(用于跟踪旧数据)
但是如果所有oldField都等于newFields,我想避免避免数据库操作。
这样做的一种方法是使用许多if条件。像这样,
if(oldAddress.equals(newAddress)){
flag = true;
}
if(oldMobileNumber.equals(newMobileNumber)){
flag = true;
}
所以我需要很多这样的if()
,我觉得这个解决方案并不好。我怎样才能以更好的方式做到这一点?
答案 0 :(得分:3)
您也可以在Person
类中丢弃所有这些double值,只需创建一个仅用于存储旧值的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;
}
}
答案 1 :(得分:2)
您可以使用ComparisonChain class from Guava来简化样板代码。在你的情况下会是这样的:
return ComparisonChain.start()
.compare(newAddress, oldAddress)
.compare(newMobileNumber, oldMobileNumber)
...
.result() == 0;
虽然我肯定会建议你按照Kevin Esche的建议摆脱复制粘贴。在这种情况下,比较链也很方便。
UPD 请注意,如果您null
的成员可以oldAddress.equals(newAddress)
而不是简单的NullPointerException
,那么{{1}}就足够了。如果您不想依赖Guava,可以使用Objects#equals method来简化繁琐的空值检查。
答案 2 :(得分:1)
您只需要在您的课程的重写if
方法中添加所有这些Object#equals
语句一次。
您可以在大多数IDE中自动为您选择它。
您可能还希望在此过程中覆盖Object#hashCode
。
在Ecplise中
Source
- > Generate hashCode() and equals()
然后,只需调用Person
即可比较两个equals
实例。
答案 3 :(得分:-1)
我建议定义一个类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;
}
}