package com.atlassian.bitbucket.installer.validation;

import com.google.common.collect.ImmutableList;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class CompositeDirectoryValidationStrategy implements DirectoryValidationStrategy {

    private final List<DirectoryValidationStrategy> directoryValidators;

    public CompositeDirectoryValidationStrategy(DirectoryValidationStrategy... directoryValidators) {
        this(Arrays.asList(directoryValidators));
    }

    public CompositeDirectoryValidationStrategy(Iterable<? extends DirectoryValidationStrategy> directoryValidators) {
        this.directoryValidators = ImmutableList.copyOf(directoryValidators);
    }

    public void validate(File directory) {
        SoftValidationException lastSoftException = null;
        for (DirectoryValidationStrategy directoryValidator : directoryValidators) {
            try {
                directoryValidator.validate(directory);
            } catch (SoftValidationException e) {
                //The SoftValidationException need to be ignored until we know there are no HardValidationException
                //that are much more important and terminal.
                lastSoftException = e;
            }
        }
        if (lastSoftException != null) {
            throw lastSoftException;
        }
    }
}
