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

io.bdeploy.common.cfg.NonExistingPathValidator Maven / Gradle / Ivy

Go to download

Public API including dependencies, ready to be used for integrations and plugins.

There is a newer version: 7.3.6
Show newest version
package io.bdeploy.common.cfg;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

import io.bdeploy.common.cfg.Configuration.ConfigValidator;
import io.bdeploy.common.cfg.Configuration.ValidationMessage;
import io.bdeploy.common.util.PathHelper;

@ValidationMessage("Path exists, but should not exist (or be an empty directory): %s")
public class NonExistingPathValidator implements ConfigValidator {

    @Override
    public boolean validate(String value) {
        Path p = Paths.get(value);
        if (Files.isDirectory(p)) {
            // Empty directories are OK.
            try (Stream list = Files.list(p)) {
                return !list.findAny().isPresent();
            } catch (IOException e) {
                throw new IllegalStateException("Cannot determine directory contents: " + p, e);
            }
        }
        return !PathHelper.exists(p);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy