-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9d84bd
commit aa0561e
Showing
10 changed files
with
177 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
core/src/main/java/org/hisrc/gtfs/graph/service/jgrapht/JGraphTGraphService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package org.hisrc.gtfs.graph.service.jgrapht; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.NavigableSet; | ||
|
||
import org.hisrc.gtfs.graph.model.edge.TransitionEdge; | ||
import org.hisrc.gtfs.graph.model.path.Path; | ||
import org.hisrc.gtfs.graph.model.util.TimeAwareComparator; | ||
import org.hisrc.gtfs.graph.model.vertex.StopTimeVertex; | ||
import org.hisrc.gtfs.graph.model.vertex.TemporalVertex; | ||
import org.hisrc.gtfs.graph.service.GraphService; | ||
import org.hisrc.util.ComparableComparator; | ||
import org.jgrapht.DirectedGraph; | ||
import org.onebusaway.gtfs.model.Stop; | ||
|
||
import com.google.common.collect.TreeMultimap; | ||
|
||
public class JGraphTGraphService implements GraphService { | ||
|
||
private final DirectedGraph<TemporalVertex, TransitionEdge> graph; | ||
private final Map<String, Stop> stopsById = new HashMap<String, Stop>(); | ||
private final TreeMultimap<String, TemporalVertex> stopTimeVerticesByStopId = TreeMultimap | ||
.create(ComparableComparator.<String> create(), | ||
TimeAwareComparator.create()); | ||
|
||
public JGraphTGraphService( | ||
DirectedGraph<TemporalVertex, TransitionEdge> graph) { | ||
this.graph = graph; | ||
for (TemporalVertex vertex : graph.vertexSet()) { | ||
if (vertex.isEntryVertex()) { | ||
final Stop stop = vertex.getStop(); | ||
final String stopId = stop.getId().toString(); | ||
stopTimeVerticesByStopId.put(stopId, vertex); | ||
stopsById.put(stopId, stop); | ||
} | ||
} | ||
} | ||
|
||
private Stop findStopById(String stopId) { | ||
final Stop stop = this.stopsById.get(stopId); | ||
return stop; | ||
} | ||
|
||
@Override | ||
public TemporalVertex findLatestTemporalVertexByStopIdBefore(String stopId, | ||
int time) { | ||
final Stop stop = findStopById(stopId); | ||
if (stop == null) { | ||
return null; | ||
} else { | ||
final StopTimeVertex stopTimeVertex = new StopTimeVertex(stop, time); | ||
final NavigableSet<TemporalVertex> stopTimeVertices = this.stopTimeVerticesByStopId | ||
.get(stopId); | ||
return stopTimeVertices.floor(stopTimeVertex); | ||
} | ||
} | ||
|
||
@Override | ||
public TemporalVertex findEarliestTemporalVertexByStopIdBefore( | ||
String stopId, int time) { | ||
final Stop stop = findStopById(stopId); | ||
if (stop == null) { | ||
return null; | ||
} else { | ||
final StopTimeVertex stopTimeVertex = new StopTimeVertex(stop, time); | ||
final NavigableSet<TemporalVertex> stopTimeVertices = this.stopTimeVerticesByStopId | ||
.get(stopId); | ||
return stopTimeVertices.ceiling(stopTimeVertex); | ||
} | ||
} | ||
|
||
@Override | ||
public Path findShortestPathStartingAfter(String fromStopId, | ||
String toStopId, int time) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Path findShortestPathEndingBefore(String fromStopId, | ||
String toStopId, int time) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Path findShortestPath(Collection<TemporalVertex> startVertices, | ||
Collection<TemporalVertex> endVertices) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Collection<TemporalVertex> findTemporalVerticesByStopIdBefore( | ||
String stopId, int time) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Collection<TemporalVertex> findTemporalVerticesByStopIdAfter( | ||
String stopId, int time) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
core/src/main/java/org/hisrc/util/ComparableComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.hisrc.util; | ||
|
||
import java.util.Comparator; | ||
|
||
public class ComparableComparator<C extends Comparable<C>> implements | ||
Comparator<C> { | ||
|
||
@Override | ||
public int compare(C o1, C o2) { | ||
if (o1 == o2) { | ||
return 0; | ||
} else if (o1 == null) { | ||
return -1; | ||
} else if (o2 == null) { | ||
return 1; | ||
} else { | ||
return o1.compareTo(o2); | ||
} | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
private static Comparator INSTANCE = new ComparableComparator(); | ||
|
||
public static <T extends Comparable<T>> Comparator<T> create() { | ||
@SuppressWarnings("unchecked") | ||
final Comparator<T> comparator = (Comparator<T>) INSTANCE; | ||
return comparator; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters