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

org.opentripplanner.transit.service.SiteRepositoryBuilder Maven / Gradle / Ivy

The newest version!
package org.opentripplanner.transit.service;

import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import org.opentripplanner.transit.model.framework.DefaultEntityById;
import org.opentripplanner.transit.model.framework.EntityById;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.framework.ImmutableEntityById;
import org.opentripplanner.transit.model.site.AreaStop;
import org.opentripplanner.transit.model.site.AreaStopBuilder;
import org.opentripplanner.transit.model.site.GroupOfStations;
import org.opentripplanner.transit.model.site.GroupStop;
import org.opentripplanner.transit.model.site.GroupStopBuilder;
import org.opentripplanner.transit.model.site.MultiModalStation;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.RegularStopBuilder;
import org.opentripplanner.transit.model.site.Station;

public class SiteRepositoryBuilder {

  private final AtomicInteger stopIndexCounter;

  private final EntityById regularStopById = new DefaultEntityById<>();
  private final EntityById areaStopById = new DefaultEntityById<>();
  private final EntityById groupStopById = new DefaultEntityById<>();
  private final EntityById stationById = new DefaultEntityById<>();
  private final EntityById multiModalStationById = new DefaultEntityById<>();
  private final EntityById groupOfStationById = new DefaultEntityById<>();

  SiteRepositoryBuilder(AtomicInteger stopIndexCounter) {
    this.stopIndexCounter = stopIndexCounter;
  }

  public ImmutableEntityById regularStopsById() {
    return regularStopById;
  }

  public RegularStopBuilder regularStop(FeedScopedId id) {
    return RegularStop.of(id, stopIndexCounter::getAndIncrement);
  }

  public RegularStop computeRegularStopIfAbsent(
    FeedScopedId id,
    Function factory
  ) {
    return regularStopById.computeIfAbsent(id, factory);
  }

  public SiteRepositoryBuilder withRegularStop(RegularStop stop) {
    regularStopById.add(stop);
    return this;
  }

  public SiteRepositoryBuilder withRegularStops(Collection stops) {
    regularStopById.addAll(stops);
    return this;
  }

  public ImmutableEntityById stationById() {
    return stationById;
  }

  public SiteRepositoryBuilder withStation(Station station) {
    stationById.add(station);
    return this;
  }

  public Station computeStationIfAbsent(FeedScopedId id, Function body) {
    return stationById.computeIfAbsent(id, body::apply);
  }

  public SiteRepositoryBuilder withStations(Collection stations) {
    stationById.addAll(stations);
    return this;
  }

  public ImmutableEntityById multiModalStationById() {
    return multiModalStationById;
  }

  public SiteRepositoryBuilder withMultiModalStation(MultiModalStation station) {
    multiModalStationById.add(station);
    return this;
  }

  public ImmutableEntityById groupOfStationById() {
    return groupOfStationById;
  }

  public SiteRepositoryBuilder withGroupOfStation(GroupOfStations station) {
    groupOfStationById.add(station);
    return this;
  }

  public AreaStopBuilder areaStop(FeedScopedId id) {
    return AreaStop.of(id, stopIndexCounter::getAndIncrement);
  }

  public ImmutableEntityById areaStopById() {
    return areaStopById;
  }

  public SiteRepositoryBuilder withAreaStop(AreaStop stop) {
    areaStopById.add(stop);
    return this;
  }

  public SiteRepositoryBuilder withAreaStops(Collection stops) {
    areaStopById.addAll(stops);
    return this;
  }

  public GroupStopBuilder groupStop(FeedScopedId id) {
    return GroupStop.of(id, stopIndexCounter::getAndIncrement);
  }

  public ImmutableEntityById groupStopById() {
    return groupStopById;
  }

  public SiteRepositoryBuilder withGroupStop(GroupStop group) {
    groupStopById.add(group);
    return this;
  }

  public SiteRepositoryBuilder withGroupStops(Collection groups) {
    groupStopById.addAll(groups);
    return this;
  }

  /**
   * Add the content of another site repository. There are no collision check, entities in the given
   * {@code other} model, will replace existing entities.
   */
  public SiteRepositoryBuilder addAll(SiteRepository other) {
    regularStopById.addAll(other.listRegularStops());
    stationById.addAll(other.listStations());
    multiModalStationById.addAll(other.listMultiModalStations());
    groupOfStationById.addAll(other.listGroupOfStations());
    areaStopById.addAll(other.listAreaStops());
    groupStopById.addAll(other.listGroupStops());
    return this;
  }

  public SiteRepository build() {
    return new SiteRepository(this);
  }

  AtomicInteger stopIndexCounter() {
    return stopIndexCounter;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy