If each builder function returns a new Builder instance, use run
:
private fun myFunc(var1: Type1 , var2: Type2?) {
val request = class1.newBuilder()
.setType1(var1)
.run { if(var2 != null) setType2(var2) else this }
.build()
clientClass.send(request)
}
If the builder functions mutate and return the same Builder instance, it’s simpler to use apply
private fun myFunc(var1: Type1 , var2: Type2?) {
val request = class1.newBuilder()
.setType1(var1)
.apply { if(var2 != null) setType2(var2) }
.build()
clientClass.send(request)
}
// or more cleanly using apply for everything instead of chaining:
private fun myFunc(var1: Type1 , var2: Type2?) {
val request = class1.newBuilder().apply {
setType1(var1)
if(var2 != null) setType2(var2)
build()
}
clientClass.send(request)
}
Example of a Builder class whose functions return new instances:
fun setType2(type2: Type2): Builder {
return CombinedBuilder(this, type2) // a new object
}
Example of a Builder class whose functions return the same instance:
fun setType2(type2: Type2): Builder {
this.type2 = type2
return this // the same object
}
The second type is more common, but sometimes the first type is used. You might have to check the source code to know for sure. If you can't be sure, use the .run
method because it will work for either.