Created
May 15, 2015 21:23
-
-
Save leifoolsen/bfd595c7f91b690900fe to your computer and use it in GitHub Desktop.
The Guava ComparisonChain is a utility that provides a fluent API to write clean compareTo methods
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 class Fruit implements Comparable<Fruit> { | |
private String name; | |
private String family; | |
private int calories; | |
@Override | |
public int compareTo( Fruit otherFruit ) { | |
return ComparisonChain.start() | |
.compare( name, otherFruit.name ) | |
.compare( family, otherFruit.family ) | |
.compare( calories, otherFruit.calories ) | |
.result(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment