class SubnetFilterStrategyMultiplePreferPrivate implements SubnetFilterStrategy {

    @Override
    public SubnetSelectionResult filter(Collection<CloudSubnet> subnets, int azCount) {
        List<CloudSubnet> result = subnetSelectorService.collectPrivateSubnets(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 private subnets which {}, falling back to private subnets: {}",
                    uniqueAzs.size(), subnets);
            List<CloudSubnet> publicSubnets = subnetSelectorService.collectPublicSubnets(subnets);
            for (CloudSubnet publicSubnet : publicSubnets) {
                if (!uniqueAzs.contains(publicSubnet.getAvailabilityZone())) {
                    result.add(publicSubnet);
                    uniqueAzs.add(publicSubnet.getAvailabilityZone());
                    if (uniqueAzs.size() >= azCount) {
                        break;
                    }
                }
            }
        }
        return new SubnetSelectionResult(result);
    }

}