ZapData.kt 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.dolin.zap.entity
  2. /**
  3. * 参考[android.os.Message]的享元模式
  4. * @author #Suyghur.
  5. * Created on 4/8/21
  6. */
  7. class ZapData {
  8. var level: Level = Level.NONE
  9. var tag = ""
  10. var msg: String = ""
  11. private var next: ZapData? = null
  12. fun recycle() {
  13. level = Level.NONE
  14. tag = ""
  15. msg = ""
  16. synchronized(sPoolSync) {
  17. if (sPoolSize < MAX_POOL_SIZE) {
  18. next = sPool
  19. sPool = this
  20. sPoolSize++
  21. }
  22. }
  23. }
  24. override fun toString(): String {
  25. return "ZapData{" +
  26. "level=" + level.name +
  27. ", tag='" + tag + '\'' +
  28. ", msg='" + msg + '\'' +
  29. '}'
  30. }
  31. companion object {
  32. private val sPoolSync = Any()
  33. private var sPool: ZapData? = null
  34. private var sPoolSize = 0
  35. private const val MAX_POOL_SIZE = 50
  36. @JvmStatic
  37. fun obtain(): ZapData {
  38. synchronized(sPoolSync) {
  39. if (sPool != null) {
  40. val data = sPool
  41. sPool = data!!.next
  42. data.next = null
  43. sPoolSize--
  44. return data
  45. }
  46. }
  47. return ZapData()
  48. }
  49. @JvmStatic
  50. fun obtain(level: Level, tag: String, msg: String): ZapData {
  51. val data = obtain()
  52. data.level = level
  53. data.tag = tag
  54. data.msg = msg
  55. return data
  56. }
  57. }
  58. }