-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tools.kt
217 lines (198 loc) · 5.14 KB
/
Tools.kt
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Multiple let. Allows you to use .let for few variables combinig its in vararg
* and if all of them is not null returning list of checked non-null values with same order.
*
* multiLet(a,b,c) { values ->
* print("a is not null and = ${values[0]}")
* print("b is not null and = ${values[1]}")
* print("c is not null and = ${values[2]}")
* }
*
* @property variables - vararg of input nullable variables
*/
inline fun <T: Any, R: Any> multiLet(vararg variables: T?, out: (List<T>) -> R?): R? {
return if (variables.all { it != null }) out (listOfNotNull(*variables)) else null
}
/**
* Returnng random element from input variables
*
* val randomLetter = randomFrom("a","b","c","d","e","f","g")
*
* @property variables
*/
fun <T> randomFrom(vararg variables: T): T {
return variables.random()
}
/**
* Allows to use when without else block like this:
*
* when(foo) {
* bar -> doAction()
* }.exhaustive
*/
val <T> T.exhaustive: T
get() = this
/**
* JUST FOR FUN!
* Because why not?
*
* replace for stadard if (condition) { action } constructon to more beautiful:
*
* condition.then {
* action
* }
*
* @property action - action that you want to do if condition is true
*
*/
fun Boolean.then(action: () -> Unit): Boolean {
if (this) action.invoke()
return this
}
/**
* JUST FOR FUN!
* Because why not?
*
* replace for stadard if (!condition) { action } constructon to more beautiful:
*
* condition.orNot {
* action
* }
*
* can be used with previous .then extension in chain like this:
*
* condition.then {
* action
* }.orNot {
* another action
* }
*
* @property action - action that you want to do if condition is false
*
*/
fun Boolean.orNot(action: () -> Unit): Boolean {
if (!this) action.invoke()
return this
}
/**
* Simple way to calculate approximated function execution time
*
* @property actionName - String to define action name in logcat
* @property action - action to measure
*/
fun benchmarkAction(actionName: String, action: () -> Unit) {
Log.i("BENCHMARK", "___________________________________")
Log.i("BENCHMARK", "Action name: $actionName")
val startTime = System.currentTimeMillis()
Log.i("BENCHMARK", "Start time: $startTime")
action.invoke()
val endTime = System.currentTimeMillis()
Log.i("BENCHMARK", "End time: $endTime")
Log.i("BENCHMARK", "Action duration (millis): ${endTime - startTime}}")
}
/**
* Getting approximated Lat/Lng degrees value from meters
*/
fun Double.metersToLatLonDegrees(): Double {
return this * 0.00001
}
/**
* Just for better reading
*/
fun <T>Collection<T>.hasOnlySingleItem(): Boolean {
return this.size == 1
}
/**
* Great thing if you wand to add some item to mutable list if it is not n this list now or
* remove this item, if list contains it in many parts of code
*
* @property element - element to adding or removing
*/
fun <T>MutableList<T>.addOrRemove(element: T) {
if (this.contains(element)) this.remove(element) else this.add(element)
}
fun <T> Collection<T>?.joinToStringOrNull(separator: CharSequence): String? {
return when {
(this == null || this.count() == 0) -> null
else -> {
var result = ""
this.forEachIndexed {index, item ->
result += if (index != this.size - 1) "$item," else "$item"
}
result
}
}
}
/**
* Like .first() but .second() =)
*/
fun <T> List<T>.second(): T? {
return when {
isEmpty() -> null
size < 2 -> null
else -> this[1]
}
}
/**
* Checking that all elements is equals
*
* @property values - vararg of checking elements
*
* allIsEqual("test", "test", "test") will return true
*/
fun <T>allIsEqual(vararg values: T): Boolean {
when {
values.isEmpty() -> return false
values.size == 1 -> return true
}
values.forEach {
if ((it == values.first()).not()) return false
}
return true
}
/**
* Executing out callback if all vararg values is not null
*
* Like .multilet , but not returning values
*
* @property values - vararg of checking elements
*
* val a = "a"
* val b = "b"
* val c = null
*
* allIsNotNull(a,b) {
* this action will be executed
* }
*
* allIsNotNull(a,b,c) {
* this action will NOT be executed
* }
*
*/
fun <T, R>allIsNotNull(vararg values: T, out: () -> R?): R? {
values.forEach {
if (it == null) return null
}
return out()
}
// Check contains ignoring case
fun List<String>.containsIgnoreCase(value: String?): Boolean {
this.forEach {
if (it.equals(value, true)) return true
}
return false
}
// Replace code like "someValue != null" with simple sugar extension
// with smart cast support by Kotlin Cotract
@OptIn(ExperimentalContracts::class)
fun <T> T?.notNull(): Boolean {
contract {
returns(true) implies (this@notNull != null)
}
return this != null
}
// Parsing enum by its name, can use dault value
inline fun <reified E : Enum<E>> parseOrDefault(name: String, default: E, ignoreCase: Boolean = true): E {
return enumValues<E>().firstOrNull { it.name.equals(name, ignoreCase) } ?: default
}