You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+31-7
Original file line number
Diff line number
Diff line change
@@ -95,6 +95,25 @@ nullableObject.whatIfNotNull(
95
95
)
96
96
```
97
97
98
+
### WhatIfNotNullAs
99
+
`WhatIfNotNullAs` is an expression for invoking `whatIf` lambda when the target object is not null. If the target is not null, the receiver will get a desired casted type.
100
+
101
+
```kotlin
102
+
parcelable.whatIfNotNullAs<Poster> { poster ->
103
+
log(poster.name)
104
+
}
105
+
```
106
+
And we can also handle the null case.
107
+
108
+
```kotlin
109
+
serializable.whatIfNotNullAs<Poster>(
110
+
whatIf = { poster -> log(poster.name) },
111
+
whatIfNot = {
112
+
// do something
113
+
}
114
+
)
115
+
```
116
+
98
117
### WhatIfNotNullOrEmpty
99
118
An expression for invoking `whatIf` lambda when the __string__, __collections__ and __array type__ is not null and not empty.<br>
100
119
If the collections or array type target is null or empty, `whatIfNot` will be invoked instead of the `whatIf`.
We can use some expressions for List, Map, and Set.
151
+
- whatIfNotNullOrEmpty: An expression for invoking `whatIf` when the `List` is not null and not empty.
152
+
- addWhatIfNotNull: An expression for adding an element and invoking `whatIf` when the element is not null.
153
+
- addAllWhatIfNotNull: An expression for adding an element and invoking `whatIf` when the element is not null.
154
+
- removeWhatIfNotNull: An expression for removing an element and invoking `whatIf` when the element is not null.
155
+
- removeAllWhatIfNotNull: An expression for removing a collection of element and invoking `whatIf` when the element is not null.
156
+
132
157
133
158
### WhatIfMap
134
-
The basic concept is the same as `whatIf` but it is useful when the receiver and the result should be different.<br>
159
+
The basic concept is the same as `whatIf`. An expression for invoking `whatIf` when the target object is not null. It is useful when the receiver and the result should be different.<br>
135
160
```kotlin
136
161
val length = nullableString.whatIfMap(
137
-
given = nullableString.length <5,
138
162
whatIf = { it.length },
139
163
whatIfNot = {
140
-
log("$it, length can not over than 5.")
141
-
5
164
+
log("$it, nullableString is null.")
165
+
-1
142
166
}
143
167
)
144
168
```
145
169
We can use default value instead of the `whatIfNot` and can be omitted the `whatIfNot`.
0 commit comments