com.tuyang.beanutils.internal.convertors.StringToEnumArrayConvertor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of BeanUtils Show documentation
Show all versions of BeanUtils Show documentation
BeanUtils library is a Java bean copy utility with powerful functionality and high performance.
The newest version!
package com.tuyang.beanutils.internal.convertors;
import java.lang.reflect.Array;
import com.tuyang.beanutils.BeanCopyConvertor;
@SuppressWarnings("rawtypes")
public class StringToEnumArrayConvertor implements BeanCopyConvertor {
private Class enumClass;
private boolean throwExceptions;
public StringToEnumArrayConvertor(Class enumClass, boolean throwExceptions) {
this.enumClass = enumClass;
this.throwExceptions = throwExceptions;
}
@SuppressWarnings("unchecked")
@Override
public Object[] convertTo(String[] object) {
if( object == null )
return null;
Object[] retList = (Object[]) Array.newInstance(enumClass, object.length);
if( throwExceptions ) {
for( int i =0; i< object.length;i++ ) {
retList[i] = Enum.valueOf(enumClass, object[i]);
}
return retList;
}
Enum[] enums = (Enum[])enumClass.getEnumConstants();
for( int i =0; i< object.length;i++ ) {
for( Enum enumKey : enums ) {
if( enumKey.name().equals(object[i])) {
retList[i] = enumKey;
}
}
}
return retList;
}
}