public void ensureIds() { Set<String> ids = new HashSet<>(); List<ProcessNode<?, ?>> unnamedNodes = new ArrayList<>(); for (ProcessNode<?, ?> node : getModelNodes()) { String id = node.getId(); if (id == null) { unnamedNodes.add(node); } else { ids.add(id); } } Map<String, Integer> startCounts = new HashMap<>(); for (ProcessNode<?, ?> unnamed : unnamedNodes) { String idBase = unnamed.getIdBase(); int startCount = getOrDefault(startCounts, idBase, 1); for (String id = idBase + Integer.toString(startCount); ids.contains(id); id = idBase + Integer.toString(startCount)) { ++startCount; } unnamed.setId(idBase + Integer.toString(startCount)); startCounts.put(idBase, startCount + 1); } }
private static int getOrDefault( final Map<String, Integer> map, final String key, final int defaultValue) { final Integer val = map.get(key); return val == null ? defaultValue : val.intValue(); }