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

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

import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;

/**
 * A simple {@link RepositoryCache} implementation which maps {@link Repository repositories} by the resource location
 * from which they were unpacked.
 */
public class MapRepositoryCache implements RepositoryCache {
    
    private final Map<String, Repository> cache;

    public MapRepositoryCache() {
        cache = new HashMap<>();
    }

    @Nonnull
    @Override
    public Iterable<Repository> getAllRepositories() {
        return cache.values();
    }

    @Override
    public Repository getRepository(@Nonnull String cacheKey) {
        return cache.get(cacheKey);
    }

    @Override
    public void removeRepository(@Nonnull String cacheKey) {
        cache.remove(cacheKey);
    }

    @Override
    public void setRepository(@Nonnull String cacheKey, @Nonnull Repository repository) {
        cache.put(cacheKey, repository);
    }
}
