class SubnetFilterStrategyMultiplePreferPublic implements SubnetFilterStrategy {

    @Override
    public SubnetSelectionResult filter(Collection<CloudSubnet> subnets, int azCount) {
        List<CloudSubnet> result = subnetSelectorService.collectPublicSubnets(subnets);
        Set<String> uniqueAzs = result.stream().map(e -> e.getAvailabilityZone()).collect(Collectors.toSet());
        if (uniqueAzs.size() < azCount) {
            LOGGER.info("There is not enough different AZ in the public subnets which {}, falling back to private subnets: {}",
                    uniqueAzs.size(), subnets);
            List<CloudSubnet> privateSubnets = subnetSelectorService.collectPrivateSubnets(subnets);
            for (CloudSubnet privateSubnet : privateSubnets) {
                if (!uniqueAzs.contains(privateSubnet.getAvailabilityZone())) {
                    result.add(privateSubnet);
                    uniqueAzs.add(privateSubnet.getAvailabilityZone());
                    if (uniqueAzs.size() >= azCount) {
                        break;
                    }
                }
            }
        }
        return new SubnetSelectionResult(result);
    }

}