Java tutorial
package com.eatthepath.gtfs.realtime; import org.json.simple.JSONAware; import org.json.simple.JSONObject; import com.eatthepath.gtfs.Route; import com.eatthepath.gtfs.Stop; import com.eatthepath.gtfs.Trip; import com.eatthepath.gtfs.util.GeospatialMath; import com.eatthepath.jeospatial.GeospatialPoint; import com.eatthepath.jeospatial.util.SimpleGeospatialPoint; import com.google.transit.realtime.GtfsRealtime.VehiclePosition.CongestionLevel; import com.google.transit.realtime.GtfsRealtime.VehiclePosition.VehicleStopStatus; public class Vehicle implements JSONAware { private final String id; private transient String label; private transient String licensePlate; private transient GeospatialPoint position; private transient Float bearing; private transient Double odometer; private transient Float speed; private transient Trip trip; private transient Route route; private transient Stop currentStop; private transient VehicleStopStatus stopStatus; private transient CongestionLevel congestionLevel; public Vehicle(final String id) { this.id = id; } public String getId() { return this.id; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public String getLicensePlate() { return licensePlate; } public void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; } public GeospatialPoint getPosition() { return position; } public void setPosition(GeospatialPoint position) { this.position = position; } public Float getBearing() { return bearing; } public void setBearing(Float bearing) { this.bearing = bearing; } public Double getInferredBearing() { // We have to know where the vehicle is to infer a bearing if (this.getPosition() != null) { // Our best bet for inferring a bearing is using the shape of the trip the vehicle is currently on if (this.getTrip() != null && this.getTrip().getShape() != null) { return this.getTrip().getShape().getBearingAtClosestPoint(this.getPosition()); } // We might also be able to infer a bearing from the next stop on the route if (this.getCurrentStop() != null) { final Stop destinationStop; if (this.getStopStatus() != null) { switch (this.getStopStatus()) { case INCOMING_AT: case IN_TRANSIT_TO: { destinationStop = this.getCurrentStop(); break; } case STOPPED_AT: { // TODO Handle vehicles stopped at the last stop on the trip destinationStop = this.getTrip().getStopAfterStop(this.currentStop); break; } default: { throw new IllegalArgumentException( String.format("Unexpected stop status: %s", this.getStopStatus())); } } } else { // Just assume we're heading toward the current stop destinationStop = this.getCurrentStop(); } if (destinationStop != null) { final GeospatialPoint destinationStopPosition = new SimpleGeospatialPoint( destinationStop.getLatitude(), destinationStop.getLongitude()); return GeospatialMath.getBearingBetweenPoints(this.getPosition(), destinationStopPosition); } else { return null; } } } return null; } public Double getOdometer() { return odometer; } public void setOdometer(Double odometer) { this.odometer = odometer; } public Float getSpeed() { return speed; } public void setSpeed(Float speed) { this.speed = speed; } public Trip getTrip() { return trip; } public void setTrip(final Trip trip) { this.trip = trip; } public Route getRoute() { if (this.route == null && this.trip != null) { return this.trip.getRoute(); } else { return this.route; } } public void setRoute(final Route route) { this.route = route; } public Stop getCurrentStop() { return currentStop; } public void setCurrentStop(Stop currentStop) { this.currentStop = currentStop; } public VehicleStopStatus getStopStatus() { return stopStatus; } public void setStopStatus(VehicleStopStatus stopStatus) { this.stopStatus = stopStatus; } public CongestionLevel getCongestionLevel() { return congestionLevel; } public void setCongestionLevel(CongestionLevel congestionLevel) { this.congestionLevel = congestionLevel; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Vehicle other = (Vehicle) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } @SuppressWarnings("unchecked") @Override public String toJSONString() { final JSONObject json = new JSONObject(); json.put("id", this.id); json.put("label", this.label); json.put("licensePlate", this.licensePlate); json.put("latitude", this.position != null ? this.position.getLatitude() : null); json.put("longitude", this.position != null ? this.position.getLongitude() : null); json.put("bearing", this.bearing); json.put("inferredBearing", this.getInferredBearing()); json.put("odometer", this.odometer); json.put("speed", this.speed); json.put("currentTripId", this.trip != null ? this.trip.getId() : null); json.put("currentRouteId", this.getRoute() != null ? this.getRoute().getId() : null); json.put("currentStopId", this.currentStop != null ? this.currentStop.getId() : null); json.put("stopStatus", this.stopStatus != null ? this.stopStatus.toString() : null); json.put("congestionLevel", this.congestionLevel != null ? this.congestionLevel.toString() : null); return json.toString(); } }