Skip to content

Commit 2fa682c

Browse files
mmathieumgemini-code-assist[bot]Copilot
authored
GTFS-RT > static<>real-time matching (out-of-sync trip_id...) & tests (#117)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent c5ae20e commit 2fa682c

24 files changed

Lines changed: 1204 additions & 124 deletions

src/main/java/org/mtransit/android/commons/PreferenceUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,14 @@ public static void savePrefLcl(@Nullable final Context context, @NonNull final S
443443
savePrefLclAsync(context, prefKey, newValue);
444444
}
445445

446+
@WorkerThread
447+
public static void savePrefLclSync(@Nullable final Context context, @NonNull final String prefKey, @Nullable final Boolean newValue) {
448+
if (context == null) {
449+
return;
450+
}
451+
savePref(getPrefLcl(context), prefKey, newValue);
452+
}
453+
446454
@MainThread
447455
public static void savePrefLclAsync(@NonNull Context context, @NonNull String prefKey, @Nullable Boolean newValue) {
448456
new MTAsyncTask<Void, Void, Void>() {

src/main/java/org/mtransit/android/commons/data/DefaultPOI.java

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,38 @@ public DefaultPOI(@NonNull String authority, int id, @DataSourceType int dataSou
6767

6868
@Override
6969
public boolean equals(Object o) {
70-
if (o == null) {
71-
return false;
72-
}
73-
if (this.getClass() != o.getClass()) {
74-
return false;
75-
}
76-
DefaultPOI otherPOI = (DefaultPOI) o;
77-
if (!this.getUUID().equals(otherPOI.getUUID())) {
78-
return false;
79-
}
80-
if (this.getType() != otherPOI.getType()) {
81-
return false;
82-
}
83-
if (this.getStatusType() != otherPOI.getStatusType()) {
84-
return false;
85-
}
86-
if (this.getActionsType() != otherPOI.getActionsType()) {
87-
return false;
88-
}
89-
//noinspection RedundantIfStatement
90-
if (!Objects.equals(this.getName(), otherPOI.getName())) {
91-
return false;
92-
}
93-
return true;
70+
if (o == null) return false;
71+
if (this.getClass() != o.getClass()) return false;
72+
final DefaultPOI that = (DefaultPOI) o;
73+
return id == that.id
74+
&& authority.equals(that.authority)
75+
&& name.equals(that.name)
76+
&& Double.compare(lat, that.lat) == 0
77+
&& Double.compare(lng, that.lng) == 0
78+
&& accessible == that.accessible
79+
&& type == that.type
80+
&& dataSourceTypeId == that.dataSourceTypeId
81+
&& statusType == that.statusType
82+
&& actionsType == that.actionsType
83+
&& Objects.equals(scoreOpt, that.scoreOpt)
84+
;
85+
}
86+
87+
@Override
88+
public int hashCode() {
89+
return Objects.hash(
90+
id,
91+
authority,
92+
name,
93+
lat,
94+
lng,
95+
accessible,
96+
type,
97+
dataSourceTypeId,
98+
statusType,
99+
actionsType,
100+
scoreOpt
101+
);
94102
}
95103

96104
@NonNull

src/main/java/org/mtransit/android/commons/data/Direction.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.lang.annotation.Retention;
2222
import java.util.Comparator;
23+
import java.util.Objects;
2324

2425
@SuppressWarnings("WeakerAccess")
2526
public class Direction implements Targetable {
@@ -273,6 +274,30 @@ public long getRouteId() {
273274
return routeId;
274275
}
275276

277+
@Override
278+
public boolean equals(Object o) {
279+
if (o == null) return false;
280+
if (!(o instanceof Direction)) return false;
281+
final Direction direction = (Direction) o;
282+
return id == direction.id
283+
&& authority.equals(direction.authority)
284+
&& headsignType == direction.headsignType
285+
&& headsignValue.equals(direction.headsignValue)
286+
&& routeId == direction.routeId
287+
;
288+
}
289+
290+
@Override
291+
public int hashCode() {
292+
return Objects.hash(
293+
id,
294+
authority,
295+
headsignType,
296+
headsignValue,
297+
routeId
298+
);
299+
}
300+
276301
public static class HeadSignComparator implements Comparator<Direction>, MTLog.Loggable {
277302

278303
private static final String LOG_TAG = Direction.class.getSimpleName() + ">" + HeadSignComparator.class.getSimpleName();

src/main/java/org/mtransit/android/commons/data/Route.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public String getColor() {
107107
@Nullable
108108
private Integer colorInt;
109109

110+
@SuppressWarnings("unused") // main app only
110111
@ColorInt
111112
public int getColorInt() {
112113
if (this.colorInt == null) {
@@ -117,24 +118,30 @@ public int getColorInt() {
117118

118119
@Override
119120
public boolean equals(Object o) {
120-
if (!(o instanceof Route)) {
121-
return false;
122-
}
123-
Route otherRoute = (Route) o;
124-
if (getId() != otherRoute.getId()) {
125-
return false;
126-
}
127-
if (!Objects.equals(getShortName(), otherRoute.getShortName())) {
128-
return false;
129-
}
130-
if (!Objects.equals(getLongName(), otherRoute.getLongName())) {
131-
return false;
132-
}
133-
//noinspection RedundantIfStatement
134-
if (!Objects.equals(getColor(), otherRoute.getColor())) {
135-
return false;
136-
}
137-
return true;
121+
if (o == null) return false;
122+
if (!(o instanceof Route)) return false;
123+
final Route route = (Route) o;
124+
return id == route.id
125+
&& authority.equals(route.authority)
126+
&& shortName.equals(route.shortName)
127+
&& longName.equals(route.longName)
128+
&& color.equals(route.color)
129+
&& Objects.equals(originalIdHash, route.originalIdHash)
130+
&& Objects.equals(type, route.type)
131+
;
132+
}
133+
134+
@Override
135+
public int hashCode() {
136+
return Objects.hash(
137+
id,
138+
authority,
139+
shortName,
140+
longName,
141+
color,
142+
originalIdHash,
143+
type
144+
);
138145
}
139146

140147
@NonNull

src/main/java/org/mtransit/android/commons/data/RouteDirection.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.util.Arrays;
1717
import java.util.Collection;
18+
import java.util.Objects;
1819

1920
public class RouteDirection implements Targetable, MTLog.Loggable {
2021

@@ -59,6 +60,24 @@ public boolean equals(int routeId, int directionIdId) {
5960
return getRoute().getId() == routeId && getDirection().getId() == directionIdId;
6061
}
6162

63+
@Override
64+
public boolean equals(Object o) {
65+
if (o == null) return false;
66+
if (!(o instanceof RouteDirection)) return false;
67+
final RouteDirection that = (RouteDirection) o;
68+
return route.equals(that.route)
69+
&& direction.equals(that.direction)
70+
;
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash(
76+
route,
77+
direction
78+
);
79+
}
80+
6281
@NonNull
6382
@Override
6483
public String toString() {

src/main/java/org/mtransit/android/commons/data/RouteDirectionStop.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.util.Arrays;
2929
import java.util.Collection;
30+
import java.util.Objects;
3031

3132
public class RouteDirectionStop extends DefaultPOI {
3233

@@ -154,6 +155,32 @@ public boolean equals(int routeId, int directionIdId, int stopId) {
154155
return getRoute().getId() == routeId && getDirection().getId() == directionIdId && getStop().getId() == stopId;
155156
}
156157

158+
@Override
159+
public boolean equals(Object o) {
160+
if (o == null) return false;
161+
if (!(o instanceof RouteDirectionStop)) return false;
162+
if (!super.equals(o)) return false;
163+
final RouteDirectionStop that = (RouteDirectionStop) o;
164+
return route.equals(that.route)
165+
&& direction.equals(that.direction)
166+
&& stop.equals(that.stop)
167+
&& noPickup == that.noPickup
168+
&& Objects.equals(alwaysLastTripStop, that.alwaysLastTripStop)
169+
;
170+
}
171+
172+
@Override
173+
public int hashCode() {
174+
return Objects.hash(
175+
super.hashCode(),
176+
route,
177+
direction,
178+
stop,
179+
noPickup,
180+
alwaysLastTripStop
181+
);
182+
}
183+
157184
@NonNull
158185
@Override
159186
public String toString() {

src/main/java/org/mtransit/android/commons/data/ServiceUpdate.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import androidx.annotation.NonNull;
88
import androidx.annotation.Nullable;
9+
import androidx.annotation.VisibleForTesting;
910

1011
import org.mtransit.android.commons.ComparatorUtils;
1112
import org.mtransit.android.commons.CursorExtKt;
@@ -258,6 +259,11 @@ public boolean isUseful() {
258259
return this.lastUpdateInMs + this.maxValidityInMs >= TimeUtils.currentTimeMillis();
259260
}
260261

262+
@VisibleForTesting
263+
protected long getMaxValidityInMs() {
264+
return maxValidityInMs;
265+
}
266+
261267
@Nullable
262268
public Integer getId() {
263269
return this.id;

src/main/java/org/mtransit/android/commons/data/Stop.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.mtransit.android.commons.provider.GTFSProviderContract;
1616
import org.mtransit.commons.GTFSCommons;
1717

18+
import java.util.Objects;
19+
1820
@SuppressWarnings("WeakerAccess")
1921
public class Stop {
2022

@@ -209,4 +211,31 @@ public boolean isSameOriginalId(@Nullable String cleanedOriginalIdHash) {
209211
return this.originalIdHash.toString().equals(cleanedOriginalIdHash);
210212

211213
}
214+
215+
@Override
216+
public boolean equals(Object o) {
217+
if (o == null) return false;
218+
if (!(o instanceof Stop)) return false;
219+
final Stop stop = (Stop) o;
220+
return id == stop.id
221+
&& code.equals(stop.code)
222+
&& name.equals(stop.name)
223+
&& Double.compare(lat, stop.lat) == 0
224+
&& Double.compare(lng, stop.lng) == 0
225+
&& accessible == stop.accessible
226+
&& Objects.equals(originalIdHash, stop.originalIdHash);
227+
}
228+
229+
@Override
230+
public int hashCode() {
231+
return Objects.hash(
232+
id,
233+
code,
234+
name,
235+
lat,
236+
lng,
237+
accessible,
238+
originalIdHash
239+
);
240+
}
212241
}

src/main/java/org/mtransit/android/commons/provider/GTFSRealTimeProvider.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,20 @@ public static String getAGENCY_TRIP_UPDATES_URL_CACHED(@NonNull Context context)
415415
return agencyTripUpdatesUrlCached;
416416
}
417417

418+
@Nullable
419+
private static String targetAuthority = null;
420+
421+
/**
422+
* Override if multiple {@link GTFSRealTimeProvider} implementations in same app.
423+
*/
424+
@NonNull
425+
public static String getTARGET_AUTHORITY(@NonNull Context context) {
426+
if (targetAuthority == null) {
427+
targetAuthority = context.getResources().getString(R.string.gtfs_real_time_for_poi_authority);
428+
}
429+
return targetAuthority;
430+
}
431+
418432
@Nullable
419433
private static Boolean ignoreDirection = null;
420434

@@ -917,6 +931,7 @@ private List<ServiceUpdate> loadAgencyServiceUpdateDataFromWWW(@NonNull Context
917931
MTLog.d(this, "loadAgencyServiceUpdateDataFromWWW() > service update: %s.", serviceUpdate);
918932
}
919933
}
934+
GTFSRealTimeServiceAlertsProvider.setTripIdsOutOfSync(this, serviceUpdates);
920935
return serviceUpdates;
921936
default:
922937
MTLog.w(this, "ERROR: HTTP URL-Connection Response Code %s (Message: %s)", response.code(),

0 commit comments

Comments
 (0)