ServiceViewModel (@NotNull ServiceViewState viewState, @NotNull ServiceModel model, @NotNull ServiceModelFilter modelFilter, @Nullable ServiceViewFilter parentFilter, @NotNull Map<String, ServiceViewContributor<?>> contributors) { switch (viewState.viewType) { case ContributorModel.TYPE: { ServiceState serviceState = ContainerUtil.getOnlyItem(viewState.roots); ServiceViewContributor<?> contributor = serviceState == null ? null : contributors.get(serviceState.contributor); return contributor == null ? null : new ContributorModel(model, modelFilter, contributor, parentFilter); } case GroupModel.TYPE: { ServiceState serviceState = ContainerUtil.getOnlyItem(viewState.roots); ServiceViewContributor<?> contributor = serviceState == null ? null : contributors.get(serviceState.contributor); if (contributor == null) return null; ServiceViewItem groupItem = model.findItemById(serviceState.path, contributor); if (!(groupItem instanceof ServiceGroupNode)) return null; AtomicReference<ServiceGroupNode> ref = new AtomicReference<>((ServiceGroupNode)groupItem); return new GroupModel(model, modelFilter, ref, parentFilter); } case SingeServiceModel.TYPE: { ServiceState serviceState = ContainerUtil.getOnlyItem(viewState.roots); ServiceViewContributor<?> contributor = serviceState == null ? null : contributors.get(serviceState.contributor); if (contributor == null) return null; ServiceViewItem serviceItem = model.findItemById(serviceState.path, contributor); if (serviceItem == null) return null; if (serviceItem.getChildren().isEmpty()) { AtomicReference<ServiceViewItem> ref = new AtomicReference<>(serviceItem); return new SingeServiceModel(model, modelFilter, ref, parentFilter); } else { new ServiceListModel(model, modelFilter, new SmartList<>(serviceItem), parentFilter); } } case ServiceListModel.TYPE: List<ServiceViewItem> items = new ArrayList<>(); for (ServiceState serviceState : viewState.roots) { ServiceViewContributor<?> contributor = contributors.get(serviceState.contributor); if (contributor != null) { ContainerUtil.addIfNotNull(items, model.findItemById(serviceState.path, contributor)); } } return items.isEmpty() ? null : new ServiceListModel(model, modelFilter, items, parentFilter); default: return null; } }