I am trying to create a dictionary filter that will filter the incoming list of parameters.
The problem is that when there are parameters in the list that should be filtered and removed - I get a NullPointerException.
Basic informations:
This is how filter looks:
data class Filter(
val filters: Map<ParameterName, Set<String>>
)
data class ParameterName(
val key: String
)
Example of filter:
Filter(filters={ParameterName(key=Manufacturer)=[brand, producer]})
The filtered list (List<Parameter>
) has the following structure:
data class Parameter(
val id: String?,
val name: String?,
val values: List<String>?
)
Example of List<Parameter>
[Parameter(id=123, name=brand, values=[Nike]), Parameter(id=345, name=color, values=[black, pink]), Parameter(id=823, name=test, values=[some, thing])]
The logic is that the filtered list is changed to Map <String, ParameterValue>. Where:
a) String (key) is the value of the filter key (The key in the filter map has the main value and any other names under it. For example - under Manufacturer we have got values "brand" or "producer")
b) The parameter value (e.g. Nike) is assigned to the ParameterValue object (val value: String).
What have I prepared:
fun filterParameters(
parametersList: List<Parameter>,
filters: Map<ParameterName, Set<String>>
): Map<String, ParameterValue> {
return parametersList.associate { param ->
filters.filter {
it.value.first().equals(param.name, ignoreCase = true)
}.keys.first().key to ParameterValue(value = param.values!!.first())
}.toMap()
}
Flow example:
- Input to the method:
a) Parameters list:
listOf(
Parameter(
"123",
"brand",
listOf("Nike")
),
Parameter(
"345",
"color",
listOf("black", "pink")
),
Parameter(
"823",
"test",
listOf("some", "thing")
)
)
b) Filter:
mapOf(
ParameterName("Manufacturer") to setOf("brand", "producer")
)
2)Expected output:
{Manufacturer=ParameterValue(value=Nike)}
Expected output structure:
Map<String, ParameterValue>
where
data class ParameterValue(
val parameterId: String? = null,
val value: String,
val valueId: String? = null
)
So we just fill value - String
CodePudding user response:
data class Parameter(
val id: String?,
val name: String?,
val values: List<String>?
)
val parameters = listOf(
Parameter("123", "brand", listOf("Nike")),
Parameter("3433", "color", listOf("red", "black")),
Parameter("22313", "model", listOf("air max")),
Parameter("2312", "origin", listOf("USA")),
Parameter(null, "size", listOf("7", "7.5", "8", "9")),
Parameter("987", null, listOf("lalala")),
Parameter("999", "code", null),
Parameter(null, null, null)
)
val filters = mapOf(
"Manufacturer" to setOf("brand", "producer"),
"Country" to setOf("origin", "from")
)
fun getValuesForParameterName(parameters: List<Parameter>, filters: Map<String, Set<String>>): Map<String, List<String>> {
return parameters
.filter { parameter -> parameter.name in filters.flatMap { map -> map.value } }
.map { parameter -> filters.filter { map -> map.value.contains(parameter.name) }.keys.first() to parameter.values }
.groupBy { (name, _) -> name }
.map { (name, values) -> name to values.map { (_, list) -> list ?: emptyList() }.flatten() }
.toMap()
}
val result = getValuesForParameterName(parameters, filters)
println(result) // Output: {Manufacturer=[Nike], Property=[USA]}
println(result["Manufacturer"]) // Output: [Nike]
println(result["Country"]) // Output: [USA]