<commit_before>package org.threadly.util;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * An abstract implementation of the {@link Service} interface.
 * <p>
 * This implementation is flexible, weather the internal service is scheduled on a thread pool 
 * runs on a unique thread, or has other means of running.
 * 
 * @since 2.6.0
 */
public abstract class AbstractService implements Service {
  private AtomicInteger state = new AtomicInteger(0); // 0 = new, 1 = started, 2 = stopped
  
  @Override
  public void start() throws IllegalStateException {
    if (! startIfNotStarted()) {
      throw new IllegalStateException();
    }
  }
  
  @Override
  public boolean startIfNotStarted() {
    if (state.get() == 0 && state.compareAndSet(0, 1)) {
      startupService();
      
      return true;
    } else {
      return false;
    }
  }
  
  /**
   * Called internally when the service should perform any actions to startup.  It is expected 
   * that this call will block until the service is running.  This invocation will only occur 
   * once.
   */
  protected abstract void startupService();
  
  @Override
  public void stop() {
    if (! stopIfRunning()) {
      throw new IllegalStateException();
    }
  }
  
  @Override
  public boolean stopIfRunning() {
    if (state.get() == 1 && state.compareAndSet(1, 2)) {
      shutdownService();
      
      return true;
    } else {
      return false;
    }
  }
  
  /**
   * Called internally when the service should perform any actions to shutdown.  It is expected 
   * that this call will block until the service is shutdown.  This invocation will only occur 
   * once.
   */
  protected abstract void shutdownService();
  
  @Override
  public boolean isRunning() {
    return state.get() == 1;
  }
  
  @Override
  public boolean hasStopped() {
    return state.get() == 2;
  }
}<commit_msg>AbstractService: Report failures to stop in finalizer automatically<commit_after>package org.threadly.util;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * An abstract implementation of the {@link Service} interface.
 * <p>
 * This implementation is flexible, weather the internal service is scheduled on a thread pool 
 * runs on a unique thread, or has other means of running.
 * 
 * @since 2.6.0
 */
public abstract class AbstractService implements Service {
  private AtomicInteger state = new AtomicInteger(0); // 0 = new, 1 = started, 2 = stopped
  
  @Override
  public void start() throws IllegalStateException {
    if (! startIfNotStarted()) {
      throw new IllegalStateException();
    }
  }
  
  @Override
  public boolean startIfNotStarted() {
    if (state.get() == 0 && state.compareAndSet(0, 1)) {
      startupService();
      
      return true;
    } else {
      return false;
    }
  }
  
  /**
   * Called internally when the service should perform any actions to startup.  It is expected 
   * that this call will block until the service is running.  This invocation will only occur 
   * once.
   */
  protected abstract void startupService();
  
  @Override
  public void stop() {
    if (! stopIfRunning()) {
      throw new IllegalStateException();
    }
  }
  
  @Override
  public boolean stopIfRunning() {
    if (state.get() == 1 && state.compareAndSet(1, 2)) {
      shutdownService();
      
      return true;
    } else {
      return false;
    }
  }
  
  /**
   * Called internally when the service should perform any actions to shutdown.  It is expected 
   * that this call will block until the service is shutdown.  This invocation will only occur 
   * once.
   */
  protected abstract void shutdownService();
  
  @Override
  public boolean isRunning() {
    return state.get() == 1;
  }
  
  @Override
  public boolean hasStopped() {
    return state.get() == 2;
  }
  
  @Override
  protected void finalize() throws Throwable {
    super.finalize();
    if (isRunning()) {
      ExceptionUtils.handleException(new IllegalStateException(getClass() + " was not stopped before GC"));
    }
  }
}
{'fraction_non_alphanumeric': 0.09679024632993281, 'fraction_numerical': 0.007464543418760886, 'mean_word_length': 2.81042654028436, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '411005', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1111, 'n_words': 401}