public Path withLast(String element) {
        if (element.isEmpty()) throw new IllegalStateException("Cannot set the last element of an empty path");
        List<String> newElements = new ArrayList<>(elements);
        newElements.set(newElements.size() -1, element);
        return new Path(newElements, delimiter);
    }
Returns a new path with the last element replaced by the given element.

@throws IllegalStateException if this path is empty
integration/intellij/src/main/java/ai/vespa/intellij/schema/utils/Path.java