<commit_before>package com.intellij.cvsSupport2.cvsoperations.cvsContent;

import com.intellij.cvsSupport2.CvsUtil;
import com.intellij.cvsSupport2.connections.CvsEnvironment;
import com.intellij.cvsSupport2.connections.CvsRootProvider;
import com.intellij.cvsSupport2.cvsoperations.common.CvsExecutionEnvironment;
import com.intellij.cvsSupport2.cvsoperations.common.LocalPathIndifferentOperation;
import com.intellij.cvsSupport2.cvsoperations.dateOrRevision.RevisionOrDate;
import com.intellij.cvsSupport2.cvsoperations.dateOrRevision.RevisionOrDateImpl;
import com.intellij.cvsSupport2.errorHandling.CannotFindCvsRootException;
import com.intellij.cvsSupport2.history.CvsRevisionNumber;
import com.intellij.cvsSupport2.util.CvsVfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.netbeans.lib.cvsclient.admin.Entry;
import org.netbeans.lib.cvsclient.command.Command;
import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
import org.netbeans.lib.cvsclient.file.FileObject;

import java.io.File;
import java.util.Collection;
import java.util.Collections;

public class GetFileContentOperation extends LocalPathIndifferentOperation {

  private final static byte NOT_LOADED = 0;
  private final static byte FILE_NOT_FOUND = 1;
  private final static byte DELETED = 2;
  private final static byte SUCCESSFULLY_LOADED = 3;
  private final static byte LOADING = 4;

  private byte myState = NOT_LOADED;

  private StringBuffer myContent = null;
  private byte[] myBinaryContent = null;

  private byte[] myFileBytes = null;
  private String myRevision;
  private String myModuleName;
  private CvsRootProvider myRoot;
  private CvsRevisionNumber myCvsRevisionNumber;
  private RevisionOrDate myRevisionOrDate;

  public static GetFileContentOperation createForFile(VirtualFile file, RevisionOrDate revisionOrDate)
    throws CannotFindCvsRootException {
    File ioFile = CvsVfsUtil.getFileFor(file);
    return new GetFileContentOperation(new File(getPathInRepository(ioFile)),
                                       CvsRootProvider.createOn(ioFile), revisionOrDate);
  }

  public static GetFileContentOperation createForFile(VirtualFile file) throws CannotFindCvsRootException {
    LOG.assertTrue(file != null);
    return createForFile(file, RevisionOrDateImpl.createOn(file));
  }

  public GetFileContentOperation(File cvsFile, CvsEnvironment environment, RevisionOrDate revisionOrDate) {
    super(environment);
    LOG.assertTrue(revisionOrDate != null);
    myRevisionOrDate = revisionOrDate;
    myRoot = CvsRootProvider.createOn(null, environment);
    myModuleName = cvsFile.getPath().replace(File.separatorChar, '/');
    myCvsRevisionNumber = myRevisionOrDate.getCvsRevisionNumber();
  }

  private static String getPathInRepository(File file) {
    return CvsUtil.getModuleName(file);
  }

  protected Collection getAllCvsRoots() {
    return Collections.singleton(myRoot);
  }

  protected Command createCommand(CvsRootProvider root, CvsExecutionEnvironment cvsExecutionEnvironment) {
    myState = LOADING;
    myRoot.changeAdminRootTo(new File("."));
    myRoot.changeLocalRootTo(new File("."));
    CheckoutCommand command = new CheckoutCommand();
    command.setRecursive(false);
    command.addModule(myModuleName);
    command.setPrintToOutput(true);

    myRevisionOrDate.setForCommand(command);

    return command;
  }

  public String getRevision() {
    if (!isLoaded()) {
      return myRevisionOrDate.getRevision();
    }
    else {
      return myRevision;
    }

  }

  public synchronized byte[] getFileBytes() {
    if (myFileBytes == null) {
      myFileBytes = loadFileBytes();
    }
    return myFileBytes;
  }

  public boolean isDeleted() {
    if (myState == LOADING) {
      getFileBytes();
    }
    return myState == DELETED;
  }

  private synchronized byte[] loadFileBytes() {
    LOG.assertTrue(myState == LOADING, "state = " + String.valueOf(myState));
    if (myContent == null && myBinaryContent == null) {
      myState = DELETED;
      return new byte[0];
    }
    else {
      myState = SUCCESSFULLY_LOADED;
      return myBinaryContent != null ? myBinaryContent : myContent.toString().getBytes();
    }
  }

  public void gotEntry(FileObject abstractFileObject, Entry entry) {
    if (entry == null) {
      myState = DELETED;
      myFileBytes = new byte[0];
    }
    else {
      myRevision = entry.getRevision();
      myCvsRevisionNumber = new CvsRevisionNumber(myRevision);
    }
  }

  public boolean fileNotFound() {
    getFileBytes();
    return myState == FILE_NOT_FOUND;
  }

  public boolean isLoaded() {<commit_msg>cvs tests fixed<commit_after>package com.intellij.cvsSupport2.cvsoperations.cvsContent;

import com.intellij.cvsSupport2.CvsUtil;
import com.intellij.cvsSupport2.connections.CvsEnvironment;
import com.intellij.cvsSupport2.connections.CvsRootProvider;
import com.intellij.cvsSupport2.cvsoperations.common.CvsExecutionEnvironment;
import com.intellij.cvsSupport2.cvsoperations.common.LocalPathIndifferentOperation;
import com.intellij.cvsSupport2.cvsoperations.dateOrRevision.RevisionOrDate;
import com.intellij.cvsSupport2.cvsoperations.dateOrRevision.RevisionOrDateImpl;
import com.intellij.cvsSupport2.errorHandling.CannotFindCvsRootException;
import com.intellij.cvsSupport2.history.CvsRevisionNumber;
import com.intellij.cvsSupport2.util.CvsVfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.netbeans.lib.cvsclient.admin.Entry;
import org.netbeans.lib.cvsclient.command.Command;
import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
import org.netbeans.lib.cvsclient.file.FileObject;

import java.io.File;
import java.util.Collection;
import java.util.Collections;

public class GetFileContentOperation extends LocalPathIndifferentOperation {

  private final static byte NOT_LOADED = 0;
  private final static byte FILE_NOT_FOUND = 1;
  private final static byte DELETED = 2;
  private final static byte SUCCESSFULLY_LOADED = 3;
  private final static byte LOADING = 4;

  private byte myState = NOT_LOADED;

  private StringBuffer myContent = null;
  private byte[] myBinaryContent = null;

  private byte[] myFileBytes = null;
  private String myRevision;
  private String myModuleName;
  private CvsRootProvider myRoot;
  private CvsRevisionNumber myCvsRevisionNumber;
  private RevisionOrDate myRevisionOrDate;

  public static GetFileContentOperation createForFile(VirtualFile file, RevisionOrDate revisionOrDate)
    throws CannotFindCvsRootException {
    File ioFile = CvsVfsUtil.getFileFor(file);
    return new GetFileContentOperation(new File(getPathInRepository(ioFile)),
                                       CvsRootProvider.createOn(ioFile), revisionOrDate);
  }

  public static GetFileContentOperation createForFile(VirtualFile file) throws CannotFindCvsRootException {
    LOG.assertTrue(file != null);
    return createForFile(file, RevisionOrDateImpl.createOn(file));
  }

  public GetFileContentOperation(File cvsFile, CvsEnvironment environment, RevisionOrDate revisionOrDate) {
    super(environment);
    LOG.assertTrue(revisionOrDate != null);
    myRevisionOrDate = revisionOrDate;
    myRoot = CvsRootProvider.createOn(null, environment);
    myModuleName = cvsFile.getPath().replace(File.separatorChar, '/');
    myCvsRevisionNumber = myRevisionOrDate.getCvsRevisionNumber();
  }

  private static String getPathInRepository(File file) {
    return CvsUtil.getModuleName(file);
  }

  protected Collection getAllCvsRoots() {
    return Collections.singleton(myRoot);
  }

  protected Command createCommand(CvsRootProvider root, CvsExecutionEnvironment cvsExecutionEnvironment) {
    myState = LOADING;
    myRoot.changeAdminRootTo(new File("."));
    myRoot.changeLocalRootTo(new File("."));
    CheckoutCommand command = new CheckoutCommand();
    command.setRecursive(false);
    command.addModule(myModuleName);
    command.setPrintToOutput(true);

    myRevisionOrDate.setForCommand(command);

    return command;
  }

  public String getRevision() {
    if (!isLoaded()) {
      return myRevisionOrDate.getRevision();
    }
    else {
      return myRevision;
    }

  }

  public synchronized byte[] getFileBytes() {
    if (myFileBytes == null) {
      myFileBytes = loadFileBytes();
    }
    return myFileBytes;
  }

  public boolean isDeleted() {
    if (myState == LOADING) {
      getFileBytes();
    }
    return myState == DELETED;
  }

  private synchronized byte[] loadFileBytes() {
    LOG.assertTrue(myState == LOADING, "state = " + String.valueOf(myState));
    if (myContent == null && myBinaryContent == null) {
      myState = DELETED;
      return null;
    }
    else {
      myState = SUCCESSFULLY_LOADED;
      return myBinaryContent != null ? myBinaryContent : myContent.toString().getBytes();
    }
  }

  public void gotEntry(FileObject abstractFileObject, Entry entry) {
    if (entry == null) {
      myState = DELETED;
      myFileBytes = new byte[0];
    }
    else {
      myRevision = entry.getRevision();
      myCvsRevisionNumber = new CvsRevisionNumber(myRevision);
    }
  }

  public boolean fileNotFound() {
    getFileBytes();
    return myState == FILE_NOT_FOUND;
  }

  public boolean isLoaded() {
{'fraction_non_alphanumeric': 0.08927989573150863, 'fraction_numerical': 0.0038014554143586403, 'mean_word_length': 4.442080378250591, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4159459', 'n_tokens_mistral': 2851, 'n_tokens_neox': 2744, 'n_words': 549}