org.hibernate.type.descriptor.java.CalendarTimeTypeDescriptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
JPMS Module-Info's for a few of the Jakarta Libraries just until they add them in themselves
/*
* 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.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import org.hibernate.cfg.Environment;
import org.hibernate.internal.util.compare.CalendarComparator;
import org.hibernate.type.descriptor.WrapperOptions;
/**
* Descriptor for {@link java.util.Calendar} handling, but just for the time portion.
*
* @author Steve Ebersole
*/
public class CalendarTimeTypeDescriptor extends AbstractTypeDescriptor {
public static final CalendarTimeTypeDescriptor INSTANCE = new CalendarTimeTypeDescriptor();
protected CalendarTimeTypeDescriptor() {
super( Calendar.class, CalendarTypeDescriptor.CalendarMutabilityPlan.INSTANCE );
}
public String toString(Calendar value) {
return DateTypeDescriptor.INSTANCE.toString( value.getTime() );
}
public Calendar fromString(String string) {
Calendar result = new GregorianCalendar();
result.setTime( DateTypeDescriptor.INSTANCE.fromString( string ) );
return result;
}
@Override
public boolean areEqual(Calendar one, Calendar another) {
if ( one == another ) {
return true;
}
if ( one == null || another == null ) {
return false;
}
return one.get(Calendar.DAY_OF_MONTH) == another.get(Calendar.DAY_OF_MONTH)
&& one.get(Calendar.MONTH) == another.get(Calendar.MONTH)
&& one.get(Calendar.YEAR) == another.get(Calendar.YEAR);
}
@Override
public int extractHashCode(Calendar value) {
int hashCode = 1;
hashCode = 31 * hashCode + value.get(Calendar.DAY_OF_MONTH);
hashCode = 31 * hashCode + value.get(Calendar.MONTH);
hashCode = 31 * hashCode + value.get(Calendar.YEAR);
return hashCode;
}
@Override
public Comparator getComparator() {
return CalendarComparator.INSTANCE;
}
@SuppressWarnings({ "unchecked" })
public X unwrap(Calendar value, Class type, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( Calendar.class.isAssignableFrom( type ) ) {
return (X) value;
}
if ( java.sql.Date.class.isAssignableFrom( type ) ) {
return (X) new java.sql.Date( value.getTimeInMillis() );
}
if ( java.sql.Time.class.isAssignableFrom( type ) ) {
return (X) new java.sql.Time( value.getTimeInMillis() );
}
if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) {
return (X) new java.sql.Timestamp( value.getTimeInMillis() );
}
if ( Date.class.isAssignableFrom( type ) ) {
return (X) new Date( value.getTimeInMillis() );
}
throw unknownUnwrap( type );
}
public Calendar wrap(X value, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( Calendar.class.isInstance( value ) ) {
return (Calendar) value;
}
if ( ! Date.class.isInstance( value ) ) {
throw unknownWrap( value.getClass() );
}
Calendar cal = new GregorianCalendar();
cal.setTime( (Date) value );
return cal;
}
}