/*
 * Copyright 2000-2010 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.compiler.impl.generic;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.io.IOUtil;
import gnu.trove.TIntHashSet;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author nik
 */
public class GenericCompilerPersistentData {
  private static final Logger LOG = Logger.getInstance("#com.intellij.compiler.impl.generic.GenericCompilerPersistentData");
  private static final int VERSION = 1;
  private File myFile;
  private Map<String, Integer> myTarget2Id = new HashMap<String, Integer>();
  private TIntHashSet myUsedIds = new TIntHashSet();
  private boolean myVersionChanged;
  private final int myCompilerVersion;

  public GenericCompilerPersistentData(File cacheStoreDirectory, int compilerVersion) throws IOException {
    myCompilerVersion = compilerVersion;
    myFile = new File(cacheStoreDirectory, "info");
    if (!myFile.exists()) {
      LOG.info("Compiler info file doesn't exists: " + myFile.getAbsolutePath());
      myVersionChanged = true;
      return;
    }

    DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream(myFile)));
    try {
      final int dataVersion = input.readInt();
      if (dataVersion != VERSION) {
        LOG.info("Version of compiler info file (" + myFile.getAbsolutePath() + ") changed: " + dataVersion + " -> " + VERSION);
        myVersionChanged = true;
        return;
      }

      final int savedCompilerVersion = input.readInt();
      if (savedCompilerVersion != compilerVersion) {
        LOG.info("Compiler caches version changed (" + myFile.getAbsolutePath() + "): " + savedCompilerVersion + " -> " + compilerVersion);
        myVersionChanged = true;
        return;
      }

      int size = input.readInt();
      while (size-- > 0) {
        final String target = IOUtil.readString(input);
        final int id = input.readInt();
        myTarget2Id.put(target, id);
        myUsedIds.add(id);
      }
    }
    finally {
      input.close();
    }
  }

  public boolean isVersionChanged() {
    return myVersionChanged;
  }

  public void save() throws IOException {
    final DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myFile)));
    try {
      output.writeInt(VERSION);
      output.writeInt(myCompilerVersion);
      output.writeInt(myTarget2Id.size());

      for (Map.Entry<String, Integer> entry : myTarget2Id.entrySet()) {
        IOUtil.writeString(entry.getKey(), output);
        output.writeInt(entry.getValue());
      }
    }
    finally {
      output.close();
    }
  }

  public int getId(@NotNull String target) {
    if (myTarget2Id.containsKey(target)) {
      return myTarget2Id.get(target);
    }
    int id = 0;
    while (myUsedIds.contains(id)) {
      id++;
    }
    myTarget2Id.put(target, id);
    myUsedIds.add(id);
    return id;
  }

  public Set<String> getAllTargets() {
    return myTarget2Id.keySet();
  }

  public int removeId(String target) {
    return myTarget2Id.remove(target);
  }
}

@@ -686,16 +686,62 @@
 Logger;%0A
+import com.intellij.openapi.util.io.FileUtil;%0A
 import c
@@ -1750,24 +1750,36 @@
 urn;%0A    %7D%0A%0A
+    try %7B%0A  
     DataInpu
@@ -1869,30 +1869,34 @@
 ile)));%0A    
+  
 try %7B%0A
+  
       final 
@@ -1932,24 +1932,26 @@
 nt();%0A      
+  
 if (dataVers
@@ -1968,32 +1968,34 @@
 SION) %7B%0A        
+ 
+ 
 LOG.info(%22Versio
@@ -2099,32 +2099,34 @@
 RSION);%0A        
+  
 myVersionChanged
@@ -2134,32 +2134,34 @@
 = true;%0A        
+  
 return;%0A      %7D%0A
@@ -2154,27 +2154,31 @@
 turn;%0A      
+  
 %7D%0A%0A
+  
       final 
@@ -2223,24 +2223,26 @@
 nt();%0A      
+  
 if (savedCom
@@ -2272,24 +2272,26 @@
 rVersion) %7B%0A
+  
         LOG.
@@ -2418,32 +2418,34 @@
 rsion);%0A        
+  
 myVersionChanged
@@ -2453,32 +2453,34 @@
 = true;%0A        
+  
 return;%0A      %7D%0A
@@ -2473,27 +2473,31 @@
 turn;%0A      
+ 
+ 
 %7D%0A%0A
+  
       int si
@@ -2514,24 +2514,26 @@
 .readInt();%0A
+  
       while 
@@ -2551,24 +2551,26 @@
 ) %7B%0A        
+  
 final String
@@ -2601,24 +2601,26 @@
 ing(input);%0A
+  
         fina
@@ -2651,24 +2651,26 @@
 ();%0A        
+  
 myTarget2Id.
@@ -2690,24 +2690,26 @@
 d);%0A        
+  
 myUsedIds.ad
@@ -2713,34 +2713,38 @@
 .add(id);%0A      
+ 
+ 
 %7D%0A
+  
     %7D%0A    finall
@@ -2729,32 +2729,34 @@
   %7D%0A      %7D%0A    
+ 
+ 
 finally %7B%0A      
@@ -2745,24 +2745,26 @@
   finally %7B%0A
+  
       input.
@@ -2764,32 +2764,120 @@
  input.close();%0A
+      %7D%0A    %7D%0A    catch (IOException e) %7B%0A      FileUtil.delete(myFile);%0A      throw e;%0A
     %7D%0A  %7D%0A%0A  pub

remove corrupted file with compiler data to ensure that exception won't repeat next time (ZD-3207)