val processor = DaggerDecoratorComponent.create().processor()
println("${processor.decorate("Hi how are you?", "noo")}")
@Component(modules = [SetModule::class])
interface DecoratorComponent {
fun processor(): SetProcessor
}
@Module
object SetModule {
@Provides
@JvmStatic
@IntoSet
fun provideSparkleDecorator() = Decorator("sparkle") { s -> "✨ $s ✨" }
@Provides
@JvmStatic
@IntoSet
fun provideEmphasisDecorator() = Decorator("emphasis") { s -> "‼️ $s ‼️" }
@Provides
@JvmStatic
@ElementsIntoSet
fun provideSetOfDecorators() = setOf(
Decorator("cool") { s -> "🆒️ $s 😎️" },
Decorator("noo") { s -> "👎 $s 👎" }
)
}
class SetProcessor @Inject constructor(
private val decorators: Set<Decorator>
) {
fun decorate(strForDecoration: String, decoratorType: String) = decorators
.filter { it.displayName == decoratorType }
.map { it.fn(strForDecoration) }
}
data class Decorator(
val displayName: String,
val fn: (String) -> String
)
[👎 Hi how are you? 👎]