From ad071e48df67c4553de77f9a7dba378bb11e160e Mon Sep 17 00:00:00 2001 From: tarsin Date: Sat, 18 Feb 2023 12:02:36 +0800 Subject: [PATCH] cleanup: Remove AssertUtils --- .../java/com/hippo/ehviewer/Settings.java | 6 +- .../com/hippo/ehviewer/client/EhEngine.kt | 3 +- .../client/parser/FavoritesParser.java | 4 +- .../hippo/ehviewer/ui/scene/DownloadsScene.kt | 15 +-- .../hippo/ehviewer/ui/scene/FavoritesScene.kt | 12 +- .../ehviewer/ui/scene/GalleryDetailScene.kt | 9 +- .../hippo/ehviewer/ui/scene/ProgressScene.kt | 5 +- .../java/com/hippo/yorozuya/AssertError.java | 24 ---- .../com/hippo/yorozuya/AssertException.java | 24 ---- .../java/com/hippo/yorozuya/AssertUtils.kt | 123 +----------------- 10 files changed, 15 insertions(+), 210 deletions(-) delete mode 100644 app/src/main/java/com/hippo/yorozuya/AssertError.java delete mode 100644 app/src/main/java/com/hippo/yorozuya/AssertException.java diff --git a/app/src/main/java/com/hippo/ehviewer/Settings.java b/app/src/main/java/com/hippo/ehviewer/Settings.java index 4592c326e..50d71db4d 100755 --- a/app/src/main/java/com/hippo/ehviewer/Settings.java +++ b/app/src/main/java/com/hippo/ehviewer/Settings.java @@ -30,7 +30,7 @@ import com.hippo.ehviewer.client.data.FavListUrlBuilder; import com.hippo.ehviewer.ui.CommonOperations; import com.hippo.ehviewer.ui.scene.GalleryListScene; import com.hippo.unifile.UniFile; -import com.hippo.yorozuya.AssertUtils; +import com.hippo.yorozuya.AssertUtilsKt; import com.hippo.yorozuya.NumberUtils; import java.util.Locale; @@ -565,7 +565,7 @@ public class Settings { } public static void putFavCat(String[] value) { - AssertUtils.assertEquals(10, value.length); + AssertUtilsKt.assertEquals(10, value.length); sSettingsPre.edit() .putString(KEY_FAV_CAT_0, value[0]) .putString(KEY_FAV_CAT_1, value[1]) @@ -596,7 +596,7 @@ public class Settings { } public static void putFavCount(int[] count) { - AssertUtils.assertEquals(10, count.length); + AssertUtilsKt.assertEquals(10, count.length); sSettingsPre.edit() .putInt(KEY_FAV_COUNT_0, count[0]) .putInt(KEY_FAV_COUNT_1, count[1]) diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt index 2d47b8b69..05c44a1c3 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt @@ -48,7 +48,6 @@ import com.hippo.ehviewer.client.parser.VoteCommentParser import com.hippo.ehviewer.client.parser.VoteTagParser import com.hippo.network.StatusCodeException import com.hippo.util.ExceptionUtils -import com.hippo.yorozuya.AssertUtils import kotlinx.coroutines.async import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay @@ -560,7 +559,7 @@ object EhEngine { gidArray: LongArray, tokenArray: Array, dstCat: Int ): Void? { - AssertUtils.assertEquals(gidArray.size, tokenArray.size) + check(gidArray.size == tokenArray.size) var i = 0 val n = gidArray.size while (i < n) { diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java index 09450a73e..4a6866a3c 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java @@ -23,7 +23,7 @@ import com.hippo.ehviewer.client.exception.EhException; import com.hippo.ehviewer.client.exception.ParseException; import com.hippo.util.ExceptionUtils; import com.hippo.util.JsoupUtils; -import com.hippo.yorozuya.AssertUtils; +import com.hippo.yorozuya.AssertUtilsKt; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -46,7 +46,7 @@ public class FavoritesParser { //noinspection ConstantConditions Elements fps = ido.getElementsByClass("fp"); // Last one is "fp fps" - AssertUtils.assertEquals(11, fps.size()); + AssertUtilsKt.assertEquals(11, fps.size()); for (int i = 0; i < 10; i++) { Element fp = fps.get(i); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt index 3ead6a7fc..7918c4d11 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt @@ -70,7 +70,6 @@ import com.hippo.widget.FabLayout.OnClickFabListener import com.hippo.widget.FabLayout.OnExpandListener import com.hippo.widget.LoadImageView import com.hippo.widget.recyclerview.AutoStaggeredGridLayoutManager -import com.hippo.yorozuya.AssertUtils import com.hippo.yorozuya.FileUtils import com.hippo.yorozuya.ObjectUtils import com.hippo.yorozuya.ViewUtils @@ -152,8 +151,6 @@ class DownloadsScene : BaseToolbarScene(), DownloadInfoListener, OnClickFabListe override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val context = context - AssertUtils.assertNotNull(context) mDownloadManager = downloadManager mDownloadManager!!.addDownloadInfoListener(this) if (savedInstanceState == null) { @@ -249,10 +246,7 @@ class DownloadsScene : BaseToolbarScene(), DownloadInfoListener, OnClickFabListe container!!.addView(mFabLayout) mTip = ViewUtils.`$$`(view, R.id.tip) as TextView mViewTransition = ViewTransition(content, mTip) - val context = context - AssertUtils.assertNotNull(content) - val resources = context!!.resources - val drawable = ContextCompat.getDrawable(context, R.drawable.big_download) + val drawable = ContextCompat.getDrawable(requireContext(), R.drawable.big_download) drawable!!.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight) mTip!!.setCompoundDrawables(null, drawable, null, null) mAdapter = DownloadAdapter() @@ -422,8 +416,6 @@ class DownloadsScene : BaseToolbarScene(), DownloadInfoListener, OnClickFabListe container: ViewGroup?, savedInstanceState: Bundle? ): View { val view = inflater.inflate(R.layout.drawer_list_rv, container, false) - val context = context - AssertUtils.assertNotNull(context) val toolbar = view.findViewById(R.id.toolbar) toolbar.setTitle(R.string.download_labels) toolbar.inflateMenu(R.menu.drawer_download) @@ -431,7 +423,7 @@ class DownloadsScene : BaseToolbarScene(), DownloadInfoListener, OnClickFabListe val id = item.itemId if (id == R.id.action_add) { val builder = - EditTextDialogBuilder(context!!, null, getString(R.string.download_labels)) + EditTextDialogBuilder(requireContext(), null, getString(R.string.download_labels)) builder.setTitle(R.string.new_label_title) builder.setPositiveButton(android.R.string.ok, null) val dialog = builder.show() @@ -449,7 +441,7 @@ class DownloadsScene : BaseToolbarScene(), DownloadInfoListener, OnClickFabListe items[i + 2] = list[i].label i++ } - BaseDialogBuilder(context!!) + BaseDialogBuilder(requireContext()) .setTitle(R.string.default_download_label) .setItems(items) { _: DialogInterface?, which: Int -> if (which == 0) { @@ -994,7 +986,6 @@ class DownloadsScene : BaseToolbarScene(), DownloadInfoListener, OnClickFabListe private val mListThumbHeight: Int init { - AssertUtils.assertNotNull(mInflater) @SuppressLint("InflateParams") val calculator = mInflater.inflate(R.layout.item_gallery_list_thumb_height, null) ViewUtils.measureView(calculator, 1024, ViewGroup.LayoutParams.WRAP_CONTENT) diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt index 30d784c4c..06c56b778 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt @@ -63,7 +63,6 @@ import com.hippo.widget.ContentLayout import com.hippo.widget.FabLayout import com.hippo.widget.FabLayout.OnClickFabListener import com.hippo.widget.FabLayout.OnExpandListener -import com.hippo.yorozuya.AssertUtils import com.hippo.yorozuya.ObjectUtils import com.hippo.yorozuya.SimpleHandler import com.hippo.yorozuya.ViewUtils @@ -132,8 +131,6 @@ class FavoritesScene : SearchBarScene(), OnDragHandlerListener, OnClickFabListen private var mModifyAdd = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val context = context - AssertUtils.assertNotNull(context) mClient = EhClient mFavLocalCount = Settings.getFavLocalCount() mFavCountSum = Settings.getFavCloudCount() @@ -186,7 +183,6 @@ class FavoritesScene : SearchBarScene(), OnDragHandlerListener, OnClickFabListen val view = inflater.inflate(R.layout.scene_favorites, container, false) val mContentLayout = view.findViewById(R.id.content_layout) val activity = mainActivity!! - AssertUtils.assertNotNull(activity) setOnApplySearch { query: String? -> onApplySearch(query) } @@ -196,7 +192,6 @@ class FavoritesScene : SearchBarScene(), OnDragHandlerListener, OnClickFabListen mFabLayout = ViewUtils.`$$`(view, R.id.fab_layout) as FabLayout mFabLayout!!.addOnExpandListener(FabLayoutListener()) (mFabLayout!!.parent as ViewGroup).removeView(mFabLayout) - AssertUtils.assertNotNull(container) container!!.addView(mFabLayout) ViewCompat.setWindowInsetsAnimationCallback( view, WindowInsetsAnimationHelper( @@ -204,9 +199,6 @@ class FavoritesScene : SearchBarScene(), OnDragHandlerListener, OnClickFabListen mFabLayout ) ) - val context = context - AssertUtils.assertNotNull(context) - val resources = context!!.resources val paddingTopSB = resources.getDimensionPixelOffset(R.dimen.gallery_padding_top_search_bar) mHelper = FavoritesHelper() mHelper!!.setEmptyString(resources.getString(R.string.gallery_list_empty_hit)) @@ -332,9 +324,7 @@ class FavoritesScene : SearchBarScene(), OnDragHandlerListener, OnClickFabListen container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.drawer_list_rv, container, false) - val context = context val toolbar = ViewUtils.`$$`(view, R.id.toolbar) as Toolbar - AssertUtils.assertNotNull(context) toolbar.setTitle(R.string.collections) toolbar.inflateMenu(R.menu.drawer_favorites) toolbar.setOnMenuItemClickListener { item: MenuItem -> @@ -345,7 +335,7 @@ class FavoritesScene : SearchBarScene(), OnDragHandlerListener, OnClickFabListen items[1] = getString(R.string.local_favorites) val favCat = Settings.getFavCat() System.arraycopy(favCat, 0, items, 2, 10) - BaseDialogBuilder(context!!) + BaseDialogBuilder(requireContext()) .setTitle(R.string.default_favorites_collection) .setItems(items) { _: DialogInterface?, which: Int -> Settings.putDefaultFavSlot( diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt index 9a56973d1..f540071ad 100755 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt @@ -106,7 +106,6 @@ import com.hippo.widget.AutoWrapLayout import com.hippo.widget.LoadImageView import com.hippo.widget.ObservedTextView import com.hippo.widget.SimpleGridAutoSpanLayout -import com.hippo.yorozuya.AssertUtils import com.hippo.yorozuya.FileUtils import com.hippo.yorozuya.IntIdGenerator import com.hippo.yorozuya.ViewUtils @@ -363,8 +362,6 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl // Get download state val gid = gid mDownloadState = if (gid != -1L) { - val context = context - AssertUtils.assertNotNull(context) downloadManager.getDownloadState(gid) } else { DownloadInfo.STATE_INVALID @@ -431,8 +428,6 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl super.onDestroyView() spiderQueen?.let { SpiderQueen.releaseSpiderQueen(it, SpiderQueen.MODE_READ) } spiderQueen = null - val context = context - AssertUtils.assertNotNull(context) downloadManager.removeDownloadInfoListener(this) (requireActivity() as MainActivity).mShareUrl = null mViewTransition = null @@ -441,8 +436,6 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl } private fun prepareData(): Boolean { - val context = context - AssertUtils.assertNotNull(context) if (mGalleryDetail != null) { return true } @@ -456,7 +449,7 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl if (mGalleryDetail != null) { return true } - val application = context!!.applicationContext as EhApplication + val application = requireContext().applicationContext as EhApplication return if (application.containGlobalStuff(mRequestId)) { // request exist true diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt index f1a6e82ee..7e15f6230 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt @@ -28,7 +28,6 @@ import com.hippo.ehviewer.client.EhClient import com.hippo.ehviewer.client.EhRequest import com.hippo.util.ExceptionUtils import com.hippo.view.ViewTransition -import com.hippo.yorozuya.AssertUtils import com.hippo.yorozuya.ViewUtils /** @@ -130,9 +129,7 @@ class ProgressScene : BaseScene(), View.OnClickListener { val view = inflater.inflate(R.layout.scene_progress, container, false) val progress = ViewUtils.`$$`(view, R.id.progress) mTip = ViewUtils.`$$`(view, R.id.tip) as TextView - val context = context - AssertUtils.assertNotNull(context) - val drawable = ContextCompat.getDrawable(context!!, R.drawable.big_sad_pandroid) + val drawable = ContextCompat.getDrawable(requireContext(), R.drawable.big_sad_pandroid) drawable!!.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight) mTip!!.setCompoundDrawables(null, drawable, null, null) mTip!!.setOnClickListener(this) diff --git a/app/src/main/java/com/hippo/yorozuya/AssertError.java b/app/src/main/java/com/hippo/yorozuya/AssertError.java deleted file mode 100644 index d5e81cd90..000000000 --- a/app/src/main/java/com/hippo/yorozuya/AssertError.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2015 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.yorozuya; - -public class AssertError extends Error { - - public AssertError(String detailMessage) { - super(detailMessage); - } -} diff --git a/app/src/main/java/com/hippo/yorozuya/AssertException.java b/app/src/main/java/com/hippo/yorozuya/AssertException.java deleted file mode 100644 index fd6ace998..000000000 --- a/app/src/main/java/com/hippo/yorozuya/AssertException.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2015 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.yorozuya; - -public class AssertException extends Exception { - - public AssertException(String detailMessage) { - super(detailMessage); - } -} diff --git a/app/src/main/java/com/hippo/yorozuya/AssertUtils.kt b/app/src/main/java/com/hippo/yorozuya/AssertUtils.kt index b8b185748..184618fd7 100644 --- a/app/src/main/java/com/hippo/yorozuya/AssertUtils.kt +++ b/app/src/main/java/com/hippo/yorozuya/AssertUtils.kt @@ -1,122 +1,5 @@ -/* - * Copyright (C) 2015 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +package com.hippo.yorozuya -package com.hippo.yorozuya; - -public final class AssertUtils { - private AssertUtils() { - } - - public static void assertTrue(boolean cond) { - assertTrue("Condition has to be true.", cond); - } - - public static void assertTrueEx(boolean cond) throws AssertException { - assertTrueEx("Condition has to be true.", cond); - } - - public static void assertTrue(String message, boolean cond) { - if (!cond) { - throw new AssertError(message); - } - } - - public static void assertTrueEx(String message, boolean cond) throws AssertException { - if (!cond) { - throw new AssertException(message); - } - } - - public static void assertNull(Object object) { - assertNull("Should be null", object); - } - - public static void assertNullEx(Object object) throws AssertException { - assertNullEx("Should be null", object); - } - - public static void assertNull(String message, Object object) { - if (object != null) { - throw new AssertError(message); - } - } - - public static void assertNullEx(String message, Object object) throws AssertException { - if (object != null) { - throw new AssertException(message); - } - } - - public static void assertNotNull(Object object) { - assertNotNull("Should not be null", object); - } - - public static void assertNotNullEx(Object object) throws AssertException { - assertNotNullEx("Should not be null", object); - } - - public static void assertNotNull(String message, Object object) { - if (object == null) { - throw new AssertError(message); - } - } - - public static void assertNotNullEx(String message, Object object) throws AssertException { - if (object == null) { - throw new AssertException(message); - } - } - - public static void assertEquals(int expected, int actual) { - assertEquals("Should be " + expected + ", but it is " + actual, expected, actual); - } - - public static void assertEqualsEx(int expected, int actual) throws AssertException { - assertEqualsEx("Should be " + expected + ", but it is " + actual, expected, actual); - } - - public static void assertEquals(String message, int expected, int actual) { - if (expected != actual) { - throw new AssertError(message); - } - } - - public static void assertEqualsEx(String message, int expected, int actual) throws AssertException { - if (expected != actual) { - throw new AssertException(message); - } - } - - public static void assertInstanceOf(Object obj, Class clazz) { - assertInstanceOf("The object should be instance of " + clazz.getName(), obj, clazz); - } - - public static void assertInstanceOfEx(Object obj, Class clazz) throws AssertException { - assertInstanceOfEx("The object should be instance of " + clazz.getName(), obj, clazz); - } - - public static void assertInstanceOf(String message, Object obj, Class clazz) { - if (!clazz.isInstance(obj)) { - throw new AssertError(message); - } - } - - public static void assertInstanceOfEx(String message, Object obj, Class clazz) throws AssertException { - if (!clazz.isInstance(obj)) { - throw new AssertException(message); - } - } +fun assertEquals(expected: Int, actual: Int) { + check(expected == actual) }