All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.powsybl.afs.Node Maven / Gradle / Ivy

There is a newer version: 6.4.0
Show newest version
/**
 * Copyright (c) 2017, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.powsybl.afs;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author Geoffroy Jamgotchian 
 */
public class Node extends AbstractNodeBase {

    protected final AppFileSystem fileSystem;

    protected final boolean folder;

    protected Node(FileCreationContext context, int codeVersion, boolean folder) {
        super(context.getInfo(), context.getStorage(), codeVersion);
        this.fileSystem = Objects.requireNonNull(context.getFileSystem());
        this.folder = folder;
    }

    public void delete() {
        storage.deleteNode(info.getId());
        storage.flush();
    }

    @Override
    public boolean isFolder() {
        return folder;
    }

    @Override
    public Optional getParent() {
        return getParentInfo().map(parentInfo -> new Folder(new FileCreationContext(parentInfo, storage, fileSystem)));
    }

    private static boolean pathStop(Node node) {
        return !node.getParent().isPresent();
    }

    private static String pathToString(List path) {
        StringBuilder builder = new StringBuilder();
        builder.append(path.get(0))
                .append(AppFileSystem.FS_SEPARATOR);
        for (int i = 1; i < path.size(); i++) {
            builder.append(AppFileSystem.PATH_SEPARATOR).append(path.get(i));
        }
        return builder.toString();
    }

    @Override
    public NodePath getPath() {
        return NodePath.find(this, Node::pathStop, Node::pathToString);
    }

    public AppFileSystem getFileSystem() {
        return fileSystem;
    }

    public  U findService(Class serviceClass) {
        return fileSystem.getData().findService(serviceClass, storage.isRemote());
    }
}