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

com.jchanghong.core.convert.impl.PrimitiveConverter Maven / Gradle / Ivy

The newest version!
package com.jchanghong.core.convert.impl;

import com.jchanghong.core.convert.AbstractConverter;
import com.jchanghong.core.date.DateUtil;
import com.jchanghong.core.util.BooleanUtil;
import com.jchanghong.core.util.NumberUtil;
import com.jchanghong.core.util.StrUtil;

import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;

/**
 * 原始类型转换器
* 支持类型为:
*
    *
  • byte
  • *
  • short
  • *
  • int
  • *
  • long
  • *
  • float
  • *
  • double
  • *
  • char
  • *
  • boolean
  • *
* * @author Looly */ public class PrimitiveConverter extends AbstractConverter { private static final long serialVersionUID = 1L; private final 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(); } else if (value instanceof Boolean) { return BooleanUtil.toByte((Boolean) value); } 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(); } else if (value instanceof Boolean) { return BooleanUtil.toShort((Boolean) value); } 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(); } else if (value instanceof Boolean) { return BooleanUtil.toInt((Boolean) value); } else if (value instanceof Date) { return ((Date) value).getTime(); } else if (value instanceof Calendar) { return ((Calendar) value).getTimeInMillis(); } else if (value instanceof TemporalAccessor) { return DateUtil.toInstant((TemporalAccessor) value).toEpochMilli(); } final String valueStr = convertToStr(value); if (StrUtil.isBlank(valueStr)) { return 0; } return NumberUtil.parseInt(valueStr); } else if (long.class == this.targetType) { if (value instanceof Number) { return ((Number) value).longValue(); } else if (value instanceof Boolean) { return BooleanUtil.toLong((Boolean) value); } else if (value instanceof Date) { return ((Date) value).getTime(); } else if (value instanceof Calendar) { return ((Calendar) value).getTimeInMillis(); } else if (value instanceof TemporalAccessor) { return DateUtil.toInstant((TemporalAccessor) value).toEpochMilli(); } final String valueStr = convertToStr(value); if (StrUtil.isBlank(valueStr)) { return 0; } return NumberUtil.parseLong(valueStr); } else if (float.class == this.targetType) { if (value instanceof Number) { return ((Number) value).floatValue(); } else if (value instanceof Boolean) { return BooleanUtil.toFloat((Boolean) value); } 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(); } else if (value instanceof Boolean) { return BooleanUtil.toDouble((Boolean) value); } 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) { //noinspection UnnecessaryUnboxing return ((Character) value).charValue(); } else if (value instanceof Boolean) { return BooleanUtil.toChar((Boolean) value); } 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) { //noinspection UnnecessaryUnboxing return ((Boolean) value).booleanValue(); } String valueStr = convertToStr(value); return BooleanUtil.toBoolean(valueStr); } } catch (Exception e) { // Ignore Exception } return 0; } @Override protected String convertToStr(Object value) { return StrUtil.trim(super.convertToStr(value)); } @Override @SuppressWarnings("unchecked") public Class getTargetType() { return (Class) this.targetType; } }