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

org.cyclopsgroup.caff.conversion.NullFriendlyConverter Maven / Gradle / Ivy

package org.cyclopsgroup.caff.conversion;

/**
 * A converter that maps between empty CharSequence and NULL
 *
 * @author Jiaqi Guo
 * @param  Type of value to convert from/to
 */
public class NullFriendlyConverter
    implements Converter
{
    private static final String EMPTY = "";

    private final Converter proxy;

    /**
     * @param proxy Internal converter that does actual conversion
     */
    public NullFriendlyConverter( Converter proxy )
    {
        if ( proxy == null )
        {
            throw new NullPointerException( "Input proxy converter can't be NULL" );
        }
        this.proxy = proxy;
    }

    /**
     * @inheritDoc
     */
    public T fromCharacters( CharSequence text )
    {
        if ( text == null || text.length() == 0 )
        {
            return null;
        }
        return proxy.fromCharacters( text );
    }

    /**
     * @inheritDoc
     */
    public CharSequence toCharacters( T value )
    {
        if ( value == null )
        {
            return EMPTY;
        }
        return proxy.toCharacters( value );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy