Apply preconditions on DTOs.

This commit is contained in:
Andreas Schildbach 2015-02-04 16:27:07 +01:00
parent cd5841cf0f
commit 7cb4e8fb06
15 changed files with 115 additions and 117 deletions

View file

@ -17,6 +17,9 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
@ -44,7 +47,8 @@ public final class Departure implements Serializable
{ {
this.plannedTime = plannedTime; this.plannedTime = plannedTime;
this.predictedTime = predictedTime; this.predictedTime = predictedTime;
this.line = line; checkArgument(plannedTime != null || predictedTime != null);
this.line = checkNotNull(line);
this.position = position; this.position = position;
this.destination = destination; this.destination = destination;
this.capacity = capacity; this.capacity = capacity;
@ -55,10 +59,8 @@ public final class Departure implements Serializable
{ {
if (predictedTime != null) if (predictedTime != null)
return predictedTime; return predictedTime;
else if (plannedTime != null)
return plannedTime;
else else
return null; return plannedTime;
} }
@Override @Override

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.Currency; import java.util.Currency;
@ -42,9 +44,9 @@ public final class Fare implements Serializable
public Fare(final String network, final Type type, final Currency currency, final float fare, final String unitName, final String units) public Fare(final String network, final Type type, final Currency currency, final float fare, final String unitName, final String units)
{ {
this.network = network; this.network = checkNotNull(network);
this.type = type; this.type = checkNotNull(type);
this.currency = currency; this.currency = checkNotNull(currency);
this.fare = fare; this.fare = fare;
this.unitName = unitName; this.unitName = unitName;
this.units = units; this.units = units;

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
@ -32,7 +34,7 @@ public final class LineDestination implements Serializable
public LineDestination(final Line line, final Location destination) public LineDestination(final Line line, final Location destination)
{ {
this.line = line; this.line = checkNotNull(line);
this.destination = destination; this.destination = destination;
} }

View file

@ -17,6 +17,9 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
@ -37,15 +40,14 @@ public final class Location implements Serializable
public Location(final LocationType type, final String id, final int lat, final int lon, final String place, final String name) public Location(final LocationType type, final String id, final int lat, final int lon, final String place, final String name)
{ {
this.type = type; this.type = checkNotNull(type);
this.id = id; this.id = id;
this.lat = lat; this.lat = lat;
this.lon = lon; this.lon = lon;
this.place = place; this.place = place;
this.name = name; this.name = name;
if (name == null && place != null) checkArgument(place == null || name != null, "place '%s' without name cannot exist", place);
throw new IllegalArgumentException("place '" + place + "' without name cannot exist");
} }
public Location(final LocationType type, final String id, final Point coord, final String place, final String name) public Location(final LocationType type, final String id, final Point coord, final String place, final String name)
@ -55,20 +57,19 @@ public final class Location implements Serializable
public Location(final LocationType type, final String id, final String place, final String name) public Location(final LocationType type, final String id, final String place, final String name)
{ {
this.type = type; this.type = checkNotNull(type);
this.id = id; this.id = id;
this.lat = 0; this.lat = 0;
this.lon = 0; this.lon = 0;
this.place = place; this.place = place;
this.name = name; this.name = name;
if (name == null && place != null) checkArgument(place == null || name != null, "place '%s' without name cannot exist", place);
throw new IllegalArgumentException("place '" + place + "' without name cannot exist");
} }
public Location(final LocationType type, final String id, final int lat, final int lon) public Location(final LocationType type, final String id, final int lat, final int lon)
{ {
this.type = type; this.type = checkNotNull(type);
this.id = id; this.id = id;
this.lat = lat; this.lat = lat;
this.lon = lon; this.lon = lon;
@ -83,7 +84,7 @@ public final class Location implements Serializable
public Location(final LocationType type, final String id) public Location(final LocationType type, final String id)
{ {
this.type = type; this.type = checkNotNull(type);
this.id = id; this.id = id;
this.lat = 0; this.lat = 0;
this.lon = 0; this.lon = 0;
@ -93,7 +94,7 @@ public final class Location implements Serializable
public Location(final LocationType type, final int lat, final int lon) public Location(final LocationType type, final int lat, final int lon)
{ {
this.type = type; this.type = checkNotNull(type);
this.id = null; this.id = null;
this.lat = lat; this.lat = lat;
this.lon = lon; this.lon = lon;

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
@ -41,13 +43,13 @@ public final class NearbyLocationsResult implements Serializable
{ {
this.header = header; this.header = header;
this.status = Status.OK; this.status = Status.OK;
this.locations = locations; this.locations = checkNotNull(locations);
} }
public NearbyLocationsResult(final ResultHeader header, final Status status) public NearbyLocationsResult(final ResultHeader header, final Status status)
{ {
this.header = header; this.header = header;
this.status = status; this.status = checkNotNull(status);
this.locations = null; this.locations = null;
} }

View file

@ -17,6 +17,9 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import com.google.common.base.Objects; import com.google.common.base.Objects;
@ -36,16 +39,10 @@ public final class Position implements Serializable
public Position(final String name, final String section) public Position(final String name, final String section)
{ {
if (name == null) this.name = checkNotNull(name);
throw new IllegalArgumentException("name cannot be null"); // checkArgument(name.length() <= 5, "name too long: %s", name);
// else if (name.length() > 5)
// throw new IllegalArgumentException("name too long: " + name);
if (section != null && section.length() > 3)
throw new IllegalArgumentException("section too long: " + section);
this.name = name;
this.section = section; this.section = section;
checkArgument(section == null || section.length() <= 3, "section too long: %s", section);
} }
@Override @Override

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -47,7 +49,7 @@ public final class QueryDeparturesResult implements Serializable
public QueryDeparturesResult(final ResultHeader header, final Status status) public QueryDeparturesResult(final ResultHeader header, final Status status)
{ {
this.header = header; this.header = header;
this.status = status; this.status = checkNotNull(status);
} }
public StationDepartures findStationDepartures(final String stationId) public StationDepartures findStationDepartures(final String stationId)

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
@ -56,8 +58,8 @@ public final class QueryTripsResult implements Serializable
this.from = from; this.from = from;
this.via = via; this.via = via;
this.to = to; this.to = to;
this.context = context; this.context = checkNotNull(context);
this.trips = trips; this.trips = checkNotNull(trips);
this.ambiguousFrom = null; this.ambiguousFrom = null;
this.ambiguousVia = null; this.ambiguousVia = null;
@ -84,7 +86,7 @@ public final class QueryTripsResult implements Serializable
public QueryTripsResult(final ResultHeader header, final Status status) public QueryTripsResult(final ResultHeader header, final Status status)
{ {
this.header = header; this.header = header;
this.status = status; this.status = checkNotNull(status);
this.ambiguousFrom = null; this.ambiguousFrom = null;
this.ambiguousVia = null; this.ambiguousVia = null;

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
@ -33,15 +35,12 @@ public final class ResultHeader implements Serializable
public ResultHeader(final String serverProduct) public ResultHeader(final String serverProduct)
{ {
this.serverProduct = serverProduct; this(serverProduct, null, 0, null);
this.serverVersion = null;
this.serverTime = 0;
this.context = null;
} }
public ResultHeader(final String serverProduct, final String serverVersion, final long serverTime, final Object context) public ResultHeader(final String serverProduct, final String serverVersion, final long serverTime, final Object context)
{ {
this.serverProduct = serverProduct; this.serverProduct = checkNotNull(serverProduct);
this.serverVersion = serverVersion; this.serverVersion = serverVersion;
this.serverTime = serverTime; this.serverTime = serverTime;
this.context = context; this.context = context;

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
@ -35,8 +37,8 @@ public final class StationDepartures implements Serializable
public StationDepartures(final Location location, final List<Departure> departures, final List<LineDestination> lines) public StationDepartures(final Location location, final List<Departure> departures, final List<LineDestination> lines)
{ {
this.location = location; this.location = checkNotNull(location);
this.departures = departures; this.departures = checkNotNull(departures);
this.lines = lines; this.lines = lines;
} }

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
@ -55,7 +57,7 @@ public final class Stop implements Serializable
final Date predictedDepartureTime, final Position plannedDeparturePosition, final Position predictedDeparturePosition, final Date predictedDepartureTime, final Position plannedDeparturePosition, final Position predictedDeparturePosition,
final boolean departureCancelled) final boolean departureCancelled)
{ {
this.location = location; this.location = checkNotNull(location);
this.plannedArrivalTime = plannedArrivalTime; this.plannedArrivalTime = plannedArrivalTime;
this.predictedArrivalTime = predictedArrivalTime; this.predictedArrivalTime = predictedArrivalTime;
this.plannedArrivalPosition = plannedArrivalPosition; this.plannedArrivalPosition = plannedArrivalPosition;
@ -77,7 +79,7 @@ public final class Stop implements Serializable
public Stop(final Location location, final boolean departure, final Date plannedTime, final Date predictedTime, final Position plannedPosition, public Stop(final Location location, final boolean departure, final Date plannedTime, final Date predictedTime, final Position plannedPosition,
final Position predictedPosition, final boolean cancelled) final Position predictedPosition, final boolean cancelled)
{ {
this.location = location; this.location = checkNotNull(location);
this.plannedArrivalTime = !departure ? plannedTime : null; this.plannedArrivalTime = !departure ? plannedTime : null;
this.predictedArrivalTime = !departure ? predictedTime : null; this.predictedArrivalTime = !departure ? predictedTime : null;
this.plannedArrivalPosition = !departure ? plannedPosition : null; this.plannedArrivalPosition = !departure ? plannedPosition : null;
@ -93,7 +95,7 @@ public final class Stop implements Serializable
public Stop(final Location location, final Date plannedArrivalTime, final Position plannedArrivalPosition, final Date plannedDepartureTime, public Stop(final Location location, final Date plannedArrivalTime, final Position plannedArrivalPosition, final Date plannedDepartureTime,
final Position plannedDeparturePosition) final Position plannedDeparturePosition)
{ {
this.location = location; this.location = checkNotNull(location);
this.plannedArrivalTime = plannedArrivalTime; this.plannedArrivalTime = plannedArrivalTime;
this.predictedArrivalTime = null; this.predictedArrivalTime = null;
this.plannedArrivalPosition = plannedArrivalPosition; this.plannedArrivalPosition = plannedArrivalPosition;

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -46,7 +48,7 @@ public class Style implements Serializable
public Style(final Shape shape, final int backgroundColor, final int foregroundColor) public Style(final Shape shape, final int backgroundColor, final int foregroundColor)
{ {
this.shape = shape; this.shape = checkNotNull(shape);
this.backgroundColor = backgroundColor; this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor; this.foregroundColor = foregroundColor;
this.borderColor = 0; this.borderColor = 0;
@ -54,7 +56,7 @@ public class Style implements Serializable
public Style(final Shape shape, final int backgroundColor, final int foregroundColor, final int borderColor) public Style(final Shape shape, final int backgroundColor, final int foregroundColor, final int borderColor)
{ {
this.shape = shape; this.shape = checkNotNull(shape);
this.backgroundColor = backgroundColor; this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor; this.foregroundColor = foregroundColor;
this.borderColor = borderColor; this.borderColor = borderColor;

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -51,7 +53,7 @@ public final class SuggestLocationsResult implements Serializable
public SuggestLocationsResult(final ResultHeader header, final Status status) public SuggestLocationsResult(final ResultHeader header, final Status status)
{ {
this.header = header; this.header = header;
this.status = status; this.status = checkNotNull(status);
this.suggestedLocations = null; this.suggestedLocations = null;
} }

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import com.google.common.base.Objects; import com.google.common.base.Objects;
@ -31,7 +33,7 @@ public final class SuggestedLocation implements Serializable, Comparable<Suggest
public SuggestedLocation(final Location location, final int priority) public SuggestedLocation(final Location location, final int priority)
{ {
this.location = location; this.location = checkNotNull(location);
this.priority = priority; this.priority = priority;
} }

View file

@ -17,6 +17,9 @@
package de.schildbach.pte.dto; package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.EnumSet; import java.util.EnumSet;
@ -47,28 +50,26 @@ public final class Trip implements Serializable
final Integer numChanges) final Integer numChanges)
{ {
this.id = id; this.id = id;
this.from = from; this.from = checkNotNull(from);
this.to = to; this.to = checkNotNull(to);
this.legs = legs; this.legs = checkNotNull(legs);
this.fares = fares; this.fares = fares;
this.capacity = capacity; this.capacity = capacity;
this.numChanges = numChanges; this.numChanges = numChanges;
checkArgument(!legs.isEmpty());
} }
public Date getFirstDepartureTime() public Date getFirstDepartureTime()
{ {
if (legs != null && !legs.isEmpty()) return legs.get(0).getDepartureTime();
return legs.get(0).getDepartureTime();
else
return null;
} }
public Public getFirstPublicLeg() public Public getFirstPublicLeg()
{ {
if (legs != null) for (final Leg leg : legs)
for (final Leg leg : legs) if (leg instanceof Public)
if (leg instanceof Public) return (Public) leg;
return (Public) leg;
return null; return null;
} }
@ -84,22 +85,16 @@ public final class Trip implements Serializable
public Date getLastArrivalTime() public Date getLastArrivalTime()
{ {
if (legs != null && !legs.isEmpty()) return legs.get(legs.size() - 1).getArrivalTime();
return legs.get(legs.size() - 1).getArrivalTime();
else
return null;
} }
public Public getLastPublicLeg() public Public getLastPublicLeg()
{ {
if (legs != null) for (int i = legs.size() - 1; i >= 0; i--)
{ {
for (int i = legs.size() - 1; i >= 0; i--) final Leg leg = legs.get(i);
{ if (leg instanceof Public)
final Leg leg = legs.get(i); return (Public) leg;
if (leg instanceof Public)
return (Public) leg;
}
} }
return null; return null;
@ -191,10 +186,9 @@ public final class Trip implements Serializable
{ {
final Set<Product> products = EnumSet.noneOf(Product.class); final Set<Product> products = EnumSet.noneOf(Product.class);
if (legs != null) for (final Leg leg : legs)
for (final Leg leg : legs) if (leg instanceof Public)
if (leg instanceof Public) products.add(Product.fromCode(((Public) leg).line.label.charAt(0)));
products.add(Product.fromCode(((Public) leg).line.label.charAt(0)));
return products; return products;
} }
@ -211,31 +205,28 @@ public final class Trip implements Serializable
{ {
final StringBuilder builder = new StringBuilder(); final StringBuilder builder = new StringBuilder();
if (legs != null && legs.size() > 0) for (final Leg leg : legs)
{ {
for (final Leg leg : legs) builder.append(leg.departure.hasId() ? leg.departure.id : leg.departure.lat + '/' + leg.departure.lon).append('-');
builder.append(leg.arrival.hasId() ? leg.arrival.id : leg.arrival.lat + '/' + leg.arrival.lon).append('-');
if (leg instanceof Individual)
{ {
builder.append(leg.departure.hasId() ? leg.departure.id : leg.departure.lat + '/' + leg.departure.lon).append('-'); builder.append(((Individual) leg).min);
builder.append(leg.arrival.hasId() ? leg.arrival.id : leg.arrival.lat + '/' + leg.arrival.lon).append('-'); }
else if (leg instanceof Public)
if (leg instanceof Individual) {
{ final Public publicLeg = (Public) leg;
builder.append(((Individual) leg).min); builder.append(publicLeg.departureStop.plannedDepartureTime.getTime()).append('-');
} builder.append(publicLeg.arrivalStop.plannedArrivalTime.getTime()).append('-');
else if (leg instanceof Public) builder.append(publicLeg.line.label);
{
final Public publicLeg = (Public) leg;
builder.append(publicLeg.departureStop.plannedDepartureTime.getTime()).append('-');
builder.append(publicLeg.arrivalStop.plannedArrivalTime.getTime()).append('-');
builder.append(publicLeg.line.label);
}
builder.append('|');
} }
builder.setLength(builder.length() - 1); builder.append('|');
} }
builder.setLength(builder.length() - 1);
return builder.toString(); return builder.toString();
} }
@ -278,8 +269,8 @@ public final class Trip implements Serializable
public Leg(final Location departure, final Location arrival, final List<Point> path) public Leg(final Location departure, final Location arrival, final List<Point> path)
{ {
this.departure = departure; this.departure = checkNotNull(departure);
this.arrival = arrival; this.arrival = checkNotNull(arrival);
this.path = path; this.path = path;
} }
@ -312,23 +303,21 @@ public final class Trip implements Serializable
{ {
super(departureStop.location, arrivalStop.location, path); super(departureStop.location, arrivalStop.location, path);
this.line = line; this.line = checkNotNull(line);
this.destination = destination; this.destination = destination;
this.departureStop = departureStop; this.departureStop = checkNotNull(departureStop);
this.arrivalStop = arrivalStop; this.arrivalStop = checkNotNull(arrivalStop);
this.intermediateStops = intermediateStops; this.intermediateStops = intermediateStops;
this.message = message; this.message = message;
checkNotNull(departureStop.getDepartureTime());
checkNotNull(arrivalStop.getArrivalTime());
} }
@Override @Override
public Date getDepartureTime() public Date getDepartureTime()
{ {
final Date departureTime = departureStop.getDepartureTime(); return departureStop.getDepartureTime();
if (departureTime == null)
throw new IllegalStateException();
return departureTime;
} }
public boolean isDepartureTimePredicted() public boolean isDepartureTimePredicted()
@ -354,12 +343,7 @@ public final class Trip implements Serializable
@Override @Override
public Date getArrivalTime() public Date getArrivalTime()
{ {
final Date arrivalTime = arrivalStop.getArrivalTime(); return arrivalStop.getArrivalTime();
if (arrivalTime == null)
throw new IllegalStateException();
return arrivalTime;
} }
public boolean isArrivalTimePredicted() public boolean isArrivalTimePredicted()
@ -422,15 +406,10 @@ public final class Trip implements Serializable
{ {
super(departure, arrival, path); super(departure, arrival, path);
this.type = type; this.type = checkNotNull(type);
this.departureTime = departureTime; this.departureTime = checkNotNull(departureTime);
this.arrivalTime = arrivalTime; this.arrivalTime = checkNotNull(arrivalTime);
this.min = (int) ((arrivalTime.getTime() - departureTime.getTime()) / 1000 / 60);
if (arrivalTime != null && departureTime != null)
this.min = (int) ((arrivalTime.getTime() - departureTime.getTime()) / 1000 / 60);
else
this.min = 0;
this.distance = distance; this.distance = distance;
} }