package com.atlassian.bitbucket.mesh.test.context;

import com.atlassian.bitbucket.mesh.repository.Repository;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * An abstraction over caching {@link Repository} instances for reuse.
 * <p>
 * This abstraction primarily exists to allow different test execution listeners to share interaction with the cache.
 */
public interface RepositoryCache {

    /**
     * @return an immutable iterable over all cached repositories
     */
    @Nonnull
    Iterable<Repository> getAllRepositories();

    /**
     * Retrieves the cached {@link Repository} for the provided cacheKey, or {@code null} if no cached repository is
     * found.
     * 
     * @param cacheKey the resource cacheKey
     * @return a previously-unpacked repository, or {@code null}
     */
    @Nullable
    Repository getRepository(@Nonnull String cacheKey);

    /**
     * Removes any {@link Repository} cached for the provided cacheKey.
     * 
     * @param cacheKey the resource cacheKey
     */
    void removeRepository(@Nonnull String cacheKey);

    /**
     * Caches the provided {@link Repository}, indexed by the specified cacheKey.
     * <p>
     * Note: If the new repository evicts/overwrites a previously-cached repository it is not an error.
     *
     * @param cacheKey   the resource cacheKey
     * @param repository the unpacked repository to cache
     */
    void setRepository(@Nonnull String cacheKey, @Nonnull Repository repository);
}
