Configurable (List<Configurable> list, int weight) { if (list != null) { replaceGroupWithSingleConfigurable(list); if (1 == list.size()) { Configurable configurable = list.get(0); if (configurable instanceof SortedConfigurableGroup group) { group.myWeight = weight; // modify weight according to the replacing group return group; } if (configurable instanceof ConfigurableWrapper wrapper) { wrapper.myWeight = weight; // modify weight according to the replacing group return wrapper; } } } return null; }