Page tree
Skip to end of metadata
Go to start of metadata

Resumo

Os mappers, ou mapeaddores são responsáveis por mapear um tipo de dado para outro, realizando conversões.

Isso é necessário porque nem sempre o tipo de dado de origem em um conector é igual ao tipo de dado no conector de destino.

Embora o Mizura suporte nativamente 14 tipos de dados diferentes, você pode implementar um mapeador customizado se for preciso. Por exemplo, em um repositório o tipo de dado de um campo pode ser uma imagem do Google Maps, e no outro essa informação ser armazenada textualmente, na forma de seu endereço.

Para criar o seu mapeamento customizado, basta implementar a interface IElementMapper:

public interface IElementMapper<S extends Object, D> {
	public D convert(S source, Class<? extends D> destination);
}

Mapeamentos Nativos

O Mizura vem com implementação de mapeamento padrão (vide classe DefaultElementMapper) para 14 tipos de dados mais comuns no Java (vide abaixo), originando 196 cobinações. Dessas, geram 21 condições de erro (ex. Converter uma data para um tipo booleano) e 11 implicam em perda de precisão (ex.: Converter um double para um inteiro). Nas condições de erro, o valor no objeto de destino será null ou o valor padrão do tipo de dado Java quando usados primitivos.

Os seguintes mapeamentos são implementados pelo Mizura:

  • java.lang.String
  • java.lang.Boolean
  • java.util.Date
  • java.lang.Long
  • java.lang.Integer
  • java.lang.Double
  • java.lang.Float
  • java.lang.Byte
  • java.lang.Short
  • java.lang.Character
  • java.math.BigInteger
  • java.math.AtomiLong
  • java.math.AtomicInteger

E aqui, o teste unitário (vide classe DefaultElementMapperTest) com as saídas dessas conversões:

CONVERT(Float,Float) = Float: 123456.79 -> Float: 123456.79
CONVERT(Float,Double) = Float: 123456.79 -> Double: 123456.7890625
CONVERT(Float,AtomicInteger) = Float: 123456.79 -> AtomicInteger: 123456
CONVERT(Float,Byte) = Float: 123456.79 -> Byte: 64
CONVERT(Float,Long) = Float: 123456.79 -> Long: 123456
CONVERT(Float,Character) = Float: 123456.79 -> Character: 1
CONVERT(Float,AtomicLong) = Float: 123456.79 -> AtomicLong: 123456
CONVERT(Float,Date) = Float: 123456.79 -> Date: Wed Dec 31 21:02:03 BRT 1969
CONVERT(Float,Boolean) = Float: 123456.79 -> Boolean: false
CONVERT(Float,BigDecimal) = Float: 123456.79 -> BigDecimal: 123456.7890625
CONVERT(Float,String) = Float: 123456.79 -> String: 123456.79
CONVERT(Float,Integer) = Float: 123456.79 -> Integer: 123456
CONVERT(Float,BigInteger) = Float: 123456.79 -> BigInteger: 123456
CONVERT(Float,Short) = Float: 123456.79 -> Short: -7616
CONVERT(Double,Float) = Double: 123456.789 -> Float: 123456.79
CONVERT(Double,Double) = Double: 123456.789 -> Double: 123456.789
CONVERT(Double,AtomicInteger) = Double: 123456.789 -> AtomicInteger: 123456
CONVERT(Double,Byte) = Double: 123456.789 -> Byte: 64
CONVERT(Double,Long) = Double: 123456.789 -> Long: 123456
CONVERT(Double,Character) = Double: 123456.789 -> Character: 1
CONVERT(Double,AtomicLong) = Double: 123456.789 -> AtomicLong: 123456
CONVERT(Double,Date) = Double: 123456.789 -> Date: Wed Dec 31 21:02:03 BRT 1969
CONVERT(Double,Boolean) = Double: 123456.789 -> Boolean: false
CONVERT(Double,BigDecimal) = Double: 123456.789 -> BigDecimal: 123456.789000000004307366907596588134765625
CONVERT(Double,String) = Double: 123456.789 -> String: 123456.789
CONVERT(Double,Integer) = Double: 123456.789 -> Integer: 123456
CONVERT(Double,BigInteger) = Double: 123456.789 -> BigInteger: 123456
CONVERT(Double,Short) = Double: 123456.789 -> Short: -7616
CONVERT(AtomicInteger,Float) = AtomicInteger: 123456 -> Float: 123456.0
CONVERT(AtomicInteger,Double) = AtomicInteger: 123456 -> Double: 123456.0
CONVERT(AtomicInteger,AtomicInteger) = AtomicInteger: 123456 -> AtomicInteger: 123456
CONVERT(AtomicInteger,Byte) = AtomicInteger: 123456 -> Byte: 64
CONVERT(AtomicInteger,Long) = AtomicInteger: 123456 -> Long: 123456
CONVERT(AtomicInteger,Character) = AtomicInteger: 123456 -> Character: 1
CONVERT(AtomicInteger,AtomicLong) = AtomicInteger: 123456 -> AtomicLong: 123456
CONVERT(AtomicInteger,Date) = AtomicInteger: 123456 -> Date: Wed Dec 31 21:02:03 BRT 1969
CONVERT(AtomicInteger,Boolean) = AtomicInteger: 123456 -> Boolean: false
CONVERT(AtomicInteger,BigDecimal) = AtomicInteger: 123456 -> BigDecimal: 123456
CONVERT(AtomicInteger,String) = AtomicInteger: 123456 -> String: 123456
CONVERT(AtomicInteger,Integer) = AtomicInteger: 123456 -> Integer: 123456
CONVERT(AtomicInteger,BigInteger) = AtomicInteger: 123456 -> BigInteger: 123456
CONVERT(AtomicInteger,Short) = AtomicInteger: 123456 -> Short: -7616
CONVERT(Integer,Float) = Integer: 123 -> Float: 123.0
CONVERT(Integer,Double) = Integer: 123 -> Double: 123.0
CONVERT(Integer,AtomicInteger) = Integer: 123 -> AtomicInteger: 123
CONVERT(Integer,Byte) = Integer: 123 -> Byte: 123
CONVERT(Integer,Long) = Integer: 123 -> Long: 123
CONVERT(Integer,Character) = Integer: 123 -> Character: 1
CONVERT(Integer,AtomicLong) = Integer: 123 -> AtomicLong: 123
CONVERT(Integer,Date) = Integer: 123 -> Date: Wed Dec 31 21:00:00 BRT 1969
CONVERT(Integer,Boolean) = Integer: 123 -> Boolean: false
CONVERT(Integer,BigDecimal) = Integer: 123 -> BigDecimal: 123
CONVERT(Integer,String) = Integer: 123 -> String: 123
CONVERT(Integer,Integer) = Integer: 123 -> Integer: 123
CONVERT(Integer,BigInteger) = Integer: 123 -> BigInteger: 123
CONVERT(Integer,Short) = Integer: 123 -> Short: 123
CONVERT(Long,Float) = Long: 123456 -> Float: 123456.0
CONVERT(Long,Double) = Long: 123456 -> Double: 123456.0
CONVERT(Long,AtomicInteger) = Long: 123456 -> AtomicInteger: 123456
CONVERT(Long,Byte) = Long: 123456 -> Byte: 64
CONVERT(Long,Long) = Long: 123456 -> Long: 123456
CONVERT(Long,Character) = Long: 123456 -> Character: 1
CONVERT(Long,AtomicLong) = Long: 123456 -> AtomicLong: 123456
CONVERT(Long,Date) = Long: 123456 -> Date: Wed Dec 31 21:02:03 BRT 1969
CONVERT(Long,Boolean) = Long: 123456 -> Boolean: false
CONVERT(Long,BigDecimal) = Long: 123456 -> BigDecimal: 123456
CONVERT(Long,String) = Long: 123456 -> String: 123456
CONVERT(Long,Integer) = Long: 123456 -> Integer: 123456
CONVERT(Long,BigInteger) = Long: 123456 -> BigInteger: 123456
CONVERT(Long,Short) = Long: 123456 -> Short: -7616
CONVERT(Character,Float) = Character: t -> Float: UNSUPPORTED
CONVERT(Character,Double) = Character: t -> Double: UNSUPPORTED
CONVERT(Character,AtomicInteger) = Character: t -> AtomicInteger: UNSUPPORTED
CONVERT(Character,Byte) = Character: t -> Byte: UNSUPPORTED
CONVERT(Character,Long) = Character: t -> Long: UNSUPPORTED
CONVERT(Character,Character) = Character: t -> Character: t
CONVERT(Character,AtomicLong) = Character: t -> AtomicLong: UNSUPPORTED
CONVERT(Character,Date) = Character: t -> Date: UNSUPPORTED
CONVERT(Character,Boolean) = Character: t -> Boolean: true
CONVERT(Character,BigDecimal) = Character: t -> BigDecimal: UNSUPPORTED
CONVERT(Character,String) = Character: t -> String: t
CONVERT(Character,Integer) = Character: t -> Integer: UNSUPPORTED
CONVERT(Character,BigInteger) = Character: t -> BigInteger: UNSUPPORTED
CONVERT(Character,Short) = Character: t -> Short: UNSUPPORTED
CONVERT(AtomicLong,Float) = AtomicLong: 123456 -> Float: 123456.0
CONVERT(AtomicLong,Double) = AtomicLong: 123456 -> Double: 123456.0
CONVERT(AtomicLong,AtomicInteger) = AtomicLong: 123456 -> AtomicInteger: 123456
CONVERT(AtomicLong,Byte) = AtomicLong: 123456 -> Byte: 64
CONVERT(AtomicLong,Long) = AtomicLong: 123456 -> Long: 123456
CONVERT(AtomicLong,Character) = AtomicLong: 123456 -> Character: 1
CONVERT(AtomicLong,AtomicLong) = AtomicLong: 123456 -> AtomicLong: 123456
CONVERT(AtomicLong,Date) = AtomicLong: 123456 -> Date: Wed Dec 31 21:02:03 BRT 1969
CONVERT(AtomicLong,Boolean) = AtomicLong: 123456 -> Boolean: false
CONVERT(AtomicLong,BigDecimal) = AtomicLong: 123456 -> BigDecimal: 123456
CONVERT(AtomicLong,String) = AtomicLong: 123456 -> String: 123456
CONVERT(AtomicLong,Integer) = AtomicLong: 123456 -> Integer: 123456
CONVERT(AtomicLong,BigInteger) = AtomicLong: 123456 -> BigInteger: 123456
CONVERT(AtomicLong,Short) = AtomicLong: 123456 -> Short: -7616
CONVERT(Date,Float) = Date: Mon Aug 15 15:23:12 BRT 2011 -> Float: 1.31343254E12
CONVERT(Date,Double) = Date: Mon Aug 15 15:23:12 BRT 2011 -> Double: 1.313432592767E12
CONVERT(Date,AtomicInteger) = Date: Mon Aug 15 15:23:12 BRT 2011 -> AtomicInteger: -827399809
CONVERT(Date,Byte) = Date: Mon Aug 15 15:23:12 BRT 2011 -> Byte: 127
CONVERT(Date,Long) = Date: Mon Aug 15 15:23:12 BRT 2011 -> Long: 1313432592767
CONVERT(Date,Character) = Date: Mon Aug 15 15:23:12 BRT 2011 -> Character: UNSUPPORTED
CONVERT(Date,AtomicLong) = Date: Mon Aug 15 15:23:12 BRT 2011 -> AtomicLong: 1313432592767
CONVERT(Date,Date) = Date: Mon Aug 15 15:23:12 BRT 2011 -> Date: Mon Aug 15 15:23:12 BRT 2011
CONVERT(Date,Boolean) = Date: Mon Aug 15 15:23:12 BRT 2011 -> Boolean: UNSUPPORTED
CONVERT(Date,BigDecimal) = Date: Mon Aug 15 15:23:12 BRT 2011 -> BigDecimal: 1313432592767
CONVERT(Date,String) = Date: Mon Aug 15 15:23:12 BRT 2011 -> String: 2011-08-15 15:23:12,767
CONVERT(Date,Integer) = Date: Mon Aug 15 15:23:12 BRT 2011 -> Integer: -827399809
CONVERT(Date,BigInteger) = Date: Mon Aug 15 15:23:12 BRT 2011 -> BigInteger: 1313432592767
CONVERT(Date,Short) = Date: Mon Aug 15 15:23:12 BRT 2011 -> Short: -7809
CONVERT(Boolean,Float) = Boolean: true -> Float: UNSUPPORTED
CONVERT(Boolean,Double) = Boolean: true -> Double: UNSUPPORTED
CONVERT(Boolean,AtomicInteger) = Boolean: true -> AtomicInteger: UNSUPPORTED
CONVERT(Boolean,Byte) = Boolean: true -> Byte: UNSUPPORTED
CONVERT(Boolean,Long) = Boolean: true -> Long: UNSUPPORTED
CONVERT(Boolean,Character) = Boolean: true -> Character: T
CONVERT(Boolean,AtomicLong) = Boolean: true -> AtomicLong: UNSUPPORTED
CONVERT(Boolean,Date) = Boolean: true -> Date: UNSUPPORTED
CONVERT(Boolean,Boolean) = Boolean: true -> Boolean: true
CONVERT(Boolean,BigDecimal) = Boolean: true -> BigDecimal: UNSUPPORTED
CONVERT(Boolean,String) = Boolean: true -> String: true
CONVERT(Boolean,Integer) = Boolean: true -> Integer: UNSUPPORTED
CONVERT(Boolean,BigInteger) = Boolean: true -> BigInteger: UNSUPPORTED
CONVERT(Boolean,Short) = Boolean: true -> Short: UNSUPPORTED
CONVERT(BigDecimal,Float) = BigDecimal: 123456.7890625 -> Float: 123456.79
CONVERT(BigDecimal,Double) = BigDecimal: 123456.7890625 -> Double: 123456.7890625
CONVERT(BigDecimal,AtomicInteger) = BigDecimal: 123456.7890625 -> AtomicInteger: 123456
CONVERT(BigDecimal,Byte) = BigDecimal: 123456.7890625 -> Byte: 64
CONVERT(BigDecimal,Long) = BigDecimal: 123456.7890625 -> Long: 123456
CONVERT(BigDecimal,Character) = BigDecimal: 123456.7890625 -> Character: 1
CONVERT(BigDecimal,AtomicLong) = BigDecimal: 123456.7890625 -> AtomicLong: 123456
CONVERT(BigDecimal,Date) = BigDecimal: 123456.7890625 -> Date: Wed Dec 31 21:02:03 BRT 1969
CONVERT(BigDecimal,Boolean) = BigDecimal: 123456.7890625 -> Boolean: false
CONVERT(BigDecimal,BigDecimal) = BigDecimal: 123456.7890625 -> BigDecimal: 123456.7890625
CONVERT(BigDecimal,String) = BigDecimal: 123456.7890625 -> String: 123456.7890625
CONVERT(BigDecimal,Integer) = BigDecimal: 123456.7890625 -> Integer: 123456
CONVERT(BigDecimal,BigInteger) = BigDecimal: 123456.7890625 -> BigInteger: 123456
CONVERT(BigDecimal,Short) = BigDecimal: 123456.7890625 -> Short: -7616
CONVERT(String,Float) = String: Acentuação -> Float: UNSUPPORTED
CONVERT(String,Double) = String: Acentuação -> Double: UNSUPPORTED
CONVERT(String,AtomicInteger) = String: Acentuação -> AtomicInteger: UNSUPPORTED
CONVERT(String,Byte) = String: Acentuação -> Byte: UNSUPPORTED
CONVERT(String,Long) = String: Acentuação -> Long: UNSUPPORTED
CONVERT(String,Character) = String: Acentuação -> Character: A
CONVERT(String,AtomicLong) = String: Acentuação -> AtomicLong: UNSUPPORTED
CONVERT(String,Date) = String: Acentuação -> Date: UNSUPPORTED
CONVERT(String,Boolean) = String: Acentuação -> Boolean: false
CONVERT(String,BigDecimal) = String: Acentuação -> BigDecimal: UNSUPPORTED
CONVERT(String,String) = String: Acentuação -> String: Acentuação
CONVERT(String,Integer) = String: Acentuação -> Integer: UNSUPPORTED
CONVERT(String,BigInteger) = String: Acentuação -> BigInteger: UNSUPPORTED
CONVERT(String,Short) = String: Acentuação -> Short: UNSUPPORTED
CONVERT(Integer,Float) = Integer: 1233456 -> Float: 1233456.0
CONVERT(Integer,Double) = Integer: 1233456 -> Double: 1233456.0
CONVERT(Integer,AtomicInteger) = Integer: 1233456 -> AtomicInteger: 1233456
CONVERT(Integer,Byte) = Integer: 1233456 -> Byte: 48
CONVERT(Integer,Long) = Integer: 1233456 -> Long: 1233456
CONVERT(Integer,Character) = Integer: 1233456 -> Character: 1
CONVERT(Integer,AtomicLong) = Integer: 1233456 -> AtomicLong: 1233456
CONVERT(Integer,Date) = Integer: 1233456 -> Date: Wed Dec 31 21:20:33 BRT 1969
CONVERT(Integer,Boolean) = Integer: 1233456 -> Boolean: false
CONVERT(Integer,BigDecimal) = Integer: 1233456 -> BigDecimal: 1233456
CONVERT(Integer,String) = Integer: 1233456 -> String: 1233456
CONVERT(Integer,Integer) = Integer: 1233456 -> Integer: 1233456
CONVERT(Integer,BigInteger) = Integer: 1233456 -> BigInteger: 1233456
CONVERT(Integer,Short) = Integer: 1233456 -> Short: -11728
CONVERT(BigInteger,Float) = BigInteger: 123456 -> Float: 123456.0
CONVERT(BigInteger,Double) = BigInteger: 123456 -> Double: 123456.0
CONVERT(BigInteger,AtomicInteger) = BigInteger: 123456 -> AtomicInteger: 123456
CONVERT(BigInteger,Byte) = BigInteger: 123456 -> Byte: 64
CONVERT(BigInteger,Long) = BigInteger: 123456 -> Long: 123456
CONVERT(BigInteger,Character) = BigInteger: 123456 -> Character: 1
CONVERT(BigInteger,AtomicLong) = BigInteger: 123456 -> AtomicLong: 123456
CONVERT(BigInteger,Date) = BigInteger: 123456 -> Date: Wed Dec 31 21:02:03 BRT 1969
CONVERT(BigInteger,Boolean) = BigInteger: 123456 -> Boolean: false
CONVERT(BigInteger,BigDecimal) = BigInteger: 123456 -> BigDecimal: 123456
CONVERT(BigInteger,String) = BigInteger: 123456 -> String: 123456
CONVERT(BigInteger,Integer) = BigInteger: 123456 -> Integer: 123456
CONVERT(BigInteger,BigInteger) = BigInteger: 123456 -> BigInteger: 123456
CONVERT(BigInteger,Short) = BigInteger: 123456 -> Short: -7616
CONVERT(Integer,Float) = Integer: 123 -> Float: 123.0
CONVERT(Integer,Double) = Integer: 123 -> Double: 123.0
CONVERT(Integer,AtomicInteger) = Integer: 123 -> AtomicInteger: 123
CONVERT(Integer,Byte) = Integer: 123 -> Byte: 123
CONVERT(Integer,Long) = Integer: 123 -> Long: 123
CONVERT(Integer,Character) = Integer: 123 -> Character: 1
CONVERT(Integer,AtomicLong) = Integer: 123 -> AtomicLong: 123
CONVERT(Integer,Date) = Integer: 123 -> Date: Wed Dec 31 21:00:00 BRT 1969
CONVERT(Integer,Boolean) = Integer: 123 -> Boolean: false
CONVERT(Integer,BigDecimal) = Integer: 123 -> BigDecimal: 123
CONVERT(Integer,String) = Integer: 123 -> String: 123
CONVERT(Integer,Integer) = Integer: 123 -> Integer: 123
CONVERT(Integer,BigInteger) = Integer: 123 -> BigInteger: 123
CONVERT(Integer,Short) = Integer: 123 -> Short: 123