package org.stagemonitor.core.util;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Formatter;
import java.util.TimeZone;
import java.util.regex.Pattern;

public class StringUtils {

	public static final Pattern CAMEL_CASE = Pattern.compile("(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])");

	private StringUtils() {
	}

	public static String removeStart(final String str, final String remove) {
		if (remove != null && str.startsWith(remove)) {
			return str.substring(remove.length());
		} else {
			return str;
		}
	}

	public static String capitalize(String self) {
		return Character.toUpperCase(self.charAt(0)) + self.substring(1);
	}

	public static String splitCamelCase(String s) {
		return CAMEL_CASE.matcher(s).replaceAll(" ");
	}

	public static String dateAsIsoString(Date date) {
		TimeZone tz = TimeZone.getDefault();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
		df.setTimeZone(tz);
		return df.format(date);
	}

	public static String timestampAsIsoString(long timestamp) {
		return dateAsIsoString(new Date(timestamp));
	}

	public static String slugify(String s) {
		return replaceWhitespacesWithDash(s.toLowerCase().replaceAll("[^\\w ]+", ""));
	}

	public static boolean isNotEmpty(String s) {
		return !isEmpty(s);
	}

	public static boolean isEmpty(String s) {
		return s == null || s.isEmpty();
	}

	public static String asCsv(String[] strings) {
		return asCsv(Arrays.asList(strings));
	}

	public static String asCsv(Collection<?> values) {
		if (values == null) {
			return null;
		}
		final String s = new ArrayList<Object>(values).toString();
		// removes []
		return s.substring(1, s.length() - 1);
	}

	public static String getLogstashStyleDate() {
		return getLogstashStyleDate(System.currentTimeMillis());
	}

	public static String getLogstashStyleDate(long time) {
		final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
		dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
		return dateFormat.format(new Date(time));
	}

	public static String replaceWhitespacesWithDash(String s) {
		if (s == null) {
			return null;
		}
		return s.replaceAll("\\s", "-");
	}

	public static String toCommaSeparatedString(String... strings) {
		StringBuilder sb = new StringBuilder();
		for (String value : strings) {
			sb.append(value).append(",");
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	public static String removeTrailingSlash(String url) {
		if (url != null && url.endsWith("/")) {
			return url.substring(0, url.length() - 1);
		}
		return url;
	}

	public static String sha1Hash(String s) {
		if (s == null) {
			return null;
		}
		final MessageDigest messageDigest;
		try {
			messageDigest = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
			throw new IllegalStateException(e);
		}
		final byte[] digest = messageDigest.digest(s.getBytes(Charset.forName("UTF-8")));
		return byteToHex(digest);
	}

	private static String byteToHex(final byte[] hash) {
		Formatter formatter = new Formatter();
		for (byte b : hash)
		{
			formatter.format("%02x", b);
		}
		String result = formatter.toString();
		formatter.close();
		return result;
	}
}

@@ -319,36 +319,8 @@
 te;%0A
-import java.util.Formatter;%0A
 impo
@@ -342,16 +342,16 @@
 meZone;%0A
+
 import j
@@ -401,29 +401,30 @@
 gUtils %7B%0A%0A%09p
-ublic
+rivate
  static fina
@@ -538,16 +538,90 @@
 a-z%5D)%22);
+%0A%09private static final char%5B%5D hexArray = %220123456789abcdef%22.toCharArray();
 %0A%0A%09priva
@@ -3229,24 +3229,25 @@
 %09return byte
+s
 ToHex(digest
@@ -3254,23 +3254,102 @@
 );%0A%09%7D%0A%0A%09
-private
+// kudos to maybeWeCouldStealAVan (http://stackoverflow.com/a/9855338/1125055)%0A%09public
  static 
@@ -3363,214 +3363,270 @@
 byte
+s
 ToHex(
-final 
 byte%5B%5D 
-hash
+bytes
 ) %7B%0A%09%09
-Formatter formatter = new Formatter();%0A%09%09for (byte b : hash)%0A%09%09%7B%0A%09%09%09formatter.format(%22%2502x%22, b);%0A%09%09%7D%0A%09%09String result = formatter.toString();%0A%09%09formatter.close();%0A%09%09return result
+char%5B%5D hexChars = new char%5Bbytes.length * 2%5D;%0A%09%09for ( int j = 0; j %3C bytes.length; j++ ) %7B%0A%09%09%09int v = bytes%5Bj%5D & 0xFF;%0A%09%09%09hexChars%5Bj * 2%5D = hexArray%5Bv %3E%3E%3E 4%5D;%0A%09%09%09hexChars%5Bj * 2 + 1%5D = hexArray%5Bv & 0x0F%5D;%0A%09%09%7D%0A%09%09return new String(hexChars)
 ;%0A%09%7D

Optimize StringUtils.sha1Hash