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

denominator.dynect.DynECTGeoResourceRecordSetApi Maven / Gradle / Ivy

There is a newer version: 4.7.1
Show newest version
package denominator.dynect;

import static denominator.common.Preconditions.checkNotNull;
import static denominator.common.Util.and;
import static denominator.common.Util.filter;
import static denominator.common.Util.nextOrNull;
import static denominator.model.ResourceRecordSets.nameAndTypeEqualTo;
import static denominator.model.ResourceRecordSets.nameEqualTo;
import static denominator.model.ResourceRecordSets.nameTypeAndQualifierEqualTo;
import static denominator.model.ResourceRecordSets.profileContainsType;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import dagger.Lazy;
import denominator.common.Filter;
import denominator.model.ResourceRecordSet;
import denominator.profile.GeoResourceRecordSetApi;

public final class DynECTGeoResourceRecordSetApi implements GeoResourceRecordSetApi {
    private static final Filter> IS_GEO = profileContainsType("geo");

    private final Map> regions;
    private final DynECT api;
    private final String zoneFQDN;

    DynECTGeoResourceRecordSetApi(Map> regions, DynECT api, String zoneFQDN) {
        this.regions = regions;
        this.api = api;
        this.zoneFQDN = zoneFQDN;
    }

    @Override
    public Map> supportedRegions() {
        return regions;
    }

    @Override
    public Iterator> iterator() {
        Collection> val = api.geoRRSetsByZone().get(zoneFQDN);
        return val != null ? val.iterator() : Collections.> emptyList().iterator();
    }

    @Override
    public Iterator> iterateByName(String name) {
        return filter(iterator(), and(nameEqualTo(name), IS_GEO));
    }

    @Override
    public Iterator> iterateByNameAndType(String name, String type) {
        return filter(iterator(), and(nameAndTypeEqualTo(name, type), IS_GEO));
    }

    @Override
    public ResourceRecordSet getByNameTypeAndQualifier(String name, String type, String qualifier) {
        return nextOrNull(filter(iterator(), and(nameTypeAndQualifierEqualTo(name, type, qualifier), IS_GEO)));
    }

    @Override
    public void put(ResourceRecordSet rrset) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void deleteByNameTypeAndQualifier(String name, String type, String qualifier) {
        throw new UnsupportedOperationException();
    }

    static final class Factory implements GeoResourceRecordSetApi.Factory {
        private final Map> regions;
        private final Lazy hasAllGeoPermissions;
        private final DynECT api;

        @Inject
        Factory(@Named("geo") Map> regions,
                @Named("hasAllGeoPermissions") Lazy hasAllGeoPermissions, DynECT api) {
            this.regions = regions;
            this.hasAllGeoPermissions = hasAllGeoPermissions;
            this.api = api;
        }

        @Override
        public GeoResourceRecordSetApi create(String idOrName) {
            checkNotNull(idOrName, "idOrName was null");
            if (!hasAllGeoPermissions.get())
                return null;
            return new DynECTGeoResourceRecordSetApi(regions, api, idOrName);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy