/*
 * Copyright 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.internal.snapshot;

import com.google.common.collect.ImmutableList;
import org.gradle.internal.file.FileType;
import org.gradle.internal.hash.HashCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * A complete snapshot of an existing directory.
 *
 * Includes complete snapshots of every child and the Merkle tree hash.
 */
public class CompleteDirectorySnapshot extends AbstractCompleteFileSystemLocationSnapshot implements CompleteFileSystemLocationSnapshot {
    private final List<CompleteFileSystemLocationSnapshot> children;
    private final HashCode contentHash;

    public CompleteDirectorySnapshot(String absolutePath, String name, List<CompleteFileSystemLocationSnapshot> children, HashCode contentHash) {
        super(absolutePath, name);
        this.children = children;
        this.contentHash = contentHash;
    }

    @Override
    public HashCode getHash() {
        return contentHash;
    }

    @Override
    public FileType getType() {
        return FileType.Directory;
    }

    @Override
    public boolean isContentAndMetadataUpToDate(CompleteFileSystemLocationSnapshot other) {
        return other instanceof CompleteDirectorySnapshot;
    }

    @Override
    public void accept(FileSystemSnapshotVisitor visitor) {
        if (!visitor.preVisitDirectory(this)) {
            return;
        }
        for (CompleteFileSystemLocationSnapshot child : children) {
            child.accept(visitor);
        }
        visitor.postVisitDirectory(this);
    }

    public List<CompleteFileSystemLocationSnapshot> getChildren() {
        return children;
    }

    @Override
    public Optional<MetadataSnapshot> getSnapshot(String absolutePath, int offset) {
        return FileSystemNode.thisOrGet(
            this, absolutePath, offset,
            () -> {
                for (CompleteFileSystemLocationSnapshot child : getChildren()) {
                    if (AbstractFileSystemNode.isChildOfOrThis(absolutePath, offset, child.getName())) {
                        int endOfThisSegment = child.getName().length() + offset;
                        return child.getSnapshot(absolutePath, endOfThisSegment + 1);
                    }
                }
                return Optional.of(missingSnapshotForAbsolutePath(absolutePath));
            });
    }

    @Override
    public Optional<FileSystemNode> invalidate(String absolutePath, int offset) {
        return AbstractFileSystemNode.handleChildren(children, absolutePath, offset, new AbstractFileSystemNode.ChildHandler<Optional<FileSystemNode>>() {
            @Override
            public Optional<FileSystemNode> handleNewChild(int insertBefore) {
                return children.isEmpty()
                    ? Optional.empty()
                    : Optional.of(new UnknownSnapshot(getPathToParent(), children));
            }

            @Override
            public Optional<FileSystemNode> handleChildOfExisting(int childIndex) {
                CompleteFileSystemLocationSnapshot foundChild = children.get(childIndex);
                int indexForSubSegment = foundChild.getPathToParent().length();
                Optional<FileSystemNode> invalidated = indexForSubSegment == absolutePath.length() - offset
                    ? Optional.empty()
                    : foundChild.invalidate(absolutePath, offset + indexForSubSegment + 1);
                if (children.size() == 1) {
                    return invalidated.map(it -> new UnknownSnapshot(getPathToParent(), ImmutableList.of(it)));
                }

                return invalidated
                    .map(invalidatedChild -> {
                        ArrayList<FileSystemNode> newChildren = new ArrayList<>(children);
                        newChildren.set(childIndex, invalidatedChild);
                        return Optional.<FileSystemNode>of(new UnknownSnapshot(getPathToParent(), newChildren));
                    }).orElseGet(() -> {
                        if (children.size() == 2) {
                            CompleteFileSystemLocationSnapshot singleChild = childIndex == 0 ? children.get(1) : children.get(0);
                            return Optional.of(new UnknownSnapshot(getPathToParent(), ImmutableList.of(singleChild)));
                        }
                        ArrayList<CompleteFileSystemLocationSnapshot> newChildren = new ArrayList<>(children);
                        newChildren.remove(childIndex);
                        return Optional.of(new UnknownSnapshot(getPathToParent(), newChildren));
                    });
            }
        });
    }
}

@@ -1087,54 +1087,8 @@
 hot 
-implements CompleteFileSystemLocationSnapshot 
 %7B%0A  

Remove unnecessary implemented interface