All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bukkit.craftbukkit.util.Waitable Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package org.bukkit.craftbukkit.util;

import java.util.concurrent.ExecutionException;


public abstract class Waitable implements Runnable {
  Throwable t = null;
  T value = null;
  Status status = Status.WAITING;

  public final void run() {
    synchronized (this) {
      if (status != Status.WAITING) {
        throw new IllegalStateException("Invalid state " + status);
      }
      status = Status.RUNNING;
    }
    try {
      value = evaluate();
    } catch (Throwable t) {
      this.t = t;
    } finally {
      synchronized (this) {
        status = Status.FINISHED;
        this.notifyAll();
      }
    }
  }

  protected abstract T evaluate();

  public synchronized T get() throws InterruptedException, ExecutionException {
    while (status != Status.FINISHED) {
      this.wait();
    }
    if (t != null) {
      throw new ExecutionException(t);
    }
    return value;
  }

  private enum Status {
    WAITING,
    RUNNING,
    FINISHED,
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy