DateLens.java
package net.zer0bandwidth.android.lib.database.sqlitehouse.refractor;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import net.zer0bandwidth.android.lib.database.SQLiteSyntax;
import java.util.Date;
import static net.zer0bandwidth.android.lib.database.SQLiteSyntax.SQLITE_NULL;
/**
* Marshals {@link java.util.Date} objects.
*
* <p>The lens converts all dates into integer GMT timestamps for storage in the
* DB, then recreates the {@code Date} objects upon retrieval. This is because
* integer timestamps are more reliably stored and are absolute (no fretting
* about timezones).</p>
*
* @see SQLDateLens
* @since zer0bandwidth-net/android 0.1.4 (#26)
*/
public class DateLens
extends Lens<Date>
implements Refractor<Date>
{
@Override
public String getSQLiteDataType()
{ return SQLiteSyntax.SQLITE_TYPE_INT ; }
/**
* When a date cannot be null, this class provides a default value at the
* start of the epoch.
* @return a {@code Date} initialized at epoch time zero
*/
@Override
public Date getSQLiteDefaultValue()
{ return new Date(0) ; }
@Override
public String toSQLiteString( Date o )
{
return ( o == null ? SQLITE_NULL :
Long.toString( o.getTime() ) ) ;
}
@Override
public DateLens addToContentValues( ContentValues vals, String sKey, Date val )
{
vals.put( sKey, val.getTime() ) ;
return this ;
}
/** @since zer0bandwidth-net/android 0.1.7 (#50) */
@Override
public DateLens addToBundle( Bundle bndl, String sKey, Date val )
{
bndl.putLong( sKey, val.getTime() ) ;
return this ;
}
@Override
public Date fromCursor( Cursor crs, String sKey )
{
long ts = crs.getLong( crs.getColumnIndex( sKey ) ) ;
return new Date( ts ) ;
}
/** @since zer0bandwidth-net/android 0.1.7 (#50) */
@Override
public Date fromBundle( Bundle bndl, String sKey )
{ return new Date( bndl.getLong( sKey ) ) ; }
}