Skip to content

Instantly share code, notes, and snippets.

@tomasmalmsten
Created April 22, 2018 10:58
Show Gist options
  • Save tomasmalmsten/8abdafe6e43c6f97a77abd912f9c971b to your computer and use it in GitHub Desktop.
Save tomasmalmsten/8abdafe6e43c6f97a77abd912f9c971b to your computer and use it in GitHub Desktop.
Clean Code - Functions snippet four
public int compareTo(final Object _other) throws ClassCastException, NullPointerException {
final ExampleClass otherExampleClass= (ExampleClass) _other;
return compareThisTo( otherExampleClass);
}
private int compareThisTo(final ExampleClass _otherExampleClass) {
if(equals(_otherExampleClass)) {
return 0;
}
return compareCreatedDates(_otherExampleClass);
}
private int compareCreatedDates(final ExampleClass _otherExampleClass) {
if(isThisAndOthersCreatedDateNull(_otherExampleClass)) {
return 0;
}
if (isThisCreatedDateNull()) {
return 1;
}
if (isOthersCreatedDateNull(_otherExampleClass)) {
return 1;
}
return compareCreatedDatesThatAreNotNull(_otherExampleClass);
}
private boolean isThisAndOthersCreatedDateNull(final ExampleClass _otherExampleClass) {
return isThisCreatedDateNull() && isOthersCreatedDateNull(_otherExampleClass);
}
private boolean isThisCreatedDateNull() {
return isOthersCreatedDateNull(this);
}
private boolean isOthersCreatedDateNull(ExampleClass _otherExampleClass) {
return _otherExampleClass.getCreatedDate() == null;
}
private int compareCreatedDatesThatAreNotNull(ExampleClass _otherExampleClass) {
return _otherExampleClass.getCreatedDate().compareTo(getCreatedDate());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment