<commit_before>package org.apache.lucene.index;

import java.util.Iterator;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.lucene.document.Document;

public abstract class DocumentsWriterPerThreadPool {
  final static class ThreadState extends ReentrantLock {
    final DocumentsWriterPerThread perThread;

    ThreadState(DocumentsWriterPerThread perThread) {
      this.perThread = perThread;
    }
  }

  private final ThreadState[] perThreads;
  private volatile int numThreadStatesActive;

  public DocumentsWriterPerThreadPool(int maxNumPerThreads) {
    maxNumPerThreads = (maxNumPerThreads < 1) ? IndexWriterConfig.DEFAULT_MAX_THREAD_STATES : maxNumPerThreads;
    this.perThreads = new ThreadState[maxNumPerThreads];

    numThreadStatesActive = 0;
  }

  public void initialize(DocumentsWriter documentsWriter) {
    for (int i = 0; i < perThreads.length; i++) {
      perThreads[i] = new ThreadState(new DocumentsWriterPerThread(documentsWriter.directory, documentsWriter, documentsWriter.chain));
    }
  }

  public int getMaxThreadStates() {
    return perThreads.length;
  }

  public ThreadState newThreadState() {
    if (numThreadStatesActive < perThreads.length) {
      ThreadState state = perThreads[numThreadStatesActive];
      numThreadStatesActive++;
      return state;
    }

    return null;
  }

  public abstract ThreadState getAndLock(Thread requestingThread, DocumentsWriter documentsWriter, Document doc);

  public abstract void clearThreadBindings(ThreadState perThread);

  public abstract void clearAllThreadBindings();

  public Iterator<ThreadState> getAllPerThreadsIterator() {
    return getPerThreadsIterator(this.perThreads.length);
  }

  public Iterator<ThreadState> getActivePerThreadsIterator() {
    return getPerThreadsIterator(this.numThreadStatesActive);
  }

  private Iterator<ThreadState> getPerThreadsIterator(final int upto) {
    return new Iterator<ThreadState>() {
      int i = 0;

      @Override<commit_msg>LUCENE-2324: make newThreadState synchronized<commit_after>package org.apache.lucene.index;

import java.util.Iterator;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.lucene.document.Document;

public abstract class DocumentsWriterPerThreadPool {
  final static class ThreadState extends ReentrantLock {
    final DocumentsWriterPerThread perThread;

    ThreadState(DocumentsWriterPerThread perThread) {
      this.perThread = perThread;
    }
  }

  private final ThreadState[] perThreads;
  private volatile int numThreadStatesActive;

  public DocumentsWriterPerThreadPool(int maxNumPerThreads) {
    maxNumPerThreads = (maxNumPerThreads < 1) ? IndexWriterConfig.DEFAULT_MAX_THREAD_STATES : maxNumPerThreads;
    this.perThreads = new ThreadState[maxNumPerThreads];

    numThreadStatesActive = 0;
  }

  public void initialize(DocumentsWriter documentsWriter) {
    for (int i = 0; i < perThreads.length; i++) {
      perThreads[i] = new ThreadState(new DocumentsWriterPerThread(documentsWriter.directory, documentsWriter, documentsWriter.chain));
    }
  }

  public int getMaxThreadStates() {
    return perThreads.length;
  }

  public synchronized ThreadState newThreadState() {
    if (numThreadStatesActive < perThreads.length) {
      ThreadState state = perThreads[numThreadStatesActive];
      numThreadStatesActive++;
      return state;
    }

    return null;
  }

  public abstract ThreadState getAndLock(Thread requestingThread, DocumentsWriter documentsWriter, Document doc);

  public abstract void clearThreadBindings(ThreadState perThread);

  public abstract void clearAllThreadBindings();

  public Iterator<ThreadState> getAllPerThreadsIterator() {
    return getPerThreadsIterator(this.perThreads.length);
  }

  public Iterator<ThreadState> getActivePerThreadsIterator() {
    return getPerThreadsIterator(this.numThreadStatesActive);
  }

  private Iterator<ThreadState> getPerThreadsIterator(final int upto) {
    return new Iterator<ThreadState>() {
      int i = 0;

      @Override
{'fraction_non_alphanumeric': 0.075527950310559, 'fraction_numerical': 0.0029813664596273293, 'mean_word_length': 4.851744186046512, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2771468', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1100, 'n_words': 247}