Skip to content

Instantly share code, notes, and snippets.

@krujos
Created October 10, 2014 14:20
Show Gist options
  • Save krujos/a40489ae31c706c91056 to your computer and use it in GitHub Desktop.
Save krujos/a40489ae31c706c91056 to your computer and use it in GitHub Desktop.
/**
* Checks if everything except id's and results are equal!
* An example of using the stream operator and a couple of gauva and apache
* language extensions to implement an equality check. We create a pair for
* each thing we want to compare then stream through and assert that
* everything matches the equality test. We use the hash code in the
* equality test as it's a little less finicky for equality tests. This is a
* pretty expensive and heavy weight implementation, not because of the
* functional stuff, but because of all the intermediary objects created. As
* this is example code we don't sweat the efficiency. A simpler
* Implementation would just use a big equality test.
* Example disabled because of hibernate bug...
* https://hibernate.atlassian.net/browse/HHH-9132
*
* @param other
* @return true if every element is equal
*/
private boolean checkEquality(final Schedule other) {
// Nifty fancy but expensive lambda Pair disabled because of hibernate
// bug.
// https://hibernate.atlassian.net/browse/HHH-9132
//@formatter:off
return Lists
.newArrayList(Pair.of(callbackURI, other.callbackURI),
Pair.of(cronExpression, other.cronExpression),
Pair.of(method, other.method),
Pair.of(scheduleType, other.scheduleType),
Pair.of(executeAt, other.executeAt)).stream()
.allMatch(pair -> hashIfNotNull(pair.getLeft()) == hashIfNotNull(pair.getRight()));
//@formatter:on
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment