/*
 * Copyright 2000-2009 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.openapi.vcs.changes.patch;

import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.diff.impl.patch.TextFilePatch;
import com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

public class LazyPatchContentRevision implements ContentRevision {
  private volatile String myContent;
  private final VirtualFile myVf;
  private final FilePath myNewFilePath;
  private final String myRevision;
  private final TextFilePatch myPatch;
  private volatile boolean myPatchApplyFailed;

  public LazyPatchContentRevision(final VirtualFile vf, final FilePath newFilePath, final String revision, final TextFilePatch patch) {
    myVf = vf;
    myNewFilePath = newFilePath;
    myRevision = revision;
    myPatch = patch;
  }

  public String getContent() {
    if (myContent == null) {
      final String localContext = ReadAction.compute(() -> {
        final Document doc = FileDocumentManager.getInstance().getDocument(myVf);
        return doc == null ? null : doc.getText();
      });
      if (localContext == null) {
        myPatchApplyFailed = true;
        return null;
      }

      GenericPatchApplier.AppliedPatch appliedPatch = GenericPatchApplier.apply(localContext, myPatch.getHunks());
      if (appliedPatch != null) {
        myContent = appliedPatch.patchedText;
      } else {
        myPatchApplyFailed = true;
      }
    }
    return myContent;
  }

  public boolean isPatchApplyFailed() {
    return myPatchApplyFailed;
  }

  @NotNull
  public FilePath getFile() {
    return myNewFilePath;
  }

  @NotNull
  public VcsRevisionNumber getRevisionNumber() {
    return new VcsRevisionNumber() {
      public String asString() {
        return myRevision;
      }

      public int compareTo(final VcsRevisionNumber o) {
        return 0;
      }
    };
  }
}

@@ -926,24 +926,81 @@
 entManager;%0A
+import com.intellij.openapi.util.AtomicNotNullLazyValue;%0A
 import com.i
@@ -1231,16 +1231,59 @@
 NotNull;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Apublic
@@ -1347,45 +1347,8 @@
 n %7B%0A
-  private volatile String myContent;%0A
   pr
@@ -1491,16 +1491,17 @@
 yPatch;%0A
+%0A
   privat
@@ -1506,43 +1506,49 @@
 ate 
-volatile boolean myPatchApplyFailed
+final AtomicNotNullLazyValue%3CData%3E myData
 ;%0A%0A 
@@ -1782,32 +1782,67 @@
 ch;%0A
+%0A
   
-%7D%0A%0A  public String get
+  myData = AtomicNotNullLazyValue.createValue(() -%3E load
 Cont
@@ -1850,51 +1850,50 @@
 nt()
- %7B%0A    if (myContent == null) %7B%0A      final
+);%0A  %7D%0A%0A  private Data loadContent() %7B%0A   
  Str
@@ -1948,16 +1948,8 @@
     
-  final 
 Docu
@@ -2014,26 +2014,24 @@
 yVf);%0A      
-  
 return doc =
@@ -2069,16 +2069,12 @@
     
-  
 %7D);%0A
-  
     
@@ -2111,69 +2111,44 @@
     
-  myPatchApplyFailed =
+return new Data(null,
  true
+)
 ;%0A
-        return null;%0A  
     %7D%0A%0A
-  
     
@@ -2260,18 +2260,16 @@
 ));%0A    
-  
 if (appl
@@ -2298,22 +2298,24 @@
     
-  myContent = 
+return new Data(
 appl
@@ -2334,25 +2334,35 @@
 chedText
+, false)
 ;%0A    
+%7D%0A
   
-%7D
+ 
  else %7B%0A
@@ -2371,65 +2371,121 @@
     
-  myPatchApplyFailed =
+return new Data(null,
  true
+)
 ;%0A    
+%7D%0A
   %7D%0A
+%0A
   
-  %7D%0A    return myC
+@Nullable%0A  public String getContent() %7B%0A    return myData.getValue().c
 onte
@@ -2546,17 +2546,33 @@
 eturn my
-P
+Data.getValue().p
 atchAppl
@@ -2915,11 +2915,287 @@
   %7D;%0A  %7D
+%0A%0A  private static class Data %7B%0A    @Nullable public final String content;%0A    public final boolean patchApplyFailed;%0A%0A    public Data(@Nullable String content, boolean patchApplyFailed) %7B%0A      this.content = content;%0A      this.patchApplyFailed = patchApplyFailed;%0A    %7D%0A  %7D
 %0A%7D%0A

fix atomicity of getting patched revision content