-
Notifications
You must be signed in to change notification settings - Fork 152
/
Route.java
114 lines (95 loc) · 3.82 KB
/
Route.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.nyubustracker.models;
import android.util.Log;
import com.google.android.gms.maps.model.PolylineOptions;
import com.nyubustracker.BuildConfig;
import com.nyubustracker.activities.MainActivity;
import com.nyubustracker.helpers.BusManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class Route {
private final BusManager sharedManager;
private final ArrayList<String> segmentIDs;
private final ArrayList<PolylineOptions> segments;
private String longName = "";
private String otherLongName = ""; // Useful for Route B Greenwich times.
private String routeID = "";
private ArrayList<Stop> stops = null;
public Route(String mLongName, String mRouteID) {
segmentIDs = new ArrayList<>();
segments = new ArrayList<>();
longName = mLongName;
routeID = mRouteID;
sharedManager = BusManager.getBusManager();
stops = sharedManager.getStopsByRouteID(routeID);
for (Stop s : stops) {
s.addRoute(this);
}
}
public static void parseJSON(JSONObject routesJson) throws JSONException {
JSONArray jRoutes = new JSONArray();
BusManager sharedManager = BusManager.getBusManager();
if (routesJson != null)
jRoutes = routesJson.getJSONObject(BusManager.TAG_DATA).getJSONArray("72");
for (int j = 0; j < jRoutes.length(); j++) {
JSONObject routeObject = jRoutes.getJSONObject(j);
String routeLongName = routeObject.getString(BusManager.TAG_LONG_NAME);
String routeID = routeObject.getString(BusManager.TAG_ROUTE_ID);
Route r = sharedManager.getRoute(routeLongName, routeID);
JSONArray stops = routeObject.getJSONArray(BusManager.TAG_STOPS);
for (int i = 0; i < stops.length(); i++) {
r.addStop(i, sharedManager.getStopByID(stops.getString(i)));
}
JSONArray segments = routeObject.getJSONArray(BusManager.TAG_SEGMENTS);
if (BuildConfig.DEBUG)
Log.v(MainActivity.REFACTOR_LOG_TAG, "Found " + segments.length() + " segments for route " + routeID);
for (int i = 0; i < segments.length(); i++) {
//if (BuildConfig.DEBUG) Log.v("MapDebugging", "parseJSON of Route adding segment ID " + segments.getJSONArray(i).getString(0) + " for " + routeID + "(" + r.getSegmentIDs().size() + " total)");
r.getSegmentIDs().add(segments.getJSONArray(i).getString(0));
}
sharedManager.addRoute(r);
//if (BuildConfig.DEBUG) Log.v("JSONDebug", "Route name: " + routeLongName + " | ID:" + routeID + " | Number of stops: " + sharedManager.getRouteByID(routeID).getStops().size());
}
}
private void addStop(int index, Stop stop) {
if (stops.contains(stop)) stops.remove(stop);
if (stops.size() == index) stops.add(stop);
else stops.add(index, stop);
}
public ArrayList<String> getSegmentIDs() {
return segmentIDs;
}
public String toString() {
return longName;
}
public ArrayList<PolylineOptions> getSegments() {
return segments;
}
public Route setName(String name) {
longName = name;
return this;
}
public Route setOtherName(String otherName) {
otherLongName = otherName;
return this;
}
public String getOtherLongName() {
return otherLongName;
}
public String getID() {
return routeID;
}
public boolean hasStop(Stop stop) {
return stops.contains(stop);
}
public ArrayList<Stop> getStops() {
return stops;
}
public void addStop(Stop stop) {
if (!stops.contains(stop)) stops.add(stop);
}
public String getLongName() {
return longName;
}
}