Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/Passaggio.java
Show All 13 Lines | BusTO (backend components) | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino.backend; | package it.reyboz.bustorino.backend; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.annotation.Nullable; | |||||
import android.util.Log; | import android.util.Log; | ||||
import java.util.Locale; | |||||
public final class Passaggio implements Comparable<Passaggio> { | public final class Passaggio implements Comparable<Passaggio> { | ||||
private static final int UNKNOWN_TIME = -3; | private static final int UNKNOWN_TIME = -3; | ||||
private static final String DEBUG_TAG = "BusTO-Passaggio"; | private static final String DEBUG_TAG = "BusTO-Passaggio"; | ||||
private final String passaggioGTT; | private final String passaggioGTT; | ||||
public final int hh,mm; | public final int hh,mm; | ||||
private @Nullable Integer realtimeDifference; | |||||
public final boolean isInRealTime; | public final boolean isInRealTime; | ||||
public final Source source; | public final Source source; | ||||
/** | /** | ||||
* Useless constructor. | * Useless constructor. | ||||
* | * | ||||
* //@param TimeGTT time in GTT format (e.g. "11:22*"), already trimmed from whitespace. | * //@param TimeGTT time in GTT format (e.g. "11:22*"), already trimmed from whitespace. | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | public Passaggio(@NonNull String TimeGTT, @NonNull Source sorgente) { | ||||
Log.w(DEBUG_TAG,"Cannot convert passaggio into hour and minutes"); | Log.w(DEBUG_TAG,"Cannot convert passaggio into hour and minutes"); | ||||
hour = UNKNOWN_TIME; | hour = UNKNOWN_TIME; | ||||
min = UNKNOWN_TIME; | min = UNKNOWN_TIME; | ||||
realtime = false; | realtime = false; | ||||
} finally { | } finally { | ||||
this.hh = hour; | this.hh = hour; | ||||
this.mm = min; | this.mm = min; | ||||
this.isInRealTime = realtime; | this.isInRealTime = realtime; | ||||
} | } | ||||
} | } | ||||
public Passaggio(int hour, int minutes, boolean realtime, Source sorgente){ | public Passaggio(int hour, int minutes, boolean realtime, Source sorgente){ | ||||
this.hh = hour; | this.hh = hour; | ||||
this.mm = minutes; | this.mm = minutes; | ||||
this.isInRealTime = realtime; | this.isInRealTime = realtime; | ||||
if (!realtime) realtimeDifference = 0; | |||||
this.source = sorgente; | this.source = sorgente; | ||||
//Build the passaggio string | //Build the passaggio string | ||||
StringBuilder sb = new StringBuilder(); | StringBuilder sb = new StringBuilder(); | ||||
sb.append(hour).append(":").append(minutes); | sb.append(hour).append(":").append(minutes); | ||||
if(realtime) sb.append("*"); | if(realtime) sb.append("*"); | ||||
this.passaggioGTT = sb.toString(); | this.passaggioGTT = sb.toString(); | ||||
} | } | ||||
public static String createPassaggioGTT(String timeInput, boolean realtime){ | public static String createPassaggioGTT(String timeInput, boolean realtime){ | ||||
final String time = timeInput.trim(); | final String time = timeInput.trim(); | ||||
if(time.contains("*")){ | if(time.contains("*")){ | ||||
if(realtime) return time; | if(realtime) return time; | ||||
else return time.substring(0,time.length()-1); | else return time.substring(0,time.length()-1); | ||||
} else{ | } else{ | ||||
if(realtime) return time.concat("*"); | if(realtime) return time.concat("*"); | ||||
else return time; | else return time; | ||||
} | } | ||||
} | } | ||||
public Passaggio(int numSeconds, boolean realtime, int timeDiff, Source source){ | |||||
int minutes = numSeconds / 60; | |||||
int hours = minutes / 60; | |||||
//this.hh = hours; | |||||
this.mm = minutes - hours*60; | |||||
this.hh = hours % 24; | |||||
this.realtimeDifference = timeDiff/60; | |||||
this.isInRealTime = realtime; | |||||
this.source = source; | |||||
this.passaggioGTT = makePassaggioGTT(this.hh, this.mm, this.isInRealTime); | |||||
} | |||||
private static String makePassaggioGTT(int hour, int minutes, boolean realtime){ | |||||
StringBuilder sb = new StringBuilder(); | |||||
sb.append(String.format(Locale.ITALIAN,"%02d", hour)).append(":").append(String.format(Locale.ITALIAN,"%02d", minutes)); | |||||
if(realtime) sb.append("*"); | |||||
return sb.toString(); | |||||
} | |||||
@Override | @Override | ||||
public int compareTo(@NonNull Passaggio other) { | public int compareTo(@NonNull Passaggio other) { | ||||
if(this.hh == UNKNOWN_TIME || other.hh == UNKNOWN_TIME) | if(this.hh == UNKNOWN_TIME || other.hh == UNKNOWN_TIME) | ||||
return 0; | return 0; | ||||
else { | else { | ||||
int diff = this.hh - other.hh; | int diff = this.hh - other.hh; | ||||
// an attempt to correctly sort arrival times around midnight (e.g. 23.59 should come before 00.01) | // an attempt to correctly sort arrival times around midnight (e.g. 23.59 should come before 00.01) | ||||
if (diff > 12) { // untested | if (diff > 12) { // untested | ||||
diff -= 24; | diff -= 24; | ||||
} else if (diff < -12) { | } else if (diff < -12) { | ||||
diff += 24; | diff += 24; | ||||
} | } | ||||
diff *= 60; | diff *= 60; | ||||
diff += this.mm - other.mm; | diff += this.mm - other.mm; | ||||
// we should take into account if one is in real time and the other isn't, shouldn't we? | // we should take into account if one is in real time and the other isn't, shouldn't we? | ||||
if (other.isInRealTime) { | if (other.isInRealTime) { | ||||
++diff; | diff+=2; | ||||
} | } | ||||
if (this.isInRealTime) { | if (this.isInRealTime) { | ||||
--diff; | diff -=2; | ||||
} | } | ||||
//TODO: separate Realtime and Non-Realtime, especially for the GTTJSONFetcher | |||||
return diff; | return diff; | ||||
} | } | ||||
} | } | ||||
// | // | ||||
// @Override | // @Override | ||||
// public String toString() { | // public String toString() { | ||||
// String resultString = (this.hh).concat(":").concat(this.mm); | // String resultString = (this.hh).concat(":").concat(this.mm); | ||||
// if(this.isInRealTime) { | // if(this.isInRealTime) { | ||||
// return resultString.concat("*"); | // return resultString.concat("*"); | ||||
// } else { | // } else { | ||||
// return resultString; | // return resultString; | ||||
// } | // } | ||||
// } | // } | ||||
public enum Source{ | public enum Source{ | ||||
FiveTAPI,GTTJSON,FiveTScraper, UNDETERMINED | FiveTAPI,GTTJSON,FiveTScraper,MatoAPI, UNDETERMINED | ||||
} | } | ||||
} | } | ||||
No newline at end of file | No newline at end of file |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register