Skip to content

Commit d01deda

Browse files
authored
Merge pull request #4 from eadm/model/1.0.4
Model/1.0.4
2 parents 430cdd9 + 9581f10 commit d01deda

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Model/src/main/java/ru/nobird/android/core/model/CollectionExtensions.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,38 @@ fun <K, V> MutableMap<K, V>.putNullable(key: K, value: V?) {
113113
if (value != null) {
114114
put(key, value)
115115
}
116-
}
116+
}
117+
118+
/**
119+
* Creates map from [pairs] where value is not null
120+
*/
121+
fun <K, V : Any> mapOfNotNull(vararg pairs: Pair<K, V?>): Map<K, V> =
122+
pairs
123+
.mapNotNull { (first, second) ->
124+
second?.let { first to it }
125+
}
126+
.toMap()
127+
128+
/**
129+
* Returns true if first elements of current list equals to ones from [other]
130+
*/
131+
fun <T> List<T>.startsWith(other: List<T>): Boolean {
132+
if (size < other.size) return false
133+
134+
for (i in 0 until minOf(size, other.size)) {
135+
if (this[i] != other[i]) return false
136+
}
137+
return true
138+
}
139+
140+
/**
141+
* Casts this to [T]
142+
*/
143+
inline fun <reified T> Any?.cast(): T =
144+
this as T
145+
146+
/**
147+
* Casts this to [T] or returns null if cast cannot be performed
148+
*/
149+
inline fun <reified T> Any?.safeCast(): T? =
150+
this as? T

dependencies.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ext.versions = [
1919
bintray : '1.8.4',
2020
kotlin : '1.3.70',
2121

22-
coreModel : '1.0.3',
22+
coreModel : '1.0.4',
2323

2424
androidx : '1.0.0',
2525
material : '1.0.0',

0 commit comments

Comments
 (0)