Java 比较法违反其总合同
我想按dateLastContact比较两个“收件人”,如果相同,则按地址比较。这是我的代码:Java 比较法违反其总合同,java,comparator,comparable,Java,Comparator,Comparable,我想按dateLastContact比较两个“收件人”,如果相同,则按地址比较。这是我的代码: public class RecipientComparator implements Comparator<Recipient> { @Override public int compare(Recipient o1, Recipient o2) { if (o1.isDateLastContactNull() || o2.isDateLastContac
public class RecipientComparator implements Comparator<Recipient> {
@Override
public int compare(Recipient o1, Recipient o2) {
if (o1.isDateLastContactNull() || o2.isDateLastContactNull()) {
if (o1.isDateLastContactNull() && o2.isDateLastContactNull()) {
return o1.getAddress().compareTo(o2.getAddress());
} else if (o1.isDateLastContactNull()) {
return -1;
} else if (o2.isDateLastContactNull()) {
return -1;
}
} else {
if (o1.getDateLastContact().equals(o2.getDateLastContact())) {
return o1.getAddress().compareTo(o2.getAddress());
} else
return o1.getDateLastContact().compareTo(o2.getDateLastContact());
}
return 0;
}
}
我尝试了很多事情,但现在,我不知道该怎么办。你能帮我吗
收件人类别:
package recipientPackage;
import paramPackage.Param;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Recipient {
private String recipientID;
private String address;
private String status;
private int contactCount;
private Date dateLastContact;
Param param;
private String[] attributes = {"recipientID", "address", "status", "contactCount", "dateLastContact"};
Recipient(String[] recipientBrut, boolean isComeFromMailing) {
if (isComeFromMailing) {
createRecipientMailing(recipientBrut);
} else {
createRecipientSurvey(recipientBrut);
}
}
public void createRecipientMailing(String[] recipientBrut) {
this.setRecipientID(recipientBrut[0].substring(recipientBrut[0].indexOf(':') + 1).replaceAll("\"", ""));
this.setAddress(recipientBrut[1].substring(recipientBrut[1].indexOf(':') + 1).replaceAll("\"", ""));
this.setStatus(recipientBrut[3].substring(recipientBrut[3].indexOf(':') + 1).replaceAll("\"", ""));
try {
this.setDateLastContactForMailing(recipientBrut[5].substring(recipientBrut[5].indexOf(':') + 1).replaceAll("\"", ""));
this.setContactCount(Integer.parseInt(recipientBrut[4].substring(recipientBrut[4].indexOf(':') + 1).replaceAll("\"", "")));
} catch (IndexOutOfBoundsException e) {
this.setDateLastContactForMailing(null);
}catch (NumberFormatException e){
e.printStackTrace();
}
}
public void createRecipientSurvey(String[] recipientBrut) {
setAddress(recipientBrut[0]);
setStatus(recipientBrut[1]);
setDateLastContactForSurvey(recipientBrut[2]);
param.setParam_point_collecte(recipientBrut[5]);
param.setParam_langue(recipientBrut[6]);
param.setParam_semaine(recipientBrut[7]);
param.setParam_periode(recipientBrut[8]);
param.setParam_envoi(recipientBrut[9]);
param.setParam_type_visiteur(recipientBrut[10]);
param.setParam_saison(recipientBrut[11]);
}
private void setDateLastContactForMailing(String dateLastContact) {
if (dateLastContact != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss");
try {
this.dateLastContact = formatter.parse(dateLastContact);
} catch (ParseException e) {
e.printStackTrace();
}
} else
this.dateLastContact = null;
}
private void setDateLastContactForSurvey(String dateLastContact) {
if (!dateLastContact.equals("")) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
try {
this.dateLastContact = formatter.parse(dateLastContact);
} catch (ParseException ignored) {
}
} else
this.dateLastContact = null;
}
public Boolean isDateLastContactNull() {
return (dateLastContact == null);
}
public String getRecipientID() {
return recipientID;
}
public void setRecipientID(String recipientID) {
this.recipientID = recipientID;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getContactCount() {
return contactCount;
}
public void setContactCount(int contactCount) {
this.contactCount = contactCount;
}
public Date getDateLastContact() {
return dateLastContact;
}
public void setDateLastContact(Date dateLastContact) {
this.dateLastContact = dateLastContact;
}
public String[] getAttributes() {
return attributes;
}
public void setAttributes(String[] attributes) {
this.attributes = attributes;
}
}
您违反了比较法的约定,您的关系不可传递。“实施者必须确保所有
x
和y
的sgn(比较(x,y))===-sgn(比较(y,x))
”
考虑这一点:
public static void main(String[] args){
Recipient r1; // with isDateLastContactNull() == true;
Recipient r2; // with isDateLastContactNull() == false;
RecipientComparator rc = new RecipientComparator();
System.out.println(rc.compare(r1, r2)); // -1
System.out.println(rc.compare(r2, r1)); // -1
// both print -1 which is not transitive.
}
原因在于此代码:
if (o1.isDateLastContactNull() && o2.isDateLastContactNull()) {
// if both null, return comparison of addresses
return o1.getAddress().compareTo(o2.getAddress());
} else if (o1.isDateLastContactNull()) {
// if first null, return -1
return -1;
} else if (o2.isDateLastContactNull()) {
// if second null, also return -1 ?
return -1; // should probably be 1 instead
}
对于第二个条件,您可能应该返回1
本合同定义如下:
int比较(to1,to2):
比较其两个参数的顺序。返回负整数、零或正整数,因为第一个参数小于、等于或大于第二个参数。在前面的描述中,符号sgn(表达式)
指定数学符号函数,该函数被定义为根据表达式的值是负、零还是正返回-1
、0
或1
中的一个
实施者必须确保所有x
和y
的sgn(比较(x,y))==-sgn(比较(y,x))
。(这意味着compare(x,y)
必须在且仅当compare(y,x)
引发异常时才会引发异常。)
实现者还必须确保关系是可传递的:((比较(x,y)>0)和&(比较(y,z)>0))
意味着比较(x,z)>0
最后,实现者必须确保compare(x,y)==0
意味着sgn(compare(x,z))==sgn(compare(y,z))
对于所有z
通常情况下,但并非严格要求(比较(x,y)==0)==(x.equals(y))
。一般来说,任何违反这一条件的比较国都应明确指出这一事实。推荐的语言是“注意:这个比较器强加的顺序与equals不一致。”
一个明显的原因是:
} else if (o1.isDateLastContactNull()) {
return -1;
} else if (o2.isDateLastContactNull()) {
return -1;
}
o1.isDateLastContactNull()
为true
xoro2.isDateLastContactNull()
为true
然后
o1
你是说:
} else if (o1.isDateLastContactNull()) {
return -1;
} else if (o2.isDateLastContactNull()) {
return 1;
}
@我看过这个。。。帮不了我。你能添加收件人类吗?也许这更容易帮助你。@Lore我已经和收件人编辑了我的问题^^是的,这是我已经理解的事情,但我真的不明白我在哪里违反了规定:/I我编辑了我的答案。问题在于,如果对于任何具有
isDateLastContactNull()==true
} else if (o1.isDateLastContactNull()) {
return -1;
} else if (o2.isDateLastContactNull()) {
return 1;
}