Author: Adam Pioterek <adam.pioterek@protonmail.ch>
safe split
.idea/misc.xml | 2 +- app/src/main/java/ml/adamsprogs/bimba/extensions.kt | 7 +++++++ app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt | 7 ++-----
diff --git a/.idea/misc.xml b/.idea/misc.xml index ba7052b8197ddf8ba8756022d905d03055c7ad60..635999df1e86791ad3787e455b4524e4d8879b93 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -24,7 +24,7 @@ </value> </option> </component> - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/build/classes" /> </component> <component name="ProjectType"> diff --git a/app/src/main/java/ml/adamsprogs/bimba/extensions.kt b/app/src/main/java/ml/adamsprogs/bimba/extensions.kt index ba33c95679e8135e10ee722929fbff1cf5ec7813..c0c65137d2a374a988d325f51c17017b09e82339 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/extensions.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/extensions.kt @@ -5,6 +5,7 @@ import android.os.Build import ml.adamsprogs.bimba.activities.StopActivity import java.text.SimpleDateFormat import java.util.* +import kotlin.collections.ArrayList internal fun String.toPascalCase(): String { //check val builder = StringBuilder(this) @@ -62,4 +63,10 @@ Calendar.SUNDAY -> StopActivity.MODE_SUNDAYS Calendar.SATURDAY -> StopActivity.MODE_SATURDAYS else -> StopActivity.MODE_WORKDAYS } +} + +internal fun CharSequence.safeSplit(vararg delimiters: String, ignoreCase: Boolean = false, limit: Int = 0): List<String> { + if (this == "") + return ArrayList() + return this.split(*delimiters, ignoreCase = ignoreCase, limit = limit) } \ No newline at end of file diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt b/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt index 1d21bc607c0228596af6b0cd6f8f9c8931d77d27..86a52ddb1ff8c1ce1983ab602a96a72e7c7041b2 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt @@ -2,6 +2,7 @@ package ml.adamsprogs.bimba.models import ml.adamsprogs.bimba.rollTime import ml.adamsprogs.bimba.gtfs.AgencyAndId +import ml.adamsprogs.bimba.safeSplit import java.io.Serializable import java.util.* import kotlin.collections.ArrayList @@ -77,11 +78,7 @@ fun fromString(string: String): Departure { val array = string.split("|") if (array.size != 9) throw IllegalArgumentException() - val modification = - if (array[4] == "") - ArrayList() - else - array[4].split(";") + val modification = array[4].safeSplit(";") return Departure(AgencyAndId.convertFromString(array[0]), array[1].split(";").map { Integer.parseInt(it) }, Integer.parseInt(array[2]), array[3] == "true",