Skip to content

Commit

Permalink
Fix Tarvis descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
elaatifi committed Oct 11, 2012
2 parents 6f7e72c + e1415f5 commit 7ae0b02
Show file tree
Hide file tree
Showing 17 changed files with 2,399 additions and 437 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

import javax.xml.datatype.XMLGregorianCalendar;

import ma.glasnost.orika.converter.ConverterFactory;

/**
Expand Down Expand Up @@ -83,6 +87,18 @@ public static void register(ConverterFactory converterFactory) {
converterFactory.registerConverter(new NumericConverters.BigIntegerToIntegerConverter(false));
converterFactory.registerConverter(new NumericConverters.BigIntegerToLongConverter(false));

converterFactory.registerConverter(new NumericConverters.IntegerToShortConverter(false));
converterFactory.registerConverter(new NumericConverters.LongToIntegerConverter(false));
converterFactory.registerConverter(new NumericConverters.LongToShortConverter(false));

converterFactory.registerConverter(new NumericConverters.FloatToShortConverter(false));
converterFactory.registerConverter(new NumericConverters.FloatToIntegerConverter(false));
converterFactory.registerConverter(new NumericConverters.FloatToLongConverter(false));

converterFactory.registerConverter(new NumericConverters.DoubleToShortConverter(false));
converterFactory.registerConverter(new NumericConverters.DoubleToIntegerConverter(false));
converterFactory.registerConverter(new NumericConverters.DoubleToLongConverter(false));

/*
* Register additional common "immutable" types
*/
Expand All @@ -97,6 +113,13 @@ public static void register(ConverterFactory converterFactory) {
Inet6Address.class,
InetSocketAddress.class
));

/*
* Register additional common "cloneable" types
*/
converterFactory.registerConverter(new CloneableConverter(
Date.class,
Calendar.class,
XMLGregorianCalendar.class
));
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
*
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 7ae0b02

Please sign in to comment.