Skip to content

Commit 3dceccb

Browse files
committed
Add Ord.hashOrd - an order instance that uses hashCode() for computing the order, can be used with a Set/Map implementation.
1 parent 6b35d99 commit 3dceccb

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

core/src/main/java/fj/Ord.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,25 @@ public Ordering f(final A a2) {
637637
}
638638
});
639639
}
640+
641+
/**
642+
* An order instance that uses {@link Object#hashCode()} for computing the order.
643+
*
644+
* @return An order instance that is based on {@link Object#hashCode()}.
645+
*/
646+
public static <A> Ord<A> hashOrd() {
647+
return Ord.<A> ord(new F<A, F<A, Ordering>>() {
648+
@Override
649+
public F<A, Ordering> f(final A a) {
650+
return new F<A, Ordering>() {
651+
@Override
652+
public Ordering f(final A a2) {
653+
final int x = a.hashCode() - a2.hashCode();
654+
return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT;
655+
}
656+
};
657+
}
658+
});
659+
}
660+
640661
}

0 commit comments

Comments
 (0)