Skip to content

Commit 92ec158

Browse files
committedDec 4, 2020
Update documentations for README
1 parent 9d547a9 commit 92ec158

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed
 

‎README.md

+31-7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ nullableObject.whatIfNotNull(
9595
)
9696
```
9797

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+
98117
### WhatIfNotNullOrEmpty
99118
An expression for invoking `whatIf` lambda when the __string__, __collections__ and __array type__ is not null and not empty.<br>
100119
If the collections or array type target is null or empty, `whatIfNot` will be invoked instead of the `whatIf`.
@@ -128,25 +147,30 @@ nullableArray.whatIfNotNullOrEmpty {
128147
Array, ByteArray, ShortArray, IntArray, LongArray, FloatArray, DoubleArray, BooleanArray, CharArray
129148

130149
#### Collections
131-
List, Map, Set
150+
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+
132157

133158
### 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>
135160
```kotlin
136161
val length = nullableString.whatIfMap(
137-
given = nullableString.length < 5,
138162
whatIf = { it.length },
139163
whatIfNot = {
140-
log("$it, length can not over than 5.")
141-
5
164+
log("$it, nullableString is null.")
165+
-1
142166
}
143167
)
144168
```
145169
We can use default value instead of the `whatIfNot` and can be omitted the `whatIfNot`.
146170
```kotlin
147171
val length = nullableString.whatIfMap(
148-
nullableString.length < 5,
149-
default = nullableString.length) {
172+
default = -1
173+
) {
150174
log("$it, length can not over than 5.")
151175
5
152176
}

0 commit comments

Comments
 (0)
Please sign in to comment.