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

org.squirrelframework.foundation.fsm.Converter Maven / Gradle / Ivy

Go to download

foundation module of squirrel framework which provided event driven infrastructure and a finite state machine implementation.

There is a newer version: 0.3.10
Show newest version
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;
            }
            
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy