Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/org/mtransit/android/commons/TimeUtilsK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ object TimeUtilsK {
fun Long.millisToInstant() = Instant.fromEpochMilliseconds(this)

fun Long.secsToInstant() = Instant.fromEpochSeconds(this)
fun Int.secsToInstant() = this.toLong().secsToInstant()

fun Instant.toMillis() = this.toEpochMilliseconds()

Expand Down
88 changes: 59 additions & 29 deletions src/main/java/org/mtransit/android/commons/data/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,17 @@ public Schedule(@NonNull POIStatus status, long providerPrecisionInMs, boolean n
);
}

public Schedule(@Nullable Integer id, @NonNull String targetUUID,
long lastUpdateInMs, long maxValidityInMs,
long readFromSourceAtInMs, long providerPrecisionInMs,
boolean noPickup, @Nullable String sourceLabel, boolean noData) {
public Schedule(
@Nullable Integer id,
@NonNull String targetUUID,
long lastUpdateInMs,
long maxValidityInMs,
long readFromSourceAtInMs,
long providerPrecisionInMs,
boolean noPickup,
@Nullable String sourceLabel,
boolean noData
) {
super(id, targetUUID, POI.ITEM_STATUS_TYPE_SCHEDULE, lastUpdateInMs, maxValidityInMs, readFromSourceAtInMs, sourceLabel, noData);
this.noPickup = noPickup;
this.providerPrecisionInMs = providerPrecisionInMs;
Expand Down Expand Up @@ -230,6 +237,10 @@ public void setTimestampsAndSort(@NonNull List<Timestamp> timestamps) {
sortTimestamps();
}

public boolean removeTimestamp(@NonNull Timestamp timestamp) {
return this.timestamps.remove(timestamp);
}

public void sortTimestamps() {
CollectionUtils.sort(this.timestamps, TIMESTAMPS_COMPARATOR);
resetUsefulUntilInMs();
Expand Down Expand Up @@ -267,7 +278,7 @@ private void resetUsefulUntilInMs() {
this.usefulUntilInMs = 0L; // NOT USEFUL
return;
}
this.usefulUntilInMs = this.timestamps.get(timestampsCount - 1).t + getUIProviderPrecisionInMs();
this.usefulUntilInMs = this.timestamps.get(timestampsCount - 1).getDepartureT() + getUIProviderPrecisionInMs();
}

private long getUsefulUntilInMs() {
Expand All @@ -286,8 +297,8 @@ public boolean isUseful() {
private static class TimestampComparator implements Comparator<Timestamp> {
@Override
public int compare(Timestamp lhs, Timestamp rhs) {
long lt = lhs == null ? 0L : lhs.t;
long rt = rhs == null ? 0L : rhs.t;
long lt = lhs == null ? 0L : lhs.getDepartureT();
long rt = rhs == null ? 0L : rhs.getDepartureT();
return (int) (lt - rt);
}
}
Expand Down Expand Up @@ -411,7 +422,7 @@ public static JSONObject toJSON(@NonNull Frequency frequency) {
}
}

public static class Timestamp implements MTLog.Loggable {
public static class Timestamp implements MTLog.Loggable { // Stop Time

private static final String LOG_TAG = Timestamp.class.getSimpleName();

Expand All @@ -421,7 +432,7 @@ public String getLogTag() {
return LOG_TAG;
}

public final long t;
private long departureInMs;
@Direction.HeadSignType
private int headsignType = Direction.HEADSIGN_TYPE_NONE;
@Nullable
Expand All @@ -435,40 +446,46 @@ public String getLogTag() {
@Nullable
private Integer accessible = null;
@Nullable
private String tripId = null; // will store trip ID int initially but replaced with real trip ID soon after
private String tripId = null; // cleaned trip ID (string) // initial used to store trip id INT but replaced after
private int stopSequence = -1;
@Nullable
private Long arrivalDiffMs = null;

@VisibleForTesting
public Timestamp(long t) {
this.t = t;
public Timestamp(long departureT) {
this.departureInMs = departureT;
}

public Timestamp(long t, @NonNull TimeZone localTimeZone) {
this(t, localTimeZone.getID());
public Timestamp(long departureT, @NonNull TimeZone localTimeZone) {
this(departureT, localTimeZone.getID());
}

public Timestamp(long t, @NonNull String localTimeZoneId) {
this.t = t;
public Timestamp(long departureT, @NonNull String localTimeZoneId) {
this.departureInMs = departureT;
this.localTimeZoneId = localTimeZoneId;
}

public long getT() {
return t;
public long getDepartureT() {
return this.departureInMs;
}

public void setDepartureT(long departureT) {
final long originalArrivalT = getArrivalT(); // stored as diff -> do not change
this.departureInMs = departureT;
setArrivalT(originalArrivalT); // stored as diff -> do not change
}

public long getArrivalT() {
return t + (arrivalDiffMs == null ? 0L : arrivalDiffMs);
return getDepartureT() - (arrivalDiffMs == null ? 0L : arrivalDiffMs);
}

@Nullable
public Long getArrivalTIfDifferent() {
return arrivalDiffMs == null ? null : t + arrivalDiffMs;
return arrivalDiffMs == null ? null : getDepartureT() - arrivalDiffMs;
}

public void setArrivalTimestamp(long arrivalTimestamp) {
setArrivalDiffMs(arrivalTimestamp - this.t);
public void setArrivalT(long arrivalT) {
setArrivalDiffMs(getDepartureT() - arrivalT);
}

public void setArrivalDiffMs(@Nullable Long arrivalDiffMs) {
Expand Down Expand Up @@ -653,7 +670,7 @@ public boolean equals(Object o) {

Timestamp timestamp = (Timestamp) o;

if (t != timestamp.t) return false;
if (departureInMs != timestamp.departureInMs) return false;
if (headsignType != timestamp.headsignType) return false;
if (!Objects.equals(headsignValue, timestamp.headsignValue)) return false;
if (!Objects.equals(localTimeZoneId, timestamp.localTimeZoneId)) return false;
Expand All @@ -669,7 +686,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = Long.hashCode(t);
int result = Long.hashCode(departureInMs);
result = 31 * result + headsignType;
result = 31 * result + (headsignValue != null ? headsignValue.hashCode() : 0);
result = 31 * result + (localTimeZoneId != null ? localTimeZoneId.hashCode() : 0);
Expand All @@ -688,7 +705,7 @@ public int hashCode() {
public String toString() {
StringBuilder sb = new StringBuilder(Timestamp.class.getSimpleName());
sb.append('{');
sb.append("t=").append(Constants.DEBUG ? MTLog.formatDateTime(t) : t);
sb.append("t=").append(Constants.DEBUG ? MTLog.formatDateTime(getDepartureT()) : getDepartureT());
if (arrivalDiffMs != null) {
sb.append(", aD:").append(arrivalDiffMs);
}
Expand Down Expand Up @@ -720,7 +737,7 @@ public String toString() {
return sb.toString();
}

private static final String JSON_TIMESTAMP = "t";
private static final String JSON_DEPARTURE = "t";
private static final String JSON_ARRIVAL_DIFF = "tDiffA";
private static final String JSON_TRIP_ID = "trip_id";
private static final String JSON_STOP_SEQUENCE = "stop_seq";
Expand All @@ -734,8 +751,8 @@ public String toString() {
@Nullable
static Timestamp parseJSON(@NonNull JSONObject jTimestamp) {
try {
final long t = jTimestamp.getLong(JSON_TIMESTAMP);
final Timestamp timestamp = new Timestamp(t);
final long departureInMs = jTimestamp.getLong(JSON_DEPARTURE);
final Timestamp timestamp = new Timestamp(departureInMs);
if (jTimestamp.has(JSON_ARRIVAL_DIFF)) {
timestamp.setArrivalDiffMs(jTimestamp.getLong(JSON_ARRIVAL_DIFF));
}
Expand Down Expand Up @@ -783,7 +800,7 @@ public JSONObject toJSON() {
public static JSONObject toJSON(@NonNull Timestamp timestamp) {
try {
JSONObject jTimestamp = new JSONObject();
jTimestamp.put(JSON_TIMESTAMP, timestamp.t);
jTimestamp.put(JSON_DEPARTURE, timestamp.departureInMs);
if (timestamp.arrivalDiffMs != null) {
jTimestamp.put(JSON_ARRIVAL_DIFF, timestamp.arrivalDiffMs);
}
Expand Down Expand Up @@ -861,6 +878,19 @@ public RouteDirectionStop getRouteDirectionStop() {
return routeDirectionStop;
}

@NonNull
public String getTargetAuthority() {
return this.routeDirectionStop.getAuthority();
}

public long getRouteId() {
return this.routeDirectionStop.getRoute().getId();
}

public long getDirectionId() {
return this.routeDirectionStop.getDirection().getId();
}

public long getLookBehindInMsOrDefault() {
return lookBehindInMs == null ? LOOK_BEHIND_IN_MS_DEFAULT : lookBehindInMs;
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/mtransit/android/commons/data/ScheduleExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.mtransit.android.commons.data

import org.mtransit.android.commons.millisToInstant
import org.mtransit.android.commons.toMillis
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Instant

fun Instant.toScheduleTimestamp(localTimeZoneId: String, arrival: Instant? = null, tripId: String? = null): Schedule.Timestamp {
return Schedule.Timestamp(this.toMillis(), localTimeZoneId).apply {
arrival?.let { this.arrival = it }
tripId?.let { this.tripId = it }
}
}

var Schedule.Timestamp.departure: Instant
get() = departureT.millisToInstant()
set(value) {
departureT = value.toMillis()
}

@Suppress("unused")
val Schedule.Timestamp.arrivalDiff: Duration? get() = this.arrivalDiffMs?.milliseconds?.coerceAtLeast(Duration.ZERO)

var Schedule.Timestamp.arrival: Instant
get() = arrivalT.millisToInstant()
set(value) {
arrivalT = value.toMillis()
}
22 changes: 21 additions & 1 deletion src/main/java/org/mtransit/android/commons/data/Stop.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.mtransit.android.commons.data;

import static org.mtransit.android.commons.StringUtils.EMPTY;

import android.database.Cursor;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -186,7 +188,25 @@ public int getAccessible() {
}

@Nullable
public Integer getOriginalIdHash() {
protected Integer getOriginalIdHash() {
return originalIdHash;
}

@Nullable
public String getOriginalIdHashString() {
return originalIdHash == null ? null : String.valueOf(originalIdHash);
}

@Deprecated
@NonNull
public String getOriginalIdHashStringOrDefault() {
return originalIdHash == null ? EMPTY : String.valueOf(originalIdHash);
}

public boolean isSameOriginalId(@Nullable String cleanedOriginalIdHash) {
if (cleanedOriginalIdHash == null) return false;
if (this.originalIdHash == null) return false;
return this.originalIdHash.toString().equals(cleanedOriginalIdHash);

}
}
Loading