Created
April 22, 2018 10:58
-
-
Save tomasmalmsten/8abdafe6e43c6f97a77abd912f9c971b to your computer and use it in GitHub Desktop.
Clean Code - Functions snippet four
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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