diff --git a/src/main/java/ch/hepia/api/transport/Connection.java b/src/main/java/ch/hepia/api/transport/Connection.java index 2c0f21247b52f196e33312431198a537adc2fc9b..076f6d18ad2ff33259d348e0601f2e65cc5e63b0 100644 --- a/src/main/java/ch/hepia/api/transport/Connection.java +++ b/src/main/java/ch/hepia/api/transport/Connection.java @@ -11,7 +11,7 @@ import org.json.JSONArray; import org.json.JSONObject; -public final class Connection { +public class Connection { private Stop from; private Stop to; private Time duration; @@ -21,6 +21,26 @@ public final class Connection { private int capacity2nd; private List<Section> sections; + /** + * Empty Connection + */ + private final static class EmptyConnection extends Connection{ + /** + * Constructor of EmptyConnection + * All values are default ones + * An Empty Connection go from Geneva to Geneva + * @throws ParseException + */ + private EmptyConnection() throws ParseException { + super(new Stop.StopBuilder(new JSONObject()).build(), + new Stop.StopBuilder(new JSONObject()).build(), + new Time(new SimpleDateFormat("dd:kk:mm:ss").parse("00:00:00:00").getTime()), + new Service.ServiceBuilder(new JSONObject()).build(), + new ArrayList<>(), + 0,0, new ArrayList<>()); + } + } + /** * Constructor of Connection Object * @param from stop object @@ -51,10 +71,11 @@ public final class Connection { return new ArrayList<>(sections); } + /** * Builder of Connection object */ - public static class ConnectionBuilder{ + public final static class ConnectionBuilder{ private JSONObject datas; /** @@ -72,6 +93,11 @@ public final class Connection { * @throws ParseException */ public Connection build() throws ParseException { + //if the datas retrieved contain nothing, we have to prevent errors + if(datas.isEmpty()){ + return new EmptyConnection(); + } + JSONObject fromJSON = new JSONObject(datas.get("from")); Stop.StopBuilder from = new Stop.StopBuilder(fromJSON); @@ -79,7 +105,7 @@ public final class Connection { Stop.StopBuilder to = new Stop.StopBuilder(toJSON); DateFormat formatter = new SimpleDateFormat("dd:kk:mm:ss"); - String date = datas.getString("duration").replace('d',':'); + String date = datas.isNull("duration") ? "00:00:00:00" : datas.getString("duration").replace('d',':'); Time duration = new Time(formatter.parse(date).getTime()); JSONObject serviceJSON = new JSONObject(datas.get("service")); diff --git a/src/main/java/ch/hepia/api/transport/Coordinates.java b/src/main/java/ch/hepia/api/transport/Coordinates.java index 0d671dc1c526115c541d846cb50178304bec3aea..d43502052d880f9a0faae291ac0666f5345ab1d8 100644 --- a/src/main/java/ch/hepia/api/transport/Coordinates.java +++ b/src/main/java/ch/hepia/api/transport/Coordinates.java @@ -2,11 +2,24 @@ package ch.hepia.api.transport; import org.json.JSONObject; -public final class Coordinates { - private Type type; +public class Coordinates { + private String type; private double x; private double y; + /** + * Default coordinates containing default values (Geneva is the default value) + * WGS84 is the default type value + */ + private final static class DefaultCoordinates extends Coordinates{ + /** + * Constructor of DefaultCoordinates, located in Geneva + */ + private DefaultCoordinates(){ + super("WGS84", 46.210237, 6.142422); + } + } + /** * Constructor of the class Coordinates * A coordinates is represented by a type of place and a point (x, y) @@ -14,7 +27,7 @@ public final class Coordinates { * @param x coordinate in x * @param y coordinate in y */ - private Coordinates(Type type, double x, double y){ + private Coordinates(String type, double x, double y){ this.type = type; this.x = x; this.y = y; @@ -23,7 +36,7 @@ public final class Coordinates { @Override public String toString(){ StringBuilder string = new StringBuilder(); - return string.append(type.toString()).append("au point (").append(x).append(", ").append(y).append(")").toString(); + return string.append("Coordonnées du type ").append(type).append(" au point (").append(x).append(", ").append(y).append(")").toString(); } /** @@ -45,7 +58,7 @@ public final class Coordinates { /** * Builder of Coordinates object */ - public static class CoordinatesBuilder{ + public final static class CoordinatesBuilder{ private JSONObject datas; /** @@ -62,7 +75,11 @@ public final class Coordinates { * @return a new Coordinates object */ public Coordinates build(){ - Type type = Type.valueOf(datas.getString("type")); + if(datas.isEmpty()){ + return new DefaultCoordinates(); + } + + String type = datas.getString("type"); double x = datas.getDouble("x"); double y = datas.getDouble("y"); diff --git a/src/main/java/ch/hepia/api/transport/Journey.java b/src/main/java/ch/hepia/api/transport/Journey.java index d2362fdeec26d37d1834ca992c757382072f491d..478b2f8a42834f7ef8296bc3bed9d50d078aa6c8 100644 --- a/src/main/java/ch/hepia/api/transport/Journey.java +++ b/src/main/java/ch/hepia/api/transport/Journey.java @@ -7,7 +7,7 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.List; -public final class Journey { +public class Journey { private String name; private String category; private int categoryCode; @@ -18,6 +18,21 @@ public final class Journey { private int capacity1st; private int capacity2nd; + /** + * Empty Journey + */ + private final static class EmptyJourney extends Journey{ + /** + * Constructor of EmptyJourney + * No Information and no Stops in the Journey + * @throws ParseException + */ + private EmptyJourney() throws ParseException { + super("", "", 0, 0, "", "", + new ArrayList<>(), 0, 0); + } + } + /** * Constructor of the Journey Object * @param name @@ -85,7 +100,7 @@ public final class Journey { /** * Builder of Journey object */ - public static class JourneyBuilder{ + public final static class JourneyBuilder{ private JSONObject datas; /** @@ -103,6 +118,10 @@ public final class Journey { * @throws ParseException */ public Journey build() throws ParseException { + if(datas.isEmpty()){ + return new EmptyJourney(); + } + String name = datas.getString("name"); String category = datas.getString("category"); int categoryCode = datas.getInt("categoryCode"); diff --git a/src/main/java/ch/hepia/api/transport/Location.java b/src/main/java/ch/hepia/api/transport/Location.java index 0bbaf8a8f04ff3f2c2bb0238c082c183e63eb37c..75c519435ba55d6d2fec333e2a96019b1fa58468 100644 --- a/src/main/java/ch/hepia/api/transport/Location.java +++ b/src/main/java/ch/hepia/api/transport/Location.java @@ -2,7 +2,7 @@ package ch.hepia.api.transport; import org.json.JSONObject; -public final class Location { +public class Location { private int id; private Type type; private String name; @@ -10,6 +10,20 @@ public final class Location { private Coordinates where; private double distance; + /** + * Default Location + */ + private final static class DefaultLocation extends Location{ + /** + * Constructor of DefaultLocation, centered in Geneva + * Type is REFINE, because it's a default choice + */ + private DefaultLocation(){ + //here building new empty coordinates will return a default coordinates corresponding to Geneva + super(8501008, Type.REFINE, "Genève", 0, new Coordinates.CoordinatesBuilder(new JSONObject()).build(), 0); + } + } + /** * Constructor of the class Location * @param id unique to the Location @@ -39,7 +53,7 @@ public final class Location { /** * Builder of Location objects */ - public static class LocationBuilder{ + public final static class LocationBuilder{ private JSONObject datas; private String type; @@ -58,15 +72,19 @@ public final class Location { * @return a new Location object */ public Location build(){ - int id = datas.getInt("id"); - Type type = Type.valueOf(this.type); - String name = datas.getString("name"); - double score = datas.getDouble("score"); + if(datas.isEmpty()){ + return new DefaultLocation(); + } + + int id = datas.isNull("id") ? 0 : datas.getInt("id"); + Type type = Type.valueOf(this.type.toUpperCase()); + String name = datas.isNull("name") ? "" : datas.getString("name"); + double score = datas.isNull("score") ? 0 : datas.getDouble("score"); - JSONObject coordinatesJSON = new JSONObject(datas.get("coordinates")); + JSONObject coordinatesJSON = datas.getJSONObject("coordinate"); Coordinates.CoordinatesBuilder coordinates = new Coordinates.CoordinatesBuilder(coordinatesJSON); - double distance = datas.getDouble("distance"); + double distance = datas.isNull("distance") ? 0 : datas.getDouble("distance"); return new Location(id, type, name, score, coordinates.build(), distance); } diff --git a/src/main/java/ch/hepia/api/transport/Prognosis.java b/src/main/java/ch/hepia/api/transport/Prognosis.java index 90ebf678d0fd4e27828c37a8af4929f8d3b106a2..9c574e8da4954bda20216aa4708ea59e580fbe92 100644 --- a/src/main/java/ch/hepia/api/transport/Prognosis.java +++ b/src/main/java/ch/hepia/api/transport/Prognosis.java @@ -7,13 +7,31 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -public final class Prognosis { +public class Prognosis { private int plateform; private Time departure; private Time arrival; private int capacity1st; private int capacity2nd; + /** + * Empty Prognosis + */ + private final static class EmptyPrognosis extends Prognosis{ + /** + * Constructor of EmptyPrognosis + * No departure and no Arrival Time + * No information on the capacity of 1st and 2nd class nor on the plateform + * @throws ParseException + */ + private EmptyPrognosis() throws ParseException { + super(0, + new Time(new SimpleDateFormat("dd:kk:mm:ss").parse("00:00:00:00").getTime()), + new Time(new SimpleDateFormat("dd:kk:mm:ss").parse("00:00:00:00").getTime()), + 0, 0); + } + } + /** * Constructor of the Prognosis class * A Prognosis contains information on the status of a Stop Object @@ -34,7 +52,7 @@ public final class Prognosis { /** * Builder of Prognosis Objects */ - public static class PrognosisBuilder{ + public final static class PrognosisBuilder{ private JSONObject datas; /** @@ -52,17 +70,21 @@ public final class Prognosis { * @throws ParseException */ public Prognosis build() throws ParseException { - int plateform = datas.getInt("plateform"); + if(datas.isEmpty()){ + return new EmptyPrognosis(); + } + + int plateform = datas.isNull("plateform") ? 0 : datas.getInt("plateform"); DateFormat formatter = new SimpleDateFormat("dd:kk:mm:ss"); - String depatureString = datas.getString("departure").replace('d',':'); + String depatureString = datas.isNull("departure") ? "00:00:00:00" : datas.getString("departure").replace('d',':'); Time departure = new Time(formatter.parse(depatureString).getTime()); - String arrivalString = datas.getString("arrival").replace('d', ':'); + String arrivalString = datas.isNull("arrival") ? "00:00:00:00" : datas.getString("arrival").replace('d', ':'); Time arrival = new Time(formatter.parse(arrivalString).getTime()); - int capacity1st = datas.getInt("capacity1st"); - int capacity2nd = datas.getInt("capacity2nd"); + int capacity1st = datas.isNull("capacity1st") ? 0 : datas.getInt("capacity1st"); + int capacity2nd = datas.isNull("capacity2nd") ? 0 : datas.getInt("capacity2nd"); return new Prognosis(plateform, departure, arrival, capacity1st, capacity2nd); } diff --git a/src/main/java/ch/hepia/api/transport/Section.java b/src/main/java/ch/hepia/api/transport/Section.java index 8fd4df952cd49f77ecf57459c0f8dd985a61b4f7..f301b3b526c7c382b07fc4c4a65eb1ccc8c8d045 100644 --- a/src/main/java/ch/hepia/api/transport/Section.java +++ b/src/main/java/ch/hepia/api/transport/Section.java @@ -4,12 +4,29 @@ import org.json.JSONObject; import java.text.ParseException; -public final class Section { +public class Section { private Journey journey; private double walktime; private Stop departure; private Stop arrival; + /** + * Empty Section + */ + private final static class EmptySection extends Section{ + /** + * Constructor of EmptySection + * An empty Section is a Journey From Geneva to Geneva + * @throws ParseException + */ + private EmptySection() throws ParseException { + super(new Journey.JourneyBuilder(new JSONObject()).build(), 0, + new Stop.StopBuilder(new JSONObject()).build(), + new Stop.StopBuilder(new JSONObject()).build()); + } + } + + /** * Constructor of the class Section * @param journey what Journey this section is part of @@ -51,7 +68,7 @@ public final class Section { /** * Builder of Section objects */ - public static class SectionBuilder{ + public final static class SectionBuilder{ private JSONObject datas; /** @@ -69,15 +86,19 @@ public final class Section { * @throws ParseException */ public Section build() throws ParseException { - JSONObject journeyJSON = new JSONObject(datas.get("journey")); + if(datas.isEmpty()){ + return new EmptySection(); + } + + JSONObject journeyJSON = datas.getJSONObject("journey"); Journey.JourneyBuilder journey = new Journey.JourneyBuilder(journeyJSON); double walktime = datas.isNull("walktime") ? 0 : datas.getDouble("walktime"); - JSONObject departureJSON = new JSONObject(datas.get("departure")); + JSONObject departureJSON = datas.getJSONObject("departure"); Stop.StopBuilder departure = new Stop.StopBuilder(departureJSON); - JSONObject arrivalJSON = new JSONObject(datas.get("arrival")); + JSONObject arrivalJSON = datas.getJSONObject("arrival"); Stop.StopBuilder arrival = new Stop.StopBuilder(arrivalJSON); return new Section(journey.build(), walktime, departure.build(), arrival.build()); diff --git a/src/main/java/ch/hepia/api/transport/Service.java b/src/main/java/ch/hepia/api/transport/Service.java index 4e04c4198f2524d7102d7c5aba877f3a93ca5394..8057580580dfdf293e9a76c7a591d956aa78639d 100644 --- a/src/main/java/ch/hepia/api/transport/Service.java +++ b/src/main/java/ch/hepia/api/transport/Service.java @@ -3,10 +3,23 @@ package ch.hepia.api.transport; import org.json.JSONObject; -public final class Service { +public class Service { private String regular; private String irregular; + /** + * Empty Service + */ + private final static class EmptyService extends Service{ + /** + * Constructor of EmptyService + * No informations given + */ + private EmptyService(){ + super("", ""); + } + } + /** * Constructor of Service class * @param regular is the service regular ? @@ -20,7 +33,7 @@ public final class Service { /** * Builder of Service objects */ - public static class ServiceBuilder{ + public final static class ServiceBuilder{ private JSONObject datas; /** @@ -37,8 +50,12 @@ public final class Service { * @return a new Service object */ public Service build(){ - String regular = datas.getString("regular"); - String irregular = datas.getString("irregular"); + if(datas.isEmpty()){ + return new EmptyService(); + } + + String regular = datas.isNull("regular") ? "" : datas.getString("regular"); + String irregular = datas.isNull("irregular") ? "" : datas.getString("irregular"); return new Service(regular, irregular); } diff --git a/src/main/java/ch/hepia/api/transport/Stop.java b/src/main/java/ch/hepia/api/transport/Stop.java index f30c41070d766aec9b9278c27078c854f352b708..b58206adad9b4d090a3c140c6f1931f7330e3da4 100644 --- a/src/main/java/ch/hepia/api/transport/Stop.java +++ b/src/main/java/ch/hepia/api/transport/Stop.java @@ -7,7 +7,7 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -public final class Stop { +public class Stop { private Location station; private Time arrival; private Time departure; @@ -15,6 +15,25 @@ public final class Stop { private int plateform; private Prognosis prognosis; + /** + * Default Stop in Geneva + */ + private final static class DefaultStop extends Stop{ + /** + * Constructor of DefaultStop + * DefaultStop is located in Geneva + * No Time of departure nor Arrival, no Prognosis, no plateform and no delay + * @throws ParseException + */ + private DefaultStop() throws ParseException { + super(new Location.LocationBuilder(new JSONObject(), "refine").build(), + new Time(new SimpleDateFormat("dd:kk:mm:ss").parse("00:00:00:00").getTime()), + new Time(new SimpleDateFormat("dd:kk:mm:ss").parse("00:00:00:00").getTime()), + 0, 0, + new Prognosis.PrognosisBuilder(new JSONObject()).build()); + } + } + /** * Constructor of the Stop class * @param station Location where the Stop is @@ -60,7 +79,7 @@ public final class Stop { /** * Builder of Stop objects */ - public static class StopBuilder{ + public final static class StopBuilder{ private JSONObject datas; /** @@ -78,21 +97,25 @@ public final class Stop { * @throws ParseException */ public Stop build() throws ParseException { + if(datas.isEmpty()){ + return new DefaultStop(); + } + //getting the first key to know the type of the Location String[] keys = (String[]) datas.keySet().toArray(); String type = keys[0]; - JSONObject locationJSON = new JSONObject(datas.get(type)); + JSONObject locationJSON = datas.getJSONObject(type); Location.LocationBuilder location = new Location.LocationBuilder(locationJSON, type); DateFormat formatter = new SimpleDateFormat("yyyy-MM-dddkk:mm:ss"); - String arrivalString = datas.getString("arrival"); + String arrivalString = datas.isNull("arrival") ? "0000-00-00000:00:00" : datas.getString("arrival"); Time arrival = new Time(formatter.parse(arrivalString).getTime()); - String depatureString = datas.getString("departure"); + String depatureString = datas.isNull("departure") ? "0000-00-00000:00:00" : datas.getString("departure"); Time departure = new Time(formatter.parse(depatureString).getTime()); - int delay = datas.getInt("delay"); - int plateform = datas.getInt("plateform"); + int delay = datas.isNull("delay") ? 0 : datas.getInt("delay"); + int plateform = datas.isNull("plateform") ? 0 : datas.getInt("plateform"); JSONObject prognosisJSON = new JSONObject(datas.get("prognosis")); Prognosis.PrognosisBuilder prognosis = new Prognosis.PrognosisBuilder(prognosisJSON);