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

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable;
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)
{
this.network = network;
this.type = type;
this.currency = currency;
this.network = checkNotNull(network);
this.type = checkNotNull(type);
this.currency = checkNotNull(currency);
this.fare = fare;
this.unitName = unitName;
this.units = units;

View file

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

View file

@ -17,6 +17,9 @@
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.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)
{
this.type = type;
this.type = checkNotNull(type);
this.id = id;
this.lat = lat;
this.lon = lon;
this.place = place;
this.name = name;
if (name == null && place != null)
throw new IllegalArgumentException("place '" + place + "' without name cannot exist");
checkArgument(place == null || name != null, "place '%s' without name cannot exist", place);
}
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)
{
this.type = type;
this.type = checkNotNull(type);
this.id = id;
this.lat = 0;
this.lon = 0;
this.place = place;
this.name = name;
if (name == null && place != null)
throw new IllegalArgumentException("place '" + place + "' without name cannot exist");
checkArgument(place == null || name != null, "place '%s' without name cannot exist", place);
}
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.lat = lat;
this.lon = lon;
@ -83,7 +84,7 @@ public final class Location implements Serializable
public Location(final LocationType type, final String id)
{
this.type = type;
this.type = checkNotNull(type);
this.id = id;
this.lat = 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)
{
this.type = type;
this.type = checkNotNull(type);
this.id = null;
this.lat = lat;
this.lon = lon;

View file

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

View file

@ -17,6 +17,9 @@
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 com.google.common.base.Objects;
@ -36,16 +39,10 @@ public final class Position implements Serializable
public Position(final String name, final String section)
{
if (name == null)
throw new IllegalArgumentException("name cannot be null");
// 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.name = checkNotNull(name);
// checkArgument(name.length() <= 5, "name too long: %s", name);
this.section = section;
checkArgument(section == null || section.length() <= 3, "section too long: %s", section);
}
@Override

View file

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

View file

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

View file

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

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable;
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)
{
this.location = location;
this.departures = departures;
this.location = checkNotNull(location);
this.departures = checkNotNull(departures);
this.lines = lines;
}

View file

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

View file

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

View file

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

View file

@ -17,6 +17,8 @@
package de.schildbach.pte.dto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable;
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)
{
this.location = location;
this.location = checkNotNull(location);
this.priority = priority;
}

View file

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