panda.bind.AbstractBinder 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.bind;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import panda.bean.BeanHandler;
import panda.bean.Beans;
import panda.cast.Castor;
import panda.cast.Castors;
import panda.lang.reflect.Types;
@SuppressWarnings({ "rawtypes", "unchecked" })
public abstract class AbstractBinder {
private Castors castors = Castors.i();
private boolean ignoreNullProperty = true;
private Set excludePropertyNames;
private Set> excludePropertyTypes;
/**
* Constructor
*/
public AbstractBinder() {
}
public boolean isIgnoreNullProperty() {
return ignoreNullProperty;
}
public void setIgnoreNullProperty(boolean ignoreNullProperty) {
this.ignoreNullProperty = ignoreNullProperty;
}
public Castors getCastors() {
return castors;
}
public void setCastors(Castors castors) {
this.castors = castors;
}
//----------------------------------------------------------
public Set getExcludePropertyNames() {
if (excludePropertyNames == null) {
excludePropertyNames = new HashSet();
}
return excludePropertyNames;
}
public void addExcludeProperty(String exclude) {
getExcludePropertyNames().add(exclude);
}
public void removeExcludeProperty(String exclude) {
getExcludePropertyNames().remove(exclude);
}
public Set> getExcludePropertyTypes() {
if (excludePropertyTypes == null) {
excludePropertyTypes = new HashSet>();
}
return excludePropertyTypes;
}
public void addExcludeProperty(Class> exclude) {
getExcludePropertyTypes().add(exclude);
}
public void removeExcludeProperty(Class> exclude) {
getExcludePropertyTypes().remove(exclude);
}
//----------------------------------------------------------
protected Beans getBeans() {
return castors.getBeans();
}
protected BeanHandler getBeanHandler(Type type) {
return getBeans().getBeanHandler(type);
}
protected Castor getCastor(Type fromType, Type toType) {
return castors.getCastor(fromType, toType);
}
protected T convertValue(Object value, Type toType) {
if (toType == null) {
return null;
}
return (T)castors.cast(value, toType);
}
protected boolean isExcludeProperty(String name) {
return excludePropertyNames != null && excludePropertyNames.contains(name);
}
protected boolean isExcludeProperty(Class type) {
return excludePropertyTypes != null && excludePropertyTypes.contains(type);
}
protected boolean isArrayType(Type type) {
return Types.isArrayType(type) || Types.isAssignable(type, Collection.class);
}
protected boolean isImmutableType(Type type) {
return Types.isImmutableType(type);
}
}