org.squirrelframework.foundation.fsm.Converter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squirrel-foundation Show documentation
Show all versions of squirrel-foundation Show documentation
foundation module of squirrel framework which provided event driven infrastructure and a finite state machine implementation.
package org.squirrelframework.foundation.fsm;
import org.squirrelframework.foundation.component.SquirrelComponent;
import com.google.common.base.Preconditions;
/**
* Convert object from string to object and object to string either.
*
* @author Henry.He
*
* @param type of converted object
*/
public interface Converter extends SquirrelComponent {
/**
* Convert object to string.
* @param obj converted object
* @return string description of object
*/
String convertToString(T obj);
/**
* Convert string to object.
* @param name name of the object
* @return converted object
*/
T convertFromString(String name);
/**
* Enumeration converter which can convert Enum object to its name, and also
* convert Enum name to Enum object.
*
* @param enum type
*/
public class EnumConverter> implements Converter {
private final Class enumType;
public EnumConverter(Class enumType) {
this.enumType = enumType;
}
@Override
public String convertToString(T obj) {
Preconditions.checkNotNull(obj);
return obj.name();
}
@Override
public T convertFromString(String name) {
try {
return Enum.valueOf(enumType, name);
} catch (IllegalArgumentException e) {
return null;
}
}
}
}