Skip to content

Instantly share code, notes, and snippets.

@leifoolsen
Created May 15, 2015 21:23
Show Gist options
  • Save leifoolsen/bfd595c7f91b690900fe to your computer and use it in GitHub Desktop.
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
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