// Copyright 2014 Boundary, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.boundary.plugin.sdk;

import java.net.InetAddress;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PluginUtil {

	private static Logger LOG = LoggerFactory.getLogger(PluginUtil.class);

	/**
	 * Returns the localhost name the JVM is running on
	 * 
	 * @return {@link String} local host name
	 */
	public static String getHostname() {
		String hostname = null;
		try {
			InetAddress addr;
			addr = InetAddress.getLocalHost();
			hostname = addr.getHostName();
		} catch (Exception e) {
			LOG.info(e.toString());
		}
		return hostname;
	}

	/**
	 * Transform a camel case string into a upper case string with underscores
	 * 
	 * @param s {@String} to transform
	 * @param d {@char} replacement character for white space
	 * @return {@link String}
	 */
	public static String toUpperUnderscore(String s, Character d) {
		StringBuilder builder = new StringBuilder();
		s = s.trim();

		boolean first = true;
		for (int i = 0; i < s.length(); i++) {
			if (Character.isWhitespace(s.charAt(i))) {
				builder.append(d);
			} else {
				if (Character.isUpperCase(s.charAt(i))) {
					if (first == false) {
						if (i + 1 < s.length()
								&& Character.isLowerCase(s.charAt(i + 1))) {
							builder.append(d);
						}
					}
				}
				builder.append(Character.toUpperCase(s.charAt(i)));
			}
			first = false;
		}
		StringBuilder r = new StringBuilder();
		r.append('\\').append(d).append('\\').append(d);
		
		return builder.toString().replaceAll(r.toString(),d.toString());
	}
	
	public static String camelCaseToSpaceSeparated(String s) {
		return StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(s),' ');
	}
	
	public static String splitCamelCase(String s) {
		   return s.replaceAll(
		      String.format("%s|%s|%s",
		         "(?<=[A-Z])(?=[A-Z][a-z])",
		         "(?<=[^A-Z])(?=[A-Z])",
		         "(?<=[A-Za-z])(?=[^A-Za-z])"
		      ),
		      " "
		   );
		}

}

@@ -752,16 +752,86 @@
 ctory;%0A%0A
+/**%0A * Various utility functions that can be leveraged by plugins%0A */%0A
 public c
@@ -2223,406 +2223,377 @@
 %0A%09%0A%09
-public static String camelCaseToSpaceSeparated(String s) %7B%0A%09%09return StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(s),' ');%0A%09%7D%0A%09%0A%09public static String splitCamelCase(String s) %7B%0A%09%09   return s.replaceAll(%0A%09%09      String.format(%22%25s%7C%25s%7C%25s%22,%0A%09%09         %22(?%3C=%5BA-Z%5D)(?=%5BA-Z%5D%5Ba-z%5D)%22,%0A%09%09         %22(?%3C=%5B%5EA-Z%5D)(?=%5BA-Z%5D)%22,%0A%09%09         %22(?%3C=%5BA-Za-z%5D)(?=%5B%5EA-Za-z%5D)%22%0A%09%09      ),%0A%09%09      %22 %22%0A%09%09   
+/**%0A%09 * Transforms a camel case string into a string with words separated by a space%0A%09 * @param s %7B@link String%7D camel case string to transform%0A%09 * @return %7B@link String%7D transformed to words and spaces%0A%09 */%0A%09public static String camelCaseToSpaceSeparated(String s) %7B%0A%09%09return StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(s),' ').replaceAll(%22%5C%5Cs+%22,%22 %22
 );%0A
-%09
 %09%7D%0A
-%0A
 %7D%0A

Remove dead code, add javadoc, fix function to eliminate multiple spaces