Drop NumberUtils
This commit is contained in:
1 parent
9be7705050
commit
839d074df1
13 files changed
+54
-133
No files matched your search
@@ -23,7 +23,7 @@ import com.hippo.ehviewer.dao.QuickSearch
|
||||
import com.hippo.ehviewer.network.UrlBuilder
|
||||
import com.hippo.ehviewer.ui.legacy.AdvanceSearchTable
|
||||
import com.hippo.ehviewer.util.encodeUTF8
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils
|
||||
import com.hippo.ehviewer.yorozuya.toIntOrDefault
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import java.io.UnsupportedEncodingException
|
||||
import java.net.URLDecoder
|
||||
@@ -158,7 +158,7 @@ data class ListUrlBuilder(
|
||||
val value = str.substring(index + 1)
|
||||
when (key) {
|
||||
"f_cats" -> {
|
||||
val cats = NumberUtils.parseIntSafely(value, EhUtils.ALL_CATEGORY)
|
||||
val cats = value.toIntOrDefault(EhUtils.ALL_CATEGORY)
|
||||
category = category or (cats.inv() and EhUtils.ALL_CATEGORY)
|
||||
}
|
||||
|
||||
@@ -237,13 +237,13 @@ data class ListUrlBuilder(
|
||||
enableMinRating = true
|
||||
}
|
||||
|
||||
"f_srdd" -> minRating = NumberUtils.parseIntSafely(value, -1)
|
||||
"f_srdd" -> minRating = value.toIntOrDefault(-1)
|
||||
"f_sp" -> if ("on" == value) {
|
||||
enablePage = true
|
||||
}
|
||||
|
||||
"f_spf" -> pageFrom = NumberUtils.parseIntSafely(value, -1)
|
||||
"f_spt" -> pageTo = NumberUtils.parseIntSafely(value, -1)
|
||||
"f_spf" -> pageFrom = value.toIntOrDefault(-1)
|
||||
"f_spt" -> pageTo = value.toIntOrDefault(-1)
|
||||
"f_shash" -> hash = value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,8 @@ import com.hippo.ehviewer.client.EhUtils.getCategory
|
||||
import com.hippo.ehviewer.client.EhUtils.handleThumbUrlResolution
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.getThumbKey
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils
|
||||
import com.hippo.ehviewer.yorozuya.toFloatOrDefault
|
||||
import com.hippo.ehviewer.yorozuya.toIntOrDefault
|
||||
import org.json.JSONObject
|
||||
|
||||
object GalleryApiParser {
|
||||
@@ -37,11 +38,12 @@ object GalleryApiParser {
|
||||
gi.uploader = g.getString("uploader")
|
||||
gi.posted =
|
||||
ParserUtils.formatDate(ParserUtils.parseLong(g.getString("posted"), 0) * 1000)
|
||||
gi.rating = NumberUtils.parseFloatSafely(g.getString("rating"), 0.0f)
|
||||
gi.rating = g.getString("rating").toFloatOrDefault(0.0f)
|
||||
// tags
|
||||
val tagJa = g.getJSONArray("tags")
|
||||
gi.simpleTags = (0 until tagJa.length()).map { tagJa.getString(it) }.let { arrayListOf<String>().apply { addAll(it) } }
|
||||
gi.pages = NumberUtils.parseIntSafely(g.getString("filecount"), 0)
|
||||
gi.simpleTags = (0 until tagJa.length()).map { tagJa.getString(it) }
|
||||
.let { arrayListOf<String>().apply { addAll(it) } }
|
||||
gi.pages = g.getString("filecount").toIntOrDefault(0)
|
||||
gi.generateSLang()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,8 @@ import com.hippo.ehviewer.client.exception.ParseException
|
||||
import com.hippo.ehviewer.client.exception.PiningException
|
||||
import com.hippo.ehviewer.client.getThumbKey
|
||||
import com.hippo.ehviewer.util.ExceptionUtils
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils
|
||||
import com.hippo.ehviewer.yorozuya.toFloatOrDefault
|
||||
import com.hippo.ehviewer.yorozuya.toIntOrDefault
|
||||
import com.hippo.ehviewer.yorozuya.trimAnd
|
||||
import com.hippo.ehviewer.yorozuya.unescapeXml
|
||||
import org.jsoup.Jsoup
|
||||
@@ -174,10 +175,7 @@ object GalleryDetailParser {
|
||||
// Rating count
|
||||
val ratingCount = gm.getElementById("rating_count")
|
||||
if (null != ratingCount) {
|
||||
gd.ratingCount = NumberUtils.parseIntSafely(
|
||||
ratingCount.text().trim(),
|
||||
0,
|
||||
)
|
||||
gd.ratingCount = ratingCount.text().trim().toIntOrDefault(0)
|
||||
} else {
|
||||
gd.ratingCount = 0
|
||||
}
|
||||
@@ -193,7 +191,7 @@ object GalleryDetailParser {
|
||||
if (index == -1 || index >= ratingStr.length) {
|
||||
gd.rating = 0f
|
||||
} else {
|
||||
gd.rating = NumberUtils.parseFloatSafely(ratingStr.substring(index + 1), 0f)
|
||||
gd.rating = ratingStr.substring(index + 1).toFloatOrDefault(0f)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -263,7 +261,7 @@ object GalleryDetailParser {
|
||||
} else if (key.startsWith("Length")) {
|
||||
val index = value.indexOf(' ')
|
||||
if (index >= 0) {
|
||||
gd.pages = NumberUtils.parseIntSafely(value.substring(0, index), 1)
|
||||
gd.pages = value.substring(0, index).toIntOrDefault(1)
|
||||
} else {
|
||||
gd.pages = 1
|
||||
}
|
||||
@@ -276,7 +274,7 @@ object GalleryDetailParser {
|
||||
if (index == -1) {
|
||||
gd.favoriteCount = 0
|
||||
} else {
|
||||
gd.favoriteCount = NumberUtils.parseIntSafely(value.substring(0, index), 0)
|
||||
gd.favoriteCount = value.substring(0, index).toIntOrDefault(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -375,10 +373,7 @@ object GalleryDetailParser {
|
||||
if (null != c5) {
|
||||
val es = c5.children()
|
||||
if (!es.isEmpty()) {
|
||||
comment.score = NumberUtils.parseIntSafely(
|
||||
es[0].text().trim(),
|
||||
0,
|
||||
)
|
||||
comment.score = es[0].text().trim().toIntOrDefault(0)
|
||||
}
|
||||
}
|
||||
// time
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils
|
||||
import com.hippo.ehviewer.yorozuya.toLongOrDefault
|
||||
import java.util.regex.Pattern
|
||||
|
||||
/**
|
||||
@@ -33,7 +33,7 @@ object GalleryDetailUrlParser {
|
||||
val pattern = if (strict) URL_STRICT_PATTERN else URL_PATTERN
|
||||
val m = pattern.matcher(url)
|
||||
return if (m.find()) {
|
||||
val gid = NumberUtils.parseLongSafely(m.group(1), 0).takeIf { it > 0 }
|
||||
val gid = m.group(1)!!.toLongOrDefault(0).takeIf { it > 0 }
|
||||
gid?.let { Result(it, m.group(2)!!) }
|
||||
} else {
|
||||
null
|
||||
|
||||
@@ -21,7 +21,7 @@ import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
import com.hippo.ehviewer.util.ExceptionUtils
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils
|
||||
import com.hippo.ehviewer.yorozuya.toIntOrDefault
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Document
|
||||
import splitties.init.appCtx
|
||||
@@ -71,7 +71,7 @@ object GalleryListParser {
|
||||
val href = e.attr("href")
|
||||
val matcher = PATTERN_NEXT_PAGE.matcher(href)
|
||||
if (matcher.find()) {
|
||||
result.nextPage = NumberUtils.parseIntSafely(matcher.group(1), 0)
|
||||
result.nextPage = matcher.group(1)!!.toIntOrDefault(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils
|
||||
import com.hippo.ehviewer.yorozuya.toIntOrDefault
|
||||
import com.hippo.ehviewer.yorozuya.toLongOrDefault
|
||||
import java.util.regex.Pattern
|
||||
|
||||
/**
|
||||
@@ -33,9 +34,9 @@ object GalleryPageUrlParser {
|
||||
val pattern = if (strict) URL_STRICT_PATTERN else URL_PATTERN
|
||||
val m = pattern.matcher(url)
|
||||
return if (m.find()) {
|
||||
val gid = NumberUtils.parseLongSafely(m.group(2), -1L)
|
||||
val gid = m.group(2)!!.toLongOrDefault(-1L)
|
||||
val pToken = m.group(1)!!
|
||||
val page = NumberUtils.parseIntSafely(m.group(3), 0) - 1
|
||||
val page = m.group(3)!!.toIntOrDefault(0) - 1
|
||||
if (gid < 0 || page < 0) {
|
||||
null
|
||||
} else {
|
||||
|
||||
@@ -15,7 +15,8 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils
|
||||
import com.hippo.ehviewer.yorozuya.toIntOrDefault
|
||||
import com.hippo.ehviewer.yorozuya.toLongOrDefault
|
||||
import com.hippo.ehviewer.yorozuya.unescapeXml
|
||||
import java.time.Instant
|
||||
import java.time.ZoneOffset
|
||||
@@ -36,10 +37,10 @@ object ParserUtils {
|
||||
}
|
||||
|
||||
fun parseInt(str: String?, defValue: Int): Int {
|
||||
return NumberUtils.parseIntSafely(trim(str).replace(",", ""), defValue)
|
||||
return trim(str).replace(",", "").toIntOrDefault(defValue)
|
||||
}
|
||||
|
||||
fun parseLong(str: String?, defValue: Long): Long {
|
||||
return NumberUtils.parseLongSafely(trim(str).replace(",", ""), defValue)
|
||||
return trim(str).replace(",", "").toLongOrDefault(defValue)
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils
|
||||
import com.hippo.ehviewer.yorozuya.toIntOrDefault
|
||||
|
||||
class AdvanceSearchTable @JvmOverloads constructor(
|
||||
context: Context,
|
||||
@@ -90,11 +90,11 @@ class AdvanceSearchTable @JvmOverloads constructor(
|
||||
return advanceSearch
|
||||
}
|
||||
set(advanceSearch) {
|
||||
mSh.isChecked = NumberUtils.int2boolean(advanceSearch and SH)
|
||||
mSto.isChecked = NumberUtils.int2boolean(advanceSearch and STO)
|
||||
mSfl.isChecked = NumberUtils.int2boolean(advanceSearch and SFL)
|
||||
mSfu.isChecked = NumberUtils.int2boolean(advanceSearch and SFU)
|
||||
mSft.isChecked = NumberUtils.int2boolean(advanceSearch and SFT)
|
||||
mSh.isChecked = advanceSearch and SH != 0
|
||||
mSto.isChecked = advanceSearch and STO != 0
|
||||
mSfl.isChecked = advanceSearch and SFL != 0
|
||||
mSfu.isChecked = advanceSearch and SFU != 0
|
||||
mSft.isChecked = advanceSearch and SFT != 0
|
||||
}
|
||||
var minRating: Int
|
||||
get() {
|
||||
@@ -113,7 +113,7 @@ class AdvanceSearchTable @JvmOverloads constructor(
|
||||
}
|
||||
var pageFrom: Int
|
||||
get() = if (mSp.isChecked) {
|
||||
NumberUtils.parseIntSafely(mSpf.text.toString(), -1)
|
||||
mSpf.text.toString().toIntOrDefault(-1)
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
@@ -128,7 +128,7 @@ class AdvanceSearchTable @JvmOverloads constructor(
|
||||
}
|
||||
var pageTo: Int
|
||||
get() = if (mSp.isChecked) {
|
||||
NumberUtils.parseIntSafely(mSpt.text.toString(), -1)
|
||||
mSpt.text.toString().toIntOrDefault(-1)
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
|
||||
@@ -28,8 +28,6 @@ import android.widget.Checkable
|
||||
import androidx.collection.LongSparseArray
|
||||
import androidx.core.os.ParcelCompat.readParcelable
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils.boolean2int
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils.int2boolean
|
||||
|
||||
/**
|
||||
* Add setChoiceMode for RecyclerView
|
||||
@@ -513,7 +511,7 @@ open class EasyRecyclerView : RecyclerView {
|
||||
readParcelable(`in`, RecyclerView::class.java.classLoader, Parcelable::class.java)
|
||||
this.superState = superState ?: EMPTY_STATE
|
||||
choiceMode = `in`.readInt()
|
||||
customChoice = int2boolean(`in`.readInt())
|
||||
customChoice = `in`.readInt() != 0
|
||||
checkedItemCount = `in`.readInt()
|
||||
checkState = `in`.readSparseBooleanArray()
|
||||
val n = `in`.readInt()
|
||||
@@ -534,7 +532,7 @@ open class EasyRecyclerView : RecyclerView {
|
||||
override fun writeToParcel(out: Parcel, flags: Int) {
|
||||
out.writeParcelable(superState, flags)
|
||||
out.writeInt(choiceMode)
|
||||
out.writeInt(boolean2int(customChoice))
|
||||
out.writeInt(customChoice.compareTo(false))
|
||||
out.writeInt(checkedItemCount)
|
||||
out.writeSparseBooleanArray(checkState)
|
||||
val n = if (checkIdState != null) checkIdState!!.size() else 0
|
||||
|
||||
@@ -42,7 +42,6 @@ import com.hippo.ehviewer.client.exception.EhException
|
||||
import com.hippo.ehviewer.databinding.SearchAdvanceBinding
|
||||
import com.hippo.ehviewer.databinding.SearchCategoryBinding
|
||||
import com.hippo.ehviewer.databinding.SearchNormalBinding
|
||||
import com.hippo.ehviewer.yorozuya.NumberUtils
|
||||
import com.hippo.ehviewer.yorozuya.ViewUtils
|
||||
|
||||
@SuppressLint("InflateParams")
|
||||
@@ -97,7 +96,7 @@ class SearchLayout @JvmOverloads constructor(
|
||||
mChip.isCheckable = true
|
||||
mChip.setText(mPair.second)
|
||||
mChip.idt = mPair.first
|
||||
mChip.isChecked = NumberUtils.int2boolean(mPair.first and mCategoryStored)
|
||||
mChip.isChecked = mPair.first and mCategoryStored != 0
|
||||
mChip.setOnLongClickListener {
|
||||
if (mChip.isChecked) {
|
||||
binding.searchCategoryChipgroup.forEach {
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 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.ehviewer.yorozuya;
|
||||
|
||||
public final class NumberUtils {
|
||||
private NumberUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 0 for false, Non 0 for true
|
||||
*
|
||||
* @param integer the int
|
||||
* @return the boolean
|
||||
*/
|
||||
public static boolean int2boolean(int integer) {
|
||||
return integer != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* false for 0, true for 1
|
||||
*
|
||||
* @param bool the boolean
|
||||
* @return the int
|
||||
*/
|
||||
public static int boolean2int(boolean bool) {
|
||||
return bool ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not throw NumberFormatException, use default value
|
||||
*
|
||||
* @param str the string to be parsed
|
||||
* @param defaultValue the value to return when get error
|
||||
* @return the value of the string
|
||||
*/
|
||||
public static int parseIntSafely(String str, int defaultValue) {
|
||||
try {
|
||||
return Integer.parseInt(str);
|
||||
} catch (Throwable e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not throw NumberFormatException, use default value
|
||||
*
|
||||
* @param str the string to be parsed
|
||||
* @param defaultValue the value to return when get error
|
||||
* @return the value of the string
|
||||
*/
|
||||
public static long parseLongSafely(String str, long defaultValue) {
|
||||
try {
|
||||
return Long.parseLong(str);
|
||||
} catch (Throwable e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not throw NumberFormatException, use default value
|
||||
*
|
||||
* @param str the string to be parsed
|
||||
* @param defaultValue the value to return when get error
|
||||
* @return the value of the string
|
||||
*/
|
||||
public static float parseFloatSafely(String str, float defaultValue) {
|
||||
try {
|
||||
return Float.parseFloat(str);
|
||||
} catch (Throwable e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ object OSUtils {
|
||||
while (reader.readLine().also { line = it } != null) {
|
||||
val matcher = PROCFS_MEMFILE_FORMAT.matcher(line!!)
|
||||
if (matcher.find() && MEMTOTAL_STRING == matcher.group(1)) {
|
||||
var mem = NumberUtils.parseLongSafely(matcher.group(2)!!, -1L)
|
||||
var mem = matcher.group(2)!!.toLongOrDefault(-1L)
|
||||
if (mem != -1L) {
|
||||
mem *= 1024
|
||||
}
|
||||
|
||||
@@ -9,3 +9,15 @@ fun String.unescapeXml(): String {
|
||||
inline infix fun <T> CharSequence.trimAnd(block: CharSequence.() -> T): T {
|
||||
return block(trim())
|
||||
}
|
||||
|
||||
fun String.toIntOrDefault(defaultValue: Int): Int {
|
||||
return toIntOrNull() ?: defaultValue
|
||||
}
|
||||
|
||||
fun String.toLongOrDefault(defaultValue: Long): Long {
|
||||
return toLongOrNull() ?: defaultValue
|
||||
}
|
||||
|
||||
fun String.toFloatOrDefault(defaultValue: Float): Float {
|
||||
return toFloatOrNull() ?: defaultValue
|
||||
}
|
||||
Reference in new issue
Block a user