org.hibernate.type.descriptor.java.UrlTypeDescriptor Maven / Gradle / Ivy
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.type.descriptor.java;
import java.net.MalformedURLException;
import java.net.URL;
import org.hibernate.HibernateException;
import org.hibernate.type.descriptor.WrapperOptions;
/**
* Descriptor for {@link URL} handling.
*
* @author Steve Ebersole
*/
public class UrlTypeDescriptor extends AbstractTypeDescriptor {
public static final UrlTypeDescriptor INSTANCE = new UrlTypeDescriptor();
public UrlTypeDescriptor() {
super( URL.class );
}
public String toString(URL value) {
return value.toExternalForm();
}
public URL fromString(String string) {
try {
return new URL( string );
}
catch ( MalformedURLException e ) {
throw new HibernateException( "Unable to convert string [" + string + "] to URL : " + e );
}
}
@SuppressWarnings({ "unchecked" })
public X unwrap(URL value, Class type, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( String.class.isAssignableFrom( type ) ) {
return (X) toString( value );
}
throw unknownUnwrap( type );
}
public URL wrap(X value, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( String.class.isInstance( value ) ) {
return fromString( (String) value );
}
throw unknownWrap( value.getClass() );
}
}