Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MiscUtils.kt « ical4android « bitfire « at « java « main « src - github.com/bitfireAT/ical4android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b0e57d2e9832dcec25ae1e55afd26d1849e77ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/***************************************************************************************************
 * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
 **************************************************************************************************/

package at.bitfire.ical4android

import android.content.ContentProviderClient
import android.content.ContentValues
import android.database.Cursor
import android.database.DatabaseUtils
import android.os.Build
import org.apache.commons.lang3.StringUtils
import java.lang.reflect.Modifier
import java.util.*

object MiscUtils {

    const val TOSTRING_MAXCHARS = 10000

    /**
     * Generates useful toString info (fields and values) from [obj] by reflection.
     *
     * @param obj   object to inspect
     * @return      string containing properties and non-static declared fields
     */
    fun reflectionToString(obj: Any): String {
        val s = LinkedList<String>()
        var clazz: Class<in Any>? = obj.javaClass
        while (clazz != null) {
            for (prop in clazz.declaredFields.filterNot { Modifier.isStatic(it.modifiers) }) {
                prop.isAccessible = true
                val valueStr = try {
                    StringUtils.abbreviate(prop.get(obj)?.toString(), TOSTRING_MAXCHARS)
                } catch(e: OutOfMemoryError) {
                    "![$e]"
                }
                s += "${prop.name}=" + valueStr
            }
            clazz = clazz.superclass
        }
        return "${obj.javaClass.simpleName}=[${s.joinToString(", ")}]"
    }

    /**
     * Removes empty [String] values from [values].
     *
     * @param values set of values to be modified
     * @return the modified object (which is the same object as passed in; for chaining)
     */
    fun removeEmptyStrings(values: ContentValues): ContentValues {
        val it = values.keySet().iterator()
        while (it.hasNext()) {
            val obj = values[it.next()]
            if (obj is String && obj.isEmpty())
                it.remove()
        }
        return values
    }


    object ContentProviderClientHelper {

        fun ContentProviderClient.closeCompat() {
            @Suppress("DEPRECATION")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                close()
            else
                release()
        }

    }


    object CursorHelper {

        /**
         * Returns the entire contents of the current row as a [ContentValues] object.
         *
         * @param  removeEmptyRows  whether rows with empty values should be removed
         * @return entire contents of the current row
         */
        fun Cursor.toValues(removeEmptyRows: Boolean = false): ContentValues {
            val values = ContentValues(columnCount)
            DatabaseUtils.cursorRowToContentValues(this, values)

            if (removeEmptyRows)
                removeEmptyStrings(values)

            return values
        }

    }

}