public static UnitConfig computeNewRootLocation( final UnitConfig currentLocationConfig, ProtoBufMessageMap<String, UnitConfig, UnitConfig.Builder> entryMap) throws CouldNotPerformException { try { HashMap<String, UnitConfig> rootLocationConfigList = new HashMap<>(); for (UnitConfig locationConfig : entryMap.getMessages()) { rootLocationConfigList.put(locationConfig.getId(), locationConfig); } rootLocationConfigList.put(currentLocationConfig.getId(), currentLocationConfig); if (rootLocationConfigList.size() == 1) { return rootLocationConfigList.values().stream().findFirst().get(); } for (UnitConfig locationConfig : new ArrayList<>(rootLocationConfigList.values())) { if (!locationConfig.hasPlacementConfig()) { } else if (!locationConfig.getPlacementConfig().hasLocationId()) { } else if (locationConfig.getPlacementConfig().getLocationId().isEmpty()) { } else if (locationConfig .getPlacementConfig() .getLocationId() .equals(locationConfig.getId())) { return locationConfig; } else { rootLocationConfigList.remove(locationConfig.getId()); } } if (rootLocationConfigList.isEmpty()) { throw new NotAvailableException("root candidate"); } else if (rootLocationConfigList.size() == 1) { return rootLocationConfigList.values().stream().findFirst().get(); } throw new InvalidStateException("To many potential root locations detected!"); } catch (CouldNotPerformException ex) { throw new CouldNotPerformException("Could not compute root location!", ex); } }
public static UnitConfig getRootLocation( final ProtoBufMessageMap<String, UnitConfig, UnitConfig.Builder> entryMap) throws NotAvailableException, CouldNotPerformException { return getRootLocation(entryMap.getMessages()); }