Skip to content

Commit 4f691f8

Browse files
authored
refactor: introduce incremental constraint collectors (#2270)
1 parent 2651d30 commit 4f691f8

201 files changed

Lines changed: 7994 additions & 3164 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core/src/main/java/ai/timefold/solver/core/api/score/stream/ConstraintCollectors.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,8 +1715,7 @@ public final class ConstraintCollectors {
17151715
@NonNull Function<A, PointType_> endExclusiveMap,
17161716
@NonNull BiFunction<PointType_, PointType_, DifferenceType_> differenceFunction) {
17171717
return InnerUniConstraintCollectors.toConnectedRanges(ConstantLambdaUtils.identity(), startInclusiveMap,
1718-
endExclusiveMap,
1719-
differenceFunction);
1718+
endExclusiveMap, differenceFunction);
17201719
}
17211720

17221721
/**

core/src/main/java/ai/timefold/solver/core/api/score/stream/bi/BiConstraintCollector.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import ai.timefold.solver.core.api.score.stream.ConstraintStream;
99
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintCollector;
1010

11-
import org.jspecify.annotations.NonNull;
11+
import org.jspecify.annotations.NullMarked;
12+
import org.jspecify.annotations.Nullable;
1213

1314
/**
1415
* As described by {@link UniConstraintCollector}, only for {@link BiConstraintStream}.
@@ -22,28 +23,24 @@
2223
* especially if this value is ever used as a group key.
2324
* @see ConstraintCollectors
2425
*/
26+
@NullMarked
2527
public interface BiConstraintCollector<A, B, ResultContainer_, Result_> {
2628

2729
/**
28-
* A lambda that creates the result container, one for each group key combination.
30+
* As defined by {@link UniConstraintCollector#supplier()}, but for {@link BiConstraintStream}.
2931
*/
30-
@NonNull
3132
Supplier<ResultContainer_> supplier();
3233

3334
/**
34-
* A lambda that extracts data from the matched facts,
35-
* accumulates it in the result container
36-
* and returns an undo operation for that accumulation.
37-
*
38-
* @return the undo operation. This lambda is called when the facts no longer matches.
35+
* As defined by {@link UniConstraintCollector#accumulator()}, but for {@link BiConstraintStream}.
36+
*
37+
* @see BiConstraintCollectorAccumulator An incremental API to be returned instead of the deprecated plain tri-function.
3938
*/
40-
@NonNull
4139
TriFunction<ResultContainer_, A, B, Runnable> accumulator();
4240

4341
/**
44-
* A lambda that converts the result container into the result.
42+
* As defined by {@link UniConstraintCollector#finisher()}, but for {@link BiConstraintStream}.
4543
*/
46-
@NonNull
47-
Function<ResultContainer_, Result_> finisher();
44+
Function<ResultContainer_, @Nullable Result_> finisher();
4845

4946
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ai.timefold.solver.core.api.score.stream.bi;
2+
3+
import ai.timefold.solver.core.api.function.TriFunction;
4+
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintCollectorAccumulator;
5+
6+
import org.jspecify.annotations.NullMarked;
7+
8+
/**
9+
* As defined by {@link UniConstraintCollectorAccumulator},
10+
* only for {@link BiConstraintCollector}.
11+
*/
12+
@NullMarked
13+
@FunctionalInterface
14+
public interface BiConstraintCollectorAccumulator<ResultContainer_, A, B>
15+
extends TriFunction<ResultContainer_, A, B, Runnable> {
16+
17+
/**
18+
* As defined by {@link UniConstraintCollectorAccumulator#intoGroup(Object)},
19+
* only for {@link BiConstraintCollector}.
20+
*/
21+
BiConstraintCollectorValueHandle<A, B> intoGroup(ResultContainer_ resultContainer);
22+
23+
/**
24+
* @deprecated Use {@link #intoGroup(Object)} instead.
25+
* @throws UnsupportedOperationException always
26+
*/
27+
@Deprecated(since = "2.2.0", forRemoval = true)
28+
@Override
29+
default Runnable apply(ResultContainer_ resultContainer, A a, B b) {
30+
throw new UnsupportedOperationException("Use intoGroup() instead.");
31+
}
32+
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ai.timefold.solver.core.api.score.stream.bi;
2+
3+
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintCollectorValueHandle;
4+
5+
import org.jspecify.annotations.NullMarked;
6+
import org.jspecify.annotations.Nullable;
7+
8+
/**
9+
* As defined by {@link UniConstraintCollectorValueHandle},
10+
* only for {@link BiConstraintCollector}.
11+
*/
12+
@NullMarked
13+
public interface BiConstraintCollectorValueHandle<A, B> {
14+
15+
/**
16+
* As defined by {@link UniConstraintCollectorValueHandle#add(Object)},
17+
* only for {@link BiConstraintCollector}
18+
*/
19+
void add(@Nullable A a, @Nullable B b);
20+
21+
/**
22+
* As defined by {@link UniConstraintCollectorValueHandle#replaceWith(Object)},
23+
* only for {@link BiConstraintCollector}
24+
*/
25+
default void replaceWith(@Nullable A a, @Nullable B b) {
26+
remove();
27+
add(a, b);
28+
}
29+
30+
/**
31+
* As defined by {@link UniConstraintCollectorValueHandle#remove()},
32+
* only for {@link BiConstraintCollector}
33+
*/
34+
void remove();
35+
36+
}

core/src/main/java/ai/timefold/solver/core/api/score/stream/quad/QuadConstraintCollector.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import ai.timefold.solver.core.api.score.stream.ConstraintStream;
99
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintCollector;
1010

11-
import org.jspecify.annotations.NonNull;
11+
import org.jspecify.annotations.NullMarked;
12+
import org.jspecify.annotations.Nullable;
1213

1314
/**
1415
* As described by {@link UniConstraintCollector}, only for {@link QuadConstraintStream}.
@@ -24,28 +25,24 @@
2425
* especially if this value is ever used as a group key.
2526
* @see ConstraintCollectors
2627
*/
28+
@NullMarked
2729
public interface QuadConstraintCollector<A, B, C, D, ResultContainer_, Result_> {
2830

2931
/**
30-
* A lambda that creates the result container, one for each group key combination.
32+
* As defined by {@link UniConstraintCollector#supplier()}, but for {@link QuadConstraintStream}.
3133
*/
32-
@NonNull
3334
Supplier<ResultContainer_> supplier();
3435

3536
/**
36-
* A lambda that extracts data from the matched facts,
37-
* accumulates it in the result container
38-
* and returns an undo operation for that accumulation.
39-
*
40-
* @return the undo operation. This lambda is called when the facts no longer matches.
37+
* As defined by {@link UniConstraintCollector#accumulator()}, but for {@link QuadConstraintStream}.
38+
*
39+
* @see QuadConstraintCollectorAccumulator An incremental API to be returned instead of the deprecated plain penta-function.
4140
*/
42-
@NonNull
4341
PentaFunction<ResultContainer_, A, B, C, D, Runnable> accumulator();
4442

4543
/**
46-
* A lambda that converts the result container into the result.
44+
* As defined by {@link UniConstraintCollector#finisher()}, but for {@link QuadConstraintStream}.
4745
*/
48-
@NonNull
49-
Function<ResultContainer_, Result_> finisher();
46+
Function<ResultContainer_, @Nullable Result_> finisher();
5047

5148
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ai.timefold.solver.core.api.score.stream.quad;
2+
3+
import ai.timefold.solver.core.api.function.PentaFunction;
4+
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintCollectorAccumulator;
5+
6+
import org.jspecify.annotations.NullMarked;
7+
8+
/**
9+
* As defined by {@link UniConstraintCollectorAccumulator},
10+
* only for {@link QuadConstraintCollector}.
11+
*/
12+
@NullMarked
13+
@FunctionalInterface
14+
public interface QuadConstraintCollectorAccumulator<ResultContainer_, A, B, C, D>
15+
extends PentaFunction<ResultContainer_, A, B, C, D, Runnable> {
16+
17+
/**
18+
* As defined by {@link UniConstraintCollectorAccumulator#intoGroup(Object)},
19+
* only for {@link QuadConstraintCollector}.
20+
*/
21+
QuadConstraintCollectorValueHandle<A, B, C, D> intoGroup(ResultContainer_ resultContainer);
22+
23+
/**
24+
* @deprecated Use {@link #intoGroup(Object)} instead.
25+
* @throws UnsupportedOperationException always
26+
*/
27+
@Deprecated(since = "2.2.0", forRemoval = true)
28+
@Override
29+
default Runnable apply(ResultContainer_ resultContainer, A a, B b, C c, D d) {
30+
throw new UnsupportedOperationException("Use intoGroup() instead.");
31+
}
32+
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ai.timefold.solver.core.api.score.stream.quad;
2+
3+
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintCollectorValueHandle;
4+
5+
import org.jspecify.annotations.NullMarked;
6+
import org.jspecify.annotations.Nullable;
7+
8+
/**
9+
* As defined by {@link UniConstraintCollectorValueHandle},
10+
* only for {@link QuadConstraintCollector}.
11+
*/
12+
@NullMarked
13+
public interface QuadConstraintCollectorValueHandle<A, B, C, D> {
14+
15+
/**
16+
* As defined by {@link UniConstraintCollectorValueHandle#add(Object)},
17+
* only for {@link QuadConstraintCollector}
18+
*/
19+
void add(@Nullable A a, @Nullable B b, @Nullable C c, @Nullable D d);
20+
21+
/**
22+
* As defined by {@link UniConstraintCollectorValueHandle#replaceWith(Object)},
23+
* only for {@link QuadConstraintCollector}
24+
*/
25+
default void replaceWith(@Nullable A a, @Nullable B b, @Nullable C c, @Nullable D d) {
26+
remove();
27+
add(a, b, c, d);
28+
}
29+
30+
/**
31+
* As defined by {@link UniConstraintCollectorValueHandle#remove()},
32+
* only for {@link QuadConstraintCollector}
33+
*/
34+
void remove();
35+
36+
}

core/src/main/java/ai/timefold/solver/core/api/score/stream/tri/TriConstraintCollector.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import ai.timefold.solver.core.api.score.stream.ConstraintStream;
99
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintCollector;
1010

11-
import org.jspecify.annotations.NonNull;
11+
import org.jspecify.annotations.NullMarked;
12+
import org.jspecify.annotations.Nullable;
1213

1314
/**
1415
* As described by {@link UniConstraintCollector}, only for {@link TriConstraintStream}.
@@ -23,28 +24,24 @@
2324
* especially if this value is ever used as a group key.
2425
* @see ConstraintCollectors
2526
*/
27+
@NullMarked
2628
public interface TriConstraintCollector<A, B, C, ResultContainer_, Result_> {
2729

2830
/**
29-
* A lambda that creates the result container, one for each group key combination.
31+
* As defined by {@link UniConstraintCollector#supplier()}, but for {@link TriConstraintStream}.
3032
*/
31-
@NonNull
3233
Supplier<ResultContainer_> supplier();
3334

3435
/**
35-
* A lambda that extracts data from the matched facts,
36-
* accumulates it in the result container
37-
* and returns an undo operation for that accumulation.
38-
*
39-
* @return the undo operation. This lambda is called when the facts no longer matches.
36+
* As defined by {@link UniConstraintCollector#accumulator()}, but for {@link TriConstraintStream}.
37+
*
38+
* @see TriConstraintCollectorAccumulator An incremental API to be returned instead of the deprecated plain quad-function.
4039
*/
41-
@NonNull
4240
QuadFunction<ResultContainer_, A, B, C, Runnable> accumulator();
4341

4442
/**
45-
* A lambda that converts the result container into the result.
43+
* As defined by {@link UniConstraintCollector#finisher()}, but for {@link TriConstraintStream}.
4644
*/
47-
@NonNull
48-
Function<ResultContainer_, Result_> finisher();
45+
Function<ResultContainer_, @Nullable Result_> finisher();
4946

5047
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ai.timefold.solver.core.api.score.stream.tri;
2+
3+
import ai.timefold.solver.core.api.function.QuadFunction;
4+
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintCollectorAccumulator;
5+
6+
import org.jspecify.annotations.NullMarked;
7+
8+
/**
9+
* As defined by {@link UniConstraintCollectorAccumulator},
10+
* only for {@link TriConstraintCollector}.
11+
*/
12+
@NullMarked
13+
@FunctionalInterface
14+
public interface TriConstraintCollectorAccumulator<ResultContainer_, A, B, C>
15+
extends QuadFunction<ResultContainer_, A, B, C, Runnable> {
16+
17+
/**
18+
* As defined by {@link UniConstraintCollectorAccumulator#intoGroup(Object)},
19+
* only for {@link TriConstraintCollector}.
20+
*/
21+
TriConstraintCollectorValueHandle<A, B, C> intoGroup(ResultContainer_ resultContainer);
22+
23+
/**
24+
* @deprecated Use {@link #intoGroup(Object)} instead.
25+
* @throws UnsupportedOperationException always
26+
*/
27+
@Deprecated(since = "2.2.0", forRemoval = true)
28+
@Override
29+
default Runnable apply(ResultContainer_ resultContainer, A a, B b, C c) {
30+
throw new UnsupportedOperationException("Use intoGroup() instead.");
31+
}
32+
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ai.timefold.solver.core.api.score.stream.tri;
2+
3+
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintCollectorValueHandle;
4+
5+
import org.jspecify.annotations.NullMarked;
6+
import org.jspecify.annotations.Nullable;
7+
8+
/**
9+
* As defined by {@link UniConstraintCollectorValueHandle},
10+
* only for {@link TriConstraintCollector}.
11+
*/
12+
@NullMarked
13+
public interface TriConstraintCollectorValueHandle<A, B, C> {
14+
15+
/**
16+
* As defined by {@link UniConstraintCollectorValueHandle#add(Object)},
17+
* only for {@link TriConstraintCollector}
18+
*/
19+
void add(@Nullable A a, @Nullable B b, @Nullable C c);
20+
21+
/**
22+
* As defined by {@link UniConstraintCollectorValueHandle#replaceWith(Object)},
23+
* only for {@link TriConstraintCollector}
24+
*/
25+
default void replaceWith(@Nullable A a, @Nullable B b, @Nullable C c) {
26+
remove();
27+
add(a, b, c);
28+
}
29+
30+
/**
31+
* As defined by {@link UniConstraintCollectorValueHandle#remove()},
32+
* only for {@link TriConstraintCollector}
33+
*/
34+
void remove();
35+
36+
}

0 commit comments

Comments
 (0)