Code style

Various code style improvements:
lines trimmed to 120 characters,
complete JavaDoc for public methods,
braces on multiline if-else,
fixed numbers defined as constants
This commit is contained in:
Stefan Kalscheuer 2017-08-04 10:06:44 +02:00
parent ee1d8bd2c8
commit 1f4bd9f411
5 changed files with 435 additions and 164 deletions

View File

@ -70,18 +70,18 @@ public class UraClient {
/**
* Constructor with base URL and default API paths.
*
* @param baseURL the base URL (with protocol, without trailing slash)
* @param baseURL The base URL (with protocol, without trailing slash).
*/
public UraClient(String baseURL) {
this(baseURL, DEFAULT_INSTANT_URL, DEFAULT_STREAM_URL);
}
/**
* Constructor with base URL and custom API paths
* Constructor with base URL and custom API paths.
*
* @param baseURL the base URL (including protocol)
* @param instantURL the path for instant requests
* @param streamURL the path for stream requests
* @param baseURL The base URL (including protocol).
* @param instantURL The path for instant requests.
* @param streamURL The path for stream requests.
*/
public UraClient(String baseURL, String instantURL, String streamURL) {
this.baseURL = baseURL;
@ -113,8 +113,8 @@ public class UraClient {
/**
* Builder pattern to request given line IDs.
*
* @param lines line IDs
* @return the request
* @param lines Line IDs.
* @return The request.
*/
public Query forLines(final String... lines) {
return new Query().forLines(lines);
@ -123,8 +123,8 @@ public class UraClient {
/**
* Builder pattern to request given line names.
*
* @param lineNames line names
* @return the request
* @param lineNames Line names.
* @return The request.
*/
public Query forLinesByName(final String... lineNames) {
return new Query().forLinesByName(lineNames);
@ -133,8 +133,8 @@ public class UraClient {
/**
* Builder pattern to request given direction.
*
* @param direction the direction ID
* @return the request
* @param direction The direction ID.
* @return The request.
*/
public Query forDirection(final Integer direction) {
return new Query().forDirection(direction);
@ -143,8 +143,8 @@ public class UraClient {
/**
* Builder pattern to request given destination names.
*
* @param destinationNames destination names
* @return the request
* @param destinationNames Destination names.
* @return The request.
* @since 1.1.0
*/
public Query forDestinationNames(final String... destinationNames) {
@ -154,8 +154,8 @@ public class UraClient {
/**
* Builder pattern to request given direction defined by stop point name.
*
* @param towards towards stop point names
* @return the request
* @param towards Towards stop point names.
* @return The request.
* @since 1.1.0
*/
public Query towards(final String... towards) {
@ -165,10 +165,10 @@ public class UraClient {
/**
* Builder pattern to request given destination names.
*
* @param latitude Latitude (WGS84)
* @param longitude Longitude (WGS84)
* @param radius Search radius (meters)
* @return the request
* @param latitude Latitude (WGS84).
* @param longitude Longitude (WGS84).
* @param radius Search radius (meters).
* @return The request.
* @since 1.1.0
*/
public Query forPosition(final Double latitude, final Double longitude, final Integer radius) {
@ -179,7 +179,7 @@ public class UraClient {
* Get list of trips.
* If forStops() and/or forLines() has been called, those will be used as filter.
*
* @return list of trips
* @return List of trips.
*/
public List<Trip> getTrips() {
return getTrips(new Query(), null);
@ -189,8 +189,8 @@ public class UraClient {
* Get list of trips with limit.
* If forStops() and/or forLines() has been called, those will be used as filter.
*
* @param limit maximum number of results
* @return list of trips
* @param limit Maximum number of results.
* @return List of trips.
*/
public List<Trip> getTrips(final Integer limit) {
return getTrips(new Query(), limit);
@ -200,8 +200,8 @@ public class UraClient {
* Get list of trips.
* If forStops() and/or forLines() has been called, those will be used as filter.
*
* @param query the query
* @return list of trips
* @param query The query.
* @return List of trips.
*/
public List<Trip> getTrips(Query query) {
return getTrips(query, null);
@ -210,25 +210,27 @@ public class UraClient {
/**
* Get list of trips for given stopIDs and lineIDs with result limit.
*
* @param query the query
* @param limit maximum number of results
* @return list of trips
* @param query The query.
* @param limit Maximum number of results.
* @return List of trips.
*/
public List<Trip> getTrips(final Query query, final Integer limit) {
List<Trip> trips = new ArrayList<>();
try (InputStream is = requestInstant(REQUEST_TRIP, query);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String version = null;
String line;
while ((line = br.readLine()) != null && (limit == null || trips.size() < limit)) {
String line = br.readLine();
while (line != null && (limit == null || trips.size() < limit)) {
List l = mapper.readValue(line, List.class);
/* Check if result exists and has correct response type */
if (l != null && l.size() > 0) {
if (l.get(0).equals(RES_TYPE_URA_VERSION))
if (l.get(0).equals(RES_TYPE_URA_VERSION)) {
version = l.get(1).toString();
else if (l.get(0).equals(RES_TYPE_PREDICTION))
} else if (l.get(0).equals(RES_TYPE_PREDICTION)) {
trips.add(new Trip(l, version));
}
}
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
@ -239,7 +241,7 @@ public class UraClient {
/**
* Get list of stops without filters.
*
* @return the list
* @return Lhe list.
*/
public List<Stop> getStops() {
return getStops(new Query());
@ -249,8 +251,8 @@ public class UraClient {
* List available stopIDs.
* If forStops() and/or forLines() has been called, those will be used as filter.
*
* @param query the query
* @return the list
* @param query The query.
* @return The list.
*/
public List<Stop> getStops(Query query) {
List<Stop> stops = new ArrayList<>();
@ -261,8 +263,9 @@ public class UraClient {
while ((line = br.readLine()) != null) {
List l = mapper.readValue(line, List.class);
/* Check if result exists and has correct response type */
if (l != null && l.size() > 0 && l.get(0).equals(RES_TYPE_STOP))
if (l != null && l.size() > 0 && l.get(0).equals(RES_TYPE_STOP)) {
stops.add(new Stop(l));
}
}
} catch (IOException e) {
e.printStackTrace();
@ -273,30 +276,41 @@ public class UraClient {
/**
* Issue request to instant endpoint and return input stream.
*
* @param returnList fields to fetch
* @param query the query
* @param returnList Fields to fetch.
* @param query The query.
* @return Input stream of the URL
* @throws IOException on errors
*/
private InputStream requestInstant(String[] returnList, Query query) throws IOException {
String urlStr = baseURL + instantURL + "?ReturnList=" + String.join(",", returnList);
if (query.stopIDs != null && query.stopIDs.length > 0)
if (query.stopIDs != null && query.stopIDs.length > 0) {
urlStr += "&" + PAR_STOP_ID + "=" + String.join(",", query.stopIDs);
if (query.stopNames != null && query.stopNames.length > 0)
}
if (query.stopNames != null && query.stopNames.length > 0) {
urlStr += "&" + PAR_STOP_NAME + "=" + String.join(",", query.stopNames);
if (query.lineIDs != null && query.lineIDs.length > 0)
}
if (query.lineIDs != null && query.lineIDs.length > 0) {
urlStr += "&" + PAR_LINE_ID + "=" + String.join(",", query.lineIDs);
if (query.lineNames != null && query.lineNames.length > 0)
}
if (query.lineNames != null && query.lineNames.length > 0) {
urlStr += "&" + PAR_LINE_NAME + "=" + String.join(",", query.lineNames);
if (query.direction != null)
}
if (query.direction != null) {
urlStr += "&" + PAR_DIR_ID + "=" + query.direction;
if (query.destinationNames != null)
}
if (query.destinationNames != null) {
urlStr += "&" + PAR_DEST_NAME + "=" + String.join(",", query.destinationNames);
if (query.towards != null)
}
if (query.towards != null) {
urlStr += "&" + PAR_TOWARDS + "=" + String.join(",", query.towards);
if (query.circle != null)
}
if (query.circle != null) {
urlStr += "&" + PAR_CIRCLE + "=" + String.join(",", query.circle);
}
URL url = new URL(urlStr);
return url.openStream();
}
@ -316,8 +330,8 @@ public class UraClient {
/**
* Builder pattern to request given line IDs.
*
* @param lineIDs line IDs
* @return the query
* @param lineIDs Line IDs.
* @return The query.
*/
public Query forLines(final String... lineIDs) {
this.lineIDs = lineIDs;
@ -327,8 +341,8 @@ public class UraClient {
/**
* Builder pattern to request given line names.
*
* @param lineNames line names
* @return the query
* @param lineNames Line names.
* @return The query.
*/
public Query forLinesByName(final String... lineNames) {
this.lineNames = lineNames;
@ -338,8 +352,8 @@ public class UraClient {
/**
* Builder pattern to request given stop IDs.
*
* @param stopIDs stop IDs
* @return the query
* @param stopIDs Stop IDs.
* @return The query.
*/
public Query forStops(final String... stopIDs) {
this.stopIDs = stopIDs;
@ -349,8 +363,8 @@ public class UraClient {
/**
* Builder pattern to request given stop names.
*
* @param stopNames line names
* @return the query
* @param stopNames Line names.
* @return The query.
*/
public Query forStopsByName(final String... stopNames) {
this.stopNames = stopNames;
@ -360,8 +374,8 @@ public class UraClient {
/**
* Builder pattern to request given direction.
*
* @param direction the direction
* @return the query
* @param direction The direction.
* @return The query.
*/
public Query forDirection(final Integer direction) {
this.direction = direction;
@ -369,10 +383,10 @@ public class UraClient {
}
/**
* Builder pattern to request given destination names
* Builder pattern to request given destination names.
*
* @param destinationNames names of destinations
* @return the query
* @param destinationNames Names of destinations.
* @return The query.
* @since 1.1.0
*/
public Query forDestinationNames(final String... destinationNames) {
@ -383,8 +397,8 @@ public class UraClient {
/**
* Builder pattern to request given direction defined by stop point name.
*
* @param towards towards stop point names
* @return the request
* @param towards Towards stop point names.
* @return The request.
* @since 1.1.0
*/
public Query towards(final String... towards) {
@ -395,10 +409,10 @@ public class UraClient {
/**
* Builder pattern to request given position and radius.
*
* @param latitude Latitude (WGS84)
* @param longitude Longitude (WGS84)
* @param radius Search radius (meters)
* @return the query
* @param latitude Latitude (WGS84).
* @param longitude Longitude (WGS84).
* @param radius Search radius (meters).
* @return The query.
* @since 1.1.0
*/
public Query forPosition(final Double latitude, final Double longitude, final Integer radius) {
@ -409,7 +423,7 @@ public class UraClient {
/**
* Get stops for set filters.
*
* @return List of matching trips
* @return List of matching trips.
*/
public List<Stop> getStops() {
return UraClient.this.getStops(this);
@ -418,7 +432,7 @@ public class UraClient {
/**
* Get trips for set filters.
*
* @return List of matching trips
* @return List of matching trips.
*/
public List<Trip> getTrips() {
return UraClient.this.getTrips(this);

View File

@ -25,6 +25,14 @@ import java.util.List;
* @author Stefan Kalscheuer
*/
public class Stop {
private static final int STOP_NAME = 1;
private static final int STOP_ID = 2;
private static final int INDICATOR = 3;
private static final int STATE = 4;
private static final int LATITUDE = 5;
private static final int LONGITUDE = 6;
private static final int NUM_OF_FIELDS = 7;
private final String id;
private final String name;
private final String indicator;
@ -32,6 +40,16 @@ public class Stop {
private final Double latitude;
private final Double longitude;
/**
* Construct Stop object.
*
* @param id Stop ID.
* @param name Stop name.
* @param indicator Stop indicator.
* @param state Stop state.
* @param latitude Stop geolocation latitude.
* @param longitude Stop geolocation longitude.
*/
public Stop(String id, String name, String indicator, Integer state, Double latitude, Double longitude) {
this.id = id;
this.name = name;
@ -41,58 +59,100 @@ public class Stop {
this.longitude = longitude;
}
/**
* Construct Stop object from raw list of attributes parsed from JSON.
*
* @param raw List of attributes from JSON line
* @throws IOException Thrown on invalid line format.
*/
public Stop(List raw) throws IOException {
if (raw == null || raw.size() < 7)
if (raw == null || raw.size() < NUM_OF_FIELDS) {
throw new IOException("Invalid number of fields");
}
if (raw.get(1) instanceof String)
name = (String)raw.get(1);
else
throw new IOException("Field 1 not of expected type String, found " + raw.get(1).getClass().getSimpleName());
if (raw.get(2) instanceof String)
id = (String)raw.get(2);
else
throw new IOException("Field 2 not of expected type String, found " + raw.get(2).getClass().getSimpleName());
if (raw.get(3) instanceof String)
indicator = (String)raw.get(3);
else if (raw.get(3) == null)
if (raw.get(1) instanceof String) {
name = (String) raw.get(STOP_NAME);
} else {
throw new IOException("Field " + STOP_NAME + " not of expected type String, found "
+ raw.get(STOP_NAME).getClass().getSimpleName());
}
if (raw.get(STOP_ID) instanceof String) {
id = (String) raw.get(STOP_ID);
} else {
throw new IOException("Field " + STOP_ID + " not of expected type String, found "
+ raw.get(STOP_ID).getClass().getSimpleName());
}
if (raw.get(INDICATOR) instanceof String) {
indicator = (String) raw.get(INDICATOR);
} else if (raw.get(INDICATOR) == null) {
indicator = null;
else
throw new IOException("Field 3 not of expected type String, found " + raw.get(3).getClass().getSimpleName());
if (raw.get(4) instanceof Integer)
state = (Integer)raw.get(4);
else
throw new IOException("Field 4 not of expected type Integer, found " + raw.get(4).getClass().getSimpleName());
if (raw.get(5) instanceof Double)
latitude = (Double)raw.get(5);
else
throw new IOException("Field 5 not of expected type Double, found " + raw.get(5).getClass().getSimpleName());
if (raw.get(6) instanceof Double)
longitude = (Double)raw.get(6);
else
throw new IOException("Field 6 not of expected type Double, found " + raw.get(6).getClass().getSimpleName());
} else {
throw new IOException("Field " + INDICATOR + " not of expected type String, found "
+ raw.get(INDICATOR).getClass().getSimpleName());
}
if (raw.get(STATE) instanceof Integer) {
state = (Integer) raw.get(STATE);
} else {
throw new IOException("Field " + STATE + " not of expected type Integer, found "
+ raw.get(STATE).getClass().getSimpleName());
}
if (raw.get(LATITUDE) instanceof Double) {
latitude = (Double) raw.get(LATITUDE);
} else {
throw new IOException("Field " + LATITUDE + " not of expected type Double, found "
+ raw.get(LATITUDE).getClass().getSimpleName());
}
if (raw.get(LONGITUDE) instanceof Double) {
longitude = (Double) raw.get(LONGITUDE);
} else {
throw new IOException("Field " + LONGITUDE + " not of expected type Double, found "
+ raw.get(LONGITUDE).getClass().getSimpleName());
}
}
/**
* @return The stop ID.
*/
public String getId() {
return id;
}
/**
* @return The stop name.
*/
public String getName() {
return name;
}
/**
* @return The stop indicator.
*/
public String getIndicator() {
return indicator;
}
/**
* @return The stop indicator.
*/
public Integer getState() {
return state;
}
/**
* @return The stop geoloaction latitude.
*/
public Double getLatitude() {
return latitude;
}
/**
* @return The stop geolocation longitude.
*/
public Double getLongitude() {
return longitude;
}

View File

@ -25,6 +25,17 @@ import java.util.List;
* @author Stefan Kalscheuer
*/
public class Trip {
private static final int VISIT_ID = 7;
private static final int LINE_ID = 8;
private static final int LINE_NAME = 9;
private static final int DIRECTION_ID = 10;
private static final int DESTINATION_NAME = 11;
private static final int DESTINATION_TEXT = 12;
private static final int VEHICLE_ID = 13;
private static final int TRIP_ID = 14;
private static final int ESTIMATED_TIME = 15;
private static final int NUM_OF_FIELDS = 16;
private final Stop stop;
private final String id;
private final Integer visitID;
@ -36,13 +47,81 @@ public class Trip {
private final Long estimatedTime;
private final String vehicleID;
public Trip(String stopID, String stopName, String stopIndicator, Integer stopState, Double stopLatitude, Double stopLongitude,
Integer visitID, String lineID, String lineName, Integer directionID, String destinationName, String destinationText, String vehicleID, String tripID, Long estimatedTime) {
this(new Stop(stopID, stopName, stopIndicator, stopState, stopLatitude, stopLongitude),
visitID, lineID, lineName, directionID, destinationName, destinationText, vehicleID, tripID, estimatedTime);
/**
* Construct Trip object from complete set of data.
*
* @param stopID Stop ID.
* @param stopName Stop name.
* @param stopIndicator Stop Indicator.
* @param stopState Stop state.
* @param stopLatitude Stop geolocation latitude.
* @param stopLongitude Stop geolocation latitude.
* @param visitID Visit ID.
* @param lineID Line ID.
* @param lineName Line name.
* @param directionID Direction ID.
* @param destinationName Destination name.
* @param destinationText Destination text.
* @param vehicleID Vehicle ID.
* @param tripID Trip ID.
* @param estimatedTime Estimated time.
*/
public Trip(String stopID,
String stopName,
String stopIndicator,
Integer stopState,
Double stopLatitude,
Double stopLongitude,
Integer visitID,
String lineID,
String lineName,
Integer directionID,
String destinationName,
String destinationText,
String vehicleID,
String tripID,
Long estimatedTime) {
this(new Stop(stopID,
stopName,
stopIndicator,
stopState,
stopLatitude,
stopLongitude),
visitID,
lineID,
lineName,
directionID,
destinationName,
destinationText,
vehicleID,
tripID,
estimatedTime);
}
public Trip(Stop stop, Integer visitID, String lineID, String lineName, Integer directionID, String destinationName, String destinationText, String vehicleID, String tripID, Long estimatedTime) {
/**
* Construct Trip object from Stop model and set of additional data.
*
* @param stop Stop model
* @param visitID Visit ID
* @param lineID Line ID
* @param lineName Line name
* @param directionID Direction ID
* @param destinationName Destination name
* @param destinationText Destination text
* @param vehicleID Vehicle ID
* @param tripID Trip ID
* @param estimatedTime Estimated time
*/
public Trip(Stop stop,
Integer visitID,
String lineID,
String lineName,
Integer directionID,
String destinationName,
String destinationText,
String vehicleID,
String tripID,
Long estimatedTime) {
this.stop = stop;
this.visitID = visitID;
this.lineID = lineID;
@ -55,94 +134,168 @@ public class Trip {
this.estimatedTime = estimatedTime;
}
/**
* Construct Trip object from raw list of attributes parsed from JSON.
*
* @param raw List of attributes from JSON line
* @throws IOException Thrown on invalid line format.
*/
public Trip(List raw) throws IOException {
this(raw, null);
}
/**
* Construct Stop object from raw list of attributes parsed from JSON with explicitly specified version.
*
* @param raw List of attributes from JSON line
* @param version API version
* @throws IOException Thrown on invalid line format.
*/
public Trip(List raw, String version) throws IOException {
if (raw == null || raw.size() < 16)
if (raw == null || raw.size() < NUM_OF_FIELDS) {
throw new IOException("Invalid number of fields");
}
stop = new Stop(raw);
if (raw.get(7) instanceof Integer)
visitID = (Integer) raw.get(7);
else
throw new IOException("Field 7 not of expected type Integer, found " + raw.get(7).getClass().getSimpleName());
if (raw.get(8) instanceof String)
lineID = (String)raw.get(8);
else
throw new IOException("Field 8 not of expected type String, found " + raw.get(8).getClass().getSimpleName());
if (raw.get(9) instanceof String)
lineName = (String)raw.get(9);
else
throw new IOException("Field 9 not of expected type String, found " + raw.get(9).getClass().getSimpleName());
if (raw.get(10) instanceof Integer) {
directionID = (Integer) raw.get(10);
if (directionID < 0 || directionID > 2)
throw new IOException("Direction out of range. Expected 1 or 2, found " + directionID);
if (raw.get(VISIT_ID) instanceof Integer) {
visitID = (Integer) raw.get(VISIT_ID);
} else {
throw new IOException("Field " + VISIT_ID + " not of expected type Integer, found "
+ raw.get(VISIT_ID).getClass().getSimpleName());
}
else
throw new IOException("Field 10 not of expected type Integer, found " + raw.get(10).getClass().getSimpleName());
if (raw.get(11) instanceof String)
destinationName = (String)raw.get(11);
else
throw new IOException("Field 11 not of expected type String, found " + raw.get(11).getClass().getSimpleName());
if (raw.get(12) instanceof String)
destinationText = (String)raw.get(12);
else
throw new IOException("Field 12 not of expected type String, found " + raw.get(12).getClass().getSimpleName());
if (raw.get(LINE_ID) instanceof String) {
lineID = (String) raw.get(LINE_ID);
} else {
throw new IOException("Field " + LINE_ID + " not of expected type String, found "
+ raw.get(LINE_ID).getClass().getSimpleName());
}
if (raw.get(LINE_NAME) instanceof String) {
lineName = (String) raw.get(LINE_NAME);
} else {
throw new IOException("Field " + LINE_NAME + " not of expected type String, found "
+ raw.get(LINE_NAME).getClass().getSimpleName());
}
if (raw.get(DIRECTION_ID) instanceof Integer) {
directionID = (Integer) raw.get(DIRECTION_ID);
if (directionID < 0 || directionID > 2) {
throw new IOException("Direction out of range. Expected 1 or 2, found " + directionID);
}
} else {
throw new IOException("Field " + DIRECTION_ID + " not of expected type Integer, found "
+ raw.get(DIRECTION_ID).getClass().getSimpleName());
}
if (raw.get(DESTINATION_NAME) instanceof String) {
destinationName = (String) raw.get(DESTINATION_NAME);
} else {
throw new IOException("Field " + DESTINATION_NAME + " not of expected type String, found "
+ raw.get(DESTINATION_NAME).getClass().getSimpleName());
}
if (raw.get(DESTINATION_TEXT) instanceof String) {
destinationText = (String) raw.get(DESTINATION_TEXT);
} else {
throw new IOException("Field " + DESTINATION_TEXT + " not of expected type String, found "
+ raw.get(DESTINATION_TEXT).getClass().getSimpleName());
}
/* TFL and ASEAG deliver different types with the same API version, so this field is a little more tolerant */
if (raw.get(13) instanceof String || raw.get(13) instanceof Integer || raw.get(13) instanceof Long)
vehicleID = raw.get(13).toString();
else
throw new IOException("Field 13 not of expected type String/Integer/Long, found " + raw.get(13).getClass().getSimpleName());
if (raw.get(14) instanceof String || raw.get(14) instanceof Integer || raw.get(14) instanceof Long)
id = raw.get(14).toString();
else
throw new IOException("Field 14 not of expected type String/Integer/Long, found " + raw.get(14).getClass().getSimpleName());
if (raw.get(15) instanceof Long)
estimatedTime = (Long)raw.get(15);
else
throw new IOException("Field 15 not of expected type Long, found " + raw.get(15).getClass().getSimpleName());
if (raw.get(VEHICLE_ID) instanceof String
|| raw.get(VEHICLE_ID) instanceof Integer
|| raw.get(VEHICLE_ID) instanceof Long) {
vehicleID = raw.get(VEHICLE_ID).toString();
} else {
throw new IOException("Field " + VEHICLE_ID + " not of expected type String/Integer/Long, found "
+ raw.get(VEHICLE_ID).getClass().getSimpleName());
}
if (raw.get(TRIP_ID) instanceof String
|| raw.get(TRIP_ID) instanceof Integer
|| raw.get(TRIP_ID) instanceof Long) {
id = raw.get(TRIP_ID).toString();
} else {
throw new IOException("Field " + TRIP_ID + " not of expected type String/Integer/Long, found "
+ raw.get(TRIP_ID).getClass().getSimpleName());
}
if (raw.get(ESTIMATED_TIME) instanceof Long) {
estimatedTime = (Long) raw.get(ESTIMATED_TIME);
} else {
throw new IOException("Field " + ESTIMATED_TIME + " not of expected type Long, found "
+ raw.get(ESTIMATED_TIME).getClass().getSimpleName());
}
}
/**
* @return The (starting) stop.
*/
public Stop getStop() {
return stop;
}
/**
* @return The trip ID.
*/
public String getId() {
return id;
}
/**
* @return The visit ID.
*/
public Integer getVisitID() {
return visitID;
}
/**
* @return The line ID.
*/
public String getLineID() {
return lineID;
}
/**
* @return The line name.
*/
public String getLineName() {
return lineName;
}
/**
* @return The direction ID.
*/
public Integer getDirectionID() {
return directionID;
}
/**
* @return The destination name.
*/
public String getDestinationName() {
return destinationName;
}
/**
* @return The destination text.
*/
public String getDestinationText() {
return destinationText;
}
/**
* @return The estimated departure time.
*/
public Long getEstimatedTime() {
return estimatedTime;
}
/**
* @return The vehicle ID.
*/
public String getVehicleID() {
return vehicleID;
}

View File

@ -50,7 +50,8 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V2_stops.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V2_stops.txt"));
/* List stops and verify some values */
List<Stop> stops = new UraClient("mocked").getStops();
@ -78,7 +79,8 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V2_stops_line.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V2_stops_line.txt"));
/* List stops and verify some values */
List<Stop> stops = new UraClient("mocked").forLines("33").getStops();
@ -96,10 +98,13 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle.txt"));
/* List stops and verify some values */
List<Stop> stops = new UraClient("mocked").forPosition(51.51009, -0.1345734, 200).getStops();
List<Stop> stops = new UraClient("mocked")
.forPosition(51.51009, -0.1345734, 200)
.getStops();
assertThat(stops, hasSize(13));
assertThat(stops.get(0).getId(), is("156"));
assertThat(stops.get(1).getName(), is("Piccadilly Circus"));
@ -108,8 +113,12 @@ public class UraClientTest {
assertThat(stops.get(4).getLongitude(), is(-0.134172));
assertThat(stops.get(5).getIndicator(), is(nullValue()));
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle_name.txt"));
stops = new UraClient("mocked").forStopsByName("Piccadilly Circus").forPosition(51.51009, -0.1345734, 200).getStops();
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle_name.txt"));
stops = new UraClient("mocked")
.forStopsByName("Piccadilly Circus")
.forPosition(51.51009, -0.1345734, 200)
.getStops();
assertThat(stops, hasSize(7));
assertThat(stops.stream().filter(t -> !t.getName().equals("Piccadilly Circus")).findAny(), is(Optional.empty()));
}
@ -119,18 +128,26 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_destination.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_destination.txt"));
/* List stops and verify some values */
List<Trip> trips = new UraClient("mocked").forDestinationNames("Piccadilly Circus").getTrips();
assertThat(trips, hasSize(9));
assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Piccadilly Cir")).findAny(), is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Piccadilly Cir")).findAny(),
is(Optional.empty()));
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_destination.txt"));
trips = new UraClient("mocked").forStops("156").forDestinationNames("Marble Arch").getTrips();
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_destination.txt"));
trips = new UraClient("mocked")
.forStops("156")
.forDestinationNames("Marble Arch")
.getTrips();
assertThat(trips, hasSize(5));
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(), is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Marble Arch")).findAny(), is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(),
is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Marble Arch")).findAny(),
is(Optional.empty()));
}
@Test
@ -138,13 +155,15 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_towards.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_towards.txt"));
/* List stops and verify some values */
List<Trip> trips = new UraClient("mocked").towards("Marble Arch").getTrips();
assertThat(trips, hasSize(10));
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_towards.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_towards.txt"));
trips = new UraClient("mocked").forStops("156").towards("Marble Arch").getTrips();
assertThat(trips, hasSize(17));
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(), is(Optional.empty()));
@ -155,7 +174,8 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt"));
/* Get trips without filters and verify some values */
List<Trip> trips = new UraClient("mocked").getTrips();
@ -172,7 +192,8 @@ public class UraClientTest {
assertThat(trips.get(9).getStop().getId(), is("100002"));
/* Repeat test for API V2 */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V2_trips_all.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V2_trips_all.txt"));
/* Get trips without filters and verify some values */
trips = new UraClient("mocked").getTrips();
assertThat(trips, hasSize(10));
@ -188,7 +209,8 @@ public class UraClientTest {
assertThat(trips.get(9).getStop().getId(), is("100002"));
/* Get limited number of trips */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt"));
trips = new UraClient("mocked").getTrips(5);
assertThat(trips, hasSize(5));
@ -209,7 +231,8 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop.txt"));
/* Get trips for stop ID 100000 (Aachen Bushof) and verify some values */
List<Trip> trips = new UraClient("mocked")
@ -223,12 +246,14 @@ public class UraClientTest {
assertThat(trips.get(3).getStop().getIndicator(), is("H.15"));
/* Get trips for stop name "Uniklinik" and verify some values */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_name.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_name.txt"));
trips = new UraClient("mocked")
.forStopsByName("Uniklinik")
.getTrips();
assertThat(trips, hasSize(10));
assertThat(trips.stream().filter(t -> !t.getStop().getName().equals("Uniklinik")).findAny(), is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getStop().getName().equals("Uniklinik")).findAny(),
is(Optional.empty()));
assertThat(trips.get(0).getId(), is("92000043013001"));
assertThat(trips.get(1).getLineID(), is("5"));
assertThat(trips.get(2).getVehicleID(), is("317"));;
@ -240,7 +265,8 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_line.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line.txt"));
/* Get trips for line ID 3 and verify some values */
List<Trip> trips = new UraClient("mocked")
@ -254,7 +280,8 @@ public class UraClientTest {
assertThat(trips.get(3).getStop().getIndicator(), is("H.4 (Pontwall)"));
/* Get trips for line name "3.A" and verify some values */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_name.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_name.txt"));
trips = new UraClient("mocked")
.forLinesByName("3.A")
.getTrips();
@ -266,7 +293,8 @@ public class UraClientTest {
assertThat(trips.get(3).getStop().getName(), is("Aachen Gartenstraße"));
/* Get trips for line 3 with direction 1 and verify some values */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt"));
trips = new UraClient("mocked")
.forLines("412")
.forDirection(2)
@ -276,7 +304,8 @@ public class UraClientTest {
assertThat(trips.stream().filter(t -> !t.getDirectionID().equals(2)).findAny(), is(Optional.empty()));
/* Test lineID and direction in different order */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt"));
trips = new UraClient("mocked")
.forDirection(2)
.forLines("412")
@ -291,7 +320,8 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_line.txt"));
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_line.txt"));
/* Get trips for line ID 25 and 25 at stop 100000 and verify some values */
List<Trip> trips = new UraClient("mocked")
@ -299,7 +329,8 @@ public class UraClientTest {
.forStops("100000")
.getTrips();
assertThat(trips, hasSize(10));
assertThat(trips.stream().filter(t -> !t.getLineID().equals("25") && !t.getLineID().equals("35")).findAny(), is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getLineID().equals("25") && !t.getLineID().equals("35")).findAny(),
is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("100000")).findAny(), is(Optional.empty()));
assertThat(trips.get(0).getId(), is("27000078014001"));
assertThat(trips.get(1).getLineID(), is("25"));

View File

@ -36,8 +36,21 @@ import static org.junit.Assert.fail;
public class TripTest {
@Test
public void basicConstructorTest() {
Trip trip = new Trip("sid", "name", "indicator", 1, 2.345, 6.789,
123, "lineid", "linename", 0, "destination name", "destination text", "vehicle", "id", 123456789123456789L);
Trip trip = new Trip("sid",
"name",
"indicator",
1,
2.345,
6.789,
123,
"lineid",
"linename",
0,
"destination name",
"destination text",
"vehicle",
"id",
123456789123456789L);
assertThat(trip.getStop().getId(), is("sid"));
assertThat(trip.getStop().getName(), is("name"));
assertThat(trip.getStop().getIndicator(), is("indicator"));