org.opentripplanner.model.calendar.CalendarServiceData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otp Show documentation
Show all versions of otp Show documentation
The OpenTripPlanner multimodal journey planning system
/* This file is based on code copied from project OneBusAway, see the LICENSE file for further information. */
package org.opentripplanner.model.calendar;
import org.opentripplanner.model.FeedScopedId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Collectors;
public class CalendarServiceData implements Serializable {
private static final long serialVersionUID = 1L;
private static final String CAL_SERVICE_FEED_ID = "CSID";
private static final Logger LOG = LoggerFactory.getLogger(CalendarServiceData.class);
private final Map timeZonesByAgencyId = new HashMap<>();
private final Map> serviceDatesByServiceId = new HashMap<>();
private final Map> serviceIdsByDate = new HashMap<>();
/**
* @return the time zone for the specified agencyId, or null if the agency was
* not found
*/
public TimeZone getTimeZoneForAgencyId(FeedScopedId agencyId) {
return timeZonesByAgencyId.get(agencyId);
}
public void putTimeZoneForAgencyId(FeedScopedId agencyId, TimeZone timeZone) {
timeZonesByAgencyId.put(agencyId, timeZone);
}
public Set getAgencyIds() {
return Collections.unmodifiableSet(timeZonesByAgencyId.keySet());
}
public Set getServiceIds() {
return Collections.unmodifiableSet(serviceDatesByServiceId.keySet());
}
public List getServiceDatesForServiceId(FeedScopedId serviceId) {
return serviceDatesByServiceId.get(serviceId);
}
public Set getServiceIdsForDate(ServiceDate date) {
Set serviceIds = serviceIdsByDate.get(date);
if (serviceIds == null) {
serviceIds = new HashSet<>();
}
return serviceIds;
}
public void putServiceDatesForServiceId(FeedScopedId serviceId, List dates) {
List serviceDates = sortedImmutableList(dates);
serviceDatesByServiceId.put(serviceId, serviceDates);
addDatesToServiceIdsByDate(serviceId, serviceDates);
}
/**
* TODO OTP2 - This is NOT THREAD-SAFE and is used in the real-time updaters, we need to fix
* - this when doing the issue #3030.
*/
public FeedScopedId getOrCreateServiceIdForDate(ServiceDate serviceDate) {
FeedScopedId serviceId = new FeedScopedId(CAL_SERVICE_FEED_ID, serviceDate.asCompactString());
if(serviceDatesByServiceId.containsKey(serviceId)) {
return serviceId;
}
serviceDatesByServiceId.put(serviceId, List.of(serviceDate));
serviceIdsByDate.computeIfAbsent(serviceDate, d -> new HashSet<>()).add(serviceId);
LOG.info("Adding serviceId {} to CalendarService", serviceId);
return serviceId;
}
public void add(CalendarServiceData other) {
for (FeedScopedId agencyId : other.getAgencyIds()) {
putTimeZoneForAgencyId(agencyId, other.getTimeZoneForAgencyId(agencyId));
}
for (FeedScopedId serviceId : other.serviceDatesByServiceId.keySet()) {
putServiceDatesForServiceId(serviceId, other.serviceDatesByServiceId.get(serviceId));
}
}
/* private methods */
private static List sortedImmutableList(Collection c) {
return Collections.unmodifiableList(c.stream().sorted().collect(Collectors.toList()));
}
private void addDatesToServiceIdsByDate(FeedScopedId serviceId, List serviceDates) {
for (ServiceDate serviceDate : serviceDates) {
Set serviceIds = serviceIdsByDate.computeIfAbsent(
serviceDate,
k -> new HashSet<>()
);
serviceIds.add(serviceId);
}
}
}