Skip to content

Commit

Permalink
Compare additions
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrimatti committed Jan 9, 2024
1 parent d96b693 commit f74059f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fi.solita.utils</groupId>
<artifactId>functional-utils</artifactId>
<version>0.12.45</version>
<version>0.12.46</version>
<build>
<resources>
<resource>
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/fi/solita/utils/functional/Compare.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,24 @@ public static final <K extends Comparable<? super K>, V extends Comparable<? sup
}

public static final <K, V> Ordering<Map<K,V>> byMap(final Comparator<? super K> keyComparator, final Comparator<? super V> valueComparator) {
return by(Transformers.<K,V>mapEntrySet(), byIterable(by(Transformers.<K,V>key(), keyComparator).then(by(Transformers.<K,V>value(), valueComparator))));
return by(Transformers.<K,V>mapEntrySet(), byIterable(byEntry(keyComparator, valueComparator)));
}

public static final <L, R> Ordering<Map.Entry<L,R>> byEntry(final Comparator<? super L> leftComparator, final Comparator<? super R> rightComparator) {
return by(Transformers.<L,R>key(), leftComparator).then(by(Transformers.<L,R>value(), rightComparator));
}

@SuppressWarnings("unchecked")
public static final <L, R> Ordering<Pair<L,R>> byPair(final Comparator<? super L> leftComparator, final Comparator<? super R> rightComparator) {
return (Ordering<Pair<L,R>>)(Object)byEntry(leftComparator, rightComparator);
}

public static final <T1, T2, T3> Ordering<Tuple3<T1, T2, T3>> byTuple(final Comparator<? super T1> comparator1, final Comparator<? super T2> comparator2, final Comparator<? super T3> comparator3) {
return Compare.<T1,Tuple3<T1, T2, T3>>by(Transformers.<T1>_1(), comparator1).thenBy(Transformers.<T2>_2(), comparator2).then(by(Transformers.<T3>_3(), comparator3));
}

public static final <T1, T2, T3, T4> Ordering<Tuple4<T1, T2, T3, T4>> byTuple(final Comparator<? super T1> comparator1, final Comparator<? super T2> comparator2, final Comparator<? super T3> comparator3, final Comparator<? super T4> comparator4) {
return Compare.<T1,Tuple4<T1, T2, T3, T4>>by(Transformers.<T1>_1(), comparator1).thenBy(Transformers.<T2>_2(), comparator2).then(by(Transformers.<T3>_3(), comparator3)).then(by(Transformers.<T4>_4(), comparator4));
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/fi/solita/utils/functional/Ordering.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,32 @@ public final Ordering<T> zero() {
public final Ordering<T> then(Comparator<? super T> next) {
return apply(Pair.of(this, Ordering.of(next)));
}

/**
* @return composition of {@code this} and transformation to an element with natural ordering.
*/
public final Ordering<T> thenBy(final Apply<? super T, ? extends Comparable<?>> next) {
return then(Compare.by(next));
}

/**
* @return composition of {@code this} and transformation to an element with natural ordering.
*/
public final <S extends Comparable<? super S>> Ordering<T> thenByOption(final Apply<? super T, ? extends Option<S>> next) {
return then(Compare.byOption(next));
}

/**
* @return composition of {@code this} and {@code next} by transforming to a type comparable by {@code targetComparator}.
*/
public final <S> Ordering<T> thenBy(final Apply<? super T, S> next, final Comparator<? super S> targetComparator) {
return then(Compare.by(next, targetComparator));
}

/**
* @return composition of {@code this} and {@code next} by transforming to a type comparable by {@code targetComparator}.
*/
public final <S> Ordering<T> thenByOption(final Apply<? super T, ? extends Option<S>> next, final Comparator<? super S> targetComparator) {
return then(Compare.byOption(next, targetComparator));
}
}

0 comments on commit f74059f

Please sign in to comment.