-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
2,399 additions
and
437 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
language: java | ||
jdk: | ||
- oraclejdk7 | ||
script: mvn -q clean install | ||
# whitelist | ||
branches: | ||
only: | ||
- master | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
core/src/main/java/ma/glasnost/orika/converter/builtin/CloneableConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ma.glasnost.orika.converter.builtin; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import ma.glasnost.orika.CustomConverter; | ||
import ma.glasnost.orika.metadata.Type; | ||
import ma.glasnost.orika.metadata.TypeFactory; | ||
|
||
/** | ||
* CloneableConverter allows configuration of a number of specific types which | ||
* should be cloned directly instead of creating a mapped copy.<br><br> | ||
* | ||
* This allows you to declare your own set of types which should be cloned instead | ||
* of mapped. | ||
* | ||
* @author [email protected] | ||
* | ||
*/ | ||
public class CloneableConverter extends CustomConverter<Object, Object> { | ||
|
||
private final Set<Type<Cloneable>> clonedTypes = new HashSet<Type<Cloneable>>(); | ||
private final Method cloneMethod; | ||
/** | ||
* Constructs a new ClonableConverter configured to handle the provided | ||
* list of types by cloning. | ||
* | ||
* @param types one or more types that should be treated as immutable | ||
*/ | ||
public CloneableConverter(java.lang.reflect.Type...types) { | ||
try { | ||
cloneMethod = Object.class.getDeclaredMethod("clone"); | ||
cloneMethod.setAccessible(true); | ||
} catch (SecurityException e) { | ||
throw new IllegalStateException(e); | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
for (java.lang.reflect.Type type: types) { | ||
clonedTypes.add(TypeFactory.<Cloneable>valueOf(type)); | ||
} | ||
} | ||
|
||
private boolean shouldClone(Type<?> type) { | ||
for (Type<?> registeredType: clonedTypes) { | ||
if (registeredType.isAssignableFrom(type)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see ma.glasnost.orika.Converter#canConvert(ma.glasnost.orika.metadata.Type, ma.glasnost.orika.metadata.Type) | ||
*/ | ||
public boolean canConvert(Type<?> sourceType, Type<?> destinationType) { | ||
return shouldClone(sourceType) && sourceType.equals(destinationType); | ||
} | ||
|
||
public Object convert(Object source, Type<? extends Object> destinationType) { | ||
try { | ||
return cloneMethod.invoke(source); | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalStateException(e); | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalStateException(e); | ||
} catch (InvocationTargetException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,10 +55,20 @@ public Date convertFrom(Calendar source, Type<Date> destinationType) { | |
public static class DateToXmlGregorianCalendarConverter extends | ||
BidirectionalConverter<Date, XMLGregorianCalendar> { | ||
|
||
private DatatypeFactory factory; | ||
|
||
{ | ||
try { | ||
factory = DatatypeFactory.newInstance(); | ||
} catch (DatatypeConfigurationException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public XMLGregorianCalendar convertTo(Date source, | ||
Type<XMLGregorianCalendar> destinationType) { | ||
return toXMLGregorianCalendar(source); | ||
return toXMLGregorianCalendar(source, factory); | ||
} | ||
|
||
@Override | ||
|
@@ -76,10 +86,20 @@ public Date convertFrom(XMLGregorianCalendar source, | |
public static class CalendarToXmlGregorianCalendarConverter extends | ||
BidirectionalConverter<Calendar, XMLGregorianCalendar> { | ||
|
||
private DatatypeFactory factory; | ||
|
||
{ | ||
try { | ||
factory = DatatypeFactory.newInstance(); | ||
} catch (DatatypeConfigurationException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public XMLGregorianCalendar convertTo(Calendar source, | ||
Type<XMLGregorianCalendar> destinationType) { | ||
return toXMLGregorianCalendar(source); | ||
return toXMLGregorianCalendar(source, factory); | ||
} | ||
|
||
@Override | ||
|
@@ -89,27 +109,6 @@ public Calendar convertFrom(XMLGregorianCalendar source, | |
} | ||
} | ||
|
||
/** | ||
* Provides conversion between Long and XMLGregorianCalendar | ||
* | ||
* @author [email protected] | ||
*/ | ||
public static class LongToXmlGregorianCalendarConverter extends | ||
BidirectionalConverter<Long, XMLGregorianCalendar> { | ||
|
||
@Override | ||
public XMLGregorianCalendar convertTo(Long source, | ||
Type<XMLGregorianCalendar> destinationType) { | ||
return toXMLGregorianCalendar(source); | ||
} | ||
|
||
@Override | ||
public Long convertFrom(XMLGregorianCalendar source, | ||
Type<Long> destinationType) { | ||
return toLong(source); | ||
} | ||
} | ||
|
||
/** | ||
* Provides conversion between Long and Date | ||
* | ||
|
@@ -150,6 +149,37 @@ public Long convertFrom(Calendar source, Type<Long> destinationType) { | |
} | ||
} | ||
|
||
|
||
/** | ||
* Provides conversion between Long and Calendar | ||
* | ||
* @author [email protected] | ||
* | ||
*/ | ||
public static class LongToXmlGregorianCalendarConverter extends | ||
BidirectionalConverter<Long, XMLGregorianCalendar> { | ||
|
||
private DatatypeFactory factory; | ||
|
||
{ | ||
try { | ||
factory = DatatypeFactory.newInstance(); | ||
} catch (DatatypeConfigurationException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public XMLGregorianCalendar convertTo(Long source, Type<XMLGregorianCalendar> destinationType) { | ||
return toXMLGregorianCalendar(source, factory); | ||
} | ||
|
||
@Override | ||
public Long convertFrom(XMLGregorianCalendar source, Type<Long> destinationType) { | ||
return toLong(source); | ||
} | ||
} | ||
|
||
private static Date toDate(XMLGregorianCalendar source) { | ||
return source.toGregorianCalendar().getTime(); | ||
} | ||
|
@@ -177,24 +207,23 @@ private static Calendar toCalendar(Long source) { | |
} | ||
|
||
private static XMLGregorianCalendar toXMLGregorianCalendar( | ||
Calendar source) { | ||
return toXMLGregorianCalendar(source.getTime()); | ||
Calendar source, DatatypeFactory factory) { | ||
return toXMLGregorianCalendar(source.getTime(), factory); | ||
} | ||
|
||
private static XMLGregorianCalendar toXMLGregorianCalendar( | ||
Date source) { | ||
GregorianCalendar c = new GregorianCalendar(); | ||
Date source, DatatypeFactory factory) { | ||
|
||
GregorianCalendar c = new GregorianCalendar(); | ||
c.setTime(source); | ||
try { | ||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(c); | ||
} catch (DatatypeConfigurationException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
|
||
return factory.newXMLGregorianCalendar(c); | ||
|
||
} | ||
|
||
private static XMLGregorianCalendar toXMLGregorianCalendar( | ||
Long source) { | ||
return toXMLGregorianCalendar(new Date(source)); | ||
Long source, DatatypeFactory factory) { | ||
return toXMLGregorianCalendar(new Date(source), factory); | ||
} | ||
|
||
private static Long toLong(Date source) { | ||
|
Oops, something went wrong.