Skip to content

Commit

Permalink
Code cleanup:
Browse files Browse the repository at this point in the history
  - Spelling errors fixed.
  - All new code formatted with the project profile.
  - Minor code style improvements, like Java 8 style generics.
(Refactor OTP to have new OTP classes to replace the OBA GTFS classes #2494)
  • Loading branch information
Thomas Gran committed Sep 19, 2017
1 parent 3c4f5dd commit 2dc8f19
Show file tree
Hide file tree
Showing 64 changed files with 3,513 additions and 3,076 deletions.
Expand Up @@ -25,29 +25,34 @@ the License, or (at your option) any later version.
class ArrayListWithIdGeneration<T extends IdentityBean<Integer>> extends ArrayList<T> {
private int maxId = 0;

ArrayListWithIdGeneration() { }
ArrayListWithIdGeneration() {
}

ArrayListWithIdGeneration(Collection<? extends T> c) {
super(c);
decorateWithIds(this);
}

@Override public boolean add(T t) {
@Override
public boolean add(T t) {
decorateWithId(t);
return super.add(t);
}

@Override public void add(int index, T element) {
@Override
public void add(int index, T element) {
decorateWithId(element);
super.add(index, element);
}

@Override public boolean addAll(Collection<? extends T> c) {
@Override
public boolean addAll(Collection<? extends T> c) {
decorateWithIds(c);
return super.addAll(c);
}

@Override public boolean addAll(int index, Collection<? extends T> c) {
@Override
public boolean addAll(int index, Collection<? extends T> c) {
decorateWithIds(c);
return super.addAll(index, c);
}
Expand Down
167 changes: 106 additions & 61 deletions src/main/java/org/onebusaway2/gtfs/impl/GtfsDaoImpl.java
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2011 Brian Ferris <bdferris@onebusaway.org>
* Copyright (C) 2011 Google, Inc.
* Copyright (C) 2011 Laurent Gregoire <laurent.gregoire@gmail.com>
Expand All @@ -17,10 +17,32 @@
*/
package org.onebusaway2.gtfs.impl;

import org.onebusaway2.gtfs.model.*;
import org.onebusaway2.gtfs.model.Agency;
import org.onebusaway2.gtfs.model.AgencyAndId;
import org.onebusaway2.gtfs.model.FareAttribute;
import org.onebusaway2.gtfs.model.FareRule;
import org.onebusaway2.gtfs.model.FeedInfo;
import org.onebusaway2.gtfs.model.Frequency;
import org.onebusaway2.gtfs.model.IdentityBean;
import org.onebusaway2.gtfs.model.Pathway;
import org.onebusaway2.gtfs.model.Route;
import org.onebusaway2.gtfs.model.ServiceCalendar;
import org.onebusaway2.gtfs.model.ServiceCalendarDate;
import org.onebusaway2.gtfs.model.ShapePoint;
import org.onebusaway2.gtfs.model.Stop;
import org.onebusaway2.gtfs.model.StopTime;
import org.onebusaway2.gtfs.model.Transfer;
import org.onebusaway2.gtfs.model.Trip;
import org.onebusaway2.gtfs.services.GtfsDao;

import java.util.*;
import java.util.ArrayList;
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 static java.util.function.Function.identity;
import static java.util.stream.Collectors.groupingBy;
Expand All @@ -36,45 +58,53 @@
public class GtfsDaoImpl implements GtfsDao {

private Collection<Agency> agencies;

private Collection<ServiceCalendarDate> calendarDates;

private Collection<ServiceCalendar> calendars;

private Collection<FareAttribute> fareAttributes;

private Collection<FareRule> fareRules;

private Collection<FeedInfo> feedInfos;

private Collection<Frequency> frequencies;

private Collection<Pathway> pathways;

private Collection<Route> routes;

private Collection<ShapePoint> shapePoints;

private Map<AgencyAndId, Stop> stops;

private Collection<StopTime> stopTimes;

private Collection<Transfer> transfers;
private Collection<Trip> trips;

private Collection<Trip> trips;

// Indexes
private Map<AgencyAndId, List<String>> tripAgencyIdsByServiceId = null;

private Map<Stop, List<Stop>> stopsByStation = null;

private Map<Trip, List<StopTime>> stopTimesByTrip = null;

private Map<AgencyAndId, List<ShapePoint>> shapePointsByShapeId = null;

private Map<AgencyAndId, List<ServiceCalendarDate>> calendarDatesByServiceId = null;

private Map<AgencyAndId, List<ServiceCalendar>> calendarsByServiceId = null;

public GtfsDaoImpl(
Collection<Agency> agencies,
Collection<ServiceCalendarDate> calendarDates,
Collection<ServiceCalendar> calendars,
Collection<FareAttribute> fareAttributes,
Collection<FareRule> fareRules,
Collection<FeedInfo> feedInfos,
Collection<Frequency> frequencies,
Collection<Pathway> pathways,
Collection<Route> routes,
Collection<ShapePoint> shapePoints,
Collection<Stop> stops,
Collection<StopTime> stopTimes,
Collection<Transfer> transfers,
Collection<Trip> trips
) {
public GtfsDaoImpl(Collection<Agency> agencies, Collection<ServiceCalendarDate> calendarDates,
Collection<ServiceCalendar> calendars, Collection<FareAttribute> fareAttributes,
Collection<FareRule> fareRules, Collection<FeedInfo> feedInfos,
Collection<Frequency> frequencies, Collection<Pathway> pathways,
Collection<Route> routes, Collection<ShapePoint> shapePoints, Collection<Stop> stops,
Collection<StopTime> stopTimes, Collection<Transfer> transfers,
Collection<Trip> trips) {
this.agencies = agencies;
this.calendarDates = insertIds(calendarDates);
this.calendars = insertIds(calendars);
Expand All @@ -85,73 +115,89 @@ public GtfsDaoImpl(
this.pathways = pathways;
this.routes = routes;
this.shapePoints = shapePoints;
this.stops = stops.stream().collect(toMap(Stop::getId, identity()));
this.stops = stops.stream().collect(toMap(Stop::getId, identity()));
this.stopTimes = insertIds(stopTimes);
this.transfers = insertIds(transfers);
this.trips = trips;
}

@Override public Collection<Agency> getAllAgencies() {
@Override
public Collection<Agency> getAllAgencies() {
return agencies;
}

@Override public Collection<ServiceCalendarDate> getAllCalendarDates() {
@Override
public Collection<ServiceCalendarDate> getAllCalendarDates() {
return calendarDates;
}

@Override public Collection<ServiceCalendar> getAllCalendars() {
@Override
public Collection<ServiceCalendar> getAllCalendars() {
return calendars;
}

@Override public Collection<FareAttribute> getAllFareAttributes() {
@Override
public Collection<FareAttribute> getAllFareAttributes() {
return fareAttributes;
}

@Override public Collection<FareRule> getAllFareRules() {
@Override
public Collection<FareRule> getAllFareRules() {
return fareRules;
}

@Override public Collection<FeedInfo> getAllFeedInfos() {
@Override
public Collection<FeedInfo> getAllFeedInfos() {
return feedInfos;
}

@Override public Collection<Frequency> getAllFrequencies() {
@Override
public Collection<Frequency> getAllFrequencies() {
return frequencies;
}

@Override public Collection<Route> getAllRoutes() {
@Override
public Collection<Route> getAllRoutes() {
return routes;
}

@Override public Collection<ShapePoint> getAllShapePoints() {
@Override
public Collection<ShapePoint> getAllShapePoints() {
return shapePoints;
}

@Override public Collection<StopTime> getAllStopTimes() {
@Override
public Collection<StopTime> getAllStopTimes() {
return stopTimes;
}

@Override public Collection<Stop> getAllStops() {
@Override
public Collection<Stop> getAllStops() {
return stops.values();
}

@Override public Collection<Transfer> getAllTransfers() {
@Override
public Collection<Transfer> getAllTransfers() {
return transfers;
}

@Override public Collection<Trip> getAllTrips() {
@Override
public Collection<Trip> getAllTrips() {
return trips;
}

@Override public Collection<Pathway> getAllPathways() {
@Override
public Collection<Pathway> getAllPathways() {
return pathways;
}

@Override public Stop getStopForId(AgencyAndId id) {
@Override
public Stop getStopForId(AgencyAndId id) {
return stops.get(id);
}

@Override public List<String> getTripAgencyIdsReferencingServiceId(AgencyAndId serviceId) {
@Override
public List<String> getTripAgencyIdsReferencingServiceId(AgencyAndId serviceId) {

if (tripAgencyIdsByServiceId == null) {

Expand All @@ -161,10 +207,8 @@ public GtfsDaoImpl(
AgencyAndId tripId = trip.getId();
String tripAgencyId = tripId.getAgencyId();
AgencyAndId tripServiceId = trip.getServiceId();
Set<String> agencyIds = agencyIdsByServiceIds.computeIfAbsent(
tripServiceId,
k -> new HashSet<>()
);
Set<String> agencyIds = agencyIdsByServiceIds
.computeIfAbsent(tripServiceId, k -> new HashSet<>());
agencyIds.add(tripAgencyId);
}

Expand All @@ -181,30 +225,31 @@ public GtfsDaoImpl(
return list(tripAgencyIdsByServiceId.get(serviceId));
}

@Override public List<Stop> getStopsForStation(Stop station) {
@Override
public List<Stop> getStopsForStation(Stop station) {
if (stopsByStation == null) {
stopsByStation = new HashMap<>();
for (Stop stop : getAllStops()) {
if (stop.getLocationType() == 0 && stop.getParentStation() != null) {
Stop parentStation = getStopForId(
new AgencyAndId(stop.getId().getAgencyId(), stop.getParentStation()));
List<Stop> subStops = stopsByStation.computeIfAbsent(
parentStation,
k -> new ArrayList<>(2)
);
List<Stop> subStops = stopsByStation
.computeIfAbsent(parentStation, k -> new ArrayList<>(2));
subStops.add(stop);
}
}
}
return list(stopsByStation.get(station));
}

@Override public List<ShapePoint> getShapePointsForShapeId(AgencyAndId shapeId) {
@Override
public List<ShapePoint> getShapePointsForShapeId(AgencyAndId shapeId) {
ensureShapePointRelation();
return list(shapePointsByShapeId.get(shapeId));
}

@Override public List<StopTime> getStopTimesForTrip(Trip trip) {
@Override
public List<StopTime> getStopTimesForTrip(Trip trip) {

if (stopTimesByTrip == null) {
stopTimesByTrip = getAllStopTimes().stream().collect(groupingBy(StopTime::getTrip));
Expand All @@ -217,7 +262,8 @@ public GtfsDaoImpl(
return list(stopTimesByTrip.get(trip));
}

@Override public List<AgencyAndId> getAllServiceIds() {
@Override
public List<AgencyAndId> getAllServiceIds() {
ensureCalendarDatesByServiceIdRelation();
ensureCalendarsByServiceIdRelation();
Set<AgencyAndId> serviceIds = new HashSet<>();
Expand All @@ -226,12 +272,14 @@ public GtfsDaoImpl(
return new ArrayList<>(serviceIds);
}

@Override public List<ServiceCalendarDate> getCalendarDatesForServiceId(AgencyAndId serviceId) {
@Override
public List<ServiceCalendarDate> getCalendarDatesForServiceId(AgencyAndId serviceId) {
ensureCalendarDatesByServiceIdRelation();
return list(calendarDatesByServiceId.get(serviceId));
}

@Override public ServiceCalendar getCalendarForServiceId(AgencyAndId serviceId) {
@Override
public ServiceCalendar getCalendarForServiceId(AgencyAndId serviceId) {
ensureCalendarsByServiceIdRelation();
List<ServiceCalendar> calendars = list(calendarsByServiceId.get(serviceId));
switch (calendars.size()) {
Expand All @@ -248,26 +296,23 @@ public GtfsDaoImpl(

private void ensureCalendarDatesByServiceIdRelation() {
if (calendarDatesByServiceId == null) {
calendarDatesByServiceId = getAllCalendarDates().stream().collect(
groupingBy(ServiceCalendarDate::getServiceId)
);
calendarDatesByServiceId = getAllCalendarDates().stream()
.collect(groupingBy(ServiceCalendarDate::getServiceId));
}
}

private void ensureCalendarsByServiceIdRelation() {
if (calendarsByServiceId == null) {
calendarsByServiceId = getAllCalendars().stream().collect(
groupingBy(ServiceCalendar::getServiceId)
);
calendarsByServiceId = getAllCalendars().stream()
.collect(groupingBy(ServiceCalendar::getServiceId));
}
}

private void ensureShapePointRelation() {
if (shapePointsByShapeId == null) {
shapePointsByShapeId = getAllShapePoints().stream().collect(
groupingBy(ShapePoint::getShapeId)
);
for(List<ShapePoint> list : shapePointsByShapeId.values()) {
shapePointsByShapeId = getAllShapePoints().stream()
.collect(groupingBy(ShapePoint::getShapeId));
for (List<ShapePoint> list : shapePointsByShapeId.values()) {
Collections.sort(list);
}
}
Expand Down
@@ -1,11 +1,11 @@
/**
/*
* Copyright (C) 2011 Brian Ferris <bdferris@onebusaway.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -19,9 +19,9 @@

public class MultipleCalendarsForServiceIdException extends RuntimeException {

private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;

public MultipleCalendarsForServiceIdException(AgencyAndId serviceId) {
super("multiple calendars found for serviceId=" + serviceId);
}
public MultipleCalendarsForServiceIdException(AgencyAndId serviceId) {
super("multiple calendars found for serviceId=" + serviceId);
}
}

0 comments on commit 2dc8f19

Please sign in to comment.