Skip to content

Commit 58f1151

Browse files
committed
hotfix of BiPredicate#flip class cast exception; refactoring out need
for various suppressed unchecked warnings
1 parent 0a9ba50 commit 58f1151

9 files changed

Lines changed: 36 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/).
55

66
## [Unreleased]
7+
### Fixed
8+
- ClassCastException `BiPredicate.flip`
9+
710
### Added
811
- `Uncons`, for destructuring an `Iterable` into its head and tail
912
- `Compose` semigroup and monoid formed over `CompletableFuture`
13+
- `Monoid` and `Semigroup` both preserve type specificity through `flip` calls
14+
15+
### Changed
16+
- Removing need for various suppressed unchecked warnings in `ChoiceN` types
1017

1118
## [1.6.1] - 2017-06-17
1219
### Changed

‎src/main/java/com/jnape/palatable/lambda/adt/choice/Choice2.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ public Choice2<B, A> invert() {
3636
}
3737

3838
@Override
39-
@SuppressWarnings("unchecked")
4039
public final <C> Choice2<A, C> fmap(Function<? super B, ? extends C> fn) {
41-
return (Choice2<A, C>) Applicative.super.fmap(fn);
40+
return Applicative.super.<C>fmap(fn).coerce();
4241
}
4342

4443
@Override

‎src/main/java/com/jnape/palatable/lambda/adt/choice/Choice3.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public final Choice2<A, B> converge(Function<? super C, ? extends CoProduct2<A,
3939
}
4040

4141
@Override
42-
@SuppressWarnings("unchecked")
4342
public final <D> Choice3<A, B, D> fmap(Function<? super C, ? extends D> fn) {
44-
return (Choice3<A, B, D>) Applicative.super.fmap(fn);
43+
return Applicative.super.<D>fmap(fn).coerce();
4544
}
4645

4746
@Override

‎src/main/java/com/jnape/palatable/lambda/adt/choice/Choice4.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ public Choice3<A, B, C> converge(Function<? super D, ? extends CoProduct3<A, B,
4343
}
4444

4545
@Override
46-
@SuppressWarnings("unchecked")
4746
public final <E> Choice4<A, B, C, E> fmap(Function<? super D, ? extends E> fn) {
48-
return (Choice4<A, B, C, E>) Applicative.super.fmap(fn);
47+
return Applicative.super.<E>fmap(fn).coerce();
4948
}
5049

5150
@Override

‎src/main/java/com/jnape/palatable/lambda/adt/choice/Choice5.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public Choice4<A, B, C, D> converge(Function<? super E, ? extends CoProduct4<A,
3939
}
4040

4141
@Override
42-
@SuppressWarnings("unchecked")
4342
public <F> Choice5<A, B, C, D, F> fmap(Function<? super E, ? extends F> fn) {
44-
return (Choice5<A, B, C, D, F>) Applicative.super.fmap(fn);
43+
return Applicative.super.<F>fmap(fn).coerce();
4544
}
4645

4746
@Override

‎src/main/java/com/jnape/palatable/lambda/functions/specialized/BiPredicate.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ default Predicate<B> apply(A a) {
3434
*/
3535
@Override
3636
default BiPredicate<B, A> flip() {
37-
return (BiPredicate<B, A>) Fn2.super.flip();
37+
return Fn2.super.flip()::apply;
3838
}
3939

4040
/**

‎src/main/java/com/jnape/palatable/lambda/monoid/Monoid.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ default <B> A foldMap(Function<? super B, ? extends A> fn, Iterable<B> bs) {
6464
return reduceLeft(map(fn, bs));
6565
}
6666

67+
/**
68+
* {@inheritDoc}
69+
*/
70+
@Override
71+
default Monoid<A> flip() {
72+
return monoid(Semigroup.super.flip(), identity());
73+
}
74+
6775
/**
6876
* Promote a {@link Semigroup} to a {@link Monoid} by supplying an identity element.
6977
*

‎src/main/java/com/jnape/palatable/lambda/semigroup/Semigroup.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,12 @@ default A foldLeft(A a, Iterable<A> as) {
3838
default A foldRight(A a, Iterable<A> as) {
3939
return FoldRight.foldRight(toBiFunction(), a, as);
4040
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
@Override
46+
default Semigroup<A> flip() {
47+
return Fn2.super.flip()::apply;
48+
}
4149
}

‎src/test/java/com/jnape/palatable/lambda/functions/specialized/BiPredicateTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.junit.Test;
44

5+
import static org.hamcrest.CoreMatchers.instanceOf;
56
import static org.junit.Assert.assertFalse;
7+
import static org.junit.Assert.assertThat;
68
import static org.junit.Assert.assertTrue;
79

810
public class BiPredicateTest {
@@ -50,4 +52,10 @@ public void jufBiPredicateNegate() {
5052
assertFalse(equals.negate().test("a", "a"));
5153
assertTrue(equals.negate().test("b", "a"));
5254
}
55+
56+
@Test
57+
public void flip() {
58+
BiPredicate<String, String> equals = String::equals;
59+
assertThat(equals.flip(), instanceOf(BiPredicate.class));
60+
}
5361
}

0 commit comments

Comments
 (0)