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

se.l4.commons.serialization.enums.IgnoreCaseNameTranslator Maven / Gradle / Ivy

There is a newer version: 0.4.2
Show newest version
package se.l4.commons.serialization.enums;

import se.l4.commons.serialization.format.ValueType;

/**
 * {@link ValueTranslator} that uses the {@link Enum#name() name} of the enum.
 * 
 * @author Andreas Holstenson
 *
 */
public class IgnoreCaseNameTranslator
	implements ValueTranslator
{
	private final Enum[] values;

	public IgnoreCaseNameTranslator(Class> type)
	{
		values = type.getEnumConstants();
	}
	
	@Override
	public ValueType getType()
	{
		return ValueType.STRING;
	}
	
	@Override
	public String fromEnum(Enum value)
	{
		return value.name();
	}
	
	@Override
	public Enum toEnum(String value)
	{
		for(Enum e : values)
		{
			if(e.name().equalsIgnoreCase(value))
			{
				return e;
			}
		}
		
		return null;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy