44import java .util .ArrayList ;
55import java .util .Arrays ;
66import java .util .List ;
7+ import java .util .function .Predicate ;
78import java .util .stream .Collectors ;
89import org .opentripplanner .api .types .Route ;
910import org .opentripplanner .client .model .Itinerary ;
@@ -23,6 +24,10 @@ public class ItineraryAssertions {
2324 private List <LegCriterion > currentLegCriteria ;
2425 private boolean strictTransitMatching = false ;
2526
27+ private void addCurrentLegCriterion (String message , Predicate <Leg > predicate ) {
28+ currentLegCriteria .add (new LegCriterion (message , predicate ));
29+ }
30+
2631 /**
2732 * Adds a new distinct leg criterion set and moves the current leg pointer to it.
2833 *
@@ -35,116 +40,54 @@ public ItineraryAssertions hasLeg() {
3540 }
3641
3742 public ItineraryAssertions withRouteLongName (String ... longNames ) {
38- var message = "route '%s'" .formatted (Arrays .toString (longNames ));
39- currentLegCriteria .add (
40- new LegCriterion (
41- message ,
42- state -> {
43- Leg leg = state .getLeg ();
44- String longName = routeLongName (leg .route ());
45- boolean matches =
46- leg .isTransit ()
47- && longName != null
48- && Arrays .asList (longNames ).contains (longName );
49- if (matches ) {
50- state .addMatch (message );
51- } else {
52- state .addFailure (message );
53- }
54- }));
43+ addCurrentLegCriterion (
44+ "route '%s'" .formatted (Arrays .toString (longNames )),
45+ leg -> {
46+ String longName = routeLongName (leg .route ());
47+ return leg .isTransit () && longName != null && Arrays .asList (longNames ).contains (longName );
48+ });
5549 return this ;
5650 }
5751
5852 public ItineraryAssertions withMaxDuration (Duration duration ) {
59- var message = "duration '%s'" .formatted (duration );
60- currentLegCriteria .add (
61- new LegCriterion (
62- message ,
63- state -> {
64- Leg leg = state .getLeg ();
65- boolean matches = leg .isTransit () && leg .duration ().compareTo (duration ) < 1 ;
66- if (matches ) {
67- state .addMatch (message );
68- } else {
69- state .addFailure (message );
70- }
71- }));
53+ addCurrentLegCriterion (
54+ "duration '%s'" .formatted (duration ),
55+ leg -> leg .isTransit () && leg .duration ().compareTo (duration ) < 1 );
7256 return this ;
7357 }
7458
7559 public ItineraryAssertions withRouteShortName (String ... shortNames ) {
76- var message = "route '%s'" .formatted (Arrays .toString (shortNames ));
77- currentLegCriteria .add (
78- new LegCriterion (
79- message ,
80- state -> {
81- Leg leg = state .getLeg ();
82- String shortName = routeShortName (leg .route ());
83- boolean matches =
84- leg .isTransit ()
85- && shortName != null
86- && Arrays .asList (shortNames ).contains (shortName );
87- if (matches ) {
88- state .addMatch (message );
89- } else {
90- state .addFailure (message );
91- }
92- }));
60+ addCurrentLegCriterion (
61+ "route '%s'" .formatted (Arrays .toString (shortNames )),
62+ leg -> {
63+ String shortName = routeShortName (leg .route ());
64+ return leg .isTransit ()
65+ && shortName != null
66+ && Arrays .asList (shortNames ).contains (shortName );
67+ });
9368 return this ;
9469 }
9570
9671 public ItineraryAssertions withFarePrice (float price , String riderCategoryId , String mediumId ) {
97- var message = "fare $%.2f" .formatted (price );
98- currentLegCriteria .add (
99- new LegCriterion (
100- message ,
101- state -> {
102- Leg leg = state .getLeg ();
103- boolean matches =
104- leg .fareProducts ().stream ()
105- .filter (fp -> fp .product ().riderCategory ().isPresent ())
106- .filter (fp -> fp .product ().medium ().isPresent ())
107- .filter (fp -> fp .product ().riderCategory ().get ().id ().equals (riderCategoryId ))
108- .filter (fp -> fp .product ().medium ().get ().id ().equals (mediumId ))
109- .anyMatch (fp -> fp .product ().price ().amount ().floatValue () == price );
110- if (matches ) {
111- state .addMatch (message );
112- } else {
113- state .addFailure (message );
114- }
115- }));
72+ addCurrentLegCriterion (
73+ "fare $%.2f" .formatted (price ),
74+ leg ->
75+ leg .fareProducts ().stream ()
76+ .filter (fp -> fp .product ().riderCategory ().isPresent ())
77+ .filter (fp -> fp .product ().medium ().isPresent ())
78+ .filter (fp -> fp .product ().riderCategory ().get ().id ().equals (riderCategoryId ))
79+ .filter (fp -> fp .product ().medium ().get ().id ().equals (mediumId ))
80+ .anyMatch (fp -> fp .product ().price ().amount ().floatValue () == price ));
11681 return this ;
11782 }
11883
11984 public ItineraryAssertions interlinedWithPreviousLeg () {
120- currentLegCriteria .add (
121- new LegCriterion (
122- "interlined with previous leg" ,
123- state -> {
124- Leg leg = state .getLeg ();
125- if (leg .interlineWithPreviousLeg ()) {
126- state .addMatch ("interlined with previous leg" );
127- } else {
128- state .addFailure ("interlined with previous leg" );
129- }
130- }));
85+ addCurrentLegCriterion ("interlined with previous leg" , Leg ::interlineWithPreviousLeg );
13186 return this ;
13287 }
13388
13489 public ItineraryAssertions withMode (String mode ) {
135- var message = "mode %s" .formatted (mode );
136- currentLegCriteria .add (
137- new LegCriterion (
138- message ,
139- state -> {
140- Leg leg = state .getLeg ();
141- boolean matches = leg .mode ().toString ().equals (mode );
142- if (matches ) {
143- state .addMatch (message );
144- } else {
145- state .addFailure (message );
146- }
147- }));
90+ addCurrentLegCriterion ("mode %s" .formatted (mode ), leg -> leg .mode ().toString ().equals (mode ));
14891 return this ;
14992 }
15093
@@ -238,7 +181,15 @@ private ItineraryMatchResult matchesAllLegs(Itinerary itinerary) {
238181 for (int i = 0 ; i < remainingLegs .size (); i ++) {
239182 Leg leg = remainingLegs .get (i );
240183 LegMatchingState state = new LegMatchingState (leg );
241- criteriaSet .forEach (criterion -> criterion .test ().accept (state ));
184+ criteriaSet .forEach (
185+ criterion -> {
186+ var pass = criterion .test ().test (leg );
187+ if (pass ) {
188+ state .addMatch (criterion .message ());
189+ } else {
190+ state .addFailure (criterion .message ());
191+ }
192+ });
242193
243194 if (state .isFullMatch ()) {
244195 remainingLegs .remove (i );
0 commit comments