package i5.las2peer.tools;

import java.util.Arrays;
import java.util.Random;

/**
 * Collection of simple tools not large enough to form a separate class.
 * 
 * 
 *
 */
public class SimpleTools {

	/**
	 * get a long hash value from a given String
	 * 
	 * @param s
	 * @return
	 */
	public static long longHash(String s) {
		long h = 1125899906842597L;
		int len = s.length();

		for (int i = 0; i < len; i++) {
			h = 31 * h + s.charAt(i);
		}
		return h;
	}

	/**
	 * set with available characters for random string generation via {@link #createRandomString}
	 */
	public static final String sRandomStringCharSet = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
			+ "0123456789";

	/**
	 * create a random string of the given length with the possible characters from {@link #sRandomStringCharSet}
	 * 
	 * @param length
	 * @return a random string
	 */
	public static String createRandomString(int length) {
		Random rand = new Random();

		StringBuffer result = new StringBuffer();
		for (int i = 0; i < length; i++) {
			result.append(sRandomStringCharSet.charAt(rand.nextInt(sRandomStringCharSet.length())));
		}

		return result.toString();
	}

	/**
	 * a simple join method to concatenate String representations of the given objects
	 * 
	 * @param objects
	 * @param glue
	 * @return join string
	 */
	public static String join(Object[] objects, String glue) {
		if (objects == null) {
			return "";
		}
		return SimpleTools.join(Arrays.asList(objects), glue);
	}

	/**
	 * a simple join method to concatenate String representations of the given objects
	 * 
	 * @param objects
	 * @param glue
	 * @return join string
	 */
	public static String join(Iterable<?> objects, String glue) {
		if (objects == null) {
			return "";
		}
		StringBuilder result = new StringBuilder();
		boolean first = true;
		for (Object obj : objects) {
			if (!first) {
				result.append(glue);
			}
			result.append(obj.toString());
			first = false;
		}
		return result.toString();
	}

	/**
	 * repeat a String <i>count</i> times
	 * 
	 * @param string
	 * @param count
	 * @return concatenated string
	 * 
	 */
	public static String repeat(String string, int count) {
		if (string == null)
			return null;
		else if (string.isEmpty() || count <= 0)
			return "";

		StringBuffer result = new StringBuffer();
		for (int i = 0; i < count; i++)
			result.append(string);

		return result.toString();
	}

	/**
	 * repeat the string representation of the given object <i>count</i> times
	 * 
	 * @param o
	 * @param count
	 * @return concatenated string
	 */
	public static String repeat(Object o, int count) {
		return repeat(o.toString(), count);
	}

}

@@ -21,16 +21,110 @@
 tools;%0A%0A
+import java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0A
 import j
@@ -2281,17 +2281,19 @@
 == null)
+ %7B
 %0A
-
 %09%09%09retur
@@ -2302,16 +2302,18 @@
 null;%0A%09%09
+%7D 
 else if 
@@ -2344,16 +2344,18 @@
 nt %3C= 0)
+ %7B
 %0A%09%09%09retu
@@ -2360,16 +2360,20 @@
 turn %22%22;
+%0A%09%09%7D
 %0A%0A%09%09Stri
@@ -2443,16 +2443,18 @@
 nt; i++)
+ %7B
 %0A%09%09%09resu
@@ -2471,16 +2471,20 @@
 string);
+%0A%09%09%7D
 %0A%0A%09%09retu
@@ -2714,16 +2714,16 @@
 ount) %7B%0A
-
 %09%09return
@@ -2756,10 +2756,336 @@
 t);%0A%09%7D%0A%0A
+%09public static byte%5B%5D toByteArray(InputStream is) throws IOException %7B%0A%09%09ByteArrayOutputStream data = new ByteArrayOutputStream();%0A%09%09int nRead;%0A%09%09byte%5B%5D buffer = new byte%5B8096%5D;%0A%09%09while ((nRead = is.read(buffer, 0, buffer.length)) != -1) %7B%0A%09%09%09data.write(buffer, 0, nRead);%0A%09%09%7D%0A%09%09data.flush();%0A%09%09return data.toByteArray();%0A%09%7D%0A%0A
 %7D%0A

add method byte[] toByteArray(InputStream) to SimpleTools