public interface ComparableUsing<T> extends Comparable<T>
Comparable
extension to enable relational operators directly on your type. Normally you only
need to implement Comparable#compareTo(T)
, the remaining method in this interface have default
implementations suitable for most use-cases. However, if necessary you can override behavior for individual
relational operators by overriding #compareToUsing(T,Operator)
.
Note implementing this interface automatically overloads ==
and !=
. The default behavior delegates
to compareTo()
, but you can easily change the behavior by overriding equalityMode()
, see
ComparableUsing.EqualityMode
options.Modifier and Type | Interface and Description |
---|---|
static class |
ComparableUsing.EqualityMode
The mode indicating the method used to implement
== and {@code ! |
static class |
ComparableUsing.Operator
Relational operators to implement
|
Modifier and Type | Method and Description |
---|---|
default boolean |
compareToUsing(T that,
ComparableUsing.Operator op)
Compare
this to that using op . |
default ComparableUsing.EqualityMode |
equalityMode()
The method used to handle
== and {@code ! |
compareTo
default boolean compareToUsing(T that, ComparableUsing.Operator op)
this
to that
using op
.
Calls to this method are generated for usage of >, >=, <, and <=
. If equalityMode()
returns
CompareTo
, calls to this method are also generated for ==
and !=
.
Normally you do not need to implement this method because it delegates to your type's #compareTo(T)
implementation. However, you can override this default implementation to customize behavior.default ComparableUsing.EqualityMode equalityMode()
==
and !=
Copyright © 2024. All rights reserved.