I started a refactoring today, where I wanted to eliminate some duplicates.
Here is a part of our chart synchronization code:
chart.onSelect { event ->
// some specific code
// common code
val selectedSample = event.data.firstOrNull()
if (selectedSample?.domain is SampleRaw) {
event.preventDefault = true
chart.select(selectedSample.domain.s.listOfSampleRaw)
}
for (ct in otherCharts) {
charts[ct]?.also { otherChart ->
val entries = mutableListOf<ISample>()
for (s in event.data) {
val entry = otherChart.dataset.data.firstOrNull {
it.domain.s.idx == s.domain.s.idx
}
if (entry != null) {
entries.add(entry.domain)
}
}
otherChart.select(entries)
}
}
}
for the chart.onSelectionAdd() the code is almost the same but:
- the event class is different
- instead of select() method the addToSelection() method should be called
I cannot extract this code because of the various Event classes. I checked those classes, and it turned out, that they have exactly the same source code.
Would it possible to create a base class (with preventDefault and data/selectedData properties) and the specific classes extend this base class?