panda.cast.Castors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of panda-core Show documentation
Show all versions of panda-core Show documentation
Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.
package panda.cast;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import panda.bean.BeanHandler;
import panda.bean.Beans;
import panda.cast.castor.ArrayCastor;
import panda.cast.castor.ByteArrayCastor;
import panda.cast.castor.CharArrayCastor;
import panda.cast.castor.ClassCastor;
import panda.cast.castor.CollectionCastor;
import panda.cast.castor.DateTypeCastor;
import panda.cast.castor.DirectCastor;
import panda.cast.castor.EmailAddressCastor;
import panda.cast.castor.EnumCastor;
import panda.cast.castor.FileCastor;
import panda.cast.castor.FileItemCastor;
import panda.cast.castor.IterableCastor;
import panda.cast.castor.IteratorCastor;
import panda.cast.castor.JavaBeanCastor;
import panda.cast.castor.JsonArrayCastor;
import panda.cast.castor.JsonObjectCastor;
import panda.cast.castor.LocaleCastor;
import panda.cast.castor.MapCastor;
import panda.cast.castor.NumberTypeCastor;
import panda.cast.castor.PrimitiveTypeCastor;
import panda.cast.castor.PrimitiveWrapCastor;
import panda.cast.castor.StreamCastor;
import panda.cast.castor.StringTypeCastor;
import panda.cast.castor.TimeZoneCastor;
import panda.lang.Asserts;
import panda.lang.collection.MultiKey;
import panda.lang.reflect.Types;
/**
* !! thread-safe !!
*/
@SuppressWarnings("rawtypes")
public class Castors {
private static Castors i = new Castors();
/**
* @return instance
*/
public static Castors i() {
return i;
}
/**
* @return instance
*/
public static Castors getInstance() {
return i;
}
/**
* @param instance the instance to set
*/
public static void setInstance(Castors instance) {
Castors.i = instance;
}
public static T scast(Object value, Type toType) {
Asserts.notNull(toType);
return i().cast(value, toType);
}
public static T scast(Object value, Type toType, boolean skipCastError) {
Asserts.notNull(toType);
Castors cs = i();
return cs.cast(value, toType, cs.newCastContext(skipCastError));
}
public static T scastTo(Object value, T target) {
if (value == null) {
return target;
}
Asserts.notNull(target);
return i().castTo(value, target);
}
public static T scastTo(Object value, T target, boolean skipCastError) {
if (value == null) {
return target;
}
Asserts.notNull(target);
Castors cs = i();
return cs.castTo(value, target, cs.newCastContext(skipCastError));
}
// ------------------------------------------------------------------------
private Beans beans = Beans.i();
private Map castors = new ConcurrentHashMap();
/**
* Constructor
*/
public Castors() {
register(new PrimitiveTypeCastor.BooleanCastor());
register(new PrimitiveTypeCastor.ByteCastor());
register(new PrimitiveTypeCastor.CharacterCastor());
register(new PrimitiveTypeCastor.DoubleCastor());
register(new PrimitiveTypeCastor.FloatCastor());
register(new PrimitiveTypeCastor.IntegerCastor());
register(new PrimitiveTypeCastor.LongCastor());
register(new PrimitiveTypeCastor.ShortCastor());
register(new PrimitiveWrapCastor.BooleanCastor());
register(new PrimitiveWrapCastor.ByteCastor());
register(new PrimitiveWrapCastor.CharacterCastor());
register(new PrimitiveWrapCastor.DoubleCastor());
register(new PrimitiveWrapCastor.FloatCastor());
register(new PrimitiveWrapCastor.IntegerCastor());
register(new PrimitiveWrapCastor.LongCastor());
register(new PrimitiveWrapCastor.ShortCastor());
register(new NumberTypeCastor.NumberCastor());
register(new NumberTypeCastor.BigIntegerCastor());
register(new NumberTypeCastor.BigDecimalCastor());
register(new ByteArrayCastor());
register(new CharArrayCastor());
DateTypeCastor.DateCastor dc = new DateTypeCastor.DateCastor();
register(dc);
register(new DateTypeCastor.CalendarCastor(dc));
register(new DateTypeCastor.GregorianCalendarCastor(dc));
register(new DateTypeCastor.SqlDateCastor(dc));
register(new DateTypeCastor.SqlTimeCastor(dc));
register(new DateTypeCastor.SqlTimestampCastor(dc));
register(new StringTypeCastor.StringCastor(dc));
register(new StringTypeCastor.StringBufferCastor(dc));
register(new StringTypeCastor.StringBuilderCastor(dc));
register(new StreamCastor.InputStreamCastor());
register(new StreamCastor.ReaderCastor());
register(new ClassCastor());
register(new IterableCastor());
register(new IteratorCastor());
register(new LocaleCastor());
register(new TimeZoneCastor());
register(new FileCastor());
register(new FileItemCastor());
register(new JsonArrayCastor());
register(new JsonObjectCastor());
register(new EmailAddressCastor());
}
public Beans getBeans() {
return beans;
}
public void setBeans(Beans beans) {
this.beans = beans;
}
/**
* Register (add) a castor for a class
*
* @param castor - the castor instance
*/
public void register(AbstractCastor castor) {
register(castor.getFromType(), castor.getToType(), castor);
}
/**
* Register (add) a castor for a class
*
* @param fromType the source class type
* @param toType the target class type
* @param castor the castor instance
*/
public void register(Type fromType, Type toType, Castor castor) {
castors.put(new MultiKey(fromType, toType), castor);
}
/**
* Unregister (remove) a castor for a class
*
* @param fromType the source class type
* @param toType the target class type
*/
public void unregister(Type fromType, Type toType) {
castors.remove(new MultiKey(fromType, toType));
}
/**
* clear converters
*/
public void clear() {
castors.clear();
}
public CastContext newCastContext() {
return new CastContext(this);
}
public CastContext newCastContext(boolean skipCastError) {
return new CastContext(this, skipCastError);
}
/**
* getCastor
* @param type object type
* @return Castor
*/
public Castor