boolean (List<? extends Statement> lst) { Set<Statement> all = new HashSet<>(lst); Set<Statement> handlers = new HashSet<>(); Set<Statement> intersection = null; for (Statement stat : lst) { Set<Statement> setNew = stat.getNeighboursSet(EdgeType.EXCEPTION, EdgeDirection.FORWARD); if (intersection == null) { intersection = setNew; } else { HashSet<Statement> interclone = new HashSet<>(intersection); interclone.removeAll(setNew); intersection.retainAll(setNew); setNew.removeAll(intersection); handlers.addAll(interclone); handlers.addAll(setNew); } } for (Statement stat : handlers) { if (!all.contains(stat) || !all.containsAll(stat.getNeighbours(EdgeType.EXCEPTION, EdgeDirection.BACKWARD))) { return false; } } // check for other handlers (excluding head) for (int i = 1; i < lst.size(); i++) { Statement stat = lst.get(i); if (!stat.getPredecessorEdges(EdgeType.EXCEPTION).isEmpty() && !handlers.contains(stat)) { return false; } } return true; }