ObjectTypeConvertor_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > ObjectTypeConvertor

ObjectTypeConvertor

 2015/1/14 15:29:52  lihongtai  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.common.utils;importjava.math.BigDecimal;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.HashSet;importjava.util.Set;importorg.apache.commons.lang.ArrayUtils;importorg.apache.commons.lang.StringUtils;importcom.inetpsa
  • 标签:
class="ObjectTypeConvertor">package com.common.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.inetpsa.rs1.common.constants.Constants;
import com.inetpsa.sfj.common.constants.SfjConstants;

/**
 * The Class ObjectTypeConvertor.
 */
public final class ObjectTypeConvertor {

	/**
	 * Instantiates a new object type convertor.
	 */
	private ObjectTypeConvertor() {
	}

	/**
	 * Convert to string.
	 * 
	 * @param obj
	 *            the obj
	 * @return the string
	 */
	public static String convertToString(Object obj) {
		Object convertedVal = convertType(obj);
		// convert BigDecimal/Long/Integer to String
		if (convertedVal instanceof Number) {
			return ((Number) convertedVal).toString();
		}

		return (String) convertedVal;
	}

	/**
	 * Convert to translated string.
	 * 
	 * @param obj
	 *            the obj
	 * @return the string
	 */
	public static String convertToTranslatedString(Object obj) {
		String translatedStr = convertToString(obj);
		if (!Constants.TRANSLATION_ITERATION_ERROR.equals(translatedStr)) {
			return translatedStr;
		}
		return null;
	}

	/**
	 * Convert date to string.
	 * 
	 * @param obj
	 *            the obj
	 * @param sdf
	 *            the sdf
	 * @return the string
	 */
	public static String convertDateToString(Object obj, SimpleDateFormat sdf) {
		if (obj == null) {
			return null;
		} else if (obj instanceof Date) {
			return sdf.format((Date) obj);
		}
		return null;
	}

	/**
	 * Convert to boolean.
	 * 
	 * @param obj
	 *            the obj
	 * @return the boolean
	 */
	public static Boolean convertToBoolean(Object obj) {
		Boolean convertedVal = convertToBooleanNullable(obj);
		if (convertedVal == null) {
			return Boolean.FALSE;
		}
		return convertedVal;
	}

	/**
	 * Convert to boolean nullable.
	 * 
	 * @param obj
	 *            the obj
	 * @return the boolean
	 */
	public static Boolean convertToBooleanNullable(Object obj) {
		Boolean convertedVal = (Boolean) convertType(obj);
		return convertedVal;
	}

	/**
	 * Convert to integer.
	 * 
	 * @param obj
	 *            the obj
	 * @return the integer
	 */
	public static Integer convertToInteger(Object obj) {
		Integer convertedVal = null;
		if (obj != null && obj instanceof String) {
			convertedVal = Integer.valueOf((String) obj);
		} else if (obj != null && obj instanceof Number) {
			convertedVal = ((Number) obj).intValue();
		}
		return convertedVal;
	}

	/**
	 * Convert to big decimal.
	 * 
	 * @param obj
	 *            the obj
	 * @return the big decimal
	 */
	public static BigDecimal convertToBigDecimal(Object obj) {
		BigDecimal convertedVal = null;
		if (obj != null && (obj instanceof String || obj instanceof Number)) {
			convertedVal = new BigDecimal(String.valueOf(obj));
		}
		return convertedVal;
	}

	/**
	 * Convert to long.
	 * 
	 * @param obj
	 *            the obj
	 * @return the long
	 */
	public static Long convertToLong(Object obj) {
		Long convertedVal = null;
		if (obj != null && obj instanceof String) {
			return Long.valueOf((String) obj);
		} else if (obj != null && obj instanceof Number) {
			convertedVal = ((Number) obj).longValue();
		}
		return convertedVal;
	}

	/**
	 * Convert to integer.
	 * 
	 * @param strs
	 *            the strs
	 * @return the integer[]
	 */
	public static Integer[] convertToIntegers(String[] strs) {
		Integer[] ints = null;

		if (strs != null && strs.length > 0) {
			ints = new Integer[strs.length];
			for (int i = 0; i < strs.length; i++) {
				ints[i] = convertToInteger(strs[i]);
			}
		}
		return ints;
	}

	/**
	 * Convert to date.
	 * 
	 * @param obj
	 *            the obj
	 * @return the date
	 */
	public static Date convertToDate(Object obj) {
		return (Date) convertType(obj);
	}

	/**
	 * Convert boolean to char.
	 * 
	 * @param booleanVal
	 *            the boolean val
	 * @return the string
	 */
	public static String convertBooleanToChar(Object booleanVal) {
		return (String) convertType(booleanVal);
	}

	/**
	 * Convert to char.
	 * 
	 * @param obj
	 *            the obj
	 * @return the character
	 */
	public static Character convertToChar(Object obj) {
		Character convertedChar = null;
		if (obj != null) {
			if (obj instanceof String && ((String) obj).length() > 0) {
				convertedChar = ((String) obj).charAt(0);
			} else if (obj instanceof Character) {
				convertedChar = (Character) obj;
			}
		}
		return convertedChar;
	}

	/**
	 * Convert string to boolean.
	 * 
	 * @param obj
	 *            the obj
	 * @return the boolean
	 */
	public static Boolean convertStringToBoolean(Object obj) {
		if (obj != null) {
			if (SfjConstants.YES.equals(obj.toString()) || Boolean.TRUE.toString().equals(obj.toString())) {
				return Boolean.TRUE;
			} else if (SfjConstants.NO.equals(obj.toString()) || Boolean.FALSE.toString().equals(obj.toString())) {
				return Boolean.FALSE;
			}
		}

		return Boolean.FALSE;
	}

	/**
	 * Convert to str array.
	 * 
	 * @param strs
	 *            the strs
	 * @param separator
	 *            the separator
	 * @return the string[]
	 */
	public static String[] convertToStrArray(String strs, String separator) {
		String[] array = StringUtils.split(strs, separator);
		if (array == null) {
			return ArrayUtils.EMPTY_STRING_ARRAY;
		}
		return array;
	}

	/**
	 * Removes the duplicate strings.
	 * 
	 * @param array
	 *            the array
	 * @return the string[]
	 */
	public static String[] removeDuplicateStrings(String[] array) {
		if (ArrayUtils.isEmpty(array)) {
			return array;
		}
		Set<String> set = new HashSet<String>();
		for (String element : array) {
			set.add(element);
		}
		return set.toArray(new String[] {});
	}

	/**
	 * Convert type.
	 * 
	 * @param obj
	 *            the obj
	 * @return the object
	 */
	private static Object convertType(Object obj) {
		Object convertedObj = null;
		if (obj != null) {
			if (obj instanceof String) {
				convertedObj = obj.toString();
			} else if (obj instanceof Character) {
				if (SfjConstants.YES.equals(obj.toString())) {
					convertedObj = Boolean.TRUE;
				} else if (SfjConstants.NO.equals(obj.toString())) {
					convertedObj = Boolean.FALSE;
				}
			} else if (obj instanceof Boolean) {
				if ((Boolean) obj) {
					convertedObj = SfjConstants.YES;
				} else {
					convertedObj = SfjConstants.NO;
				}
			} else {
				convertedObj = obj;
			}
		}
		return convertedObj;
	}

}

?

  • 相关文章
发表评论
用户名: 匿名