package org.squiddev.plethora.api.method;

import com.google.common.base.Preconditions;

import java.util.concurrent.Callable;

/**
 * The returnValue of a method
 */
public final class MethodResult {
	private static final MethodResult empty = new MethodResult(null);

	private final Object[] result;
	private final Callable<MethodResult> next;
	private final int delay;

	private MethodResult(Object[] result) {
		this.result = result;
		this.next = null;
		this.delay = -1;
	}

	private MethodResult(Callable<MethodResult> next, int delay) {
		Preconditions.checkNotNull(next, "next cannot be null");
		Preconditions.checkArgument(delay >= 0, "delay must be >= 0");

		this.result = null;
		this.next = next;
		this.delay = delay;
	}

	public boolean isFinal() {
		return next == null;
	}

	public Object[] getResult() {
		if (!isFinal()) throw new IllegalStateException("MethodResult is a callback");
		return result;
	}

	public Callable<MethodResult> getCallback() {
		if (isFinal()) throw new IllegalStateException("MethodResult is a result");
		return next;
	}

	public int getDelay() {
		if (isFinal()) throw new IllegalStateException("MethodResult is a result");
		return delay;
	}

	/**
	 * Defer a function until next tick
	 *
	 * @param next The callback to execute
	 * @return The built method returnValue
	 * @see #nextTick(Runnable)
	 */
	public static MethodResult nextTick(Callable<MethodResult> next) {
		return new MethodResult(next, 0);
	}

	/**
	 * Defer a function until next tick
	 *
	 * @param next The callback to execute
	 * @return The built method returnValue
	 * @see #nextTick(Callable)
	 */
	public static MethodResult nextTick(Runnable next) {
		return new MethodResult(wrap(next), 0);
	}

	/**
	 * Delay a function by a number of ticks
	 *
	 * @param delay The number of ticks to sit idle before executing. 0 will result in the method being executed next tick.
	 * @param next  The callback to execute
	 * @return The built method returnValue
	 * @see #delayed(int, Runnable)
	 */
	public static MethodResult delayed(int delay, Callable<MethodResult> next) {
		return new MethodResult(next, delay);
	}

	/**
	 * Delay a function by a number of ticks
	 *
	 * @param delay The number of ticks to sit idle before executing. 0 will result in the method being executed n
	 * @param next  The callback to execute
	 * @return The built method returnValue
	 * @see #delayed(int, Callable)
	 */
	public static MethodResult delayed(int delay, Runnable next) {
		return new MethodResult(wrap(next), delay);
	}

	/**
	 * Get a final method returnValue
	 *
	 * @param args The arguments to return
	 * @return The built method returnValue
	 */
	public static MethodResult result(Object... args) {
		return new MethodResult(args);
	}

	/**
	 * Get a final method returnValue with no values
	 *
	 * @return An empty method returnValue
	 */
	public static MethodResult empty() {
		return empty;
	}

	private static Callable<MethodResult> wrap(final Runnable runnable) {
		return new Callable<MethodResult>() {
			@Override
			public MethodResult call() throws Exception {
				runnable.run();
				return empty;
			}
		};
	}
}

@@ -134,25 +134,20 @@
 * The re
-turnValue
+sult
  of a me
@@ -1288,34 +1288,28 @@
 e built 
-m
+M
 ethod
- returnValue
+Result
 %0A%09 * @se
@@ -1551,34 +1551,28 @@
 e built 
-m
+M
 ethod
- returnValue
+Result
 %0A%09 * @se
@@ -1933,34 +1933,28 @@
 e built 
-m
+M
 ethod
- returnValue
+Result
 %0A%09 * @se
@@ -2332,34 +2332,28 @@
 e built 
-m
+M
 ethod
- returnValue
+Result
 %0A%09 * @se
@@ -2521,35 +2521,36 @@
 inal method 
-returnValue
+MethodResult
 %0A%09 *%0A%09 * @pa
@@ -2604,34 +2604,28 @@
 e built 
-m
+M
 ethod
- returnValue
+Result
 %0A%09 */%0A%09p
@@ -2733,34 +2733,283 @@
 a final 
-method returnValue
+MethodResult representing a failure%0A%09 *%0A%09 * @param message The failure message%0A%09 * @return The built MethodResult%0A%09 */%0A%09public static MethodResult failure(String message) %7B%0A%09%09return new MethodResult(new Object%5B%5D%7Bfalse, message%7D);%0A%09%7D%0A%0A%09/**%0A%09 * Get a final MethodResult
  with no
@@ -3045,26 +3045,20 @@
 pty 
-m
+M
 ethod
- returnValue
+Result
 %0A%09 *

Fix typo