From 9d3c63cf62822331185a847f34f288adb433888d Mon Sep 17 00:00:00 2001 From: sgallese Date: Sat, 18 Sep 2021 23:05:48 -0700 Subject: [PATCH] Removes JVM dependencies from StringUtils See Issue #1075 --- .../org/isoron/uhabits/widgets/StackWidget.kt | 2 +- .../uhabits/widgets/StackWidgetService.kt | 2 +- .../org/isoron/platform/utils/StringUtils.kt | 30 ++++++++ .../isoron/platform/utils/StringUtilsTest.kt | 39 +++++++++++ .../isoron/uhabits/core/models/WeekdayList.kt | 8 +-- .../uhabits/core/preferences/Preferences.kt | 4 +- .../isoron/uhabits/core/utils/StringUtils.kt | 68 ------------------- .../uhabits/core/models/WeekdayListTest.kt | 14 ++++ 8 files changed, 88 insertions(+), 79 deletions(-) create mode 100644 uhabits-core/src/commonMain/kotlin/org/isoron/platform/utils/StringUtils.kt create mode 100644 uhabits-core/src/commonTest/kotlin/org/isoron/platform/utils/StringUtilsTest.kt delete mode 100644 uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/utils/StringUtils.kt diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StackWidget.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StackWidget.kt index deed570ac..fa6452a0e 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StackWidget.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StackWidget.kt @@ -26,8 +26,8 @@ import android.content.Intent import android.net.Uri import android.view.View import android.widget.RemoteViews +import org.isoron.platform.utils.StringUtils import org.isoron.uhabits.core.models.Habit -import org.isoron.uhabits.core.utils.StringUtils class StackWidget( context: Context, diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StackWidgetService.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StackWidgetService.kt index 0e60914e5..dc86b9b66 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StackWidgetService.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StackWidgetService.kt @@ -27,11 +27,11 @@ import android.util.Log import android.widget.RemoteViews import android.widget.RemoteViewsService import android.widget.RemoteViewsService.RemoteViewsFactory +import org.isoron.platform.utils.StringUtils.Companion.splitLongs import org.isoron.uhabits.HabitsApplication import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.HabitNotFoundException import org.isoron.uhabits.core.preferences.Preferences -import org.isoron.uhabits.core.utils.StringUtils.Companion.splitLongs import org.isoron.uhabits.utils.InterfaceUtils.dpToPixels import java.util.ArrayList diff --git a/uhabits-core/src/commonMain/kotlin/org/isoron/platform/utils/StringUtils.kt b/uhabits-core/src/commonMain/kotlin/org/isoron/platform/utils/StringUtils.kt new file mode 100644 index 000000000..8f3cf62d0 --- /dev/null +++ b/uhabits-core/src/commonMain/kotlin/org/isoron/platform/utils/StringUtils.kt @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.platform.utils + +class StringUtils { + + companion object { + + fun joinLongs(values: LongArray): String = values.joinToString(separator = ",") + + fun splitLongs(str: String): LongArray = str.split(",").map { it.toLong() }.toLongArray() + } +} diff --git a/uhabits-core/src/commonTest/kotlin/org/isoron/platform/utils/StringUtilsTest.kt b/uhabits-core/src/commonTest/kotlin/org/isoron/platform/utils/StringUtilsTest.kt new file mode 100644 index 000000000..a92219f5f --- /dev/null +++ b/uhabits-core/src/commonTest/kotlin/org/isoron/platform/utils/StringUtilsTest.kt @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.platform.utils + +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals + +class StringUtilsTest { + + @Test + fun testJoinLongs() { + val string = StringUtils.joinLongs(longArrayOf(0, 1, 2)) + assertEquals(string, "0,1,2") + } + + @Test + fun testSplitLongs() { + val longArray = StringUtils.splitLongs("0,1,2") + assertContentEquals(longArray, longArrayOf(0, 1, 2)) + } +} diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/WeekdayList.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/WeekdayList.kt index f104cb32b..55dfa3b9b 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/WeekdayList.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/WeekdayList.kt @@ -20,8 +20,6 @@ package org.isoron.uhabits.core.models import org.apache.commons.lang3.builder.EqualsBuilder import org.apache.commons.lang3.builder.HashCodeBuilder -import org.apache.commons.lang3.builder.ToStringBuilder -import org.isoron.uhabits.core.utils.StringUtils.Companion.defaultToStringStyle import java.util.Arrays class WeekdayList { @@ -71,11 +69,7 @@ class WeekdayList { return HashCodeBuilder(17, 37).append(weekdays).toHashCode() } - override fun toString(): String { - return ToStringBuilder(this, defaultToStringStyle()) - .append("weekdays", weekdays) - .toString() - } + override fun toString() = "{weekdays: [${weekdays.joinToString(separator = ",")}]}" companion object { val EVERY_DAY = WeekdayList(127) diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/preferences/Preferences.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/preferences/Preferences.kt index 66d255d3c..6e6f6f85a 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/preferences/Preferences.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/preferences/Preferences.kt @@ -19,12 +19,12 @@ package org.isoron.uhabits.core.preferences import org.isoron.platform.time.DayOfWeek +import org.isoron.platform.utils.StringUtils.Companion.joinLongs +import org.isoron.platform.utils.StringUtils.Companion.splitLongs import org.isoron.uhabits.core.models.HabitList import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.ui.ThemeSwitcher import org.isoron.uhabits.core.utils.DateUtils.Companion.getFirstWeekdayNumberAccordingToLocale -import org.isoron.uhabits.core.utils.StringUtils.Companion.joinLongs -import org.isoron.uhabits.core.utils.StringUtils.Companion.splitLongs import java.util.LinkedList import kotlin.math.max import kotlin.math.min diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/utils/StringUtils.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/utils/StringUtils.kt deleted file mode 100644 index a65cf4241..000000000 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/utils/StringUtils.kt +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2016-2021 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.core.utils - -import org.apache.commons.lang3.builder.StandardToStringStyle -import org.apache.commons.lang3.builder.ToStringStyle -import java.math.BigInteger -import java.util.Random - -class StringUtils { - - companion object { - private lateinit var toStringStyle: StandardToStringStyle - - @JvmStatic - fun getRandomId(): String { - return BigInteger(260, Random()).toString(32).subSequence(0, 32).toString() - } - - @JvmStatic - fun defaultToStringStyle(): ToStringStyle { - toStringStyle = StandardToStringStyle() - toStringStyle.apply { - fieldSeparator = ", " - isUseClassName = false - isUseIdentityHashCode = false - contentStart = "{" - contentEnd = "}" - fieldNameValueSeparator = ": " - arrayStart = "[" - arrayEnd = "]" - } - - return toStringStyle - } - - @JvmStatic - fun joinLongs(values: LongArray): String { - return org.apache.commons.lang3.StringUtils.join(values, ',') - } - - @JvmStatic - fun splitLongs(str: String): LongArray { - val parts: Array = org.apache.commons.lang3.StringUtils.split(str, ',') - return LongArray(parts.size) { - i -> - parts[i].toLong() - } - } - } -} diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/WeekdayListTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/WeekdayListTest.kt index 8bcde06fb..9d622d540 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/WeekdayListTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/WeekdayListTest.kt @@ -44,4 +44,18 @@ class WeekdayListTest : BaseUnitTest() { assertTrue(list.isEmpty) assertFalse(WeekdayList.EVERY_DAY.isEmpty) } + + @Test + fun testWeekdayList_IntConstructor_toString() { + val string = WeekdayList(0).toString() + assertThat(string, equalTo("{weekdays: [false,false,false,false,false,false,false]}")) + } + + @Test + fun testWeekdayList_BooleanArrayConstructor_toString() { + val string = WeekdayList( + booleanArrayOf(false, false, true, true, true, true, true) + ).toString() + assertThat(string, equalTo("{weekdays: [false,false,true,true,true,true,true]}")) + } }