package com.airlock.iam.base.misc.concurrent;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public final class ThreadUtils {
   public static void sleep(Duration duration) {
      sleep(duration.toMillis(), TimeUnit.MILLISECONDS);
   }

   public static void sleep(long sleepFor, TimeUnit timeUnit) {
      try {
         timeUnit.sleep(sleepFor);
      } catch (InterruptedException e) {
         Thread.currentThread().interrupt();
         throw new RuntimeException(e);
      }
   }

   public static void sleepInterruptibly(Duration duration) {
      sleepInterruptibly(duration.toMillis(), TimeUnit.MILLISECONDS);
   }

   public static void sleepInterruptibly(long sleepFor, TimeUnit timeUnit) {
      try {
         timeUnit.sleep(sleepFor);
      } catch (InterruptedException e) {
         Thread.currentThread().interrupt();
      }
   }

   private ThreadUtils() {
      throw new AssertionError();
   }
}
