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:
2017-08-04 10:06:44 +02:00
parent ee1d8bd2c8
commit 1f4bd9f411
5 changed files with 435 additions and 164 deletions

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;
}