Skip to content

Commit f031e22

Browse files
committed
Adding inequality functions
1 parent f0cbbb5 commit f031e22

File tree

21 files changed

+695
-0
lines changed

21 files changed

+695
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
77
### Added
88
- `Fn3-8` static factory overloads to aid in coercing lambdas
99
- Adding composition guarantees to `LensLike`
10+
- `CmpEqBy`, `CmpEq`, `GTBy`, `GT`, `LTBy`, `LT`, `GTEBy`, `GTE`, `LTEBy`, and `LTE` inequality checks
1011

1112
## [3.0.3] - 2018-05-27
1213
### Added
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.builtin.fn3.CmpEqBy;
4+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
5+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
6+
7+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
8+
import static com.jnape.palatable.lambda.functions.builtin.fn3.CmpEqBy.cmpEqBy;
9+
10+
/**
11+
* Given two {@link Comparable} values of type <code>A</code>, return <code>true</code> if the first value is strictly
12+
* equal to the second value (according to {@link Comparable#compareTo(Object)}; otherwise, return false.
13+
*
14+
* @param <A> the value type
15+
* @see CmpEqBy
16+
* @see LT
17+
* @see GT
18+
*/
19+
public final class CmpEq<A extends Comparable<A>> implements BiPredicate<A, A> {
20+
21+
private static final CmpEq INSTANCE = new CmpEq();
22+
23+
private CmpEq() {
24+
}
25+
26+
@Override
27+
public Boolean apply(A x, A y) {
28+
return cmpEqBy(id(), x, y);
29+
}
30+
31+
@SuppressWarnings("unchecked")
32+
public static <A extends Comparable<A>> CmpEq<A> cmpEq() {
33+
return INSTANCE;
34+
}
35+
36+
public static <A extends Comparable<A>> Predicate<A> cmpEq(A x) {
37+
return CmpEq.<A>cmpEq().apply(x);
38+
}
39+
40+
public static <A extends Comparable<A>> Boolean cmpEq(A x, A y) {
41+
return cmpEq(x).apply(y);
42+
}
43+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.builtin.fn3.GTBy;
4+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
5+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
6+
7+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
8+
import static com.jnape.palatable.lambda.functions.builtin.fn3.GTBy.gtBy;
9+
10+
/**
11+
* Given two {@link Comparable} values of type <code>A</code>, return <code>true</code> if the first value is strictly
12+
* greater than the second value; otherwise, return false.
13+
*
14+
* @param <A> the value type
15+
* @see GTBy
16+
* @see LT
17+
*/
18+
public final class GT<A extends Comparable<A>> implements BiPredicate<A, A> {
19+
20+
private static final GT INSTANCE = new GT();
21+
22+
private GT() {
23+
}
24+
25+
@Override
26+
public Boolean apply(A x, A y) {
27+
return gtBy(id(), x, y);
28+
}
29+
30+
@SuppressWarnings("unchecked")
31+
public static <A extends Comparable<A>> GT<A> gt() {
32+
return INSTANCE;
33+
}
34+
35+
public static <A extends Comparable<A>> Predicate<A> gt(A x) {
36+
return GT.<A>gt().apply(x);
37+
}
38+
39+
public static <A extends Comparable<A>> Boolean gt(A x, A y) {
40+
return gt(x).apply(y);
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.builtin.fn3.GTEBy;
4+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
5+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
6+
7+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
8+
import static com.jnape.palatable.lambda.functions.builtin.fn3.GTEBy.gteBy;
9+
10+
/**
11+
* Given two {@link Comparable} values of type <code>A</code>, return <code>true</code> if the first value is greater
12+
* than or equal to the second value according to {@link Comparable#compareTo(Object)}; otherwise, return false.
13+
*
14+
* @param <A> the value type
15+
* @see GTEBy
16+
* @see LTE
17+
*/
18+
public final class GTE<A extends Comparable<A>> implements BiPredicate<A, A> {
19+
20+
private static final GTE INSTANCE = new GTE();
21+
22+
private GTE() {
23+
}
24+
25+
@Override
26+
public Boolean apply(A x, A y) {
27+
return gteBy(id(), x, y);
28+
}
29+
30+
@SuppressWarnings("unchecked")
31+
public static <A extends Comparable<A>> GTE<A> gte() {
32+
return INSTANCE;
33+
}
34+
35+
public static <A extends Comparable<A>> Predicate<A> gte(A x) {
36+
return GTE.<A>gte().apply(x);
37+
}
38+
39+
public static <A extends Comparable<A>> Boolean gte(A x, A y) {
40+
return gte(x).apply(y);
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.builtin.fn3.LTBy;
4+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
5+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
6+
7+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
8+
import static com.jnape.palatable.lambda.functions.builtin.fn3.LTBy.ltBy;
9+
10+
/**
11+
* Given two {@link Comparable} values of type <code>A</code>, return <code>true</code> if the first value is strictly
12+
* less than the second value; otherwise, return false.
13+
*
14+
* @param <A> the value type
15+
* @see LTBy
16+
* @see GT
17+
*/
18+
public final class LT<A extends Comparable<A>> implements BiPredicate<A, A> {
19+
20+
private static final LT INSTANCE = new LT();
21+
22+
private LT() {
23+
}
24+
25+
@Override
26+
public Boolean apply(A x, A y) {
27+
return ltBy(id(), x, y);
28+
}
29+
30+
@SuppressWarnings("unchecked")
31+
public static <A extends Comparable<A>> LT<A> lt() {
32+
return INSTANCE;
33+
}
34+
35+
public static <A extends Comparable<A>> Predicate<A> lt(A x) {
36+
return LT.<A>lt().apply(x);
37+
}
38+
39+
public static <A extends Comparable<A>> Boolean lt(A x, A y) {
40+
return lt(x).apply(y);
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.builtin.fn3.LTEBy;
4+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
5+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
6+
7+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
8+
import static com.jnape.palatable.lambda.functions.builtin.fn3.LTEBy.lteBy;
9+
10+
/**
11+
* Given two {@link Comparable} values of type <code>A</code>, return <code>true</code> if the first value is less than
12+
* or equal to the second value according to {@link Comparable#compareTo(Object);} otherwise, return false.
13+
*
14+
* @param <A> the value typ
15+
* @see LTEBy
16+
* @see GTE
17+
*/
18+
public final class LTE<A extends Comparable<A>> implements BiPredicate<A, A> {
19+
20+
private static final LTE INSTANCE = new LTE();
21+
22+
private LTE() {
23+
}
24+
25+
@Override
26+
public Boolean apply(A x, A y) {
27+
return lteBy(id(), x, y);
28+
}
29+
30+
@SuppressWarnings("unchecked")
31+
public static <A extends Comparable<A>> LTE<A> lte() {
32+
return INSTANCE;
33+
}
34+
35+
public static <A extends Comparable<A>> Predicate<A> lte(A x) {
36+
return LTE.<A>lte().apply(x);
37+
}
38+
39+
public static <A extends Comparable<A>> Boolean lte(A x, A y) {
40+
return lte(x).apply(y);
41+
}
42+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn3;
2+
3+
import com.jnape.palatable.lambda.functions.Fn3;
4+
import com.jnape.palatable.lambda.functions.builtin.fn2.CmpEq;
5+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
6+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
7+
8+
import java.util.function.Function;
9+
10+
/**
11+
* Given a mapping function from some type <code>A</code> to some {@link Comparable} type <code>B</code> and two values
12+
* of type <code>A</code>, return <code>true</code> if the first value is strictly equal to the second value (according
13+
* to {@link Comparable#compareTo(Object)} in terms of their mapped <code>B</code> results; otherwise, return false.
14+
*
15+
* @param <A> the value type
16+
* @param <B> the mapped comparison type
17+
* @see CmpEq
18+
* @see LTBy
19+
* @see GTBy
20+
*/
21+
public final class CmpEqBy<A, B extends Comparable<B>> implements Fn3<Function<? super A, ? extends B>, A, A, Boolean> {
22+
23+
private static final CmpEqBy INSTANCE = new CmpEqBy();
24+
25+
private CmpEqBy() {
26+
}
27+
28+
@Override
29+
public Boolean apply(Function<? super A, ? extends B> compareFn, A x, A y) {
30+
return compareFn.apply(x).compareTo(compareFn.apply(y)) == 0;
31+
}
32+
33+
@Override
34+
public BiPredicate<A, A> apply(Function<? super A, ? extends B> compareFn) {
35+
return Fn3.super.apply(compareFn)::apply;
36+
}
37+
38+
@Override
39+
public Predicate<A> apply(Function<? super A, ? extends B> compareFn, A x) {
40+
return Fn3.super.apply(compareFn, x)::apply;
41+
}
42+
43+
@SuppressWarnings("unchecked")
44+
public static <A, B extends Comparable<B>> CmpEqBy<A, B> cmpEqBy() {
45+
return INSTANCE;
46+
}
47+
48+
public static <A, B extends Comparable<B>> BiPredicate<A, A> cmpEqBy(Function<? super A, ? extends B> compareFn) {
49+
return CmpEqBy.<A, B>cmpEqBy().apply(compareFn);
50+
}
51+
52+
public static <A, B extends Comparable<B>> Predicate<A> cmpEqBy(Function<? super A, ? extends B> compareFn, A x) {
53+
return CmpEqBy.<A, B>cmpEqBy(compareFn).apply(x);
54+
}
55+
56+
public static <A, B extends Comparable<B>> Boolean cmpEqBy(Function<? super A, ? extends B> compareFn, A x, A y) {
57+
return cmpEqBy(compareFn, x).apply(y);
58+
}
59+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn3;
2+
3+
import com.jnape.palatable.lambda.functions.Fn3;
4+
import com.jnape.palatable.lambda.functions.builtin.fn2.GT;
5+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
6+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
7+
8+
import java.util.function.Function;
9+
10+
/**
11+
* Given a mapping function from some type <code>A</code> to some {@link Comparable} type <code>B</code> and two values
12+
* of type <code>A</code>, return <code>true</code> if the first value is strictly greater than the second value in
13+
* terms of their mapped <code>B</code> results; otherwise, return false.
14+
*
15+
* @param <A> the value type
16+
* @param <B> the mapped comparison type
17+
* @see GT
18+
* @see LTBy
19+
*/
20+
public final class GTBy<A, B extends Comparable<B>> implements Fn3<Function<? super A, ? extends B>, A, A, Boolean> {
21+
22+
private static final GTBy INSTANCE = new GTBy();
23+
24+
private GTBy() {
25+
}
26+
27+
@Override
28+
public Boolean apply(Function<? super A, ? extends B> compareFn, A x, A y) {
29+
return compareFn.apply(x).compareTo(compareFn.apply(y)) > 0;
30+
}
31+
32+
@Override
33+
public BiPredicate<A, A> apply(Function<? super A, ? extends B> compareFn) {
34+
return Fn3.super.apply(compareFn)::apply;
35+
}
36+
37+
@Override
38+
public Predicate<A> apply(Function<? super A, ? extends B> compareFn, A x) {
39+
return Fn3.super.apply(compareFn, x)::apply;
40+
}
41+
42+
@SuppressWarnings("unchecked")
43+
public static <A, B extends Comparable<B>> GTBy<A, B> gtBy() {
44+
return INSTANCE;
45+
}
46+
47+
public static <A, B extends Comparable<B>> BiPredicate<A, A> gtBy(Function<? super A, ? extends B> fn) {
48+
return GTBy.<A, B>gtBy().apply(fn);
49+
}
50+
51+
public static <A, B extends Comparable<B>> Predicate<A> gtBy(Function<? super A, ? extends B> fn, A x) {
52+
return GTBy.<A, B>gtBy(fn).apply(x);
53+
}
54+
55+
public static <A, B extends Comparable<B>> Boolean gtBy(Function<? super A, ? extends B> fn, A x, A y) {
56+
return gtBy(fn, x).apply(y);
57+
}
58+
}

0 commit comments

Comments
 (0)