/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * 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.intellij.vcs.log.util;

import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.io.*;
import com.intellij.vcs.log.VcsLogProvider;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class PersistentUtil {
  @NotNull public static final File LOG_CACHE = new File(PathManager.getSystemPath(), "vcs-log");

  @NotNull
  public static String calcLogId(@NotNull Project project, @NotNull Map<VirtualFile, VcsLogProvider> logProviders) {
    int hashcode = calcLogProvidersHash(logProviders);
    return project.getLocationHash() + "." + Integer.toHexString(hashcode);
  }

  private static int calcLogProvidersHash(@NotNull final Map<VirtualFile, VcsLogProvider> logProviders) {
    List<VirtualFile> sortedRoots = ContainerUtil.sorted(logProviders.keySet(), new Comparator<VirtualFile>() {
      @Override
      public int compare(@NotNull VirtualFile o1, @NotNull VirtualFile o2) {
        return o1.getPath().compareTo(o2.getPath());
      }
    });
    return StringUtil.join(sortedRoots, new Function<VirtualFile, String>() {
      @Override
      public String fun(VirtualFile root) {
        return root.getPath() + "." + logProviders.get(root).getSupportedVcs().getName();
      }
    }, ".").hashCode();
  }

  @NotNull
  private static File getStorageFile(@NotNull String logId, @NotNull String logKind, int version) {
    File subdir = new File(LOG_CACHE, logKind);
    final File mapFile = new File(subdir, logId + "." + version);
    if (!mapFile.exists()) {
      IOUtil.deleteAllFilesStartingWith(new File(subdir, logId));
    }
    return mapFile;
  }

  @NotNull
  public static <T> PersistentEnumerator<T> createPersistentEnumerator(@NotNull final KeyDescriptor<T> keyDescriptor,
                                                                       @NotNull String logKind,
                                                                       @NotNull String logId,
                                                                       int version) throws IOException {
    final File storageFile = getStorageFile(logId, logKind, version);

    return IOUtil.openCleanOrResetBroken(new ThrowableComputable<PersistentEnumerator<T>, IOException>() {
      @Override
      public PersistentEnumerator<T> compute() throws IOException {
        return new PersistentEnumerator<T>(storageFile, keyDescriptor, Page.PAGE_SIZE);
      }
    }, storageFile);
  }

  @NotNull
  public static PersistentStringEnumerator createPersistentStringEnumerator(@NotNull String logKind, @NotNull String logId, int version)
    throws IOException {
    final File storageFile = getStorageFile(logId, logKind, version);

    return IOUtil.openCleanOrResetBroken(new ThrowableComputable<PersistentStringEnumerator, IOException>() {
      @Override
      public PersistentStringEnumerator compute() throws IOException {
        return new PersistentStringEnumerator(storageFile);
      }
    }, storageFile);
  }
}

@@ -732,62 +732,8 @@
 ct;%0A
-import com.intellij.openapi.util.ThrowableComputable;%0A
 impo
@@ -774,24 +774,24 @@
 StringUtil;%0A
+
 import com.i
@@ -827,43 +827,8 @@
 le;%0A
-import com.intellij.util.Function;%0A
 impo
@@ -1699,147 +1699,19 @@
 (), 
-new Comparator%3CVirtualFile%3E() %7B%0A      @Override%0A      public int compare(@NotNull VirtualFile o1, @NotNull VirtualFile o2) %7B%0A        return
+(o1, o2) -%3E
  o1.
@@ -1743,31 +1743,16 @@
 tPath())
-;%0A      %7D%0A    %7D
 );%0A    r
@@ -1790,120 +1790,15 @@
 ts, 
-new Function%3CVirtualFile, String%3E() %7B%0A      @Override%0A      public String fun(VirtualFile root) %7B%0A        return
+root -%3E
  roo
@@ -1867,31 +1867,16 @@
 etName()
-;%0A      %7D%0A    %7D
 , %22.%22).h
@@ -2782,160 +2782,16 @@
 ken(
-new ThrowableComputable%3CPersistentEnumerator%3CT%3E, IOException%3E() %7B%0A      @Override%0A      public PersistentEnumerator%3CT%3E compute() throws IOException %7B%0A  
+() -%3E %7B%0A
     
@@ -2824,17 +2824,16 @@
 merator%3C
-T
 %3E(storag
@@ -2871,24 +2871,16 @@
 _SIZE);%0A
-      %7D%0A
     %7D, s
@@ -3186,166 +3186,16 @@
 ken(
-new ThrowableComputable%3CPersistentStringEnumerator, IOException%3E() %7B%0A      @Override%0A      public PersistentStringEnumerator compute() throws IOException
+() -%3E
  %7B%0A
-  
     
@@ -3252,16 +3252,8 @@
 e);%0A
-      %7D%0A
     

use lambdas in PersistentUtil