Select Git revision
routerVMs.go
Connection.java 5.17 KiB
package ch.hepia.api.transport;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.Serializable;
public class Connection implements Serializable {
private Stop from;
private Stop to;
private Time duration;
private Service service;
private List<String> products;
private int capacity1st;
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
* @param to stop object
* @param duration time of the connection
* @param service how regular is the connection
* @param products list the products you have to purchase for this connection
* @param capacity1st number of seats in 1st class
* @param capacity2nd number of seats in 2nd class
* @param sections Where the connection will go through
*/
private Connection(Stop from, Stop to, Time duration, Service service, List<String> products, int capacity1st, int capacity2nd, List<Section> sections){
this.from = from;
this.to = to;
this.duration = duration;
this.service = service;
this.products = new ArrayList<>(products);
this.capacity1st = capacity1st;
this.capacity2nd = capacity2nd;
this.sections = new ArrayList<>(sections);
}
/**
* GEt all the sections of the connection
* @return A list of Section objects
*/
public List<Section> getSections(){
return new ArrayList<>(sections);
}
public Stop getFrom(){
return this.from;
}
public Stop getTo(){
return this.to;
}
/**
* Builder of Connection object
*/
public final static class ConnectionBuilder{
private JSONObject datas;
/**
* Constructor of the class ConnectionBuilder
* Stock Datas from a JSON object given
* @param datas contains datas to construct a Connection object
*/
public ConnectionBuilder(JSONObject datas){
this.datas = datas;
}
/**
* Build a Connection object from the datas obtained
* @return a new Connection object
* @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 = datas.isNull("from") ? new JSONObject() : datas.getJSONObject("from");
Stop.StopBuilder from = new Stop.StopBuilder(fromJSON);
JSONObject toJSON = datas.isNull("to") ? new JSONObject() : datas.getJSONObject("to");
Stop.StopBuilder to = new Stop.StopBuilder(toJSON);
DateFormat formatter = new SimpleDateFormat("dd:kk:mm:ss");
String date = datas.isNull("duration") ? "00:00:00:00" : datas.getString("duration").replace('d',':');
Time duration = new Time(formatter.parse(date).getTime());
JSONObject serviceJSON = datas.isNull("service") ? new JSONObject() : new JSONObject(datas.get("service"));
Service.ServiceBuilder service = new Service.ServiceBuilder(serviceJSON);
JSONArray productsJSON = datas.isNull("from") ? new JSONArray() : datas.getJSONArray("products");
List<String> products = new ArrayList<>();
productsJSON.forEach(k -> products.add(k.toString()));
int capacity1st = datas.isNull("capacity1st") ? 0 : datas.getInt("capacity1st");
int capacity2nd = datas.isNull("capacity2nd") ? 0 : datas.getInt("capacity2nd");
JSONArray sectionsJSON = datas.isNull("from") ? new JSONArray() : datas.getJSONArray("sections");
List<Section> sections = new ArrayList<>();
sectionsJSON.forEach(o -> {
JSONObject k = (JSONObject) o;
Section.SectionBuilder section = new Section.SectionBuilder(k);
try {
sections.add(section.build());
} catch (ParseException e) {
e.printStackTrace();
}
});
return new Connection(from.build(), to.build(), duration, service.build(), products, capacity1st, capacity2nd, sections);
}
}
}