StringCollectionLens.java

  1. package net.zer0bandwidth.android.lib.database.sqlitehouse.refractor;

  2. import android.content.ContentValues;
  3. import android.database.Cursor;
  4. import android.os.Bundle;
  5. import android.text.TextUtils;

  6. import net.zer0bandwidth.android.lib.database.SQLiteSyntax;

  7. import java.util.Collection;
  8. import java.util.Collections;

  9. import static net.zer0bandwidth.android.lib.database.SQLiteSyntax.SQLITE_NULL;

  10. /**
  11.  * Provides a canonical implementation of a {@link Lens} which can marshal a
  12.  * collection of strings to/from a single column in a database row.
  13.  * @since zer0bandwidth-net/android 0.1.5 (#42)
  14.  */
  15. public abstract class StringCollectionLens<C extends Collection<String>>
  16. extends Lens<C>
  17. implements Refractor<C>
  18. {
  19. /**
  20.  * Implementation classes must provide a concrete override of this method to
  21.  * declare the delimiter that is used/sought between items in the serialized
  22.  * list.
  23.  * @return the delimiter in the serialized list
  24.  */
  25. protected abstract String getDelimiter() ;

  26. /**
  27.  * Implementation classes must provide a concrete override of this method to
  28.  * generate instances of the collection class which contains the list items.
  29.  * @return a collection capable of storing the list items
  30.  */
  31. protected abstract C getCollectionInstance() ;

  32. /**
  33.  * Serializes the list as a string, using the delimiter provided by
  34.  * {@link #getDelimiter()}.
  35.  * @param asValues a collection of string values
  36.  * @return a serialization of the list
  37.  */
  38. public String toStringValue( C asValues )
  39. {
  40. return ( asValues == null ? null :
  41. TextUtils.join( this.getDelimiter(), asValues ) ) ;
  42. }

  43. @Override
  44. public String getSQLiteDataType()
  45. { return SQLiteSyntax.SQLITE_TYPE_TEXT ; }

  46. @Override
  47. public String toSQLiteString( C o )
  48. {
  49. return ( o == null ? SQLITE_NULL :
  50. String.format( "'%s'", this.toStringValue(o) ) ) ;
  51. }

  52. @Override
  53. public StringCollectionLens<C> addToContentValues(
  54. ContentValues vals, String sKey, C val )
  55. {
  56. vals.put( sKey, this.toStringValue(val) ) ;
  57. return this ;
  58. }

  59. /**
  60.  * Adds the collection of strings to a {@link Bundle} as a string array.
  61.  *
  62.  * This will <i>always</i> be rendered in the bundle as a string array,
  63.  * regardless of the algorithm that would marshal the collection to/from a
  64.  * database as a string serialization.
  65.  *
  66.  * @since zer0bandwidth-net/android 0.1.7 (#50)
  67.  */
  68. @Override
  69. public StringCollectionLens<C> addToBundle(
  70. Bundle bndl, String sKey, C val )
  71. {
  72. bndl.putStringArray( sKey, val.toArray( new String[val.size()] ) ) ;
  73. return this ;
  74. }

  75. @Override
  76. public C fromCursor( Cursor crs, String sKey )
  77. {
  78. String sValues = crs.getString( crs.getColumnIndex( sKey ) ) ;
  79. if( sValues == null ) return null ;
  80. C asValues = this.getCollectionInstance() ;
  81. Collections.addAll( asValues, sValues.split( this.getDelimiter() ) ) ;
  82. return asValues ;
  83. }

  84. /**
  85.  * Fetches a collection of strings from a {@link Bundle} as a string array.
  86.  *
  87.  * This will <i>always</i> be rendered in the bundle as a string array,
  88.  * regardless of the algorithm that would marshal the collection to/from a
  89.  * database as a string serialization.
  90.  *
  91.  * @since zer0bandwidth-net/android 0.1.7 (#50)
  92.  */
  93. @Override
  94. public C fromBundle( Bundle bndl, String sKey )
  95. {
  96. String[] asBundled = bndl.getStringArray( sKey ) ;
  97. if( asBundled == null || asBundled.length == 0 ) return null ;
  98. C asValues = this.getCollectionInstance() ;
  99. Collections.addAll( asValues, asBundled ) ;
  100. return asValues ;
  101. }
  102. }