// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.testutil;

import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.RepositoryCaseMismatchException;

import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;

import java.util.Map;
import java.util.SortedSet;

/** Repository manager that uses in-memory repositories. */
public class InMemoryRepositoryManager implements GitRepositoryManager {
  public static InMemoryRepository newRepository(Project.NameKey name) {
    return new Repo(name);
  }

  private static class Description extends DfsRepositoryDescription {
    private String desc;

    private Description(Project.NameKey name) {
      super(name.get());
      desc = "In-memory repository " + name.get();
    }
  }

  private static class Repo extends InMemoryRepository {
    private Repo(Project.NameKey name) {
      super(new Description(name));
    }

    @Override
    public Description getDescription() {
      return (Description) super.getDescription();
    }
  }

  private Map<String, Repo> repos = Maps.newHashMap();

  @Override
  public InMemoryRepository openRepository(Project.NameKey name)
      throws RepositoryNotFoundException {
    return get(name);
  }

  @Override
  public InMemoryRepository createRepository(Project.NameKey name)
      throws RepositoryCaseMismatchException, RepositoryNotFoundException {
    Repo repo;
    try {
      repo = get(name);
      if (!repo.getDescription().getRepositoryName().equals(name.get())) {
        throw new RepositoryCaseMismatchException(name);
      }
    } catch (RepositoryNotFoundException e) {
      repo = new Repo(name);
      repos.put(name.get().toLowerCase(), repo);
    }
    return repo;
  }

  @Override
  public InMemoryRepository openMetadataRepository(Project.NameKey name)
      throws RepositoryNotFoundException {
    return openRepository(name);
  }

  @Override
  public SortedSet<Project.NameKey> list() {
    SortedSet<Project.NameKey> names = Sets.newTreeSet();
    for (DfsRepository repo : repos.values()) {
      names.add(new Project.NameKey(repo.getDescription().getRepositoryName()));
    }
    return ImmutableSortedSet.copyOf(names);
  }

  @Override
  public String getProjectDescription(Project.NameKey name)
      throws RepositoryNotFoundException {
    return get(name).getDescription().desc;
  }

  @Override
  public void setProjectDescription(Project.NameKey name, String description) {
    try {
      get(name).getDescription().desc = description;
    } catch (RepositoryNotFoundException e) {
      // Ignore.
    }
  }

  private Repo get(Project.NameKey name) throws RepositoryNotFoundException {
    Repo repo = repos.get(name.get().toLowerCase());
    if (repo != null) {
      return repo;
    } else {
      throw new RepositoryNotFoundException(name.get());
    }
  }
}

@@ -2053,32 +2053,45 @@
 erride%0A  public 
+synchronized 
 InMemoryReposito
@@ -2213,32 +2213,45 @@
 erride%0A  public 
+synchronized 
 InMemoryReposito
@@ -2730,16 +2730,29 @@
  public 
+synchronized 
 InMemory
@@ -2777,32 +2777,39 @@
 adataRepository(
+%0A      
 Project.NameKey 
@@ -2805,38 +2805,32 @@
 ct.NameKey name)
-%0A     
  throws Reposito
@@ -2910,16 +2910,29 @@
  public 
+synchronized 
 SortedSe
@@ -3223,16 +3223,29 @@
  public 
+synchronized 
 String g
@@ -3400,16 +3400,29 @@
  public 
+synchronized 
 void set
@@ -3461,16 +3461,22 @@
 ey name,
+%0A     
  String 
@@ -3633,24 +3633,37 @@
 %0A%0A  private 
+synchronized 
 Repo get(Pro
@@ -3672,32 +3672,38 @@
 ct.NameKey name)
+%0A     
  throws Reposito

Synchronize InMemoryRepositoryManager methods