Minor changes following review
This commit is contained in:
parent
14d33019b7
commit
a06ce1fd81
|
@ -14,7 +14,6 @@ public class Dest {
|
|||
}
|
||||
|
||||
public Dest(String name, int age) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ public class Name {
|
|||
private String lastName;
|
||||
|
||||
public Name(String firstName, String lastName) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@ public class Person {
|
|||
}
|
||||
|
||||
public Person(String name, String nickname, int age) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.nickname = nickname;
|
||||
this.age = age;
|
||||
|
|
|
@ -9,7 +9,6 @@ public class Person3 {
|
|||
}
|
||||
|
||||
public Person3(String name, String dtob) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.dtob = dtob;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ public class PersonContainer {
|
|||
private Name name;
|
||||
|
||||
public PersonContainer(Name name) {
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.orika;
|
||||
|
||||
import ma.glasnost.orika.CustomMapper;
|
||||
import ma.glasnost.orika.MappingContext;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
class PersonCustomMapper extends CustomMapper<Personne3, Person3> {
|
||||
|
||||
@Override
|
||||
public void mapAtoB(Personne3 a, Person3 b, MappingContext context) {
|
||||
Date date = new Date(a.getDtob());
|
||||
DateFormat format = new SimpleDateFormat(
|
||||
"yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
String isoDate = format.format(date);
|
||||
b.setDtob(isoDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mapBtoA(Person3 b, Personne3 a, MappingContext context) {
|
||||
DateFormat format = new SimpleDateFormat(
|
||||
"yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
Date date = null;
|
||||
try {
|
||||
date = format.parse(b.getDtob());
|
||||
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
long timestamp = date.getTime();
|
||||
a.setDtob(timestamp);
|
||||
}
|
||||
}
|
|
@ -6,7 +6,6 @@ public class PersonListContainer {
|
|||
private List<Name> names;
|
||||
|
||||
public PersonListContainer(List<Name> names) {
|
||||
super();
|
||||
this.names = names;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ public class PersonNameArray {
|
|||
private String[] nameArray;
|
||||
|
||||
public PersonNameArray(String[] nameArray) {
|
||||
super();
|
||||
this.nameArray = nameArray;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ public class PersonNameList {
|
|||
private List<String> nameList;
|
||||
|
||||
public PersonNameList(List<String> nameList) {
|
||||
super();
|
||||
this.nameList = nameList;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,15 +6,9 @@ public class PersonNameMap {
|
|||
private Map<String, String> nameMap;
|
||||
|
||||
public PersonNameMap(Map<String, String> nameMap) {
|
||||
super();
|
||||
this.nameMap = nameMap;
|
||||
}
|
||||
|
||||
public PersonNameMap() {
|
||||
super();
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public Map<String, String> getNameMap() {
|
||||
return nameMap;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ public class PersonNameParts {
|
|||
private String lastName;
|
||||
|
||||
public PersonNameParts(String firstName, String lastName) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
|
|
@ -5,12 +5,7 @@ public class Personne {
|
|||
private String surnom;
|
||||
private int age;
|
||||
|
||||
public Personne() {
|
||||
|
||||
}
|
||||
|
||||
public Personne(String nom, String surnom, int age) {
|
||||
super();
|
||||
this.nom = nom;
|
||||
this.surnom = surnom;
|
||||
this.age = age;
|
||||
|
|
|
@ -9,7 +9,6 @@ public class Personne3 {
|
|||
}
|
||||
|
||||
public Personne3(String name, long dtob) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.dtob = dtob;
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@ public class Source {
|
|||
}
|
||||
|
||||
public Source(String name, int age) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
|
|
@ -32,33 +32,7 @@ public class OrikaTest {
|
|||
@Before
|
||||
public void before() {
|
||||
mapperFactory = new DefaultMapperFactory.Builder().build();
|
||||
customMapper = new CustomMapper<Personne3, Person3>() {
|
||||
|
||||
@Override
|
||||
public void mapAtoB(Personne3 a, Person3 b, MappingContext context) {
|
||||
Date date = new Date(a.getDtob());
|
||||
DateFormat format = new SimpleDateFormat(
|
||||
"yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
String isoDate = format.format(date);
|
||||
b.setDtob(isoDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mapBtoA(Person3 b, Personne3 a, MappingContext context) {
|
||||
DateFormat format = new SimpleDateFormat(
|
||||
"yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
Date date = null;
|
||||
try {
|
||||
date = format.parse(b.getDtob());
|
||||
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
long timestamp = date.getTime();
|
||||
a.setDtob(timestamp);
|
||||
}
|
||||
|
||||
};
|
||||
customMapper = new PersonCustomMapper();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -358,16 +332,15 @@ public class OrikaTest {
|
|||
.customize(customMapper).register();
|
||||
MapperFacade mapper = mapperFactory.getMapperFacade();
|
||||
long timestamp = new Long("1182882159000");
|
||||
Personne3 person0 = new Personne3("Leornardo", timestamp);
|
||||
Person3 person = mapper.map(person0, Person3.class);
|
||||
Personne3 personne3 = new Personne3("Leornardo", timestamp);
|
||||
Person3 person3 = mapper.map(personne3, Person3.class);
|
||||
|
||||
String timestampTest = person.getDtob();
|
||||
String timestampTest = person3.getDtob();
|
||||
// since different timezones will resolve the timestamp to a different
|
||||
// datetime string, it suffices to check only for format rather than
|
||||
// specific date
|
||||
assertTrue(timestampTest.charAt(10) == 'T'
|
||||
&& timestampTest.charAt(19) == 'Z');
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -378,9 +351,9 @@ public class OrikaTest {
|
|||
|
||||
String dateTime = "2007-06-26T21:22:39Z";
|
||||
long timestamp = new Long("1182882159000");
|
||||
Person3 person0 = new Person3("Leornardo", dateTime);
|
||||
Personne3 person = mapper.map(person0, Personne3.class);
|
||||
long timestampToTest = person.getDtob();
|
||||
Person3 person3 = new Person3("Leornardo", dateTime);
|
||||
Personne3 personne3 = mapper.map(person3, Personne3.class);
|
||||
long timestampToTest = personne3.getDtob();
|
||||
/*
|
||||
* since different timezones will resolve the datetime to a different
|
||||
* unix timestamp, we must provide a range of tolerance
|
||||
|
|
Loading…
Reference in New Issue