Lens.java

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

  2. import net.zer0bandwidth.android.lib.database.sqlitehouse.SQLightable;

  3. import java.lang.reflect.Field;

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

  5. /**
  6.  * Provides canonical implementations of various methods of {@link Refractor}.
  7.  * Custom implementations of {@code Refractor} may choose to extend this class
  8.  * or not; all internal code within the library refers to the interface, not the
  9.  * abstract class.
  10.  * @since zer0bandwidth-net/android 0.1.4 (#26)
  11.  */
  12. public abstract class Lens<T>
  13. implements Refractor<T>
  14. {
  15. /** @return {@code null} */
  16. @Override
  17. public T getSQLiteDefaultValue()
  18. { return null ; }

  19. @Override
  20. public String toSQLiteString( T o )
  21. { return ( o == null ? SQLITE_NULL : o.toString() ) ; }

  22. /**
  23.  * This is the simplest and canonical implementation of the method specified
  24.  * by {@link Refractor}.
  25.  */
  26. @Override
  27. public String getSQLiteDefaultString()
  28. { return this.toSQLiteString( this.getSQLiteDefaultValue() ) ; }

  29. /**
  30.  * This implementation simply uses {@link Field#get} and tries to cast it to
  31.  * the lens's template parameter type. Actual implementation classes should
  32.  * override this method if the data type is a primitive.
  33.  */
  34. @SuppressWarnings("unchecked")
  35. @Override
  36. public T getValueFrom( SQLightable o, Field fld )
  37. throws IllegalAccessException
  38. { return ((T)(fld.get(o))) ; }
  39. }