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

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

The 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 NameTranslator
	implements ValueTranslator
{
	private final Enum[] values;

	public NameTranslator(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().equals(value))
			{
				return e;
			}
		}
		
		return null;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy