Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/ktextension/CursorExtensions.kt

106 lines
2.6 KiB
Kotlin
Raw Normal View History

2016-06-29 15:47:52 +02:00
package org.mariotaku.ktextension
import android.database.Cursor
2016-12-29 06:50:18 +01:00
import org.mariotaku.library.objectcursor.ObjectCursor
import java.util.*
2016-06-29 15:47:52 +02:00
/**
* Created by mariotaku on 16/6/29.
*/
fun Cursor.safeMoveToPosition(pos: Int): Boolean {
try {
return moveToPosition(pos)
} catch(e: IllegalStateException) {
return false
}
2016-12-29 06:50:18 +01:00
}
2017-03-23 15:22:07 +01:00
fun Cursor.safeGetLong(columnIndex: Int, def: Long = -1): Long {
try {
return getLong(columnIndex)
} catch(e: IllegalStateException) {
return def
}
}
fun Cursor.safeGetInt(columnIndex: Int, def: Int = -1): Int {
try {
return getInt(columnIndex)
} catch(e: IllegalStateException) {
return def
}
}
2017-07-07 10:01:59 +02:00
fun Cursor.safeGetString(columnIndex: Int, def: String = ""): String {
try {
return getString(columnIndex)
} catch(e: IllegalStateException) {
return def
}
}
2016-12-29 06:50:18 +01:00
fun <T> Cursor.map(indices: ObjectCursor.CursorIndices<T>): List<T> {
val list = ArrayList<T>()
moveToFirst()
while (!isAfterLast) {
list.add(indices.newObject(this))
moveToNext()
}
return list
2017-01-05 14:08:49 +01:00
}
/**
* Executes the given [block] function on this resource and then closes it down correctly whether an exception
* is thrown or not.
*
* @param block a function to process this closable resource.
* @return the result of [block] function on this closable resource.
*/
inline fun <R> Cursor.useCursor(block: (Cursor) -> R): R {
var closed = false
try {
return block(this)
} catch (e: Exception) {
closed = true
try {
2017-02-04 08:39:08 +01:00
this.close()
2017-01-05 14:08:49 +01:00
} catch (closeException: Exception) {
// eat the closeException as we are already throwing the original cause
// and we don't want to mask the real exception
// TODO on Java 7 we should call
// e.addSuppressed(closeException)
// to work like try-with-resources
// http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html#suppressed-exceptions
}
throw e
} finally {
if (!closed) {
2017-02-04 08:39:08 +01:00
this.close()
2017-01-05 14:08:49 +01:00
}
}
}
2017-01-12 17:26:44 +01:00
val Cursor.isEmpty: Boolean
get() = count == 0
/**
* @param limit -1 for no limit
* @return Remaining count, -1 if no rows present
*/
inline fun Cursor.forEachRow(limit: Int = -1, action: (cur: Cursor, pos: Int) -> Boolean): Int {
moveToFirst()
var current = 0
while (!isAfterLast) {
@Suppress("ConvertTwoComparisonsToRangeCheck")
if (limit >= 0 && current >= limit) break
if (action(this, current)) {
current++
}
moveToNext()
}
return count - position
}