void orderBefore()

in src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java [237:271]


    void orderBefore(Item source, Item destination) throws RepositoryException {
        if (source == null) {
            // Nothing to do
            return;
        }

        // Find all items matching the source
        final String sourcePath = source.getPath();
        final String sourcePathPrefix = String.format("%s/", sourcePath);
        List<ItemData> itemsToMove = new LinkedList<>();
        for (String key : new ArrayList<>(items.keySet())) {
            if (key.equals(sourcePath) || key.startsWith(sourcePathPrefix)) {
                itemsToMove.add(items.remove(key));
            }
        }

        if (destination == null) {
            // Move items to end
            for (ItemData item : itemsToMove) {
                items.put(item.getPath(), item);
            }
            return;
        }

        // Cycle items and add them back at the end
        for (String key : new ArrayList<>(items.keySet())) {
            if (key.equals(destination.getPath())) {
                // Move items before destination
                for (ItemData item : itemsToMove) {
                    items.put(item.getPath(), item);
                }
            }
            items.put(key, items.remove(key));
        }
    }