toStateOrNull

fun <TResult : Any, TFirst> toStateOrNull(constructor: (TFirst) -> TResult, ps1: PropertyState<TFirst>, isAdditionalStateCorrectFunction: (TFirst) -> Boolean? = null): TResult?
fun <TResult : Any, TFirst, TSecond> toStateOrNull(constructor: (TFirst, TSecond) -> TResult, ps1: PropertyState<TFirst>, ps2: PropertyState<TSecond>, isAdditionalStateCorrectFunction: (TFirst, TSecond) -> Boolean? = null): TResult?
fun <TResult : Any, TFirst, TSecond, TThird> toStateOrNull(constructor: (TFirst, TSecond, TThird) -> TResult, ps1: PropertyState<TFirst>, ps2: PropertyState<TSecond>, ps3: PropertyState<TThird>, isAdditionalStateCorrectFunction: (TFirst, TSecond, TThird) -> Boolean? = null): TResult?
fun <TResult : Any, TFirst, TSecond, TThird, TFourth> toStateOrNull(constructor: (TFirst, TSecond, TThird, TFourth) -> TResult, ps1: PropertyState<TFirst>, ps2: PropertyState<TSecond>, ps3: PropertyState<TThird>, ps4: PropertyState<TFourth>, isAdditionalStateCorrectFunction: (TFirst, TSecond, TThird, TFourth) -> Boolean? = null): TResult?
fun <TResult : Any, TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond> toStateOrNull(constructor: (TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond) -> TResult, ps1: PropertyState<TFirst>, ps2: PropertyState<TSecond>, ps3: PropertyState<TThird>, ps4: PropertyState<TFourth>, ps5: PropertyState<TFifth>, ps6: PropertyState<TSixth>, ps7: PropertyState<TSeventh>, ps8: PropertyState<TEighth>, ps9: PropertyState<TNinth>, ps10: PropertyState<TTenth>, ps11: PropertyState<TEleventh>, ps12: PropertyState<TTwelfth>, ps13: PropertyState<TThirteenth>, ps14: PropertyState<TFourteenth>, ps15: PropertyState<TFifteenth>, ps16: PropertyState<TSixteenth>, ps17: PropertyState<TSeventeenth>, ps18: PropertyState<TEighteenth>, ps19: PropertyState<TNineteenth>, ps20: PropertyState<TTwentieth>, ps21: PropertyState<TTwentyFirst>, ps22: PropertyState<TTwentySecond>, isAdditionalStateCorrectFunction: (TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond) -> Boolean? = null): TResult?
fun <TResult : Any, TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond, TTwentyThird> toStateOrNull(constructor: (TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond, TTwentyThird) -> TResult, ps1: PropertyState<TFirst>, ps2: PropertyState<TSecond>, ps3: PropertyState<TThird>, ps4: PropertyState<TFourth>, ps5: PropertyState<TFifth>, ps6: PropertyState<TSixth>, ps7: PropertyState<TSeventh>, ps8: PropertyState<TEighth>, ps9: PropertyState<TNinth>, ps10: PropertyState<TTenth>, ps11: PropertyState<TEleventh>, ps12: PropertyState<TTwelfth>, ps13: PropertyState<TThirteenth>, ps14: PropertyState<TFourteenth>, ps15: PropertyState<TFifteenth>, ps16: PropertyState<TSixteenth>, ps17: PropertyState<TSeventeenth>, ps18: PropertyState<TEighteenth>, ps19: PropertyState<TNineteenth>, ps20: PropertyState<TTwentieth>, ps21: PropertyState<TTwentyFirst>, ps22: PropertyState<TTwentySecond>, ps23: PropertyState<TTwentyThird>, isAdditionalStateCorrectFunction: (TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond, TTwentyThird) -> Boolean? = null): TResult?
fun <TResult : Any, TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond, TTwentyThird, TTwentyFourth> toStateOrNull(constructor: (TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond, TTwentyThird, TTwentyFourth) -> TResult, ps1: PropertyState<TFirst>, ps2: PropertyState<TSecond>, ps3: PropertyState<TThird>, ps4: PropertyState<TFourth>, ps5: PropertyState<TFifth>, ps6: PropertyState<TSixth>, ps7: PropertyState<TSeventh>, ps8: PropertyState<TEighth>, ps9: PropertyState<TNinth>, ps10: PropertyState<TTenth>, ps11: PropertyState<TEleventh>, ps12: PropertyState<TTwelfth>, ps13: PropertyState<TThirteenth>, ps14: PropertyState<TFourteenth>, ps15: PropertyState<TFifteenth>, ps16: PropertyState<TSixteenth>, ps17: PropertyState<TSeventeenth>, ps18: PropertyState<TEighteenth>, ps19: PropertyState<TNineteenth>, ps20: PropertyState<TTwentieth>, ps21: PropertyState<TTwentyFirst>, ps22: PropertyState<TTwentySecond>, ps23: PropertyState<TTwentyThird>, ps24: PropertyState<TTwentyFourth>, isAdditionalStateCorrectFunction: (TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond, TTwentyThird, TTwentyFourth) -> Boolean? = null): TResult?
fun <TResult : Any, TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond, TTwentyThird, TTwentyFourth, TTwentyFifth> toStateOrNull(constructor: (TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond, TTwentyThird, TTwentyFourth, TTwentyFifth) -> TResult, ps1: PropertyState<TFirst>, ps2: PropertyState<TSecond>, ps3: PropertyState<TThird>, ps4: PropertyState<TFourth>, ps5: PropertyState<TFifth>, ps6: PropertyState<TSixth>, ps7: PropertyState<TSeventh>, ps8: PropertyState<TEighth>, ps9: PropertyState<TNinth>, ps10: PropertyState<TTenth>, ps11: PropertyState<TEleventh>, ps12: PropertyState<TTwelfth>, ps13: PropertyState<TThirteenth>, ps14: PropertyState<TFourteenth>, ps15: PropertyState<TFifteenth>, ps16: PropertyState<TSixteenth>, ps17: PropertyState<TSeventeenth>, ps18: PropertyState<TEighteenth>, ps19: PropertyState<TNineteenth>, ps20: PropertyState<TTwentieth>, ps21: PropertyState<TTwentyFirst>, ps22: PropertyState<TTwentySecond>, ps23: PropertyState<TTwentyThird>, ps24: PropertyState<TTwentyFourth>, ps25: PropertyState<TTwentyFifth>, isAdditionalStateCorrectFunction: (TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TTwelfth, TThirteenth, TFourteenth, TFifteenth, TSixteenth, TSeventeenth, TEighteenth, TNineteenth, TTwentieth, TTwentyFirst, TTwentySecond, TTwentyThird, TTwentyFourth, TTwentyFifth) -> Boolean? = null): TResult?

Create an object with a known, correct state, if all the property states are correct. Each property is validated separately, and it is also possible to validate the combination of properties.

The easiest way to use this function is to create an extension function on the input object, and then calling this function. For example:

fun User.toCompleteOrNull(): UserComplete? =
toStateOrNull(
::UserComplete,
isNotNull(name).and { length > 3 },
isNotNull(password).and { length > 10 },
isNotNull(emailAddress),
isNotNull(remarks)
) { name, password, _, _ -> !password.contains(name, true) }

Return

if all property states are correct, a result object created using constructor. Otherwise null.

Parameters

TResult

the type of the result object.

TFirst

(TSecond, ...) the types of the properties in the correct state.

constructor

the function used to create the result object if the properties are in the correct state.

ps1

(ps2, ...) the states of the properties based on the input.

isAdditionalStateCorrectFunction

an optional function to check if the combination of (individually correct) properties is correct.