
io.bdeploy.common.cfg.NonExistingOrEmptyDirPathValidator Maven / Gradle / Ivy
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;
/**
* Checks if the given {@link Path} does not exist. Empty directories are also considered valid.
*/
@ValidationMessage("Path exists, but should not exist (or be an empty directory): %s")
public class NonExistingOrEmptyDirPathValidator implements ConfigValidator {
@Override
public boolean test(String value) {
Path p = Paths.get(value);
if (Files.isDirectory(p)) {
// Empty directories are OK.
try (Stream list = Files.list(p)) {
return list.findAny().isEmpty();
} catch (IOException e) {
throw new IllegalStateException("Cannot determine directory contents: " + p, e);
}
}
return !PathHelper.exists(p);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy