From d9f5ed496c6d5245a347adbea31cad191791c44c Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Mon, 24 Jul 2023 15:00:57 +0200 Subject: [PATCH] Improve name handling --- .../org/onebusaway/gtfs/model/StopArea.java | 57 ++++++++++--------- .../gtfs/model/StopAreaElement.java | 18 +++--- .../gtfs/serialization/GtfsReader.java | 6 +- .../gtfs/serialization/FlexReaderTest.java | 4 +- 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/StopArea.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/StopArea.java index c7b1275d..2d2d74a6 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/StopArea.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/StopArea.java @@ -19,40 +19,45 @@ import java.util.Set; public class StopArea extends IdentityBean implements StopLocation { - private static final long serialVersionUID = 1L; - private AgencyAndId id; + private static final long serialVersionUID = 1L; - private Set stops = new HashSet<>(); + private Area area; - private String name; + private Set stops = new HashSet<>(); - @Override - public AgencyAndId getId() { - return id; - } + @Override + public AgencyAndId getId() { + return area.getId(); + } - public void setId(AgencyAndId id) { - this.id = id; - } + @Override + public void setId(AgencyAndId id) { + } - public Set getLocations() { - return stops; - } + public void setArea(Area area) { + this.area = area; + } - private void setLocations(Set stops) { - this.stops = stops; - } + public Set getLocations() { + return stops; + } - public void addLocation(StopLocation location) { - this.stops.add(location); - } + private void setLocations(Set stops) { + this.stops = stops; + } - public String getName() { - return name; - } + public void addLocation(StopLocation location) { + this.stops.add(location); + } + + public String getName() { + return area.getName(); + } + + @Override + public void setName(String name) { + + } - public void setName(String name) { - this.name = name; - } } diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/StopAreaElement.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/StopAreaElement.java index 934d0f2f..b0ee1cf6 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/StopAreaElement.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/StopAreaElement.java @@ -17,33 +17,33 @@ import org.onebusaway.csv_entities.schema.annotations.CsvField; import org.onebusaway.csv_entities.schema.annotations.CsvFields; -import org.onebusaway.gtfs.serialization.mappings.DefaultAgencyIdFieldMappingFactory; +import org.onebusaway.gtfs.serialization.mappings.EntityFieldMappingFactory; import org.onebusaway.gtfs.serialization.mappings.StopLocationFieldMappingFactory; @CsvFields(filename = "stop_areas.txt", required = false) public final class StopAreaElement extends IdentityBean { - @CsvField(name = "area_id", mapping = DefaultAgencyIdFieldMappingFactory.class) - private AgencyAndId areaId; + @CsvField(name = "area_id", mapping = EntityFieldMappingFactory.class) + private Area area; @CsvField(name = "stop_id", mapping = StopLocationFieldMappingFactory.class) private StopLocation stopLocation; - public void setAreaId(AgencyAndId id) { - this.areaId = id; + public void setArea(Area area) { + this.area = area; } - public AgencyAndId getAreaId() { - return areaId; + public Area getArea() { + return area; } @Override public AgencyAndId getId() { - return new AgencyAndId(areaId.getAgencyId(), String.format("%s_%s", areaId.getId(), stopLocation.getId().getId())); + return new AgencyAndId(getArea().getId().getAgencyId(), String.format("%s_%s", area.getId().getId(), stopLocation.getId().getId())); } @Override public void setId(AgencyAndId id) { - this.areaId = id; + } public void setStopLocation(StopLocation stopLocation) { diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java index 37f97627..e834e8bc 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java @@ -355,6 +355,7 @@ public void handleEntity(Object entity) { } else if (entity instanceof Area) { Area area = (Area) entity; registerAgencyId(Area.class, area.getId()); + } else if (entity instanceof Location) { Location location = (Location) entity; registerAgencyId(Location.class, location.getId()); @@ -370,11 +371,10 @@ public void handleEntity(Object entity) { locationGroup.addLocation(locationGroupElement.getLocation()); } else if (entity instanceof StopAreaElement) { var stopAreaElement = (StopAreaElement) entity; - var stopArea = _entityStore.getEntityForId(StopArea.class, stopAreaElement.getAreaId()); + var stopArea = _entityStore.getEntityForId(StopArea.class, stopAreaElement.getArea().getId()); if (stopArea == null) { stopArea = new StopArea(); - stopArea.setId(stopAreaElement.getAreaId()); - stopArea.setName("area"); + stopArea.setArea(stopAreaElement.getArea()); _entityStore.saveEntity(stopArea); } stopArea.addLocation(stopAreaElement.getStopLocation()); diff --git a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java index a81c590a..cc29bc4f 100644 --- a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java +++ b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java @@ -43,7 +43,7 @@ public void pierceTransitStopAreas() throws CsvEntityIOException, IOException { assertEquals(15, areaElements.size()); var first = areaElements.get(0); - assertEquals("4210813", first.getAreaId().getId()); + assertEquals("1_4210813", first.getArea().getId().toString()); var stop = first.getStopLocation(); assertEquals("4210806", stop.getId().getId()); assertEquals("Bridgeport Way & San Francisco Ave SW (Northbound)", stop.getName()); @@ -78,6 +78,8 @@ public void pierceTransitStopAreas() throws CsvEntityIOException, IOException { var classes = stopTimes.stream().map(st -> st.getStop().getClass()).collect(Collectors.toList()); assertEquals(List.of(StopArea.class, StopArea.class), classes); + assertEquals("JBLM Stops", area.getName()); + } private static StopArea getArea(List stopAreas, String id) {