diff --git a/enabler/src/de/schildbach/pte/dto/Departure.java b/enabler/src/de/schildbach/pte/dto/Departure.java index fc415cc3..21c2b527 100644 --- a/enabler/src/de/schildbach/pte/dto/Departure.java +++ b/enabler/src/de/schildbach/pte/dto/Departure.java @@ -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 diff --git a/enabler/src/de/schildbach/pte/dto/Fare.java b/enabler/src/de/schildbach/pte/dto/Fare.java index 6f4654f4..f0e64b55 100644 --- a/enabler/src/de/schildbach/pte/dto/Fare.java +++ b/enabler/src/de/schildbach/pte/dto/Fare.java @@ -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; diff --git a/enabler/src/de/schildbach/pte/dto/LineDestination.java b/enabler/src/de/schildbach/pte/dto/LineDestination.java index 035115f4..40f45566 100644 --- a/enabler/src/de/schildbach/pte/dto/LineDestination.java +++ b/enabler/src/de/schildbach/pte/dto/LineDestination.java @@ -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; } diff --git a/enabler/src/de/schildbach/pte/dto/Location.java b/enabler/src/de/schildbach/pte/dto/Location.java index d46b0cfb..af6dc314 100644 --- a/enabler/src/de/schildbach/pte/dto/Location.java +++ b/enabler/src/de/schildbach/pte/dto/Location.java @@ -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; diff --git a/enabler/src/de/schildbach/pte/dto/NearbyLocationsResult.java b/enabler/src/de/schildbach/pte/dto/NearbyLocationsResult.java index 1a81c977..e36a7dab 100644 --- a/enabler/src/de/schildbach/pte/dto/NearbyLocationsResult.java +++ b/enabler/src/de/schildbach/pte/dto/NearbyLocationsResult.java @@ -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; } diff --git a/enabler/src/de/schildbach/pte/dto/Position.java b/enabler/src/de/schildbach/pte/dto/Position.java index f70bb684..60053f10 100644 --- a/enabler/src/de/schildbach/pte/dto/Position.java +++ b/enabler/src/de/schildbach/pte/dto/Position.java @@ -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 diff --git a/enabler/src/de/schildbach/pte/dto/QueryDeparturesResult.java b/enabler/src/de/schildbach/pte/dto/QueryDeparturesResult.java index bc6472e3..bbc127fa 100644 --- a/enabler/src/de/schildbach/pte/dto/QueryDeparturesResult.java +++ b/enabler/src/de/schildbach/pte/dto/QueryDeparturesResult.java @@ -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) diff --git a/enabler/src/de/schildbach/pte/dto/QueryTripsResult.java b/enabler/src/de/schildbach/pte/dto/QueryTripsResult.java index 0480290d..111c8c1a 100644 --- a/enabler/src/de/schildbach/pte/dto/QueryTripsResult.java +++ b/enabler/src/de/schildbach/pte/dto/QueryTripsResult.java @@ -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; diff --git a/enabler/src/de/schildbach/pte/dto/ResultHeader.java b/enabler/src/de/schildbach/pte/dto/ResultHeader.java index 9dee0c57..e5ff002c 100644 --- a/enabler/src/de/schildbach/pte/dto/ResultHeader.java +++ b/enabler/src/de/schildbach/pte/dto/ResultHeader.java @@ -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; diff --git a/enabler/src/de/schildbach/pte/dto/StationDepartures.java b/enabler/src/de/schildbach/pte/dto/StationDepartures.java index 5e2d8a50..780441c7 100644 --- a/enabler/src/de/schildbach/pte/dto/StationDepartures.java +++ b/enabler/src/de/schildbach/pte/dto/StationDepartures.java @@ -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 departures, final List lines) { - this.location = location; - this.departures = departures; + this.location = checkNotNull(location); + this.departures = checkNotNull(departures); this.lines = lines; } diff --git a/enabler/src/de/schildbach/pte/dto/Stop.java b/enabler/src/de/schildbach/pte/dto/Stop.java index 3ff28bbf..f8f3bc9a 100644 --- a/enabler/src/de/schildbach/pte/dto/Stop.java +++ b/enabler/src/de/schildbach/pte/dto/Stop.java @@ -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; diff --git a/enabler/src/de/schildbach/pte/dto/Style.java b/enabler/src/de/schildbach/pte/dto/Style.java index a3b0f86d..a7107b1b 100644 --- a/enabler/src/de/schildbach/pte/dto/Style.java +++ b/enabler/src/de/schildbach/pte/dto/Style.java @@ -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; diff --git a/enabler/src/de/schildbach/pte/dto/SuggestLocationsResult.java b/enabler/src/de/schildbach/pte/dto/SuggestLocationsResult.java index 81368470..4a48214d 100644 --- a/enabler/src/de/schildbach/pte/dto/SuggestLocationsResult.java +++ b/enabler/src/de/schildbach/pte/dto/SuggestLocationsResult.java @@ -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; } diff --git a/enabler/src/de/schildbach/pte/dto/SuggestedLocation.java b/enabler/src/de/schildbach/pte/dto/SuggestedLocation.java index 532d1615..c3f07f73 100644 --- a/enabler/src/de/schildbach/pte/dto/SuggestedLocation.java +++ b/enabler/src/de/schildbach/pte/dto/SuggestedLocation.java @@ -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= 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 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 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; }