TreePath (@NotNull Iterable<? extends TreePath> paths) { TreePath ancestor = null; for (int i = 0; i < Integer.MAX_VALUE; i++) { TreePath first = null; for (TreePath path : paths) { int count = path.getPathCount(); if (count <= i) return ancestor; // a path is too short while (--count > i) path = path.getParentPath(); if (path == null) throw new IllegalStateException("unexpected"); if (first == null) first = path; // initialize with the first path in the given collection if (first != path && !Objects.equals(first.getLastPathComponent(), path.getLastPathComponent())) { return ancestor; // different components at the current index } } if (first == null) return ancestor; // nothing to iterate ancestor = createTreePath(ancestor, first.getLastPathComponent()); } return ancestor; }