All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xiaoleilu.hutool.convert.impl.PrimitiveConverter Maven / Gradle / Ivy

package com.xiaoleilu.hutool.convert.impl;

import com.xiaoleilu.hutool.convert.AbstractConverter;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 原始类型转换器
* 支持类型为:
*
    *
  • byte
  • *
  • short
  • *
  • int
  • *
  • long
  • *
  • float
  • *
  • double
  • *
  • char
  • *
  • boolean
  • *
* * @author Looly * */ public class PrimitiveConverter extends AbstractConverter { private Class targetType; /** * 构造
* @param clazz 需要转换的原始 * @throws IllegalArgumentException 传入的转换类型非原始类型时抛出 */ public PrimitiveConverter(Class clazz) { if(null == clazz){ throw new NullPointerException("PrimitiveConverter not allow null target type!"); }else if(false == clazz.isPrimitive()){ throw new IllegalArgumentException("[" + clazz + "] is not a primitive class!"); } this.targetType = clazz; } @Override protected Object convertInternal(Object value) { try { if (byte.class == this.targetType) { if (value instanceof Number) { return ((Number) value).byteValue(); } final String valueStr = convertToStr(value); if (StrUtil.isBlank(valueStr)) { return 0; } return Byte.parseByte(valueStr); } else if (short.class == this.targetType) { if (value instanceof Number) { return ((Number) value).shortValue(); } final String valueStr = convertToStr(value); if (StrUtil.isBlank(valueStr)) { return 0; } return Short.parseShort(valueStr); } else if (int.class == this.targetType) { if (value instanceof Number) { return ((Number) value).intValue(); } final String valueStr = convertToStr(value); if (StrUtil.isBlank(valueStr)) { return 0; } return Integer.parseInt(valueStr); } else if (long.class == this.targetType) { if (value instanceof Number) { return ((Number) value).longValue(); } final String valueStr = convertToStr(value); if (StrUtil.isBlank(valueStr)) { return 0; } return Long.parseLong(valueStr); } else if (float.class == this.targetType) { if (value instanceof Number) { return ((Number) value).floatValue(); } final String valueStr = convertToStr(value); if (StrUtil.isBlank(valueStr)) { return 0; } return Float.parseFloat(valueStr); } else if (double.class == this.targetType) { if (value instanceof Number) { return ((Number) value).doubleValue(); } final String valueStr = convertToStr(value); if (StrUtil.isBlank(valueStr)) { return 0; } return Double.parseDouble(valueStr); } else if (char.class == this.targetType) { if(value instanceof Character){ return ((Character)value).charValue(); } final String valueStr = convertToStr(value); if (StrUtil.isBlank(valueStr)) { return 0; } return valueStr.charAt(0); } else if (boolean.class == this.targetType) { if(value instanceof Boolean){ return ((Boolean)value).booleanValue(); } String valueStr = convertToStr(value); return parseBoolean(valueStr); } } catch (Exception e) { // Ignore Exception } return 0; } /** * 转换字符串为boolean值 * @param valueStr 字符串 * @return boolean值 */ static boolean parseBoolean(String valueStr){ if (StrUtil.isNotBlank(valueStr)) { valueStr = valueStr.trim().toLowerCase(); switch (valueStr) { case "true": return true; case "false": return false; case "yes": return true; case "y": return true; case "ok": return true; case "no": return false; case "n": return false; case "1": return true; case "0": return false; } } return false; } @Override @SuppressWarnings("unchecked") public Class getTargetType() { return (Class) this.targetType; } }