Skip to content

Commit ce4aadb

Browse files
committed
refactor: replace score()+impact() with getScore()+getImpact()
In the fluent API, it reads better this way.
1 parent de83e86 commit ce4aadb

6 files changed

Lines changed: 54 additions & 57 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import ai.timefold.solver.core.api.score.Score;
44
import ai.timefold.solver.core.api.score.stream.ConstraintProvider;
55

6-
import org.jspecify.annotations.NonNull;
6+
import org.jspecify.annotations.NullMarked;
77
import org.jspecify.annotations.Nullable;
88

9+
@NullMarked
910
public interface MultiConstraintAssertion {
1011

1112
/**
@@ -14,7 +15,7 @@ public interface MultiConstraintAssertion {
1415
* @param score total score calculated for the given set of facts
1516
* @throws AssertionError when the expected score does not match the calculated score
1617
*/
17-
default void scores(@NonNull Score<?> score) {
18+
default void scores(Score<?> score) {
1819
scores(score, null);
1920
}
2021

@@ -25,7 +26,7 @@ default void scores(@NonNull Score<?> score) {
2526
* @param message description of the scenario being asserted
2627
* @throws AssertionError when the expected score does not match the calculated score
2728
*/
28-
void scores(@NonNull Score<?> score, @Nullable String message);
29+
void scores(Score<?> score, @Nullable String message);
2930

3031
/**
3132
* Returns the {@link Score} produced by all constraints in the {@link ConstraintProvider} for the given set of facts.
@@ -39,16 +40,15 @@ default void scores(@NonNull Score<?> score) {
3940
* {@snippet :
4041
* HardSoftScore scoreA = constraintVerifier.verifyThat()
4142
* .givenSolution(solutionA)
42-
* .score();
43+
* .getScore();
4344
* HardSoftScore scoreB = constraintVerifier.verifyThat()
4445
* .givenSolution(solutionB)
45-
* .score();
46+
* .getScore();
4647
* assertThat(scoreA).isGreaterThan(scoreB);
4748
* }
4849
*
4950
* @return the score produced by all constraints for the given facts, never null
5051
*/
51-
@NonNull
52-
<S extends Score<S>> S score();
52+
<S extends Score<S>> S getScore();
5353

5454
}

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

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import ai.timefold.solver.core.api.score.stream.Constraint;
88
import ai.timefold.solver.core.api.score.stream.ConstraintJustification;
99

10-
import org.jspecify.annotations.NonNull;
10+
import org.jspecify.annotations.NullMarked;
1111
import org.jspecify.annotations.Nullable;
1212

13+
@NullMarked
1314
public interface SingleConstraintAssertion {
1415

1516
/**
@@ -19,9 +20,8 @@ public interface SingleConstraintAssertion {
1920
* @param message description of the scenario being asserted
2021
* @throws AssertionError when the expected penalty is not observed
2122
*/
22-
@NonNull
2323
SingleConstraintAssertion justifiesWith(@Nullable String message,
24-
@NonNull ConstraintJustification @NonNull... justifications);
24+
ConstraintJustification... justifications);
2525

2626
/**
2727
* Asserts that the {@link Constraint} being tested, given a set of facts, results in a given
@@ -30,7 +30,7 @@ SingleConstraintAssertion justifiesWith(@Nullable String message,
3030
* @param justifications the expected justifications.
3131
* @throws AssertionError when the expected penalty is not observed
3232
*/
33-
default @NonNull SingleConstraintAssertion justifiesWith(@NonNull ConstraintJustification @NonNull... justifications) {
33+
default SingleConstraintAssertion justifiesWith(ConstraintJustification... justifications) {
3434
return justifiesWith(null, justifications);
3535
}
3636

@@ -41,9 +41,8 @@ SingleConstraintAssertion justifiesWith(@Nullable String message,
4141
* @param message description of the scenario being asserted
4242
* @throws AssertionError when the expected penalty is not observed
4343
*/
44-
@NonNull
4544
SingleConstraintAssertion justifiesWithExactly(@Nullable String message,
46-
@NonNull ConstraintJustification @NonNull... justifications);
45+
ConstraintJustification... justifications);
4746

4847
/**
4948
* Asserts that the {@link Constraint} being tested, given a set of facts, results in a given
@@ -52,8 +51,8 @@ SingleConstraintAssertion justifiesWithExactly(@Nullable String message,
5251
* @param justifications the expected justifications.
5352
* @throws AssertionError when the expected penalty is not observed
5453
*/
55-
default @NonNull SingleConstraintAssertion
56-
justifiesWithExactly(@NonNull ConstraintJustification @NonNull... justifications) {
54+
default SingleConstraintAssertion
55+
justifiesWithExactly(ConstraintJustification... justifications) {
5756
return justifiesWithExactly(null, justifications);
5857
}
5958

@@ -126,7 +125,7 @@ default void penalizesBy(long matchWeightTotal) {
126125
* @param matchWeightTotal at least 0, expected sum of match weights of matches of the constraint.
127126
* @throws AssertionError when the expected penalty is not observed
128127
*/
129-
default void penalizesBy(@NonNull BigDecimal matchWeightTotal) {
128+
default void penalizesBy(BigDecimal matchWeightTotal) {
130129
penalizesBy(null, matchWeightTotal);
131130
}
132131

@@ -137,7 +136,7 @@ default void penalizesBy(@NonNull BigDecimal matchWeightTotal) {
137136
* @param matchWeightTotal at least 0, expected sum of match weights of matches of the constraint.
138137
* @throws AssertionError when the expected penalty is not observed
139138
*/
140-
void penalizesBy(@Nullable String message, @NonNull BigDecimal matchWeightTotal);
139+
void penalizesBy(@Nullable String message, BigDecimal matchWeightTotal);
141140

142141
/**
143142
* Asserts that the {@link Constraint} being tested, given a set of facts, results in a given number of penalties.
@@ -232,7 +231,7 @@ default void rewardsWith(long matchWeightTotal) {
232231
* @param matchWeightTotal at least 0, expected sum of match weights of matches of the constraint.
233232
* @throws AssertionError when the expected reward is not observed
234233
*/
235-
default void rewardsWith(@NonNull BigDecimal matchWeightTotal) {
234+
default void rewardsWith(BigDecimal matchWeightTotal) {
236235
rewardsWith(null, matchWeightTotal);
237236
}
238237

@@ -243,7 +242,7 @@ default void rewardsWith(@NonNull BigDecimal matchWeightTotal) {
243242
* @param matchWeightTotal at least 0, expected sum of match weights of matches of the constraint.
244243
* @throws AssertionError when the expected reward is not observed
245244
*/
246-
void rewardsWith(@Nullable String message, @NonNull BigDecimal matchWeightTotal);
245+
void rewardsWith(@Nullable String message, BigDecimal matchWeightTotal);
247246

248247
/**
249248
* Asserts that the {@link Constraint} being tested, given a set of facts, results in a given number of rewards.
@@ -341,7 +340,7 @@ default void penalizesByMoreThan(long matchWeightTotal) {
341340
* @param matchWeightTotal at least 0, expected sum of match weights of matches of the constraint.
342341
* @throws AssertionError when the expected penalty is not observed
343342
*/
344-
default void penalizesByMoreThan(@NonNull BigDecimal matchWeightTotal) {
343+
default void penalizesByMoreThan(BigDecimal matchWeightTotal) {
345344
penalizesByMoreThan(null, matchWeightTotal);
346345
}
347346

@@ -352,7 +351,7 @@ default void penalizesByMoreThan(@NonNull BigDecimal matchWeightTotal) {
352351
* @param matchWeightTotal at least 0, expected sum of match weights of matches of the constraint.
353352
* @throws AssertionError when the expected penalty is not observed
354353
*/
355-
void penalizesByMoreThan(@Nullable String message, @NonNull BigDecimal matchWeightTotal);
354+
void penalizesByMoreThan(@Nullable String message, BigDecimal matchWeightTotal);
356355

357356
/**
358357
* Asserts that the {@link Constraint} being tested, given a set of facts,
@@ -431,7 +430,7 @@ default void rewardsWithMoreThan(long matchWeightTotal) {
431430
* @param matchWeightTotal at least 0, expected sum of match weights of matches of the constraint.
432431
* @throws AssertionError when the expected reward is not observed
433432
*/
434-
default void rewardsWithMoreThan(@NonNull BigDecimal matchWeightTotal) {
433+
default void rewardsWithMoreThan(BigDecimal matchWeightTotal) {
435434
rewardsWithMoreThan(null, matchWeightTotal);
436435
}
437436

@@ -442,7 +441,7 @@ default void rewardsWithMoreThan(@NonNull BigDecimal matchWeightTotal) {
442441
* @param matchWeightTotal at least 0, expected sum of match weights of matches of the constraint.
443442
* @throws AssertionError when the expected reward is not observed
444443
*/
445-
void rewardsWithMoreThan(@Nullable String message, @NonNull BigDecimal matchWeightTotal);
444+
void rewardsWithMoreThan(@Nullable String message, BigDecimal matchWeightTotal);
446445

447446
/**
448447
* Asserts that the {@link Constraint} being tested, given a set of facts,
@@ -522,7 +521,7 @@ default void penalizesByLessThan(long matchWeightTotal) {
522521
* @param matchWeightTotal at least 1, expected sum of match weights of matches of the constraint.
523522
* @throws AssertionError when the expected penalty is not observed
524523
*/
525-
default void penalizesByLessThan(@NonNull BigDecimal matchWeightTotal) {
524+
default void penalizesByLessThan(BigDecimal matchWeightTotal) {
526525
penalizesByLessThan(null, matchWeightTotal);
527526
}
528527

@@ -533,7 +532,7 @@ default void penalizesByLessThan(@NonNull BigDecimal matchWeightTotal) {
533532
* @param matchWeightTotal at least 1, expected sum of match weights of matches of the constraint.
534533
* @throws AssertionError when the expected penalty is not observed
535534
*/
536-
void penalizesByLessThan(@Nullable String message, @NonNull BigDecimal matchWeightTotal);
535+
void penalizesByLessThan(@Nullable String message, BigDecimal matchWeightTotal);
537536

538537
/**
539538
* Asserts that the {@link Constraint} being tested, given a set of facts,
@@ -612,7 +611,7 @@ default void rewardsWithLessThan(long matchWeightTotal) {
612611
* @param matchWeightTotal at least 1, expected sum of match weights of matches of the constraint.
613612
* @throws AssertionError when the expected reward is not observed
614613
*/
615-
default void rewardsWithLessThan(@NonNull BigDecimal matchWeightTotal) {
614+
default void rewardsWithLessThan(BigDecimal matchWeightTotal) {
616615
rewardsWithLessThan(null, matchWeightTotal);
617616
}
618617

@@ -623,7 +622,7 @@ default void rewardsWithLessThan(@NonNull BigDecimal matchWeightTotal) {
623622
* @param matchWeightTotal at least 1, expected sum of match weights of matches of the constraint.
624623
* @throws AssertionError when the expected reward is not observed
625624
*/
626-
void rewardsWithLessThan(@Nullable String message, @NonNull BigDecimal matchWeightTotal);
625+
void rewardsWithLessThan(@Nullable String message, BigDecimal matchWeightTotal);
627626

628627
/**
629628
* Asserts that the {@link Constraint} being tested, given a set of facts,
@@ -662,22 +661,21 @@ default void rewardsLessThan(long times) {
662661
* {@snippet :
663662
* HardSoftScore scoreA = constraintVerifier.verifyThat(MyConstraints::roomConflict)
664663
* .given(entity1, entity2)
665-
* .score();
664+
* .getScore();
666665
* HardSoftScore scoreB = constraintVerifier.verifyThat(MyConstraints::roomConflict)
667666
* .given(entity3, entity4)
668-
* .score();
667+
* .getScore();
669668
* assertThat(scoreA).isLessThan(scoreB);
670669
* }
671670
*
672671
* @return the score produced by this single constraint for the given facts, never null
673672
*/
674-
@NonNull
675-
<S extends Score<S>> S score();
673+
<S extends Score<S>> S getScore();
676674

677675
/**
678676
* Returns the match weight total of the {@link Constraint} being tested for the given set of facts.
679677
* <p>
680-
* Unlike {@link #score()}, which returns the full score (match weight × constraint weight),
678+
* Unlike {@link #getScore()}, which returns the full score (match weight × constraint weight),
681679
* this method returns only the <b>match weight</b> — the same number you would pass
682680
* to assertion methods like {@link #penalizesBy(int)} or {@link #rewardsWith(int)}.
683681
* <p>
@@ -687,19 +685,18 @@ default void rewardsLessThan(long times) {
687685
* Usage example:
688686
*
689687
* {@snippet :
690-
* Number impactA = constraintVerifier.verifyThat(MyConstraints::roomConflict)
688+
* var impactA = constraintVerifier.verifyThat(MyConstraints::roomConflict)
691689
* .given(entity1, entity2)
692-
* .impact();
693-
* Number impactB = constraintVerifier.verifyThat(MyConstraints::roomConflict)
690+
* .getImpact();
691+
* var impactB = constraintVerifier.verifyThat(MyConstraints::roomConflict)
694692
* .given(entity3, entity4)
695-
* .impact();
693+
* .getImpact();
696694
* assertThat(impactA.intValue()).isGreaterThan(impactB.intValue());
697695
* }
698696
*
699697
* @return the match weight total produced by this single constraint, never null.
700698
* Positive for rewards, negative for penalties, zero when there is no impact.
701699
*/
702-
@NonNull
703-
Number impact();
700+
Number getImpact();
704701

705702
}

core/src/main/java/ai/timefold/solver/core/impl/score/stream/test/AbstractMultiConstraintAssertion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void scores(@NonNull Score<?> score, String message) {
5959

6060
@Override
6161
@SuppressWarnings("unchecked")
62-
public <S extends Score<S>> S score() {
62+
public <S extends Score<S>> S getScore() {
6363
ensureInitialized();
6464
return (S) actualScore.raw();
6565
}

core/src/main/java/ai/timefold/solver/core/impl/score/stream/test/AbstractSingleConstraintAssertion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,13 @@ public void rewardsWithLessThan(@Nullable String message, @NonNull BigDecimal ma
254254

255255
@Override
256256
@SuppressWarnings("unchecked")
257-
public <S extends Score<S>> S score() {
257+
public <S extends Score<S>> S getScore() {
258258
ensureInitialized();
259259
return (S) actualScore.raw();
260260
}
261261

262262
@Override
263-
public Number impact() {
263+
public Number getImpact() {
264264
ensureInitialized();
265265
return deduceImpact().key();
266266
}

core/src/test/java/ai/timefold/solver/core/impl/score/stream/test/MultiConstraintAssertionTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void checksScore() {
7171
}
7272

7373
@Test
74-
void scoreReturnsTypedScoreWithGivenFacts() {
74+
void getScoreReturnsTypedScoreWithGivenFacts() {
7575
var constraintVerifier = ConstraintVerifier.build(new TestdataConstraintVerifierConstraintProvider(),
7676
TestdataConstraintVerifierExtendedSolution.class,
7777
TestdataConstraintVerifierFirstEntity.class,
@@ -81,7 +81,7 @@ void scoreReturnsTypedScoreWithGivenFacts() {
8181
HardSoftScore score = constraintVerifier
8282
.verifyThat()
8383
.given(entity)
84-
.score();
84+
.getScore();
8585

8686
assertThat(score).isNotNull();
8787
assertThat(score).isInstanceOf(HardSoftScore.class);
@@ -91,7 +91,7 @@ void scoreReturnsTypedScoreWithGivenFacts() {
9191
}
9292

9393
@Test
94-
void scoreEnablesRelativeComparisonWithGivenFacts() {
94+
void getScoreEnablesRelativeComparisonWithGivenFacts() {
9595
var constraintVerifier = ConstraintVerifier.build(new TestdataConstraintVerifierConstraintProvider(),
9696
TestdataConstraintVerifierExtendedSolution.class,
9797
TestdataConstraintVerifierFirstEntity.class,
@@ -106,11 +106,11 @@ void scoreEnablesRelativeComparisonWithGivenFacts() {
106106
HardSoftScore scoreA = constraintVerifier
107107
.verifyThat()
108108
.given(entityA)
109-
.score();
109+
.getScore();
110110
HardSoftScore scoreB = constraintVerifier
111111
.verifyThat()
112112
.given(entityB1, entityB2)
113-
.score();
113+
.getScore();
114114

115115
assertThat(scoreA).isGreaterThan(scoreB);
116116
}

core/src/test/java/ai/timefold/solver/core/impl/score/stream/test/SingleConstraintAssertionTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -587,35 +587,35 @@ void impactsByLessThan() {
587587
}
588588

589589
@Test
590-
void impactEnablesRelativeComparison() {
590+
void getImpactEnablesRelativeComparison() {
591591
var entityA = new TestdataConstraintVerifierFirstEntity("A", new TestdataValue());
592592
var solution = TestdataConstraintVerifierSolution.generateSolution(2, 3);
593593

594-
Number impactA = constraintVerifier
594+
var impactA = constraintVerifier
595595
.verifyThat(TestdataConstraintVerifierConstraintProvider::penalizeEveryEntity)
596596
.given(entityA)
597-
.impact();
598-
Number impactB = constraintVerifier
597+
.getImpact();
598+
var impactB = constraintVerifier
599599
.verifyThat(TestdataConstraintVerifierConstraintProvider::penalizeEveryEntity)
600600
.given(solution.getEntityList().toArray())
601-
.impact();
601+
.getImpact();
602602

603603
assertThat(impactB.intValue()).isGreaterThan(impactA.intValue());
604604
}
605605

606606
@Test
607-
void scoreEnablesRelativeComparisonForRewards() {
607+
void getScoreEnablesRelativeComparisonForRewards() {
608608
var entityA = new TestdataConstraintVerifierFirstEntity("A", new TestdataValue());
609609
var solution = TestdataConstraintVerifierSolution.generateSolution(2, 3);
610610

611611
HardSoftScore scoreA = constraintVerifier
612612
.verifyThat(TestdataConstraintVerifierConstraintProvider::rewardEveryEntity)
613613
.given(entityA)
614-
.score();
614+
.getScore();
615615
HardSoftScore scoreB = constraintVerifier
616616
.verifyThat(TestdataConstraintVerifierConstraintProvider::rewardEveryEntity)
617617
.given(solution.getEntityList().toArray())
618-
.score();
618+
.getScore();
619619

620620
assertThat(scoreA).isNotNull();
621621
assertThat(scoreA).isInstanceOf(HardSoftScore.class);
@@ -631,18 +631,18 @@ void scoreEnablesRelativeComparisonForRewards() {
631631
}
632632

633633
@Test
634-
void scoreComparesGivenSolutions() {
634+
void getScoreComparesGivenSolutions() {
635635
var smallSolution = TestdataConstraintVerifierExtendedSolution.generateSolution(2, 2);
636636
var largeSolution = TestdataConstraintVerifierExtendedSolution.generateSolution(2, 4);
637637

638638
HardSoftScore smallScore = constraintVerifier
639639
.verifyThat(TestdataConstraintVerifierConstraintProvider::penalizeEveryEntity)
640640
.givenSolution(smallSolution)
641-
.score();
641+
.getScore();
642642
HardSoftScore largeScore = constraintVerifier
643643
.verifyThat(TestdataConstraintVerifierConstraintProvider::penalizeEveryEntity)
644644
.givenSolution(largeSolution)
645-
.score();
645+
.getScore();
646646

647647
assertThat(smallScore).isGreaterThan(largeScore);
648648
}

0 commit comments

Comments
 (0)