Skip to content

Commit 22ae34e

Browse files
committed
Reuse code
1 parent 4ef81ee commit 22ae34e

17 files changed

+50
-324
lines changed

core/src/main/java/ai/timefold/solver/core/impl/score/stream/common/inliner/AbstractScoreInliner.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,24 +153,20 @@ protected final ScoreImpact<Score_> addConstraintMatch(Constraint constraint,
153153
*/
154154
var entry = constraintMatchList.add(new ConstraintMatchCarrier<>(constraintMatchSupplier, constraint, scoreImpact));
155155
clearMaps();
156-
return new WrappingScoreImpact<>(scoreImpact, entry);
156+
return new WrappingScoreImpact<>(this, scoreImpact, entry);
157157
}
158158

159-
private record WrappingScoreImpact<Score_ extends Score<Score_>>(ScoreImpact<Score_> delegate,
159+
private record WrappingScoreImpact<Score_ extends Score<Score_>>(AbstractScoreInliner<Score_> inliner,
160+
ScoreImpact<Score_> delegate,
160161
ElementAwareLinkedList.Entry<ConstraintMatchCarrier<Score_>> entry)
161162
implements
162163
ScoreImpact<Score_> {
163164

164-
@Override
165-
public AbstractScoreInliner<Score_> scoreInliner() {
166-
return delegate.scoreInliner();
167-
}
168-
169165
@Override
170166
public void undo() {
171167
delegate.undo();
172168
entry.remove();
173-
delegate.scoreInliner().clearMaps();
169+
inliner.clearMaps();
174170
}
175171

176172
@Override

core/src/main/java/ai/timefold/solver/core/impl/score/stream/common/inliner/BendableBigDecimalScoreContext.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,15 @@ public ScoreImpact<BendableBigDecimalScore> changeSoftScoreBy(BigDecimal matchWe
3434
var softImpact = scoreLevelWeight.multiply(matchWeight);
3535
inliner.softScores[scoreLevel] = inliner.softScores[scoreLevel].add(softImpact);
3636
var scoreImpact = new SingleSoftImpact(this, softImpact);
37-
if (!constraintMatchPolicy.isEnabled()) {
38-
return scoreImpact;
39-
}
40-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
37+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
4138
}
4239

4340
public ScoreImpact<BendableBigDecimalScore> changeHardScoreBy(BigDecimal matchWeight,
4441
ConstraintMatchSupplier<BendableBigDecimalScore> constraintMatchSupplier) {
4542
var hardImpact = scoreLevelWeight.multiply(matchWeight);
4643
inliner.hardScores[scoreLevel] = inliner.hardScores[scoreLevel].add(hardImpact);
4744
var scoreImpact = new SingleHardImpact(this, hardImpact);
48-
if (!constraintMatchPolicy.isEnabled()) {
49-
return scoreImpact;
50-
}
51-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
45+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
5246
}
5347

5448
public ScoreImpact<BendableBigDecimalScore> changeScoreBy(BigDecimal matchWeight,
@@ -66,21 +60,13 @@ public ScoreImpact<BendableBigDecimalScore> changeScoreBy(BigDecimal matchWeight
6660
inliner.softScores[softScoreLevel] = inliner.softScores[softScoreLevel].add(softImpact);
6761
}
6862
var scoreImpact = new ComplexImpact(this, hardImpacts, softImpacts);
69-
if (!constraintMatchPolicy.isEnabled()) {
70-
return scoreImpact;
71-
}
72-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
63+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
7364
}
7465

7566
@NullMarked
7667
private record SingleSoftImpact(BendableBigDecimalScoreContext ctx,
7768
BigDecimal impact) implements ScoreImpact<BendableBigDecimalScore> {
7869

79-
@Override
80-
public AbstractScoreInliner<BendableBigDecimalScore> scoreInliner() {
81-
return ctx.inliner;
82-
}
83-
8470
@Override
8571
public void undo() {
8672
var inliner = ctx.inliner;
@@ -98,11 +84,6 @@ public BendableBigDecimalScore toScore() {
9884
private record SingleHardImpact(BendableBigDecimalScoreContext ctx,
9985
BigDecimal impact) implements ScoreImpact<BendableBigDecimalScore> {
10086

101-
@Override
102-
public AbstractScoreInliner<BendableBigDecimalScore> scoreInliner() {
103-
return ctx.inliner;
104-
}
105-
10687
@Override
10788
public void undo() {
10889
var inliner = ctx.inliner;
@@ -120,11 +101,6 @@ public BendableBigDecimalScore toScore() {
120101
private record ComplexImpact(BendableBigDecimalScoreContext ctx, BigDecimal[] hardImpacts,
121102
BigDecimal[] softImpacts) implements ScoreImpact<BendableBigDecimalScore> {
122103

123-
@Override
124-
public AbstractScoreInliner<BendableBigDecimalScore> scoreInliner() {
125-
return ctx.inliner;
126-
}
127-
128104
@Override
129105
public void undo() {
130106
var inliner = ctx.inliner;

core/src/main/java/ai/timefold/solver/core/impl/score/stream/common/inliner/BendableLongScoreContext.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,15 @@ public ScoreImpact<BendableLongScore> changeSoftScoreBy(long matchWeight,
3232
var softImpact = scoreLevelWeight * matchWeight;
3333
inliner.softScores[scoreLevel] += softImpact;
3434
var scoreImpact = new SingleSoftImpact(this, softImpact);
35-
if (!constraintMatchPolicy.isEnabled()) {
36-
return scoreImpact;
37-
}
38-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
35+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
3936
}
4037

4138
public ScoreImpact<BendableLongScore> changeHardScoreBy(long matchWeight,
4239
ConstraintMatchSupplier<BendableLongScore> constraintMatchSupplier) {
4340
var hardImpact = scoreLevelWeight * matchWeight;
4441
inliner.hardScores[scoreLevel] += hardImpact;
4542
var scoreImpact = new SingleHardImpact(this, hardImpact);
46-
if (!constraintMatchPolicy.isEnabled()) {
47-
return scoreImpact;
48-
}
49-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
43+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
5044
}
5145

5246
public ScoreImpact<BendableLongScore> changeScoreBy(long matchWeight,
@@ -64,21 +58,13 @@ public ScoreImpact<BendableLongScore> changeScoreBy(long matchWeight,
6458
inliner.softScores[softScoreLevel] += softImpact;
6559
}
6660
var scoreImpact = new ComplexImpact(this, hardImpacts, softImpacts);
67-
if (!constraintMatchPolicy.isEnabled()) {
68-
return scoreImpact;
69-
}
70-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
61+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
7162
}
7263

7364
@NullMarked
7465
private record SingleSoftImpact(BendableLongScoreContext ctx,
7566
long impact) implements ScoreImpact<BendableLongScore> {
7667

77-
@Override
78-
public AbstractScoreInliner<BendableLongScore> scoreInliner() {
79-
return ctx.inliner;
80-
}
81-
8268
@Override
8369
public void undo() {
8470
ctx.inliner.softScores[ctx.scoreLevel] -= impact;
@@ -94,11 +80,6 @@ public BendableLongScore toScore() {
9480
private record SingleHardImpact(BendableLongScoreContext ctx,
9581
long impact) implements ScoreImpact<BendableLongScore> {
9682

97-
@Override
98-
public AbstractScoreInliner<BendableLongScore> scoreInliner() {
99-
return ctx.inliner;
100-
}
101-
10283
@Override
10384
public void undo() {
10485
ctx.inliner.hardScores[ctx.scoreLevel] -= impact;
@@ -114,11 +95,6 @@ public BendableLongScore toScore() {
11495
private record ComplexImpact(BendableLongScoreContext ctx, long[] hardImpacts,
11596
long[] softImpacts) implements ScoreImpact<BendableLongScore> {
11697

117-
@Override
118-
public AbstractScoreInliner<BendableLongScore> scoreInliner() {
119-
return ctx.inliner;
120-
}
121-
12298
@Override
12399
public void undo() {
124100
var inliner = ctx.inliner;

core/src/main/java/ai/timefold/solver/core/impl/score/stream/common/inliner/BendableScoreContext.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,15 @@ public ScoreImpact<BendableScore> changeSoftScoreBy(int matchWeight,
3232
var softImpact = scoreLevelWeight * matchWeight;
3333
inliner.softScores[scoreLevel] += softImpact;
3434
var scoreImpact = new SingleSoftImpact(this, softImpact);
35-
if (!constraintMatchPolicy.isEnabled()) {
36-
return scoreImpact;
37-
}
38-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
35+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
3936
}
4037

4138
public ScoreImpact<BendableScore> changeHardScoreBy(int matchWeight,
4239
ConstraintMatchSupplier<BendableScore> constraintMatchSupplier) {
4340
var hardImpact = scoreLevelWeight * matchWeight;
4441
inliner.hardScores[scoreLevel] += hardImpact;
4542
var scoreImpact = new SingleHardImpact(this, hardImpact);
46-
if (!constraintMatchPolicy.isEnabled()) {
47-
return scoreImpact;
48-
}
49-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
43+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
5044
}
5145

5246
public ScoreImpact<BendableScore> changeScoreBy(int matchWeight,
@@ -64,20 +58,12 @@ public ScoreImpact<BendableScore> changeScoreBy(int matchWeight,
6458
inliner.softScores[softScoreLevel] += softImpact;
6559
}
6660
var scoreImpact = new ComplexImpact(this, hardImpacts, softImpacts);
67-
if (!constraintMatchPolicy.isEnabled()) {
68-
return scoreImpact;
69-
}
70-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
61+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
7162
}
7263

7364
@NullMarked
7465
private record SingleSoftImpact(BendableScoreContext ctx, int impact) implements ScoreImpact<BendableScore> {
7566

76-
@Override
77-
public AbstractScoreInliner<BendableScore> scoreInliner() {
78-
return ctx.inliner;
79-
}
80-
8167
@Override
8268
public void undo() {
8369
ctx.inliner.softScores[ctx.scoreLevel] -= impact;
@@ -92,11 +78,6 @@ public BendableScore toScore() {
9278
@NullMarked
9379
private record SingleHardImpact(BendableScoreContext ctx, int impact) implements ScoreImpact<BendableScore> {
9480

95-
@Override
96-
public AbstractScoreInliner<BendableScore> scoreInliner() {
97-
return ctx.inliner;
98-
}
99-
10081
@Override
10182
public void undo() {
10283
ctx.inliner.hardScores[ctx.scoreLevel] -= impact;
@@ -112,11 +93,6 @@ public BendableScore toScore() {
11293
private record ComplexImpact(BendableScoreContext ctx, int[] hardImpacts,
11394
int[] softImpacts) implements ScoreImpact<BendableScore> {
11495

115-
@Override
116-
public AbstractScoreInliner<BendableScore> scoreInliner() {
117-
return ctx.inliner;
118-
}
119-
12096
@Override
12197
public void undo() {
12298
var inliner = ctx.inliner;

core/src/main/java/ai/timefold/solver/core/impl/score/stream/common/inliner/HardMediumSoftBigDecimalScoreContext.java

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,23 @@ public ScoreImpact<HardMediumSoftBigDecimalScore> changeSoftScoreBy(BigDecimal m
2020
var softImpact = constraintWeight.softScore().multiply(matchWeight);
2121
inliner.softScore = inliner.softScore.add(softImpact);
2222
var scoreImpact = new SoftImpact(inliner, softImpact);
23-
if (!constraintMatchPolicy.isEnabled()) {
24-
return scoreImpact;
25-
}
26-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
23+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
2724
}
2825

2926
public ScoreImpact<HardMediumSoftBigDecimalScore> changeMediumScoreBy(BigDecimal matchWeight,
3027
ConstraintMatchSupplier<HardMediumSoftBigDecimalScore> constraintMatchSupplier) {
3128
var mediumImpact = constraintWeight.mediumScore().multiply(matchWeight);
3229
inliner.mediumScore = inliner.mediumScore.add(mediumImpact);
3330
var scoreImpact = new MediumImpact(inliner, mediumImpact);
34-
if (!constraintMatchPolicy.isEnabled()) {
35-
return scoreImpact;
36-
}
37-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
31+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
3832
}
3933

4034
public ScoreImpact<HardMediumSoftBigDecimalScore> changeHardScoreBy(BigDecimal matchWeight,
4135
ConstraintMatchSupplier<HardMediumSoftBigDecimalScore> constraintMatchSupplier) {
4236
var hardImpact = constraintWeight.hardScore().multiply(matchWeight);
4337
inliner.hardScore = inliner.hardScore.add(hardImpact);
4438
var scoreImpact = new HardImpact(inliner, hardImpact);
45-
if (!constraintMatchPolicy.isEnabled()) {
46-
return scoreImpact;
47-
}
48-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
39+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
4940
}
5041

5142
public ScoreImpact<HardMediumSoftBigDecimalScore> changeScoreBy(BigDecimal matchWeight,
@@ -57,21 +48,13 @@ public ScoreImpact<HardMediumSoftBigDecimalScore> changeScoreBy(BigDecimal match
5748
inliner.mediumScore = inliner.mediumScore.add(mediumImpact);
5849
inliner.softScore = inliner.softScore.add(softImpact);
5950
var scoreImpact = new ComplexImpact(inliner, hardImpact, mediumImpact, softImpact);
60-
if (!constraintMatchPolicy.isEnabled()) {
61-
return scoreImpact;
62-
}
63-
return impactWithConstraintMatch(scoreImpact, constraintMatchSupplier);
51+
return possiblyAddConstraintMatch(scoreImpact, constraintMatchSupplier);
6452
}
6553

6654
@NullMarked
6755
private record SoftImpact(HardMediumSoftBigDecimalScoreInliner inliner,
6856
BigDecimal impact) implements ScoreImpact<HardMediumSoftBigDecimalScore> {
6957

70-
@Override
71-
public AbstractScoreInliner<HardMediumSoftBigDecimalScore> scoreInliner() {
72-
return inliner;
73-
}
74-
7558
@Override
7659
public void undo() {
7760
inliner.softScore = inliner.softScore.subtract(impact);
@@ -88,11 +71,6 @@ public HardMediumSoftBigDecimalScore toScore() {
8871
private record MediumImpact(HardMediumSoftBigDecimalScoreInliner inliner,
8972
BigDecimal impact) implements ScoreImpact<HardMediumSoftBigDecimalScore> {
9073

91-
@Override
92-
public AbstractScoreInliner<HardMediumSoftBigDecimalScore> scoreInliner() {
93-
return inliner;
94-
}
95-
9674
@Override
9775
public void undo() {
9876
inliner.mediumScore = inliner.mediumScore.subtract(impact);
@@ -109,11 +87,6 @@ public HardMediumSoftBigDecimalScore toScore() {
10987
private record HardImpact(HardMediumSoftBigDecimalScoreInliner inliner,
11088
BigDecimal impact) implements ScoreImpact<HardMediumSoftBigDecimalScore> {
11189

112-
@Override
113-
public AbstractScoreInliner<HardMediumSoftBigDecimalScore> scoreInliner() {
114-
return inliner;
115-
}
116-
11790
@Override
11891
public void undo() {
11992
inliner.hardScore = inliner.hardScore.subtract(impact);
@@ -131,11 +104,6 @@ private record ComplexImpact(HardMediumSoftBigDecimalScoreInliner inliner, BigDe
131104
BigDecimal mediumImpact,
132105
BigDecimal softImpact) implements ScoreImpact<HardMediumSoftBigDecimalScore> {
133106

134-
@Override
135-
public AbstractScoreInliner<HardMediumSoftBigDecimalScore> scoreInliner() {
136-
return inliner;
137-
}
138-
139107
@Override
140108
public void undo() {
141109
inliner.hardScore = inliner.hardScore.subtract(hardImpact);

0 commit comments

Comments
 (0)