main

square/leakcanary

Last updated at: 29/12/2023 09:39

ValueHolder.kt

TLDR

The provided file defines a Kotlin sealed class ValueHolder, which represents a value in the heap dump. It includes several data classes for different types of values, such as ReferenceHolder, BooleanHolder, CharHolder, and so on.

Classes

ValueHolder

This sealed class represents a value in the heap dump. It includes the following nested data classes:

  • ReferenceHolder: Represents a reference value in the heap dump. It contains a value property of type Long, which represents the reference value. It also has a isNull property that returns true if the reference value is NULL_REFERENCE (which is defined as 0L in the companion object).
  • BooleanHolder: Represents a boolean value in the heap dump. It contains a value property of type Boolean, which represents the boolean value.
  • CharHolder: Represents a char value in the heap dump. It contains a value property of type Char, which represents the char value.
  • FloatHolder: Represents a float value in the heap dump. It contains a value property of type Float, which represents the float value.
  • DoubleHolder: Represents a double value in the heap dump. It contains a value property of type Double, which represents the double value.
  • ByteHolder: Represents a byte value in the heap dump. It contains a value property of type Byte, which represents the byte value.
  • ShortHolder: Represents a short value in the heap dump. It contains a value property of type Short, which represents the short value.
  • IntHolder: Represents an int value in the heap dump. It contains a value property of type Int, which represents the int value.
  • LongHolder: Represents a long value in the heap dump. It contains a value property of type Long, which represents the long value.

Companion object

The companion object contains a single property:

  • NULL_REFERENCE: Represents the value of a null reference in the heap dump, which is defined as 0L.
package shark

import shark.ValueHolder.ReferenceHolder

/**
 * A value in the heap dump, which can be a [ReferenceHolder] or
 * a primitive type.
 */
sealed class ValueHolder {
  data class ReferenceHolder(val value: Long) : ValueHolder() {
    val isNull
      get() = value == NULL_REFERENCE
  }

  data class BooleanHolder(val value: Boolean) : ValueHolder()
  data class CharHolder(val value: Char) : ValueHolder()
  data class FloatHolder(val value: Float) : ValueHolder()
  data class DoubleHolder(val value: Double) : ValueHolder()
  data class ByteHolder(val value: Byte) : ValueHolder()
  data class ShortHolder(val value: Short) : ValueHolder()
  data class IntHolder(val value: Int) : ValueHolder()
  data class LongHolder(val value: Long) : ValueHolder()

  companion object {
    const val NULL_REFERENCE = 0L
  }
}