-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ItineraryFares.java
236 lines (212 loc) · 7.01 KB
/
ItineraryFares.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package org.opentripplanner.routing.core;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.opentripplanner.ext.fares.model.FareProduct;
import org.opentripplanner.ext.fares.model.FareProductInstance;
import org.opentripplanner.ext.fares.model.LegProducts;
import org.opentripplanner.framework.tostring.ToStringBuilder;
import org.opentripplanner.model.plan.Leg;
import org.opentripplanner.transit.model.basic.Money;
import org.opentripplanner.transit.model.framework.FeedScopedId;
/**
* ItineraryFares is a set of fares for different legs, rider categories or fare media.
*/
public class ItineraryFares {
private static final String FARES_V1_FEED_ID = "faresv1";
/**
* The fare products that are valid for all legs of an itinerary, like a day pass.
* <p>
* Note: LinkedHashSet keeps the insertion order
*/
private final Set<FareProduct> itineraryProducts = new LinkedHashSet<>();
/**
* Fare products that apply to one or more legs but _not_ the entire journey.
* <p>
* Note: LinkedHashMultimap keeps the insertion order
*/
private final Multimap<Leg, FareProductInstance> legProducts = LinkedHashMultimap.create();
/**
* The fares V1 fare "components" that apply to individual legs (not the entire price of the
* itinerary).
* <p>
* This is an ill-thought-out concept that was bolted onto the existing implementation in 2016 and
* is going to be removed once HSL has migrated off it.
* <p>
* Note: LinkedHashMultimap keeps the insertion order
*/
@Deprecated
private final Multimap<FareType, FareComponent> components = LinkedHashMultimap.create();
public ItineraryFares(ItineraryFares aFare) {
if (aFare != null) {
itineraryProducts.addAll(aFare.itineraryProducts);
}
}
public ItineraryFares() {}
public static ItineraryFares empty() {
return new ItineraryFares();
}
/**
* Get those fare products that cover the entire itinerary.
*/
public List<FareProduct> getItineraryProducts() {
return List.copyOf(itineraryProducts);
}
/**
* Get those fare products that are valid for a subset of legs but not the entire itinerary.
*/
public Multimap<Leg, FareProductInstance> getLegProducts() {
return ImmutableMultimap.copyOf(legProducts);
}
/**
* Add a "fare". This is an ill-defined concept (is it for the entire itinerary or only some
* legs?) from the early days of OTP which will be removed in the future.
* <p>
* Use {@link ItineraryFares#addFareProduct(Leg, FareProduct)},
* {@link ItineraryFares#addLegProducts(Collection)} or
* {@link ItineraryFares#addItineraryProducts(Collection)} instead.
*/
@Deprecated
public void addFare(FareType fareType, Money money) {
itineraryProducts.add(
new FareProduct(
faresV1Id(fareType),
"Itinerary fare for type %s".formatted(fareType.name()),
money,
null,
null,
null
)
);
}
/**
* Add a collection of "fare components" for a type. These concepts are ill-defined and will be
* removed in the future.
* <p>
* Use @{link {@link ItineraryFares#addItineraryProducts(Collection)}},
* {@link ItineraryFares#addFareProduct(Leg, FareProduct)} or
* {@link ItineraryFares#addLegProducts(Collection)} instead.
*/
@Deprecated
public void addFareComponent(FareType fareType, List<FareComponent> components) {
this.components.replaceValues(fareType, components);
for (var c : components) {
var firstLegStartTime = c.legs().get(0).getStartTime();
for (var leg : c.legs()) {
final FareProduct fareProduct = new FareProduct(
c.fareId(),
fareType.name(),
c.price(),
null,
null,
null
);
legProducts.put(
leg,
new FareProductInstance(fareProduct.uniqueInstanceId(firstLegStartTime), fareProduct)
);
}
}
}
/**
* Add fare products that cover the entire itinerary, i.e. are valid for all legs.
*/
public void addItineraryProducts(Collection<FareProduct> products) {
itineraryProducts.addAll(products);
}
/**
* Get the "fare" for a specific fare type.
* <p>
* It is ill-defined what this actually means (entire itinerary?, some legs?).
* <p>
* Use {@link ItineraryFares#getItineraryProducts()} or {@link ItineraryFares#getLegProducts()}
* instead.
*/
public Money getFare(FareType type) {
return itineraryProducts
.stream()
.filter(f -> faresV1Id(type).equals(f.id()))
.findAny()
.map(FareProduct::amount)
.orElse(null);
}
/**
* Get the "components" of a fare for a specific type.
* <p>
* Use {@link ItineraryFares#getItineraryProducts()} or {@link ItineraryFares#getLegProducts()}
* instead.
*/
@Deprecated
public List<FareComponent> getComponents(FareType type) {
return List.copyOf(components.get(type));
}
/**
* Return the set of {@link FareType}s that are contained in this instance.
*/
@Deprecated
public Set<FareType> getFaresV1Types() {
return itineraryProducts
.stream()
.filter(fp -> fp.id().getFeedId().equals(FARES_V1_FEED_ID))
.map(fp -> fp.id().getId())
.map(FareType::valueOf)
.collect(Collectors.toSet());
}
@Override
public int hashCode() {
return Objects.hash(itineraryProducts, legProducts, components);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ItineraryFares fare1 = (ItineraryFares) o;
return (
Objects.equals(itineraryProducts, fare1.itineraryProducts) &&
Objects.equals(legProducts, fare1.legProducts)
);
}
@Override
public String toString() {
return ToStringBuilder
.of(this.getClass())
.addObj("itineraryProducts", itineraryProducts)
.addObj("legProducts", legProducts)
.toString();
}
/**
* Add a complex set of fare products for a specific leg;
*/
public void addLegProducts(Collection<LegProducts> legProducts) {
legProducts.forEach(lp -> {
var time = lp.leg().getStartTime();
var products = lp
.products()
.stream()
.map(LegProducts.ProductWithTransfer::product)
.map(fp -> new FareProductInstance(fp.uniqueInstanceId(time), fp))
.toList();
this.legProducts.putAll(lp.leg(), products);
});
}
/**
* Add a single fare product for a single leg.
*/
public void addFareProduct(Leg leg, FareProduct fareProduct) {
this.legProducts.put(
leg,
new FareProductInstance(fareProduct.uniqueInstanceId(leg.getStartTime()), fareProduct)
);
}
@Nonnull
private static FeedScopedId faresV1Id(FareType fareType) {
return new FeedScopedId(FARES_V1_FEED_ID, fareType.name());
}
}