package me.allenz.zlog;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * Utility tools.
 * 
 * @author Allenz
 * @since 0.1.0-RELEASE
 */
public class Utils {

	private Utils() {
		throw new UnsupportedOperationException();
	}

	/**
	 * Consider if the string is empty.
	 * 
	 * @param str
	 *            String
	 * @return if the string is {@code null} or length equals 0 return
	 *         true,otherwise return false;
	 * @since 0.1.0-RELEASE
	 */
	public static boolean isEmpty(final String str) {
		return str == null || str.length() == 0;
	}

	/**
	 * Get the length of the string by byte.
	 * 
	 * @param str
	 *            String
	 * @return the length of the string
	 */
	public static int getLength(final String str) {
		if (isEmpty(str)) {
			return 0;
		}
		int length = 0;
		for (int i = 0; i < str.length(); i++) {
			final int ascii = Character.codePointAt(str, i);
			if (ascii >= 0 && ascii <= 255) {
				length++;
			} else {
				length += 2;
			}
		}
		return length;
	}

	/**
	 * Get the integer value of static field the declared in specified class.
	 * 
	 * @param className
	 *            the fullname of the class
	 * @param fieldName
	 *            the name of the field
	 * @param defaultValue
	 *            the default value to return when failed to get value of the
	 *            field
	 * @return the value of the field when successed, or the default value when
	 *         failure
	 * @since 0.1.0-RELEASE
	 */
	public static int intReflectStaticFieldValue(final String className,
			final String fieldName, final int defaultValue) {
		try {
			final Class<?> clazz = Class.forName(className);
			final Field field = clazz.getDeclaredField(fieldName);
			if (Modifier.isStatic(field.getModifiers())
					&& field.getType().getName().equals("int")) {
				field.setAccessible(true);
				return field.getInt(null);
			}
		} catch (final Exception e) {
			LoggerFactory.getInternalLogger().verbose(e,
					"Can not get the value of %s.%s", className, fieldName);
		}
		return defaultValue;
	}

	/**
	 * Get the boolean value of static field the declared in specified class.
	 * 
	 * @param className
	 *            the fullname of the class
	 * @param fieldName
	 *            the name of the field
	 * @param defaultValue
	 *            the default value to return when failed to get value of the
	 *            field
	 * @return the value of the field when successed, or the default value when
	 *         failure
	 * @since 0.1.0-RELEASE
	 */
	public static boolean booleanReflectStaticFieldValue(
			final String className, final String fieldName,
			final boolean defaultValue) {
		try {
			final Class<?> clazz = Class.forName(className);
			final Field field = clazz.getDeclaredField(fieldName);
			if (Modifier.isStatic(field.getModifiers())
					&& field.getType().getName().equals("int")) {
				field.setAccessible(true);
				return field.getBoolean(null);
			}
		} catch (final Exception e) {
			LoggerFactory.getInternalLogger().verbose(e,
					"Can not get the value of %s.%s", className, fieldName);
		}
		return defaultValue;
	}
}

@@ -573,450 +573,8 @@
 %09%7D%0A%0A
-%09/**%0A%09 * Get the length of the string by byte.%0A%09 * %0A%09 * @param str%0A%09 *            String%0A%09 * @return the length of the string%0A%09 */%0A%09public static int getLength(final String str) %7B%0A%09%09if (isEmpty(str)) %7B%0A%09%09%09return 0;%0A%09%09%7D%0A%09%09int length = 0;%0A%09%09for (int i = 0; i %3C str.length(); i++) %7B%0A%09%09%09final int ascii = Character.codePointAt(str, i);%0A%09%09%09if (ascii %3E= 0 && ascii %3C= 255) %7B%0A%09%09%09%09length++;%0A%09%09%09%7D else %7B%0A%09%09%09%09length += 2;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return length;%0A%09%7D%0A%0A
 %09/**

remove Utils.getLength(String) method