<commit_before>/*
 * Copyright 2000-2005 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.util;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Disposer;
import com.intellij.util.containers.HashMap;
import com.intellij.util.containers.LongArrayList;

import java.util.ArrayList;
import java.util.Iterator;

public class Alarm implements Disposable {
  private static final Logger LOG = Logger.getInstance("#com.intellij.util.Alarm");

  private final Object LOCK = new Object();

  private final MyThread myThread;
  private HashMap<Runnable, Runnable> myOriginalToThreadRequestMap = new HashMap<Runnable, Runnable>();

  private static MyThread ourThreadNormal = new MyThread(false);
  private static MyThread ourThreadUseSwing = new MyThread(true);

  public void dispose() {
    cancelAllRequests();
  }

  static {
    ourThreadNormal.start();
    ourThreadUseSwing.start();
  }

  public enum ThreadToUse {
    SWING_THREAD,
    SHARED_THREAD,
    OWN_THREAD
  }

  /**
   * Creates alarm that works in Swing thread
   */
  public Alarm() {
    this(ThreadToUse.SWING_THREAD);
  }

  public Alarm(Disposable parentDisposable) {
    this(ThreadToUse.SWING_THREAD, parentDisposable);
  }

  public Alarm(ThreadToUse threadToUse) {
    this(threadToUse, null);
  }
  public Alarm(ThreadToUse threadToUse, Disposable parentDisposable) {
    if (threadToUse == ThreadToUse.SWING_THREAD) {
      myThread = ourThreadUseSwing;
    }
    else if (threadToUse == ThreadToUse.SHARED_THREAD) {
      myThread = ourThreadNormal;
    }
    else {
      myThread = new MyThread(false);
      myThread.start();
    }

    if (parentDisposable != null) {
      Disposer.register(parentDisposable, this);
    }
  }

  public void addRequest(final Runnable request, int delay) {
    _addRequest(request, delay, myThread == ourThreadUseSwing ? ModalityState.current() : null);
  }

  public void addRequest(final Runnable request, int delay, ModalityState modalityState) {
    LOG.assertTrue(myThread == ourThreadUseSwing);
    _addRequest(request, delay, modalityState);
  }

  private void _addRequest(final Runnable request, int delay, ModalityState modalityState) {
    synchronized (LOCK) {
      Runnable request1 = new Runnable() {
        public void run() {
          synchronized (LOCK) {
            myOriginalToThreadRequestMap.remove(request);
          }
          try {
            request.run();
          }
          catch (Throwable e) {
            LOG.error(e);
          }
        }
      };
      myOriginalToThreadRequestMap.put(request, request1);
      myThread.addRequest(request1, delay, modalityState);
    }
  }

  public boolean cancelRequest(Runnable request) {
    synchronized (LOCK) {
      Runnable request1 = myOriginalToThreadRequestMap.get(request);
      if (request1 == null) return false;
      boolean success = myThread.cancelRequest(request1);<commit_msg>Fixed disposed<commit_after>/*
 * Copyright 2000-2005 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.util;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Disposer;
import com.intellij.util.containers.HashMap;
import com.intellij.util.containers.LongArrayList;

import java.util.ArrayList;
import java.util.Iterator;

public class Alarm implements Disposable {
  private static final Logger LOG = Logger.getInstance("#com.intellij.util.Alarm");

  private final Object LOCK = new Object();

  private final MyThread myThread;
  private HashMap<Runnable, Runnable> myOriginalToThreadRequestMap = new HashMap<Runnable, Runnable>();

  private static MyThread ourThreadNormal = new MyThread(false);
  private static MyThread ourThreadUseSwing = new MyThread(true);
  private boolean myDisposed;

  public void dispose() {
    myDisposed = true;
    cancelAllRequests();
  }

  static {
    ourThreadNormal.start();
    ourThreadUseSwing.start();
  }

  public enum ThreadToUse {
    SWING_THREAD,
    SHARED_THREAD,
    OWN_THREAD
  }

  /**
   * Creates alarm that works in Swing thread
   */
  public Alarm() {
    this(ThreadToUse.SWING_THREAD);
  }

  public Alarm(Disposable parentDisposable) {
    this(ThreadToUse.SWING_THREAD, parentDisposable);
  }

  public Alarm(ThreadToUse threadToUse) {
    this(threadToUse, null);
  }
  public Alarm(ThreadToUse threadToUse, Disposable parentDisposable) {
    if (threadToUse == ThreadToUse.SWING_THREAD) {
      myThread = ourThreadUseSwing;
    }
    else if (threadToUse == ThreadToUse.SHARED_THREAD) {
      myThread = ourThreadNormal;
    }
    else {
      myThread = new MyThread(false);
      myThread.start();
    }

    if (parentDisposable != null) {
      Disposer.register(parentDisposable, this);
    }
  }

  public void addRequest(final Runnable request, int delay) {
    _addRequest(request, delay, myThread == ourThreadUseSwing ? ModalityState.current() : null);
  }

  public void addRequest(final Runnable request, int delay, ModalityState modalityState) {
    LOG.assertTrue(myThread == ourThreadUseSwing);
    _addRequest(request, delay, modalityState);
  }

  private void _addRequest(final Runnable request, int delay, ModalityState modalityState) {
    synchronized (LOCK) {
      Runnable request1 = new Runnable() {
        public void run() {
          synchronized (LOCK) {
            myOriginalToThreadRequestMap.remove(request);
          }
          try {
            if (!myDisposed) {
              request.run();
            }
          }
          catch (Throwable e) {
            LOG.error(e);
          }
        }
      };
      myOriginalToThreadRequestMap.put(request, request1);
      myThread.addRequest(request1, delay, modalityState);
    }
  }

  public boolean cancelRequest(Runnable request) {
    synchronized (LOCK) {
      Runnable request1 = myOriginalToThreadRequestMap.get(request);
      if (request1 == null) return false;
      boolean success = myThread.cancelRequest(request1);
{'fraction_non_alphanumeric': 0.09099577714207874, 'fraction_numerical': 0.004903964037597058, 'mean_word_length': 3.4713763702801463, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4541613', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2109, 'n_words': 563}