package net.kopeph.ld31.util;

import java.io.Closeable;
import java.io.IOException;

import net.kopeph.ld31.LD31;
import processing.core.PConstants;
import processing.core.PImage;

/**
 * @author alexg
 */
public class Util {
	private Util() {
		throw new AssertionError("No Instantiation of: " + getClass().getName()); //$NON-NLS-1$
	}

	/**
	 * Closes the supplied resource, catching all Exceptions.
	 * @throws RuntimeException when the resource's close() throws an exception.
	 */
	public static void forceClose(AutoCloseable res) {
		try {
			if (res != null)
				res.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Closes the supplied resource, catching all Exceptions.
	 * @throws RuntimeException when the resource's close() throws an exception.
	 */
	public static void forceClose(Closeable res) {
		try {
			if (res != null)
				res.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/** @return 0 if number is zero, otherwise equivalent to abs(d)/d */
	public static int sign(double d) {
		if (d > 0) return  1;
		if (d < 0) return -1;
				   return  0;
	}

	public static PImage crop(PImage img, int x, int y, int w, int h) {
		PImage cropped = LD31.getContext().createImage(w, h, PConstants.ARGB);
		cropped.copy(img, x, y, w, h, 0, 0, w, h);

		return cropped;
	}

	public static boolean boxContains(double x, double y, double w, double h,
	                                  double xTest, double yTest) {
		return xTest > x     && yTest > y &&
		       xTest < x + w && yTest < y + h;
	}
}

@@ -77,16 +77,43 @@
 ception;
+%0Aimport java.util.Iterator;
 %0A%0Aimport
@@ -1589,11 +1589,486 @@
  + h;%0A%09%7D
+%0A%0A%09/**%0A%09 * Missing counterpart for String.split()%0A%09 *%0A%09 * @param array%0A%09 * @param joiner The delimiter to add between each element%0A%09 * @return The joined string%0A%09 */%0A%09public static %3CT%3E String join(Iterable%3CT%3E array, String joiner) %7B%0A%09%09StringBuilder b = new StringBuilder();%0A%09%09Iterator%3CT%3E i = array.iterator();%0A%09%09boolean first = true;%0A%0A%09%09while (i.hasNext()) %7B%0A%09%09%09if (first)%0A%09%09%09%09first = false;%0A%09%09%09else%0A%09%09%09%09b.append(joiner);%0A%09%09%09b.append(i.next());%0A%09%09%7D%0A%0A%09%09return b.toString();%0A%09%7D
 %0A%7D%0A

Add Util.join()