Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. // Those are two tasks.
  2.  
  3. // TASK 1
  4. open class SingletonHolder<out T, in A>(creator: (A) -> T) {
  5.  
  6. private var creator: ((A) -> T)? = creator
  7.  
  8. @Volatile
  9. private var instance: T? = null
  10.  
  11. fun getInstance(arg: A): T {
  12. val i = instance
  13. if (i != null) {
  14. return i
  15. }
  16.  
  17. return synchronized(this) {
  18. val i2 = instance
  19. if (i2 != null) {
  20. i2
  21. } else {
  22. val created = creator!!(arg)
  23. instance = created
  24. creator = null
  25. created
  26. }
  27. }
  28. }
  29.  
  30. }
  31.  
  32.  
  33. // Used Like This
  34.  
  35. @Database(entities = [User::class, Product::class], version = 1, exportSchema = false)
  36. abstract class ECommerceDatabase : RoomDatabase()
  37. {
  38. companion object : SingletonHolder<ECommerceDatabase, Context>({
  39. Room.databaseBuilder(
  40. it.applicationContext,
  41. ECommerceDatabase::class.java,
  42. "ecommerce"
  43. ).build()
  44. })
  45. }
  46.  
  47.  
  48. // -----------------
  49.  
  50. // second task
  51. // Using higher order function find the solution :
  52. // Describe what this function is doing
  53.  
  54. fun <T> Activity.startActivityFinishThis(target: Class<T>) {
  55. val intent = Intent(this, target)
  56. intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
  57. // I want to add some custom behaviour on the intent in each activity I use this in.
  58. startActivity(intent)
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement