All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.netex.index.NetexEntityIndex Maven / Gradle / Ivy

package org.opentripplanner.netex.index;

import java.util.Collection;
import org.opentripplanner.netex.index.api.NetexEntityIndexReadOnlyView;
import org.opentripplanner.netex.index.api.ReadOnlyHierarchicalMap;
import org.opentripplanner.netex.index.api.ReadOnlyHierarchicalMapById;
import org.opentripplanner.netex.index.api.ReadOnlyHierarchicalVersionMapById;
import org.opentripplanner.netex.index.hierarchy.HierarchicalElement;
import org.opentripplanner.netex.index.hierarchy.HierarchicalMap;
import org.opentripplanner.netex.index.hierarchy.HierarchicalMapById;
import org.opentripplanner.netex.index.hierarchy.HierarchicalMultimap;
import org.opentripplanner.netex.index.hierarchy.HierarchicalVersionMapById;
import org.rutebanken.netex.model.Authority;
import org.rutebanken.netex.model.Branding;
import org.rutebanken.netex.model.DatedServiceJourney;
import org.rutebanken.netex.model.DayType;
import org.rutebanken.netex.model.DayTypeAssignment;
import org.rutebanken.netex.model.DestinationDisplay;
import org.rutebanken.netex.model.FlexibleLine;
import org.rutebanken.netex.model.FlexibleStopPlace;
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.OperatingDay;
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.ServiceJourneyInterchange;
import org.rutebanken.netex.model.ServiceLink;
import org.rutebanken.netex.model.StopPlace;
import org.rutebanken.netex.model.TariffZone;

/**
 * This class holds indexes of Netex objects for lookup during the NeTEx import using the
 * {@link NetexEntityIndexReadOnlyView}.
 * 

* 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.index.hierarchy.AbstractHierarchicalMap} and the * {@link HierarchicalElement} classes. *

* The mapping code should not insert entities, so an instance of this class implements the * {@link NetexEntityIndexReadOnlyView} which is passed to the mapping code for translation into * OTP domain model objects. */ public class NetexEntityIndex { private final NetexEntityIndex parent; // Indexes to entities public final HierarchicalMapById authoritiesById; public final HierarchicalMapById datedServiceJourneys; public final HierarchicalMapById dayTypeById; public final HierarchicalMultimap dayTypeAssignmentByDayTypeId; public final HierarchicalMapById destinationDisplayById; public final HierarchicalMapById flexibleStopPlaceById; public final HierarchicalMapById groupOfLinesById; public final HierarchicalMapById groupOfStopPlacesById; public final HierarchicalMapById journeyPatternsById; public final HierarchicalMapById flexibleLineByid; public final HierarchicalMapById lineById; public final HierarchicalMapById multiModalStopPlaceById; public final HierarchicalMapById networkById; public final HierarchicalMapById noticeById; public final HierarchicalMapById noticeAssignmentById; public final HierarchicalMapById operatingDayById; public final HierarchicalMapById operatingPeriodById; public final HierarchicalMapById operatorsById; public final HierarchicalVersionMapById quayById; public final HierarchicalMap flexibleStopPlaceByStopPointRef; public final HierarchicalMap quayIdByStopPointRef; public final HierarchicalMapById routeById; public final HierarchicalMapById serviceJourneyById; public final HierarchicalMapById serviceJourneyInterchangeById; public final HierarchicalMapById serviceLinkById; public final HierarchicalVersionMapById stopPlaceById; public final HierarchicalVersionMapById tariffZonesById; public final HierarchicalMapById brandingById; // 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 NetexEntityIndex() { this.parent = null; this.authoritiesById = new HierarchicalMapById<>(); this.dayTypeById = new HierarchicalMapById<>(); this.dayTypeAssignmentByDayTypeId = new HierarchicalMultimap<>(); this.datedServiceJourneys = new HierarchicalMapById<>(); this.destinationDisplayById = new HierarchicalMapById<>(); this.flexibleStopPlaceById = new HierarchicalMapById<>(); this.groupOfLinesById = new HierarchicalMapById<>(); this.groupOfStopPlacesById = new HierarchicalMapById<>(); this.journeyPatternsById = new HierarchicalMapById<>(); this.flexibleLineByid = 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.operatingDayById = new HierarchicalMapById<>(); this.operatingPeriodById = new HierarchicalMapById<>(); this.operatorsById = new HierarchicalMapById<>(); this.quayById = new HierarchicalVersionMapById<>(); this.flexibleStopPlaceByStopPointRef = new HierarchicalMap<>(); this.quayIdByStopPointRef = new HierarchicalMap<>(); this.routeById = new HierarchicalMapById<>(); this.serviceJourneyById = new HierarchicalMapById<>(); this.serviceLinkById = new HierarchicalMapById<>(); this.serviceJourneyInterchangeById = new HierarchicalMapById<>(); this.stopPlaceById = new HierarchicalVersionMapById<>(); this.tariffZonesById = new HierarchicalVersionMapById<>(); this.brandingById = new HierarchicalMapById<>(); this.timeZone = new HierarchicalElement<>(); } /** * Create a child node. * @param parent can not be null. */ public NetexEntityIndex(NetexEntityIndex parent) { this.parent = parent; this.authoritiesById = new HierarchicalMapById<>(parent.authoritiesById); this.dayTypeById = new HierarchicalMapById<>(parent.dayTypeById); this.dayTypeAssignmentByDayTypeId = new HierarchicalMultimap<>(parent.dayTypeAssignmentByDayTypeId); this.datedServiceJourneys = new HierarchicalMapById<>(parent.datedServiceJourneys); this.destinationDisplayById = new HierarchicalMapById<>(parent.destinationDisplayById); this.flexibleStopPlaceById = new HierarchicalMapById<>(parent.flexibleStopPlaceById); this.groupOfLinesById = new HierarchicalMapById<>(parent.groupOfLinesById); this.groupOfStopPlacesById = new HierarchicalMapById<>(parent.groupOfStopPlacesById); this.journeyPatternsById = new HierarchicalMapById<>(parent.journeyPatternsById); this.flexibleLineByid = new HierarchicalMapById<>(parent.flexibleLineByid); 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.operatingDayById = new HierarchicalMapById<>(parent.operatingDayById); this.operatingPeriodById = new HierarchicalMapById<>(parent.operatingPeriodById); this.operatorsById = new HierarchicalMapById<>(parent.operatorsById); this.quayById = new HierarchicalVersionMapById<>(parent.quayById); this.flexibleStopPlaceByStopPointRef = new HierarchicalMap<>(parent.flexibleStopPlaceByStopPointRef); this.quayIdByStopPointRef = new HierarchicalMap<>(parent.quayIdByStopPointRef); this.routeById = new HierarchicalMapById<>(parent.routeById); this.serviceJourneyById = new HierarchicalMapById<>(parent.serviceJourneyById); this.serviceLinkById = new HierarchicalMapById<>(parent.serviceLinkById); this.serviceJourneyInterchangeById = new HierarchicalMapById<>(parent.serviceJourneyInterchangeById); this.stopPlaceById = new HierarchicalVersionMapById<>(parent.stopPlaceById); this.tariffZonesById = new HierarchicalVersionMapById<>(parent.tariffZonesById); this.brandingById = new HierarchicalMapById<>(parent.brandingById); this.timeZone = new HierarchicalElement<>(parent.timeZone); } /** * Prepare to for indexing of a new sub-level of entities(shared-files, shared-group-files and * group-files). This is a life-cycle method used to notify this class that a new dataset is * about to be processed. Any existing intermediate state must be saved(pushed down the stack). */ public NetexEntityIndex push() { return new NetexEntityIndex(this); } /** * It is now safe to discard any intermediate state added since last call to the {@link #push()} * method. */ public NetexEntityIndex pop() { return this.parent; } public NetexEntityIndexReadOnlyView readOnlyView() { return new NetexEntityIndexReadOnlyView() { /** * 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. */ @Override public Network lookupNetworkForLine(String groupOfLineOrNetworkId) { GroupOfLines groupOfLines = groupOfLinesById.lookup(groupOfLineOrNetworkId); String networkId = groupOfLines == null ? groupOfLineOrNetworkId : networkIdByGroupOfLineId.lookup(groupOfLines.getId()); return networkById.lookup(networkId); } @Override public ReadOnlyHierarchicalMapById getAuthoritiesById() { return authoritiesById; } @Override public ReadOnlyHierarchicalMapById getDayTypeById() { return dayTypeById; } /** * @deprecated This should be replaced with a collection of DayTypeAssignment. The * mapper is responsible for indexing its data, except for entities by id. */ @Deprecated public ReadOnlyHierarchicalMap> getDayTypeAssignmentByDayTypeId() { return dayTypeAssignmentByDayTypeId; } @Override public ReadOnlyHierarchicalMapById getDatedServiceJourneys() { return datedServiceJourneys; } @Override public ReadOnlyHierarchicalMapById getDestinationDisplayById() { return destinationDisplayById; } @Override public ReadOnlyHierarchicalMapById getFlexibleStopPlacesById() { return flexibleStopPlaceById; } @Override public ReadOnlyHierarchicalMapById getGroupOfStopPlacesById() { return groupOfStopPlacesById; } @Override public ReadOnlyHierarchicalMapById getJourneyPatternsById() { return journeyPatternsById; } @Override public ReadOnlyHierarchicalMapById getFlexibleLineById() { return flexibleLineByid; } @Override public ReadOnlyHierarchicalMapById getLineById() { return lineById; } @Override public ReadOnlyHierarchicalMapById getMultiModalStopPlaceById() { return multiModalStopPlaceById; } @Override public ReadOnlyHierarchicalMapById getNoticeById() { return noticeById; } @Override public ReadOnlyHierarchicalMapById getNoticeAssignmentById() { return noticeAssignmentById; } @Override public ReadOnlyHierarchicalMapById getOperatingDayById() { return operatingDayById; } @Override public ReadOnlyHierarchicalMapById getOperatingPeriodById() { return operatingPeriodById; } @Override public ReadOnlyHierarchicalMapById getOperatorsById() { return operatorsById; } @Override public ReadOnlyHierarchicalVersionMapById getQuayById() { return quayById; } @Override public ReadOnlyHierarchicalMap getFlexibleStopPlaceByStopPointRef() { return flexibleStopPlaceByStopPointRef; } @Override public ReadOnlyHierarchicalMap getQuayIdByStopPointRef() { return quayIdByStopPointRef; } @Override public ReadOnlyHierarchicalMapById getRouteById() { return routeById; } @Override public ReadOnlyHierarchicalMapById getServiceJourneyById() { return serviceJourneyById; } @Override public ReadOnlyHierarchicalMapById getServiceJourneyInterchangeById() { return serviceJourneyInterchangeById; } @Override public ReadOnlyHierarchicalMapById getServiceLinkById() { return serviceLinkById; } @Override public ReadOnlyHierarchicalVersionMapById getStopPlaceById() { return stopPlaceById; } @Override public ReadOnlyHierarchicalVersionMapById getTariffZonesById() { return tariffZonesById; } @Override public String getTimeZone() { return timeZone.get(); } @Override public ReadOnlyHierarchicalMapById getBrandingById() { return brandingById; } }; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy