All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tuyang.beanutils.internal.convertors.ObjectToEnumConvertor Maven / Gradle / Ivy

Go to download

BeanUtils library is a Java bean copy utility with powerful functionality and high performance.

There is a newer version: 1.0.11
Show newest version
package com.tuyang.beanutils.internal.convertors;

import com.tuyang.beanutils.BeanCopyConvertor;

@SuppressWarnings("rawtypes")
public class ObjectToEnumConvertor implements BeanCopyConvertor {
	
	private Class enumClass;
	private boolean throwExceptions;
	
	public ObjectToEnumConvertor(Class enumClass, boolean throwExceptions) {
		this.enumClass = enumClass;
		this.throwExceptions = throwExceptions;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Enum convertTo(Object object) {
		if( object == null ) 
			return null;
		if( throwExceptions ) {
			return Enum.valueOf(enumClass, object.toString());
		}
		Enum[] enums = (Enum[])enumClass.getEnumConstants();
		for( Enum enumKey : enums ) {
			if( enumKey.name().equals(object.toString())) {
				return enumKey;
			}
		}
		return null;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy