Skip to content

Commit 72a62cd

Browse files
authored
Merge pull request #337 from jeffgbutler/master
Better Type Constraints for Map/Filter
2 parents 7ad1c45 + f3e78fc commit 72a62cd

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

src/main/java/org/mybatis/dynamic/sql/AbstractListValueCondition.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
import java.util.stream.Collectors;
2525
import java.util.stream.Stream;
2626

27-
public abstract class AbstractListValueCondition<T>
28-
implements VisitableCondition<T> {
27+
public abstract class AbstractListValueCondition<T> implements VisitableCondition<T> {
2928
protected final Collection<T> values;
3029
protected final Callback emptyCallback;
3130

@@ -67,22 +66,22 @@ private Collection<T> applyFilter(Predicate<? super T> predicate) {
6766
return values.stream().filter(predicate).collect(Collectors.toList());
6867
}
6968

70-
protected <S> S filterSupport(Predicate<? super T> predicate,
71-
BiFunction<Collection<T>, Callback, S> constructor, S self, Supplier<S> empty) {
69+
protected <S extends AbstractListValueCondition<T>> S filterSupport(Predicate<? super T> predicate,
70+
BiFunction<Collection<T>, Callback, S> constructor, S self, Supplier<S> emptySupplier) {
7271
if (shouldRender()) {
7372
Collection<T> filtered = applyFilter(predicate);
74-
return filtered.isEmpty() ? empty.get() : constructor.apply(filtered, emptyCallback);
73+
return filtered.isEmpty() ? emptySupplier.get() : constructor.apply(filtered, emptyCallback);
7574
} else {
7675
return self;
7776
}
7877
}
7978

80-
protected <R, S> S mapSupport(Function<? super T, ? extends R> mapper,
81-
BiFunction<Collection<R>, Callback, S> constructor, Supplier<S> empty) {
79+
protected <R, S extends AbstractListValueCondition<R>> S mapSupport(Function<? super T, ? extends R> mapper,
80+
BiFunction<Collection<R>, Callback, S> constructor, Supplier<S> emptySupplier) {
8281
if (shouldRender()) {
8382
return constructor.apply(applyMapper(mapper), emptyCallback);
8483
} else {
85-
return empty.get();
84+
return emptySupplier.get();
8685
}
8786
}
8887

src/main/java/org/mybatis/dynamic/sql/AbstractNoValueCondition.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public <R> R accept(ConditionVisitor<T, R> visitor) {
2525
return visitor.visit(this);
2626
}
2727

28-
protected <S> S filterSupport(BooleanSupplier booleanSupplier, Supplier<S> empty, S self) {
28+
protected <S extends AbstractNoValueCondition<?>> S filterSupport(BooleanSupplier booleanSupplier,
29+
Supplier<S> emptySupplier, S self) {
2930
if (shouldRender()) {
30-
return booleanSupplier.getAsBoolean() ? self : empty.get();
31+
return booleanSupplier.getAsBoolean() ? self : emptySupplier.get();
3132
} else {
3233
return self;
3334
}

src/main/java/org/mybatis/dynamic/sql/AbstractSingleValueCondition.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,21 @@ public <R> R accept(ConditionVisitor<T, R> visitor) {
3535
return visitor.visit(this);
3636
}
3737

38-
protected <S> S filterSupport(Predicate<? super T> predicate, Supplier<S> empty, S self) {
38+
protected <S extends AbstractSingleValueCondition<T>> S filterSupport(Predicate<? super T> predicate,
39+
Supplier<S> emptySupplier, S self) {
3940
if (shouldRender()) {
40-
return predicate.test(value) ? self : empty.get();
41+
return predicate.test(value) ? self : emptySupplier.get();
4142
} else {
4243
return self;
4344
}
4445
}
4546

46-
protected <R, S> S mapSupport(Function<? super T, ? extends R> mapper, Function<R, S> constructor,
47-
Supplier<S> empty) {
47+
protected <R, S extends AbstractSingleValueCondition<R>> S mapSupport(Function<? super T, ? extends R> mapper,
48+
Function<R, S> constructor, Supplier<S> emptySupplier) {
4849
if (shouldRender()) {
4950
return constructor.apply(mapper.apply(value));
5051
} else {
51-
return empty.get();
52+
return emptySupplier.get();
5253
}
5354
}
5455

src/main/java/org/mybatis/dynamic/sql/AbstractTwoValueCondition.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,21 @@ public <R> R accept(ConditionVisitor<T, R> visitor) {
4343
return visitor.visit(this);
4444
}
4545

46-
protected <S> S filterSupport(BiPredicate<? super T, ? super T> predicate, Supplier<S> empty, S self) {
46+
protected <S extends AbstractTwoValueCondition<T>> S filterSupport(BiPredicate<? super T, ? super T> predicate,
47+
Supplier<S> emptySupplier, S self) {
4748
if (shouldRender()) {
48-
return predicate.test(value1, value2) ? self : empty.get();
49+
return predicate.test(value1, value2) ? self : emptySupplier.get();
4950
} else {
5051
return self;
5152
}
5253
}
5354

54-
protected <R, S> S mapSupport(Function<? super T, ? extends R> mapper1,
55-
Function<? super T, ? extends R> mapper2,
56-
BiFunction<R, R, S> constructor,
57-
Supplier<S> empty) {
55+
protected <R, S extends AbstractTwoValueCondition<R>> S mapSupport(Function<? super T, ? extends R> mapper1,
56+
Function<? super T, ? extends R> mapper2, BiFunction<R, R, S> constructor, Supplier<S> emptySupplier) {
5857
if (shouldRender()) {
5958
return constructor.apply(mapper1.apply(value1), mapper2.apply(value2));
6059
} else {
61-
return empty.get();
60+
return emptySupplier.get();
6261
}
6362
}
6463

0 commit comments

Comments
 (0)