I am trying to implement a Filter class that can be composed of other Filter classes.The goal is that each filter can process an object and return a boolean, and if it is made of several filters it computes the AND of all its filters.
I am trying to get one of the constructors to take a vararg of predicates, which needs to be converted into a vararg of Filters. The compiler says that none of the functions (the primary and first secondary constructors) can be called with these parameters.
My understanding is that * should convert List<Filter<T>>
into vararg Filter<T>
, but it is not working.
class Filter<in T>(private vararg val filters: Filter<T>) {
constructor(predicate: (T) -> Boolean) : this(Filter(predicate))
constructor(vararg predicates: (T) -> Boolean) : this(Filter<T>(*(predicates.map { predicate -> Filter<T>(predicate) })))
fun process(input: T): Boolean {
for (filter in filters) { if (!filter.process(input)) return false }
return true
}
}
What can I do to achieve that?
Also, is it a big overhead iterating over filters of just one predicate compared to just checking the predicate (considering this will be called a lot of times, to filter a list)?