Skip to content

Commit

Permalink
Refactor one comparator to a more functional style
Browse files Browse the repository at this point in the history
  • Loading branch information
bxt committed Oct 30, 2015
1 parent 593bbf4 commit 89a8edf
Showing 1 changed file with 5 additions and 6 deletions.
@@ -1,9 +1,13 @@
package bxt.unilectures.algogeo.fun.rectintersect;

import java.awt.geom.Rectangle2D;
import java.util.Comparator;

public class Event implements Comparable<Event>{

private static final Comparator<Event> NATURAL_COMPARATOR =
Comparator.comparingDouble(Event::getPosition).thenComparing(Event::getMark);

private Rectangle2D rectangle;
private Mark mark;

Expand Down Expand Up @@ -32,12 +36,7 @@ public String toString() {

@Override
public int compareTo(Event o) {
int positionComparison = Double.compare(getPosition(), o.getPosition());
if(positionComparison == 0) {
return getMark().compareTo(o.getMark());
} else {
return positionComparison;
}
return NATURAL_COMPARATOR.compare(this, o);
}

}

0 comments on commit 89a8edf

Please sign in to comment.