I want to compare two "Recipients" by the dateLastContact, and if it's the same, by the address. So this is my code:
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;
}
}
And I always have this error :
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:899)
at java.util.TimSort.mergeAt(TimSort.java:516)
at java.util.TimSort.mergeCollapse(TimSort.java:439)
at java.util.TimSort.sort(TimSort.java:245)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.ArrayList.sort(ArrayList.java:1462)
at managers.RecipientManager.getAllRecipientFromAPI(RecipientManager.java:28)
at com.company.Main.main(Main.java:18)
I have tried a lot of things but now, I don't know what to do. Can you help me?
The recipient class:
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;
}
}