26 |
REGIONAL |
REGIONAL |
27 |
} |
} |
28 |
|
|
29 |
|
enum FetchTrainType { |
30 |
|
STOG, |
31 |
|
REGIONAL, |
32 |
|
BOTH |
33 |
|
} |
34 |
|
|
35 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
36 |
|
|
37 |
Map<String, DepartureBean> cache; |
Map<String, DepartureBean> cache; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
54 |
final String key = "" + stationID + ":" + arrival; |
|
55 |
|
final String key = "" + stationID + ":" + arrival + ":" + type.toString(); |
56 |
|
|
57 |
DepartureBean departureBean = cache.get(key); |
DepartureBean departureBean = cache.get(key); |
58 |
|
|
59 |
|
|
60 |
if (departureBean == null) { |
if (departureBean == null) { |
61 |
departureBean = lookupDepartures(stationID,arrival); |
departureBean = lookupDepartures(stationID, arrival, type); |
62 |
cache.put(key, departureBean); |
cache.put(key, departureBean); |
63 |
} else { |
} else { |
64 |
Statistics.getInstance().incrementDepartureCacheHits(); |
Statistics.getInstance().incrementDepartureCacheHits(); |
68 |
} |
} |
69 |
|
|
70 |
|
|
71 |
public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
72 |
|
|
73 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
74 |
|
|
75 |
StationEntry station = stationDao.getById(stationID); |
StationEntry station = stationDao.getById(stationID); |
76 |
|
|
77 |
departureBean.stationName = station.getName(); |
departureBean.stationName = station.getName(); |
78 |
|
|
79 |
if (station.getRegional() != null) { |
if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) { |
80 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
81 |
departureBean.entries.addAll( tempBean.entries ); |
departureBean.entries.addAll( tempBean.entries ); |
82 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
83 |
} |
} |
84 |
|
|
85 |
if (station.getStrain() != null) { |
if (station.getStrain() != null && (type == FetchTrainType.STOG||type == FetchTrainType.BOTH)) { |
86 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
87 |
departureBean.entries.addAll( tempBean.entries ); |
departureBean.entries.addAll( tempBean.entries ); |
88 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
92 |
logger.info("No departures found for station " + stationID); |
logger.info("No departures found for station " + stationID); |
93 |
} |
} |
94 |
|
|
95 |
Collections.sort( departureBean.entries ); |
if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time |
96 |
|
Collections.sort( departureBean.entries ); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
return departureBean; |
return departureBean; |