Luke Hutchison created FLINK-6070:
-------------------------------------
Summary: Suggestion: add ComparableTuple types
Key: FLINK-6070
URL: https://issues.apache.org/jira/browse/FLINK-6070
Project: Flink
Issue Type: Improvement
Components: Core
Affects Versions: 1.2.0
Reporter: Luke Hutchison
Priority: Minor
Since Java doesn't have built-in tuple types, I find myself using Flink tuples
for a lot of tasks in Flink programs. One downside is that these tuples are not
inherently comparable, so when you want to sort a collection of tuples, you
have to provide a custom comparator.
I created a ComparableTuple2 type, as follows. I wanted to get feedback on
whether something like this would be considered useful for Flink before I
submitted a PR. Also, I don't know how high I should go with the field arity
for a ComparableTuple -- presumably not as high as for non-comparable tuples?
{code}
import org.apache.flink.api.java.tuple.Tuple2;
/** A comparable tuple, consisting of comparable fields that act as primary and
secondary sort keys. */
public class ComparableTuple2<T0 extends Comparable<T0>, T1 extends
Comparable<T1>> extends Tuple2<T0, T1>
implements Comparable<Tuple2<T0, T1>> {
private static final long serialVersionUID = 1L;
private boolean invertSortOrder0;
private boolean invertSortOrder1;
public ComparableTuple2() {
}
/**
* Create a 2-tuple of comparable elements.
*
* @param f0
* The first element, which is also the primary sort key, and
sorts in ascending order.
* @param f1
* The second element, which is also the secondary sort key, and
sorts in ascending order.
* @param invertSortOrder0
* If true, invert the sort order for the first field (i.e. sort
in descending order).
* @param invertSortOrder1
* If true, invert the sort order for the second field (i.e.
sort in descending order).
*/
public ComparableTuple2(T0 f0, T1 f1) {
super(f0, f1);
}
/**
* Create a comparable 2-tuple out of comparable elements.
*
* @param f0
* The first element, which is also the primary sort key, and
sorts in ascending order if
* invertSortOrder0 == false, else sorts in descending order.
* @param f1
* The second element, which is also the secondary sort key, and
sorts in decending order if
* invertSortOrder1 == false, else sorts in descending order.
* @param invertSortOrder0
* If true, invert the sort order for the first field (i.e. sort
in descending order).
* @param invertSortOrder1
* If true, invert the sort order for the second field (i.e.
sort in descending order).
*/
public ComparableTuple2(final T0 f0, final T1 f1, final boolean
invertSortOrder0,
final boolean invertSortOrder1) {
super(f0, f1);
this.invertSortOrder0 = invertSortOrder0;
this.invertSortOrder1 = invertSortOrder1;
}
/**
* Comparison function that compares first the primary sort key, f0, and
then if equal, compares the secondary sort
* key, f1.
*/
@Override
public int compareTo(final Tuple2<T0, T1> o) {
int diff = this.f0.compareTo(o.f0);
if (invertSortOrder0) {
diff = -diff;
}
if (diff != 0) {
return diff;
}
diff = this.f1.compareTo(o.f1);
if (invertSortOrder1) {
diff = -diff;
}
return diff;
}
}
{code}
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)