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

com.yahoo.vespa.hosted.provision.archive.ArchiveUris Maven / Gradle / Ivy

There is a newer version: 8.441.21
Show newest version
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.archive;

import com.yahoo.config.provision.CloudAccount;
import com.yahoo.config.provision.TenantName;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

public record ArchiveUris(Map tenantArchiveUris, Map accountArchiveUris) {
    private static final Pattern validUriPattern = Pattern.compile("[a-z0-9]+://(?:(?:[a-z0-9]+(?:[-_][a-z0-9.]+)*)+/)+");

    public ArchiveUris(Map tenantArchiveUris, Map accountArchiveUris) {
        this.tenantArchiveUris = Map.copyOf(tenantArchiveUris);
        this.accountArchiveUris = Map.copyOf(accountArchiveUris);
    }

    public ArchiveUris with(TenantName tenant, Optional archiveUri) {
        Map updated = updateIfChanged(tenantArchiveUris, tenant, archiveUri);
        if (updated == tenantArchiveUris) return this;
        return new ArchiveUris(updated, accountArchiveUris);
    }

    public ArchiveUris with(CloudAccount account, Optional archiveUri) {
        Map updated = updateIfChanged(accountArchiveUris, account, archiveUri);
        if (updated == accountArchiveUris) return this;
        return new ArchiveUris(tenantArchiveUris, updated);
    }

    private static  Map updateIfChanged(Map current, T key, Optional archiveUri) {
        archiveUri = archiveUri.map(ArchiveUris::normalizeUri);
        if (Optional.ofNullable(current.get(key)).equals(archiveUri)) return current;
        Map updated = new HashMap<>(current);
        archiveUri.ifPresentOrElse(uri -> updated.put(key, uri), () -> updated.remove(key));
        return updated;
    }

    static String normalizeUri(String uri) {
        if (!uri.endsWith("/")) uri = uri + "/";
        if (!validUriPattern.matcher(uri).matches())
            throw new IllegalArgumentException("Invalid archive URI: " + uri);
        return uri;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy