org.opentripplanner.netex.loader.NetexImportDataIndex Maven / Gradle / Ivy
package org.opentripplanner.netex.loader;
import org.opentripplanner.netex.loader.util.HierarchicalElement;
import org.opentripplanner.netex.loader.util.HierarchicalMap;
import org.opentripplanner.netex.loader.util.HierarchicalMapById;
import org.opentripplanner.netex.loader.util.HierarchicalMultimap;
import org.opentripplanner.netex.loader.util.HierarchicalVersionMapById;
import org.opentripplanner.netex.loader.util.ReadOnlyHierarchicalMap;
import org.opentripplanner.netex.loader.util.ReadOnlyHierarchicalMapById;
import org.opentripplanner.netex.loader.util.ReadOnlyHierarchicalVersionMapById;
import org.opentripplanner.netex.support.DayTypeRefsToServiceIdAdapter;
import org.rutebanken.netex.model.Authority;
import org.rutebanken.netex.model.DayType;
import org.rutebanken.netex.model.DayTypeAssignment;
import org.rutebanken.netex.model.DestinationDisplay;
import org.rutebanken.netex.model.GroupOfLines;
import org.rutebanken.netex.model.GroupOfStopPlaces;
import org.rutebanken.netex.model.JourneyPattern;
import org.rutebanken.netex.model.Line;
import org.rutebanken.netex.model.Network;
import org.rutebanken.netex.model.Notice;
import org.rutebanken.netex.model.NoticeAssignment;
import org.rutebanken.netex.model.OperatingPeriod;
import org.rutebanken.netex.model.Operator;
import org.rutebanken.netex.model.Quay;
import org.rutebanken.netex.model.Route;
import org.rutebanken.netex.model.ServiceJourney;
import org.rutebanken.netex.model.ServiceLink;
import org.rutebanken.netex.model.StopPlace;
import org.rutebanken.netex.model.TariffZone;
import org.rutebanken.netex.model.TimetabledPassingTime;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* This class holds indexes of Netex objects for lookup during the NeTEx import using the
* {@link NetexImportDataIndexReadOnlyView}.
*
* A NeTEx import is grouped into several levels: shard data, group of shared data,
* and single files. We create a hierarchy of {@code NetexImportDataIndex} to avoid keeping everything
* in memory and to be able to override values in a more specific(lower) level.
*
* There is one instance of this class for shard data - the ROOT.
* For each group of shared data a new {@code NetexImportDataIndex} is created with the ROOT as a
* parent. When such group of shared data is not needed any more it is discard and become
* ready for garbage collection.
* For each single files a new {@code NetexImportDataIndex} is created with the corresponding
* group of shared data as parent. The single files object is thrown away when
* the file is loaded.
*
* This hierarchy make it possible to override values in child instances of the {@code NetexImportDataIndex}
* and save memory during the load operation, because data not needed any more can be thrown away.
*
* The hierarchy implementation is delegated to the
* {@link org.opentripplanner.netex.loader.util.AbstractHierarchicalMap} and the
* {@link HierarchicalElement} classes.
*
* The mapping code should not insert entities, so an instance of this class implements the
* {@link NetexImportDataIndexReadOnlyView} witch is passed to the mapping code for translation into
* OTP domain model objects.
*/
public class NetexImportDataIndex {
// Indexes to entities
public final HierarchicalMapById authoritiesById;
public final HierarchicalMapById dayTypeById;
public final HierarchicalMultimap dayTypeAssignmentByDayTypeId;
/**
* DayTypeRefs is only needed in the local scope, no need to lookup values in the parent.
* */
public final Set dayTypeRefs;
public final HierarchicalMapById destinationDisplayById;
public final HierarchicalMapById groupOfLinesById;
public final HierarchicalMapById groupOfStopPlacesById;
public final HierarchicalMapById journeyPatternsById;
public final HierarchicalMapById lineById;
public final HierarchicalMapById multiModalStopPlaceById;
public final HierarchicalMapById networkById;
public final HierarchicalMapById noticeById;
public final HierarchicalMapById noticeAssignmentById;
public final HierarchicalMapById operatingPeriodById;
public final HierarchicalMapById operatorsById;
public final HierarchicalMultimap passingTimeByStopPointId;
public final HierarchicalVersionMapById quayById;
public final HierarchicalMap quayIdByStopPointRef;
public final HierarchicalMapById routeById;
public final HierarchicalMultimap serviceJourneyByPatternId;
public final HierarchicalMapById serviceLinkById;
public final HierarchicalVersionMapById stopPlaceById;
public final HierarchicalMapById tariffZonesById;
// Relations between entities - The Netex XML sometimes rely on the the
// nested structure of the XML document, rater than explicit references.
// Since we throw away the document we need to keep track of these.
public final HierarchicalMap networkIdByGroupOfLineId;
// Shared data
public final HierarchicalElement timeZone;
/**
* Create a root node.
*/
public NetexImportDataIndex() {
this.authoritiesById = new HierarchicalMapById<>();
this.dayTypeById = new HierarchicalMapById<>();
this.dayTypeAssignmentByDayTypeId = new HierarchicalMultimap<>();
this.dayTypeRefs = new HashSet<>();
this.destinationDisplayById = new HierarchicalMapById<>();
this.groupOfLinesById = new HierarchicalMapById<>();
this.groupOfStopPlacesById = new HierarchicalMapById<>();
this.journeyPatternsById = new HierarchicalMapById<>();
this.lineById = new HierarchicalMapById<>();
this.multiModalStopPlaceById = new HierarchicalMapById<>();
this.networkById = new HierarchicalMapById<>();
this.networkIdByGroupOfLineId = new HierarchicalMap<>();
this.noticeById = new HierarchicalMapById<>();
this.noticeAssignmentById = new HierarchicalMapById<>();
this.operatingPeriodById = new HierarchicalMapById<>();
this.operatorsById = new HierarchicalMapById<>();
this.passingTimeByStopPointId = new HierarchicalMultimap<>();
this.quayById = new HierarchicalVersionMapById<>();
this.quayIdByStopPointRef = new HierarchicalMap<>();
this.routeById = new HierarchicalMapById<>();
this.serviceJourneyByPatternId = new HierarchicalMultimap<>();
this.serviceLinkById = new HierarchicalMapById<>();
this.stopPlaceById = new HierarchicalVersionMapById<>();
this.tariffZonesById = new HierarchicalMapById<>();
this.timeZone = new HierarchicalElement<>();
}
/**
* Create a child node.
* @param parent can not be null
.
*/
NetexImportDataIndex(NetexImportDataIndex parent) {
this.authoritiesById = new HierarchicalMapById<>(parent.authoritiesById);
this.dayTypeById = new HierarchicalMapById<>(parent.dayTypeById);
this.dayTypeAssignmentByDayTypeId = new HierarchicalMultimap<>(parent.dayTypeAssignmentByDayTypeId);
this.dayTypeRefs = new HashSet<>();
this.destinationDisplayById = new HierarchicalMapById<>(parent.destinationDisplayById);
this.groupOfLinesById = new HierarchicalMapById<>(parent.groupOfLinesById);
this.groupOfStopPlacesById = new HierarchicalMapById<>(parent.groupOfStopPlacesById);
this.journeyPatternsById = new HierarchicalMapById<>(parent.journeyPatternsById);
this.lineById = new HierarchicalMapById<>(parent.lineById);
this.multiModalStopPlaceById = new HierarchicalMapById<>(parent.multiModalStopPlaceById);
this.networkById = new HierarchicalMapById<>(parent.networkById);
this.networkIdByGroupOfLineId = new HierarchicalMap<>(parent.networkIdByGroupOfLineId);
this.noticeById = new HierarchicalMapById<>(parent.noticeById);
this.noticeAssignmentById = new HierarchicalMapById<>(parent.noticeAssignmentById);
this.operatingPeriodById = new HierarchicalMapById<>(parent.operatingPeriodById);
this.operatorsById = new HierarchicalMapById<>(parent.operatorsById);
this.passingTimeByStopPointId = new HierarchicalMultimap<>(parent.passingTimeByStopPointId);
this.quayById = new HierarchicalVersionMapById<>(parent.quayById);
this.quayIdByStopPointRef = new HierarchicalMap<>(parent.quayIdByStopPointRef);
this.routeById = new HierarchicalMapById<>(parent.routeById);
this.serviceJourneyByPatternId = new HierarchicalMultimap<>(parent.serviceJourneyByPatternId);
this.serviceLinkById = new HierarchicalMapById<>(parent.serviceLinkById);
this.stopPlaceById = new HierarchicalVersionMapById<>(parent.stopPlaceById);
this.tariffZonesById = new HierarchicalMapById<>(parent.tariffZonesById);
this.timeZone = new HierarchicalElement<>(parent.timeZone);
}
public NetexImportDataIndexReadOnlyView readOnlyView() {
return new NetexImportDataIndexReadOnlyView() {
/**
* Lookup a Network given a GroupOfLine id or an Network id. If the given
* {@code groupOfLineOrNetworkId} is a GroupOfLine ID, we lookup the GroupOfLine, and then
* lookup its Network. If the given {@code groupOfLineOrNetworkId} is a Network ID then we
* can lookup the Network directly.
*
* If no Network is found {@code null} is returned.
*/
public Network lookupNetworkForLine(String groupOfLineOrNetworkId) {
GroupOfLines groupOfLines = groupOfLinesById.lookup(groupOfLineOrNetworkId);
String networkId = groupOfLines == null
? groupOfLineOrNetworkId
: networkIdByGroupOfLineId.lookup(groupOfLines.getId());
return networkById.lookup(networkId);
}
public ReadOnlyHierarchicalMapById getAuthoritiesById() {
return authoritiesById;
}
public ReadOnlyHierarchicalMapById getDayTypeById() {
return dayTypeById;
}
public ReadOnlyHierarchicalMap> getDayTypeAssignmentByDayTypeId() {
return dayTypeAssignmentByDayTypeId;
}
public Iterable getDayTypeRefs() {
return Collections.unmodifiableSet(dayTypeRefs);
}
public ReadOnlyHierarchicalMapById getDestinationDisplayById() {
return destinationDisplayById;
}
public ReadOnlyHierarchicalMapById getGroupOfStopPlacesById() {
return groupOfStopPlacesById;
}
public ReadOnlyHierarchicalMapById getJourneyPatternsById() {
return journeyPatternsById;
}
public ReadOnlyHierarchicalMapById getLineById() {
return lineById;
}
public ReadOnlyHierarchicalMapById getMultiModalStopPlaceById() {
return multiModalStopPlaceById;
}
public ReadOnlyHierarchicalMapById getNoticeById() {
return noticeById;
}
public ReadOnlyHierarchicalMapById getNoticeAssignmentById() {
return noticeAssignmentById;
}
public ReadOnlyHierarchicalMapById getOperatingPeriodById() {
return operatingPeriodById;
}
public ReadOnlyHierarchicalMapById getOperatorsById() {
return operatorsById;
}
public ReadOnlyHierarchicalMap> getPassingTimeByStopPointId() {
return passingTimeByStopPointId;
}
public ReadOnlyHierarchicalVersionMapById getQuayById() {
return quayById;
}
public ReadOnlyHierarchicalMap getQuayIdByStopPointRef() {
return quayIdByStopPointRef;
}
public ReadOnlyHierarchicalMapById getRouteById() {
return routeById;
}
public ReadOnlyHierarchicalMap> getServiceJourneyByPatternId() {
return serviceJourneyByPatternId;
}
public ReadOnlyHierarchicalMapById getServiceLinkById() {
return serviceLinkById;
}
public ReadOnlyHierarchicalVersionMapById getStopPlaceById() {
return stopPlaceById;
}
public ReadOnlyHierarchicalMapById getTariffZonesById() {
return tariffZonesById;
}
public String getTimeZone() {
return timeZone.get();
}
};
}
}